use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsPlatformResources method gateways.
@Override
public CloudGateWays gateways(ExtendedCloudCredential cloudCredential, Region region, Map<String, String> filters) {
Map<String, Set<CloudGateWay>> resultCloudGateWayMap = new HashMap<>();
if (region != null && !Strings.isNullOrEmpty(region.value())) {
CloudRegions regions = regions(cloudCredential, region, filters, true);
for (Entry<Region, List<AvailabilityZone>> regionListEntry : regions.getCloudRegions().entrySet()) {
if (regionListEntry.getKey().value().equals(region.value())) {
AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(cloudCredential), regionListEntry.getKey().value());
DescribeInternetGatewaysRequest describeInternetGatewaysRequest = new DescribeInternetGatewaysRequest();
DescribeInternetGatewaysResult describeInternetGatewaysResult = ec2Client.describeInternetGateways(describeInternetGatewaysRequest);
Set<CloudGateWay> gateWays = new HashSet<>();
for (InternetGateway internetGateway : describeInternetGatewaysResult.getInternetGateways()) {
CloudGateWay cloudGateWay = new CloudGateWay();
cloudGateWay.setId(internetGateway.getInternetGatewayId());
cloudGateWay.setName(internetGateway.getInternetGatewayId());
Collection<String> vpcs = new ArrayList<>();
for (InternetGatewayAttachment internetGatewayAttachment : internetGateway.getAttachments()) {
vpcs.add(internetGatewayAttachment.getVpcId());
}
Map<String, Object> properties = new HashMap<>();
properties.put("attachment", vpcs);
cloudGateWay.setProperties(properties);
gateWays.add(cloudGateWay);
}
for (AvailabilityZone availabilityZone : regionListEntry.getValue()) {
resultCloudGateWayMap.put(availabilityZone.value(), gateWays);
}
}
}
}
return new CloudGateWays(resultCloudGateWayMap);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsPublicKeyConnector method unregister.
@Override
public void unregister(PublicKeyUnregisterRequest request) {
LOGGER.debug("Deleting public key {} in {} region on AWS", request.getPublicKeyId(), request.getRegion());
AwsCredentialView awsCredential = new AwsCredentialView(request.getCredential());
try {
AmazonEc2Client client = awsClient.createEc2Client(awsCredential, request.getRegion());
DeleteKeyPairRequest deleteKeyPairRequest = new DeleteKeyPairRequest(request.getPublicKeyId());
client.deleteKeyPair(deleteKeyPairRequest);
} catch (Exception e) {
String errorMessage = String.format("Failed to delete public key [%s: '%s', region: '%s'], detailed message: %s", getType(awsCredential), getAwsId(awsCredential), request.getRegion(), e.getMessage());
LOGGER.error(errorMessage, e);
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsSetup method validateExistingKeyPair.
private void validateExistingKeyPair(InstanceAuthentication instanceAuthentication, AwsCredentialView credentialView, String region, AuthenticatedContext ac) {
String keyPairName = awsClient.getExistingKeyPairName(instanceAuthentication);
if (StringUtils.isNotEmpty(keyPairName)) {
boolean keyPairIsPresentOnEC2 = false;
try {
AmazonEc2Client client = new AuthenticatedContextView(ac).getAmazonEC2Client();
DescribeKeyPairsResult describeKeyPairsResult = client.describeKeyPairs(new DescribeKeyPairsRequest().withKeyNames(keyPairName));
keyPairIsPresentOnEC2 = describeKeyPairsResult.getKeyPairs().stream().findFirst().isPresent();
} catch (RuntimeException e) {
String errorMessage = String.format("Failed to get the key pair [name: '%s'] from EC2 [roleArn:'%s'], detailed message: %s.", keyPairName, credentialView.getRoleArn(), e.getMessage());
LOGGER.info(errorMessage, e);
}
if (!keyPairIsPresentOnEC2) {
throw new CloudConnectorException(String.format("The key pair '%s' could not be found in the '%s' region of EC2. " + "Please check AWS EC2 console because probably you are using a wrong key or " + "refer to Cloudera documentation at %s for the required setup", keyPairName, region, DocumentationLinkProvider.awsSshKeySetupLink()));
}
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class VolumeBuilderUtil method getRootDeviceName.
public String getRootDeviceName(AuthenticatedContext ac, CloudStack cloudStack) {
AmazonEc2Client ec2Client = new AuthenticatedContextView(ac).getAmazonEC2Client();
DescribeImagesResult images = ec2Client.describeImages(new DescribeImagesRequest().withImageIds(cloudStack.getImage().getImageName()));
if (images.getImages().isEmpty()) {
throw new CloudConnectorException(String.format("AMI is not available: '%s'.", cloudStack.getImage().getImageName()));
}
com.amazonaws.services.ec2.model.Image image = images.getImages().get(0);
if (image == null) {
throw new CloudConnectorException(String.format("Couldn't describe AMI '%s'.", cloudStack.getImage().getImageName()));
}
return image.getRootDeviceName();
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsAttachmentResourceBuilder method build.
@Override
public List<CloudResource> build(AwsContext context, CloudInstance cloudInstance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) throws Exception {
LOGGER.debug("Attach volumes to instance");
CloudResource instance = buildableResource.stream().filter(cloudResource -> cloudResource.getType().equals(ResourceType.AWS_INSTANCE)).findFirst().orElseThrow(() -> new AwsResourceException("Instance resource not found"));
Optional<CloudResource> volumeSetOpt = buildableResource.stream().filter(cloudResource -> cloudResource.getType().equals(ResourceType.AWS_VOLUMESET)).findFirst();
if (volumeSetOpt.isEmpty()) {
LOGGER.debug("No volumes to attach");
return List.of();
}
CloudResource volumeSet = volumeSetOpt.get();
AmazonEc2Client client = getAmazonEc2Client(auth);
VolumeSetAttributes volumeSetAttributes = volumeSet.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
LOGGER.debug("Creating attach volume requests and submitting to executor for stack '{}', group '{}'", auth.getCloudContext().getName(), group.getName());
List<Future<?>> futures = volumeSetAttributes.getVolumes().stream().filter(volume -> !StringUtils.equals(AwsDiskType.Ephemeral.value(), volume.getType())).map(volume -> new AttachVolumeRequest().withInstanceId(instance.getInstanceId()).withVolumeId(volume.getId()).withDevice(volume.getDevice())).map(request -> intermediateBuilderExecutor.submit(() -> client.attachVolume(request))).collect(Collectors.toList());
LOGGER.debug("Waiting for attach volumes request");
for (Future<?> future : futures) {
try {
future.get();
} catch (AmazonClientException e) {
LOGGER.error("Attach was unsuccesful. Group: {}, Volume: {} Instance: {}, AWS error: {}", group.getName(), instance.getName(), volumeSet.getName(), e.getMessage());
throw e;
}
}
LOGGER.debug("Attach volume requests sent");
volumeSet.setInstanceId(instance.getInstanceId());
volumeSet.setStatus(CommonStatus.CREATED);
return List.of(volumeSet);
}
Aggregations