use of com.sequenceiq.cloudbreak.util.DeviceNameGenerator 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.util.DeviceNameGenerator in project cloudbreak by hortonworks.
the class GcpAttachedDiskResourceBuilder method createAttachedDiskSet.
private CloudResource createAttachedDiskSet(GcpContext context, long privateId, AuthenticatedContext auth, Group group, CloudInstance instance) {
InstanceTemplate template = group.getReferenceInstanceTemplate();
GcpResourceNameService resourceNameService = getResourceNameService();
String groupName = group.getName();
CloudContext cloudContext = auth.getCloudContext();
String stackName = cloudContext.getName();
List<VolumeSetAttributes.Volume> volumes = new ArrayList<>();
DeviceNameGenerator generator = new DeviceNameGenerator(DEVICE_NAME_TEMPLATE, 0);
for (int i = 0; i < template.getVolumes().size(); i++) {
String volumeName = resourceNameService.resourceName(resourceType(), stackName, groupName, privateId, i);
Volume volume = template.getVolumes().get(i);
volumes.add(new VolumeSetAttributes.Volume(volumeName, generator.next(), volume.getSize(), volume.getType(), volume.getVolumeUsageType()));
}
String resourceName = resourceNameService.resourceName(resourceType(), stackName, groupName, privateId, 0);
Map<String, Object> attributes = new HashMap<>(Map.of(CloudResource.ATTRIBUTES, new VolumeSetAttributes.Builder().withAvailabilityZone(instance.getAvailabilityZone()).withDeleteOnTermination(Boolean.TRUE).withVolumes(volumes).build()));
return new Builder().type(resourceType()).status(CommonStatus.REQUESTED).name(resourceName).group(groupName).availabilityZone(instance.getAvailabilityZone()).params(attributes).build();
}
use of com.sequenceiq.cloudbreak.util.DeviceNameGenerator in project cloudbreak by hortonworks.
the class AzureVolumeResourceBuilder method build.
@Override
public List<CloudResource> build(AzureContext context, CloudInstance instance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) throws Exception {
LOGGER.info("Create volumes on provider");
AzureClient client = getAzureClient(auth);
Map<String, List<VolumeSetAttributes.Volume>> volumeSetMap = Collections.synchronizedMap(new HashMap<>());
List<Future<?>> futures = new ArrayList<>();
List<CloudResource> requestedResources = buildableResource.stream().filter(cloudResource -> CommonStatus.REQUESTED.equals(cloudResource.getStatus())).collect(toList());
CloudContext cloudContext = auth.getCloudContext();
String resourceGroupName = azureResourceGroupMetadataProvider.getResourceGroupName(cloudContext, cloudStack);
String region = cloudContext.getLocation().getRegion().getRegionName();
String diskEncryptionSetId = getDiskEncryptionSetId(group);
for (CloudResource resource : requestedResources) {
volumeSetMap.put(resource.getName(), Collections.synchronizedList(new ArrayList<>()));
VolumeSetAttributes volumeSet = getVolumeSetAttributes(resource);
DeviceNameGenerator generator = new DeviceNameGenerator(DEVICE_NAME_TEMPLATE, 1);
futures.addAll(volumeSet.getVolumes().stream().map(volume -> intermediateBuilderExecutor.submit(() -> {
Disk result = client.getDiskByName(resourceGroupName, volume.getId());
if (result == null) {
result = client.createManagedDisk(volume.getId(), volume.getSize(), AzureDiskType.getByValue(volume.getType()), region, resourceGroupName, cloudStack.getTags(), diskEncryptionSetId);
} else {
LOGGER.info("Managed disk for resource group: {}, name: {} already exists: {}", resourceGroupName, volume.getId(), result);
}
String volumeId = result.id();
volumeSetMap.get(resource.getName()).add(new VolumeSetAttributes.Volume(volumeId, generator.next(), volume.getSize(), volume.getType(), volume.getCloudVolumeUsageType()));
})).collect(toList()));
}
for (Future<?> future : futures) {
future.get();
}
return buildableResource.stream().peek(resource -> {
List<VolumeSetAttributes.Volume> volumes = volumeSetMap.get(resource.getName());
if (!CollectionUtils.isEmpty(volumes)) {
getVolumeSetAttributes(resource).setVolumes(volumes);
}
}).map(copyResourceWithNewStatus(CommonStatus.CREATED)).collect(toList());
}
Aggregations