Search in sources :

Example 66 with AmazonEc2Client

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);
}
Also used : DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CloudGateWays(com.sequenceiq.cloudbreak.cloud.model.CloudGateWays) CloudRegions(com.sequenceiq.cloudbreak.cloud.model.CloudRegions) InternetGatewayAttachment(com.amazonaws.services.ec2.model.InternetGatewayAttachment) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest) CloudGateWay(com.sequenceiq.cloudbreak.cloud.model.CloudGateWay) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) InternetGateway(com.amazonaws.services.ec2.model.InternetGateway) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 67 with AmazonEc2Client

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);
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) DeleteKeyPairRequest(com.amazonaws.services.ec2.model.DeleteKeyPairRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)

Example 68 with AmazonEc2Client

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()));
        }
    }
}
Also used : DescribeKeyPairsRequest(com.amazonaws.services.ec2.model.DescribeKeyPairsRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) DescribeKeyPairsResult(com.amazonaws.services.ec2.model.DescribeKeyPairsResult) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)

Example 69 with AmazonEc2Client

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();
}
Also used : DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) DescribeImagesRequest(com.amazonaws.services.ec2.model.DescribeImagesRequest) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)

Example 70 with AmazonEc2Client

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);
}
Also used : AsyncTaskExecutor(org.springframework.core.task.AsyncTaskExecutor) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest) LoggerFactory(org.slf4j.LoggerFactory) AwsDiskType(com.sequenceiq.common.model.AwsDiskType) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Image(com.sequenceiq.cloudbreak.cloud.model.Image) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) Inject(javax.inject.Inject) Future(java.util.concurrent.Future) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) AwsContext(com.sequenceiq.cloudbreak.cloud.aws.common.context.AwsContext) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Pair(org.springframework.data.util.Pair) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) Collectors(java.util.stream.Collectors) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) Component(org.springframework.stereotype.Component) PreserveResourceException(com.sequenceiq.cloudbreak.cloud.template.compute.PreserveResourceException) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Optional(java.util.Optional) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) AmazonClientException(com.amazonaws.AmazonClientException) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest) AmazonClientException(com.amazonaws.AmazonClientException) Future(java.util.concurrent.Future) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes)

Aggregations

AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)97 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)44 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)41 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)30 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)29 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)29 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)28 HashMap (java.util.HashMap)28 Group (com.sequenceiq.cloudbreak.cloud.model.Group)24 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)24 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)23 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)23 Network (com.sequenceiq.cloudbreak.cloud.model.Network)23 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)22 AmazonServiceException (com.amazonaws.AmazonServiceException)21 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)21 List (java.util.List)21 Vpc (com.amazonaws.services.ec2.model.Vpc)20 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)20