Search in sources :

Example 11 with AmazonEC2Exception

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

the class AWSSecurityGroupClient method removeEgressRules.

public DeferredResult<Void> removeEgressRules(String groupId, List<IpPermission> rules) {
    if (CollectionUtils.isNotEmpty(rules)) {
        RevokeSecurityGroupEgressRequest req = new RevokeSecurityGroupEgressRequest().withGroupId(groupId).withIpPermissions(rules);
        String message = "Remove Egress Rules from AWS Security Group with id [" + groupId + "].";
        AWSDeferredResultAsyncHandler<RevokeSecurityGroupEgressRequest, RevokeSecurityGroupEgressResult> handler = new AWSDeferredResultAsyncHandler<RevokeSecurityGroupEgressRequest, RevokeSecurityGroupEgressResult>(this.service, message) {

            @Override
            protected Exception consumeError(Exception e) {
                if (e instanceof AmazonEC2Exception && ((AmazonEC2Exception) e).getErrorCode().equals(SECURITY_GROUP_RULE_NOT_FOUND)) {
                    Utils.log(AWSUtils.class, AWSUtils.class.getSimpleName(), Level.WARNING, () -> String.format("Egress rules cannot be removed because " + "they do not exist: %s", Utils.toString(e)));
                    return null;
                } else {
                    return e;
                }
            }
        };
        this.client.revokeSecurityGroupEgressAsync(req, handler);
        return handler.toDeferredResult().thenApply(r -> (Void) null);
    } else {
        return DeferredResult.completed(null);
    }
}
Also used : RevokeSecurityGroupEgressRequest(com.amazonaws.services.ec2.model.RevokeSecurityGroupEgressRequest) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) RevokeSecurityGroupEgressResult(com.amazonaws.services.ec2.model.RevokeSecurityGroupEgressResult) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 12 with AmazonEC2Exception

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

the class AWSSecurityGroupClient method addEgressRules.

public DeferredResult<Void> addEgressRules(String groupId, List<IpPermission> rules) {
    if (CollectionUtils.isNotEmpty(rules)) {
        AuthorizeSecurityGroupEgressRequest req = new AuthorizeSecurityGroupEgressRequest().withGroupId(groupId).withIpPermissions(rules);
        String message = "Create Egress Rules on AWS Security Group with id [" + groupId + "].";
        AWSDeferredResultAsyncHandler<AuthorizeSecurityGroupEgressRequest, AuthorizeSecurityGroupEgressResult> handler = new AWSDeferredResultAsyncHandler<AuthorizeSecurityGroupEgressRequest, AuthorizeSecurityGroupEgressResult>(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("Egress rules already exist: %s", Utils.toString(e)));
                    return null;
                } else {
                    return e;
                }
            }
        };
        this.client.authorizeSecurityGroupEgressAsync(req, handler);
        return handler.toDeferredResult().thenApply(r -> (Void) null);
    } else {
        return DeferredResult.completed(null);
    }
}
Also used : AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) AuthorizeSecurityGroupEgressResult(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupEgressResult) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) AuthorizeSecurityGroupEgressRequest(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupEgressRequest)

Example 13 with AmazonEC2Exception

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

the class AWSSecurityGroupClient method removeIngressRules.

public DeferredResult<Void> removeIngressRules(String groupId, List<IpPermission> rules) {
    if (CollectionUtils.isNotEmpty(rules)) {
        RevokeSecurityGroupIngressRequest req = new RevokeSecurityGroupIngressRequest().withGroupId(groupId).withIpPermissions(rules);
        String message = "Remove Ingress Rules from AWS Security Group with id [" + groupId + "].";
        AWSDeferredResultAsyncHandler<RevokeSecurityGroupIngressRequest, RevokeSecurityGroupIngressResult> handler = new AWSDeferredResultAsyncHandler<RevokeSecurityGroupIngressRequest, RevokeSecurityGroupIngressResult>(this.service, message) {

            @Override
            protected Exception consumeError(Exception e) {
                if (e instanceof AmazonEC2Exception && ((AmazonEC2Exception) e).getErrorCode().equals(SECURITY_GROUP_RULE_NOT_FOUND)) {
                    Utils.log(AWSUtils.class, AWSUtils.class.getSimpleName(), Level.WARNING, () -> String.format("Ingress rules cannot be removed because " + "they do not exist: %s", Utils.toString(e)));
                    return null;
                } else {
                    return e;
                }
            }
        };
        this.client.revokeSecurityGroupIngressAsync(req, handler);
        return handler.toDeferredResult().thenApply(r -> (Void) null);
    } else {
        return DeferredResult.completed(null);
    }
}
Also used : RevokeSecurityGroupIngressResult(com.amazonaws.services.ec2.model.RevokeSecurityGroupIngressResult) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) RevokeSecurityGroupIngressRequest(com.amazonaws.services.ec2.model.RevokeSecurityGroupIngressRequest)

Example 14 with AmazonEC2Exception

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

the class TestAWSSetupUtils method getAwsDisksByIds.

/**
 * Method to get Disk details directly from Amazon
 */
public static List<Volume> getAwsDisksByIds(AmazonEC2AsyncClient client, VerificationHost host, List<String> diskIds) throws Throwable {
    try {
        host.log("Getting disks with ids " + diskIds + " from the AWS endpoint using the EC2 client.");
        DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest().withVolumeIds(diskIds);
        DescribeVolumesResult describeVolumesResult = client.describeVolumes(describeVolumesRequest);
        return describeVolumesResult.getVolumes();
    } catch (Exception e) {
        if (e instanceof AmazonEC2Exception && ((AmazonEC2Exception) e).getErrorCode().equalsIgnoreCase(AWS_INVALID_VOLUME_ID_ERROR_CODE)) {
            return null;
        }
    }
    return new ArrayList<>();
}
Also used : ArrayList(java.util.ArrayList) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) UnknownHostException(java.net.UnknownHostException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) TimeoutException(java.util.concurrent.TimeoutException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 15 with AmazonEC2Exception

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

the class AwsInstanceConnector method stop.

@Override
public List<CloudVmInstanceStatus> stop(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> statuses = new ArrayList<>();
    AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    for (String group : getGroups(vms)) {
        Collection<String> instances = new ArrayList<>();
        Collection<CloudInstance> cloudInstances = new ArrayList<>();
        for (CloudInstance vm : vms) {
            if (vm.getTemplate().getGroupName().equals(group)) {
                instances.add(vm.getInstanceId());
                cloudInstances.add(vm);
            }
        }
        try {
            instances = removeInstanceIdsWhichAreNotInCorrectState(instances, amazonEC2Client, "Stopped");
            if (!instances.isEmpty()) {
                amazonEC2Client.stopInstances(new StopInstancesRequest().withInstanceIds(instances));
            }
            for (CloudInstance cloudInstance : cloudInstances) {
                statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.IN_PROGRESS));
            }
        } catch (RuntimeException e) {
            LOGGER.error("Stop instances failed on AWS", e);
            String message = e instanceof AmazonEC2Exception ? ((AmazonEC2Exception) e).getErrorCode() : e.getMessage();
            for (CloudInstance cloudInstance : cloudInstances) {
                statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.FAILED, message));
            }
        }
    }
    return statuses;
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) 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