Search in sources :

Example 6 with AmazonEC2Exception

use of com.amazonaws.services.ec2.model.AmazonEC2Exception in project cloudbreak by hortonworks.

the class AwsInstanceConnector method check.

@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> cloudVmInstanceStatuses = new ArrayList<>();
    for (CloudInstance vm : vms) {
        try {
            String region = ac.getCloudContext().getLocation().getRegion().value();
            DescribeInstancesResult result = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()).describeInstances(new DescribeInstancesRequest().withInstanceIds(vm.getInstanceId()));
            for (Reservation reservation : result.getReservations()) {
                for (Instance instance : reservation.getInstances()) {
                    if ("Stopped".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.STOPPED));
                    } else if ("Running".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.STARTED));
                    } else if ("Terminated".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
                    } else {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.IN_PROGRESS));
                    }
                }
            }
        } catch (AmazonEC2Exception e) {
            LOGGER.warn("Instance does not exist with this id: {}, original message: {}", vm.getInstanceId(), e.getMessage());
        }
    }
    return cloudVmInstanceStatuses;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) Reservation(com.amazonaws.services.ec2.model.Reservation) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 7 with AmazonEC2Exception

use of com.amazonaws.services.ec2.model.AmazonEC2Exception in project photon-model by vmware.

the class AWSTaskStatusChecker method buildHandler.

private AsyncHandler buildHandler(T type) {
    return new AsyncHandler<AmazonWebServiceRequest, AmazonWebServiceResult>() {

        @Override
        public void onError(Exception exception) {
            // particular instanceId.
            if (exception instanceof AmazonServiceException && ((AmazonServiceException) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_INSTANCE_ID_ERROR_CODE)) {
                AWSTaskStatusChecker.this.service.logWarning("Could not retrieve status for instance %s. Retrying... Exception on AWS is %s", AWSTaskStatusChecker.this.instanceId, exception);
                AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
                return;
            } else if (exception instanceof AmazonEC2Exception && ((AmazonEC2Exception) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_VOLUME_ID_ERROR_CODE)) {
                AWSTaskStatusChecker.this.consumer.accept(null);
                return;
            }
            AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(exception);
            return;
        }

        @Override
        public void onSuccess(AmazonWebServiceRequest request, AmazonWebServiceResult result) {
            String status;
            Object instance;
            String failureMessage = null;
            String stateReason = null;
            if (result instanceof DescribeInstancesResult) {
                instance = ((DescribeInstancesResult) result).getReservations().get(0).getInstances().get(0);
                Instance vm = (Instance) instance;
                status = vm.getState().getName();
                stateReason = vm.getStateReason() != null ? vm.getStateReason().getMessage() : null;
            } else if (result instanceof DescribeNatGatewaysResult) {
                instance = ((DescribeNatGatewaysResult) result).getNatGateways().get(0);
                status = ((NatGateway) instance).getState();
                // if NAT gateway creation fails, the status is still "pending";
                // rather than keep checking for status and eventually time out, get the
                // failure message and fail the task
                failureMessage = ((NatGateway) instance).getFailureMessage();
            } else if (result instanceof DescribeVolumesResult) {
                instance = ((DescribeVolumesResult) result).getVolumes().get(0);
                status = ((Volume) instance).getState().toLowerCase();
            } else {
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + result));
                return;
            }
            if (failureMessage != null) {
                // operation failed; no need to keep checking for desired status
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException(failureMessage));
                return;
            } else if (AWSTaskStatusChecker.this.failureStates.contains(status)) {
                // operation failed; no need to keep checking for desired status
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException("Resource is state:[" + status + "]," + "reason:" + stateReason));
                return;
            } else if (!status.equals(AWSTaskStatusChecker.this.desiredState)) {
                AWSTaskStatusChecker.this.service.logInfo("Instance %s not yet in desired state %s. Current state %s, failure states %s, waiting 5s", AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, status, AWSTaskStatusChecker.this.failureStates);
                // if the instance is not in the desired state, schedule thread
                // to run again in 5 seconds
                AWSTaskStatusChecker.this.service.getHost().schedule(() -> {
                    AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
                }, 5, TimeUnit.SECONDS);
                return;
            }
            AWSTaskStatusChecker.this.consumer.accept(instance);
            return;
        }
    };
}
Also used : AsyncHandler(com.amazonaws.handlers.AsyncHandler) Instance(com.amazonaws.services.ec2.model.Instance) DescribeNatGatewaysResult(com.amazonaws.services.ec2.model.DescribeNatGatewaysResult) NatGateway(com.amazonaws.services.ec2.model.NatGateway) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AmazonWebServiceResult(com.amazonaws.AmazonWebServiceResult) Volume(com.amazonaws.services.ec2.model.Volume) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 8 with AmazonEC2Exception

