Search in sources :

Example 96 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsNativeEIPResourceBuilder method build.

@Override
public List<CloudResource> build(AwsContext context, CloudInstance cloudInstance, long privateId, AuthenticatedContext ac, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) throws Exception {
    List<CloudResource> ret = new ArrayList<>();
    if (!buildableResource.isEmpty()) {
        LOGGER.info("Trying to create EIp for instance with privateId: {}, resource name: {}", privateId, buildableResource.get(0).getName());
        AmazonEc2Client amazonEC2Client = context.getAmazonEc2Client();
        TagSpecification tagSpecification = awsTaggingService.prepareEc2TagSpecification(cloudStack.getTags(), com.amazonaws.services.ec2.model.ResourceType.ElasticIp);
        tagSpecification.getTags().add(new Tag().withKey("Name").withValue(buildableResource.get(0).getName()));
        AllocateAddressRequest allocateAddressRequest = new AllocateAddressRequest().withTagSpecifications(tagSpecification).withDomain(DomainType.Vpc);
        AllocateAddressResult allocateAddressResult = amazonEC2Client.allocateAddress(allocateAddressRequest);
        Optional<CloudResource> instanceResourceOpt = persistenceRetriever.notifyRetrieve(ac.getCloudContext().getId(), String.valueOf(privateId), CommonStatus.CREATED, ResourceType.AWS_INSTANCE);
        CloudResource instanceResource = instanceResourceOpt.orElseThrow();
        String allocationId = allocateAddressResult.getAllocationId();
        List<String> eips = List.of(allocationId);
        String instanceId = instanceResource.getInstanceId();
        List<AssociateAddressResult> associateAddressResults = awsElasticIpService.associateElasticIpsToInstances(amazonEC2Client, eips, List.of(instanceId));
        String associationId = associateAddressResults.get(0).getAssociationId();
        CloudResource cloudResource = CloudResource.builder().cloudResource(buildableResource.get(0)).instanceId(instanceId).status(CommonStatus.CREATED).reference(allocationId).params(Map.of(CloudResource.ATTRIBUTES, EIpAttributes.EIpAttributesBuilder.builder().withAllocateId(allocationId).withAssociationId(associationId).build())).build();
        ret.add(cloudResource);
        LOGGER.info("EIp created for instance with private id: '{}' EC2 instance id: '{}' association id: '{}', allocationId: '{}'", privateId, instanceId, associationId, allocationId);
    } else {
        LOGGER.debug("No buildable EIp for {}", cloudInstance.getInstanceId());
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) TagSpecification(com.amazonaws.services.ec2.model.TagSpecification) AllocateAddressRequest(com.amazonaws.services.ec2.model.AllocateAddressRequest) AllocateAddressResult(com.amazonaws.services.ec2.model.AllocateAddressResult) AssociateAddressResult(com.amazonaws.services.ec2.model.AssociateAddressResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Tag(com.amazonaws.services.ec2.model.Tag)

Example 97 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsNativeInstanceResourceBuilder method build.

@Override
public List<CloudResource> build(AwsContext context, CloudInstance cloudInstance, long privateId, AuthenticatedContext ac, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) throws Exception {
    if (buildableResource.isEmpty()) {
        throw new CloudConnectorException("Buildable resources cannot be empty!");
    }
    AmazonEc2Client amazonEc2Client = context.getAmazonEc2Client();
    InstanceTemplate instanceTemplate = group.getReferenceInstanceTemplate();
    AwsCloudStackView awsCloudStackView = new AwsCloudStackView(cloudStack);
    CloudResource cloudResource = buildableResource.get(0);
    Optional<Instance> existedOpt = resourceByName(amazonEc2Client, cloudResource.getName());
    Instance instance;
    if (existedOpt.isPresent() && existedOpt.get().getState().getCode() != AWS_INSTANCE_TERMINATED_CODE) {
        instance = existedOpt.get();
        LOGGER.info("Instance exists with name: {} ({}), check the state: {}", cloudResource.getName(), instance.getInstanceId(), instance.getState().getName());
        if (!instanceRunning(instance)) {
            LOGGER.info("Instance is existing but not running, try to start: {}, {}", instance.getInstanceId(), instance.getState());
            amazonEc2Client.startInstances(new StartInstancesRequest().withInstanceIds(instance.getInstanceId()));
        }
    } else {
        LOGGER.info("Create new instance with name: {}", cloudResource.getName());
        TagSpecification tagSpecification = awsTaggingService.prepareEc2TagSpecification(awsCloudStackView.getTags(), com.amazonaws.services.ec2.model.ResourceType.Instance);
        String securityGroupId = getSecurityGroupId(context, group);
        tagSpecification.withTags(new Tag().withKey("Name").withValue(cloudResource.getName()));
        RunInstancesRequest request = new RunInstancesRequest().withInstanceType(instanceTemplate.getFlavor()).withImageId(cloudStack.getImage().getImageName()).withSubnetId(cloudInstance.getSubnetId()).withSecurityGroupIds(singletonList(securityGroupId)).withEbsOptimized(isEbsOptimized(instanceTemplate)).withTagSpecifications(tagSpecification).withIamInstanceProfile(getIamInstanceProfile(group)).withUserData(getUserData(cloudStack, group)).withMinCount(1).withMaxCount(1).withBlockDeviceMappings(blocks(group, cloudStack, ac)).withKeyName(cloudStack.getInstanceAuthentication().getPublicKeyId());
        RunInstancesResult instanceResult = amazonEc2Client.createInstance(request);
        instance = instanceResult.getReservation().getInstances().get(0);
        LOGGER.info("Instance creation inited with name: {} and instance id: {}", cloudResource.getName(), instance.getInstanceId());
    }
    cloudResource.setInstanceId(instance.getInstanceId());
    return buildableResource;
}
Also used : CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) TagSpecification(com.amazonaws.services.ec2.model.TagSpecification) AwsCloudStackView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCloudStackView) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Tag(com.amazonaws.services.ec2.model.Tag) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

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