use of com.sequenceiq.cloudbreak.cloud.aws.common.context.AwsContext in project cloudbreak by hortonworks.
the class AwsEfsResourceBuilder method checkResources.
@Override
protected List<CloudResourceStatus> checkResources(ResourceType type, AwsContext context, AuthenticatedContext auth, Iterable<CloudResource> resources) {
AmazonEfsClient client = getAmazonEfsClient(auth);
List<CloudResource> efsResources = StreamSupport.stream(resources.spliterator(), false).filter(r -> r.getType().equals(resourceType())).collect(Collectors.toList());
List<String> efsIds = new ArrayList<>();
List<CloudResourceStatus> cloudResourceStatusList = new ArrayList<>();
for (CloudResource efsResource : efsResources) {
CloudEfsAttributes efsAttributes = efsResource.getParameter(CloudResource.ATTRIBUTES, CloudEfsAttributes.class);
String efsId = efsAttributes.getFileSystemId();
efsIds.add(efsId);
DescribeFileSystemsRequest request = new DescribeFileSystemsRequest().withFileSystemId(efsId);
DescribeFileSystemsResult result = client.describeFileSystems(request);
List<CloudResourceStatus> efsStatusList = getResourceStatus(efsResource, efsId, result);
cloudResourceStatusList.addAll(efsStatusList);
}
LOGGER.debug("got EFS status for [{}]", String.join(",", efsIds));
return cloudResourceStatusList;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.context.AwsContext in project cloudbreak by hortonworks.
the class AwsVolumeResourceBuilder method checkResources.
@Override
protected List<CloudResourceStatus> checkResources(ResourceType type, AwsContext context, AuthenticatedContext auth, Iterable<CloudResource> resources) {
AmazonEc2Client client = getAmazonEC2Client(auth);
Pair<List<String>, List<CloudResource>> volumes = volumeResourceCollector.getVolumeIdsByVolumeResources(resources, resourceType(), volumeSetAttributes());
DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest(volumes.getFirst());
LOGGER.debug("Going to describe volume(s) with id(s): [{}]", String.join(",", describeVolumesRequest.getVolumeIds()));
AtomicReference<ResourceStatus> volumeSetStatus = new AtomicReference<>();
try {
DescribeVolumesResult result = client.describeVolumes(describeVolumesRequest);
volumeSetStatus.set(getResourceStatus(result));
} catch (AmazonEC2Exception e) {
if (!"InvalidVolume.NotFound".equals(e.getErrorCode())) {
throw e;
}
LOGGER.info("The volume doesn't need to be deleted as it does not exist on the provider side. Reason: {}", e.getMessage());
volumeSetStatus.set(DELETED);
}
LOGGER.debug("[{}] volume set status is {}", String.join(",", volumes.getFirst()), volumeSetStatus);
return volumes.getSecond().stream().map(resource -> new CloudResourceStatus(resource, volumeSetStatus.get())).collect(Collectors.toList());
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.context.AwsContext in project cloudbreak by hortonworks.
the class AwsVolumeResourceBuilder method build.
@Override
public List<CloudResource> build(AwsContext context, CloudInstance instance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) throws Exception {
LOGGER.debug("Create volumes on provider: {}", buildableResource.stream().map(CloudResource::getName).collect(Collectors.toList()));
AmazonEc2Client client = getAmazonEC2Client(auth);
Map<String, List<Volume>> volumeSetMap = Collections.synchronizedMap(new HashMap<>());
List<Future<?>> futures = new ArrayList<>();
boolean encryptedVolume = isEncryptedVolumeRequested(group);
String volumeEncryptionKey = getVolumeEncryptionKey(group, encryptedVolume);
TagSpecification tagSpecification = new TagSpecification().withResourceType(com.amazonaws.services.ec2.model.ResourceType.Volume).withTags(awsTaggingService.prepareEc2Tags(cloudStack.getTags()));
List<CloudResource> requestedResources = buildableResource.stream().filter(cloudResource -> CommonStatus.REQUESTED.equals(cloudResource.getStatus())).collect(Collectors.toList());
Long ephemeralCount = getEphemeralCount(group);
LOGGER.debug("Start creating data volumes for stack: '{}' group: '{}'", auth.getCloudContext().getName(), group.getName());
for (CloudResource resource : requestedResources) {
volumeSetMap.put(resource.getName(), Collections.synchronizedList(new ArrayList<>()));
VolumeSetAttributes volumeSet = resource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
DeviceNameGenerator generator = new DeviceNameGenerator(DEVICE_NAME_TEMPLATE, ephemeralCount.intValue());
futures.addAll(volumeSet.getVolumes().stream().map(createVolumeRequest(encryptedVolume, volumeEncryptionKey, tagSpecification, volumeSet)).map(requestWithUsage -> intermediateBuilderExecutor.submit(() -> {
CreateVolumeRequest request = requestWithUsage.getFirst();
CreateVolumeResult result = client.createVolume(request);
String volumeId = result.getVolume().getVolumeId();
Volume volume = new Volume(volumeId, generator.next(), request.getSize(), request.getVolumeType(), requestWithUsage.getSecond());
volumeSetMap.get(resource.getName()).add(volume);
})).collect(Collectors.toList()));
}
LOGGER.debug("Waiting for volumes creation requests");
for (Future<?> future : futures) {
future.get();
}
LOGGER.debug("Volume creation requests sent");
String defaultAvailabilityZone = auth.getCloudContext().getLocation().getAvailabilityZone().value();
return requestedResources.stream().peek(resource -> {
List<Volume> volumes = volumeSetMap.get(resource.getName());
if (!CollectionUtils.isEmpty(volumes)) {
resource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class).setVolumes(volumes);
}
}).map(copyResourceWithCreatedStatus(defaultAvailabilityZone)).collect(Collectors.toList());
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.context.AwsContext in project cloudbreak by hortonworks.
the class CreateResourcesHandler method accept.
@Override
public void accept(Event<CreateResourcesRequest> event) {
LOGGER.info("Re-create the resources during the AWS migration");
CreateResourcesRequest request = event.getData();
try {
CloudContext cloudContext = request.getCloudContext();
CloudCredential cloudCredential = request.getCloudCredential();
CloudStack cloudStack = request.getCloudStack();
AuthenticatedContext ac = awsAuthenticator.authenticate(cloudContext, cloudCredential);
Network network = cloudStack.getNetwork();
AwsContext awsContext = awsContextBuilder.contextInit(cloudContext, ac, network, List.of(), true);
for (ResourceRecreator resourceRecreator : resourceRecreators) {
resourceRecreator.recreate(request, awsContext, ac);
}
CreateResourcesResult result = new CreateResourcesResult(request.getResourceId());
request.getResult().onNext(result);
eventBus.notify(result.selector(), new Event<>(event.getHeaders(), result));
} catch (Exception e) {
LOGGER.error("Cannot re-create the AWS security group during the variant migration", e);
CreateResourcesResult result = new CreateResourcesResult(e.getMessage(), e, request.getResourceId());
request.getResult().onNext(result);
eventBus.notify(AWS_VARIANT_MIGRATION_FAILED_EVENT.event(), new Event<>(event.getHeaders(), result));
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.context.AwsContext 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