Search in sources :

Example 1 with NotFoundException

use of com.netflix.simianarmy.NotFoundException in project SimianArmy by Netflix.

the class AWSClient method deleteDNSRecord.

/** {@inheritDoc} */
@Override
public void deleteDNSRecord(String dnsName, String dnsType, String hostedZoneID) {
    Validate.notEmpty(dnsName);
    Validate.notEmpty(dnsType);
    if (dnsType.equals("A") || dnsType.equals("AAAA") || dnsType.equals("CNAME")) {
        LOGGER.info(String.format("Deleting DNS Route 53 record %s", dnsName));
        AmazonRoute53Client route53Client = route53Client();
        // AWS API requires us to query for the record first
        ListResourceRecordSetsRequest listRequest = new ListResourceRecordSetsRequest(hostedZoneID);
        listRequest.setMaxItems("1");
        listRequest.setStartRecordType(dnsType);
        listRequest.setStartRecordName(dnsName);
        ListResourceRecordSetsResult listResult = route53Client.listResourceRecordSets(listRequest);
        if (listResult.getResourceRecordSets().size() < 1) {
            throw new NotFoundException("Could not find Route53 record for " + dnsName + " (" + dnsType + ") in zone " + hostedZoneID);
        } else {
            ResourceRecordSet resourceRecord = listResult.getResourceRecordSets().get(0);
            ArrayList<Change> changeList = new ArrayList<>();
            Change recordChange = new Change(ChangeAction.DELETE, resourceRecord);
            changeList.add(recordChange);
            ChangeBatch recordChangeBatch = new ChangeBatch(changeList);
            ChangeResourceRecordSetsRequest request = new ChangeResourceRecordSetsRequest(hostedZoneID, recordChangeBatch);
            ChangeResourceRecordSetsResult result = route53Client.changeResourceRecordSets(request);
        }
    } else {
        LOGGER.error("dnsType must be one of 'A', 'AAAA', or 'CNAME'");
    }
}
Also used : NotFoundException(com.netflix.simianarmy.NotFoundException) AmazonRoute53Client(com.amazonaws.services.route53.AmazonRoute53Client)

Example 2 with NotFoundException

use of com.netflix.simianarmy.NotFoundException in project SimianArmy by Netflix.

the class AWSClient method terminateInstance.

/** {@inheritDoc} */
@Override
public void terminateInstance(String instanceId) {
    Validate.notEmpty(instanceId);
    LOGGER.info(String.format("Terminating instance %s in region %s.", instanceId, region));
    try {
        ec2Client().terminateInstances(new TerminateInstancesRequest(Arrays.asList(instanceId)));
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidInstanceID.NotFound")) {
            throw new NotFoundException("AWS instance " + instanceId + " not found", e);
        }
        throw e;
    }
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) NotFoundException(com.netflix.simianarmy.NotFoundException)

Example 3 with NotFoundException

use of com.netflix.simianarmy.NotFoundException in project SimianArmy by Netflix.

the class ChaosMonkeyResource method addTerminationEvent.

private Response.Status addTerminationEvent(String groupType, String groupName, ChaosType chaosType, JsonGenerator gen) throws IOException {
    LOGGER.info("Running on-demand termination for instance group type '{}' and name '{}'", groupType, groupName);
    Response.Status responseStatus;
    try {
        Event evt = monkey.terminateNow(groupType, groupName, chaosType);
        if (evt != null) {
            responseStatus = Response.Status.OK;
            gen.writeStringField("monkeyType", evt.monkeyType().name());
            gen.writeStringField("eventId", evt.id());
            gen.writeNumberField("eventTime", evt.eventTime().getTime());
            gen.writeStringField("region", evt.region());
            for (Map.Entry<String, String> pair : evt.fields().entrySet()) {
                gen.writeStringField(pair.getKey(), pair.getValue());
            }
        } else {
            responseStatus = Response.Status.INTERNAL_SERVER_ERROR;
            gen.writeStringField("message", String.format("Failed to terminate instance in group %s [type %s]", groupName, groupType));
        }
    } catch (FeatureNotEnabledException e) {
        responseStatus = Response.Status.FORBIDDEN;
        gen.writeStringField("message", e.getMessage());
    } catch (InstanceGroupNotFoundException e) {
        responseStatus = Response.Status.NOT_FOUND;
        gen.writeStringField("message", e.getMessage());
    } catch (NotFoundException e) {
        // Available instance cannot be found to terminate, maybe the instance is already gone
        responseStatus = Response.Status.GONE;
        gen.writeStringField("message", e.getMessage());
    }
    LOGGER.info("On-demand termination completed.");
    return responseStatus;
}
Also used : Response(javax.ws.rs.core.Response) InstanceGroupNotFoundException(com.netflix.simianarmy.InstanceGroupNotFoundException) FeatureNotEnabledException(com.netflix.simianarmy.FeatureNotEnabledException) Event(com.netflix.simianarmy.MonkeyRecorder.Event) NotFoundException(com.netflix.simianarmy.NotFoundException) InstanceGroupNotFoundException(com.netflix.simianarmy.InstanceGroupNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with NotFoundException

use of com.netflix.simianarmy.NotFoundException in project SimianArmy by Netflix.

the class AWSClient method detachVolume.

@Override
public void detachVolume(String instanceId, String volumeId, boolean force) {
    Validate.notEmpty(instanceId);
    LOGGER.info(String.format("Detach volumes from instance %s in region %s.", instanceId, region));
    try {
        DetachVolumeRequest detachVolumeRequest = new DetachVolumeRequest();
        detachVolumeRequest.setForce(force);
        detachVolumeRequest.setInstanceId(instanceId);
        detachVolumeRequest.setVolumeId(volumeId);
        ec2Client().detachVolume(detachVolumeRequest);
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidInstanceID.NotFound")) {
            throw new NotFoundException("AWS instance " + instanceId + " not found", e);
        }
        throw e;
    }
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) NotFoundException(com.netflix.simianarmy.NotFoundException)

Example 5 with NotFoundException

use of com.netflix.simianarmy.NotFoundException in project SimianArmy by Netflix.

the class AWSClient method setInstanceSecurityGroups.

/** {@inheritDoc} */
public void setInstanceSecurityGroups(String instanceId, List<String> groupIds) {
    Validate.notEmpty(instanceId);
    LOGGER.info(String.format("Removing all security groups from instance %s in region %s.", instanceId, region));
    try {
        ModifyInstanceAttributeRequest request = new ModifyInstanceAttributeRequest();
        request.setInstanceId(instanceId);
        request.setGroups(groupIds);
        ec2Client().modifyInstanceAttribute(request);
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidInstanceID.NotFound")) {
            throw new NotFoundException("AWS instance " + instanceId + " not found", e);
        }
        throw e;
    }
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) NotFoundException(com.netflix.simianarmy.NotFoundException)

Aggregations

NotFoundException (com.netflix.simianarmy.NotFoundException)6 AmazonServiceException (com.amazonaws.AmazonServiceException)4 Instance (com.amazonaws.services.ec2.model.Instance)1 AmazonRoute53Client (com.amazonaws.services.route53.AmazonRoute53Client)1 FeatureNotEnabledException (com.netflix.simianarmy.FeatureNotEnabledException)1 InstanceGroupNotFoundException (com.netflix.simianarmy.InstanceGroupNotFoundException)1 Event (com.netflix.simianarmy.MonkeyRecorder.Event)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Response (javax.ws.rs.core.Response)1