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);
}
}
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);
}
}
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);
}
}
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<>();
}
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;
}
Aggregations