use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsVolumeResourceBuilder method getAvailabilityZoneFromSubnet.
private String getAvailabilityZoneFromSubnet(AuthenticatedContext auth, String subnetId, Optional<String> availabilityZone) {
if (availabilityZone.isPresent()) {
LOGGER.debug("AZ is present: {}", availabilityZone.get());
return availabilityZone.get();
} else {
String defaultAvailabilityZone = auth.getCloudContext().getLocation().getAvailabilityZone().value();
AmazonEc2Client amazonEC2Client = getAmazonEC2Client(auth);
DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest().withSubnetIds(subnetId);
DescribeSubnetsResult describeSubnetsResult = awsMethodExecutor.execute(() -> amazonEC2Client.describeSubnets(describeSubnetsRequest), null);
if (describeSubnetsResult == null) {
LOGGER.debug("Describe subnet is null, fallback to default: {}", defaultAvailabilityZone);
return defaultAvailabilityZone;
} else {
return describeSubnetsResult.getSubnets().stream().filter(subnet -> subnetId.equals(subnet.getSubnetId())).map(Subnet::getAvailabilityZone).findFirst().orElseGet(() -> {
LOGGER.debug("Cannot find subnet in describe subnet result, fallback to default: {}", defaultAvailabilityZone);
return defaultAvailabilityZone;
});
}
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsVolumeResourceBuilder method deleteOrphanedVolumes.
private void deleteOrphanedVolumes(List<CloudResourceStatus> cloudResourceStatuses, AmazonEc2Client client) {
LOGGER.debug("Deleting orphan volumes");
cloudResourceStatuses.stream().filter(cloudResourceStatus -> CREATED.equals(cloudResourceStatus.getStatus())).map(CloudResourceStatus::getCloudResource).map(cloudResource -> cloudResource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class)).map(VolumeSetAttributes::getVolumes).flatMap(List::stream).map(VolumeSetAttributes.Volume::getId).map(volumeId -> new DeleteVolumeRequest().withVolumeId(volumeId)).forEach(request -> deleteVolumeByDeleteVolumeRequest(client, request));
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsVolumeResourceBuilder method delete.
@Override
public CloudResource delete(AwsContext context, AuthenticatedContext auth, CloudResource resource) throws PreserveResourceException {
LOGGER.debug("Set delete on termination to true, on instances");
VolumeSetAttributes volumeSetAttributes = resource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
List<CloudResourceStatus> cloudResourceStatuses = checkResources(ResourceType.AWS_VOLUMESET, context, auth, List.of(resource));
boolean anyDeleted = cloudResourceStatuses.stream().map(CloudResourceStatus::getStatus).anyMatch(DELETED::equals);
if (!volumeSetAttributes.getDeleteOnTermination() && !anyDeleted) {
LOGGER.debug("Volumes will be preserved.");
resource.setStatus(CommonStatus.DETACHED);
volumeSetAttributes.setDeleteOnTermination(Boolean.TRUE);
resource.putParameter(CloudResource.ATTRIBUTES, volumeSetAttributes);
resourceNotifier.notifyUpdate(resource, auth.getCloudContext());
throw new PreserveResourceException("Resource will be preserved for later reattachment.");
}
AmazonEc2Client client = getAmazonEC2Client(auth);
deleteOrphanedVolumes(cloudResourceStatuses, client);
turnOnDeleteOnterminationOnAttachedVolumes(resource, cloudResourceStatuses, client);
return null;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsClient method createAuthenticatedContext.
public AuthenticatedContext createAuthenticatedContext(CloudContext cloudContext, CloudCredential cloudCredential) {
AuthenticatedContext authenticatedContext = new AuthenticatedContext(cloudContext, cloudCredential);
try {
AuthenticatedContextView authenticatedContextView = new AuthenticatedContextView(authenticatedContext);
String region = authenticatedContextView.getRegion();
AwsCredentialView awsCredentialView = authenticatedContextView.getAwsCredentialView();
AmazonEc2Client amazonEC2Client = null;
if (region != null) {
amazonEC2Client = createEc2Client(awsCredentialView, region);
AmazonElasticLoadBalancingClient loadBalancingClient = createElasticLoadBalancingClient(awsCredentialView, region);
authenticatedContext.putParameter(AmazonElasticLoadBalancingClient.class, loadBalancingClient);
} else {
amazonEC2Client = createEc2Client(awsCredentialView);
}
authenticatedContext.putParameter(AmazonEc2Client.class, amazonEC2Client);
} catch (AmazonServiceException e) {
throw new CredentialVerificationException(e.getErrorMessage(), e);
}
return authenticatedContext;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsAvailabilityZoneProvider method describeAvailabilityZones.
@Cacheable(cacheNames = "cloudResourceAzCache", key = "{ #cloudCredential?.id, #awsRegion.regionName }")
public List<AvailabilityZone> describeAvailabilityZones(CloudCredential cloudCredential, DescribeAvailabilityZonesRequest describeAvailabilityZonesRequest, com.amazonaws.services.ec2.model.Region awsRegion) {
AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(cloudCredential), awsRegion.getRegionName());
DescribeAvailabilityZonesResult describeAvailabilityZonesResult = ec2Client.describeAvailabilityZones(describeAvailabilityZonesRequest);
return describeAvailabilityZonesResult.getAvailabilityZones();
}
Aggregations