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