use of com.amazonaws.services.ec2.model.AmazonEC2Exception in project photon-model by vmware.

the class AWSSubnetTaskServiceTest method testDeleteSubnet.

@Test
public void testDeleteSubnet() throws Throwable {
    Subnet awsSubnet = createAwsSubnet();
    SubnetState subnetState = createSubnetState(awsSubnet.getSubnetId(), AWS_NON_EXISTING_SUBNET_NAME, AWS_NON_EXISTING_SUBNET_CIDR, null);
    kickOffSubnetProvision(InstanceRequestType.DELETE, subnetState, TaskStage.FINISHED);
    if (!this.isMock) {
        // Verify that the subnet was deleted.
        DescribeSubnetsRequest describeRequest = new DescribeSubnetsRequest().withSubnetIds(Collections.singletonList(awsSubnet.getSubnetId()));
        try {
            this.client.describeSubnets(describeRequest).getSubnets();
            fail("Subnet should not exist in AWS.");
        } catch (AmazonEC2Exception ex) {
            assertEquals(HttpResponseStatus.BAD_REQUEST.code(), ex.getStatusCode());
        }
    }
}
Also used : Subnet(com.amazonaws.services.ec2.model.Subnet) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 9 with AmazonEC2Exception

use of com.amazonaws.services.ec2.model.AmazonEC2Exception in project photon-model by vmware.

the class AWSNetworkClient method deleteSubnet.

/**
 * Delete the specified subnet
 *
 * @throws AmazonEC2Exception if the subnet doesn't exist.
 */
public void deleteSubnet(String subnetId) throws AmazonEC2Exception {
    DeleteSubnetRequest req = new DeleteSubnetRequest().withSubnetId(subnetId);
    this.client.deleteSubnet(req);
}
Also used : DeleteSubnetRequest(com.amazonaws.services.ec2.model.DeleteSubnetRequest)

Example 10 with AmazonEC2Exception

use of com.amazonaws.services.ec2.model.AmazonEC2Exception in project photon-model by vmware.

the class AWSSecurityGroupClient method addInnerIngressRule.

public DeferredResult<Void> addInnerIngressRule(String securityGroupId) {
    AuthorizeSecurityGroupIngressRequest req = new AuthorizeSecurityGroupIngressRequest().withGroupId(securityGroupId).withIpPermissions(Collections.singletonList(buildInnerRule(securityGroupId)));
    String message = "Create internal Ingress Rule on AWS Security Group with id [" + securityGroupId + "].";
    AWSDeferredResultAsyncHandler<AuthorizeSecurityGroupIngressRequest, AuthorizeSecurityGroupIngressResult> handler = new AWSDeferredResultAsyncHandler<AuthorizeSecurityGroupIngressRequest, AuthorizeSecurityGroupIngressResult>(this.service, message) {

        @Override
        protected Exception consumeError(Exception e) {
            if (e instanceof AmazonEC2Exception && ((AmazonEC2Exception) e).getErrorCode().equals(SECURITY_GROUP_RULE_DUPLICATE)) {
                Utils.log(AWSUtils.class, AWSUtils.class.getSimpleName(), Level.WARNING, () -> String.format("Ingress rule already exists: %s", Utils.toString(e)));
                return null;
            } else {
                return e;
            }
        }
    };
    this.client.authorizeSecurityGroupIngressAsync(req, handler);
    return handler.toDeferredResult().thenApply(r -> (Void) null);
}
Also used : AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) AuthorizeSecurityGroupIngressResult(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressResult) AuthorizeSecurityGroupIngressRequest(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Aggregations

AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)15 AmazonServiceException (com.amazonaws.AmazonServiceException)7 AWSUtils (com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils)6 ArrayList (java.util.ArrayList)4 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)3 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)3 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)3 SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)3 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)2 AuthorizeSecurityGroupEgressRequest (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupEgressRequest)2 AuthorizeSecurityGroupEgressResult (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupEgressResult)2 AuthorizeSecurityGroupIngressRequest (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest)2 AuthorizeSecurityGroupIngressResult (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressResult)2 DeleteSubnetRequest (com.amazonaws.services.ec2.model.DeleteSubnetRequest)2 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)2 DescribeSubnetsRequest (com.amazonaws.services.ec2.model.DescribeSubnetsRequest)2 DescribeVolumesResult (com.amazonaws.services.ec2.model.DescribeVolumesResult)2 Instance (com.amazonaws.services.ec2.model.Instance)2 NatGateway (com.amazonaws.services.ec2.model.NatGateway)2 Subnet (com.amazonaws.services.ec2.model.Subnet)2