use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes 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.model.VolumeSetAttributes 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.model.VolumeSetAttributes in project cloudbreak by hortonworks.
the class AzureAttachmentResourceBuilder method build.
@Override
public List<CloudResource> build(AzureContext context, CloudInstance instance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) {
CloudResource cloudResourceInstance = buildableResource.stream().filter(cloudResource -> cloudResource.getType().equals(ResourceType.AZURE_INSTANCE)).findFirst().orElseThrow(() -> new AzureResourceException("Instance resource not found"));
LOGGER.info("Attach disk to the instance {}", cloudResourceInstance);
CloudContext cloudContext = auth.getCloudContext();
String resourceGroupName = azureResourceGroupMetadataProvider.getResourceGroupName(cloudContext, cloudStack);
AzureClient client = getAzureClient(auth);
VirtualMachine vm = client.getVirtualMachineByResourceGroup(resourceGroupName, cloudResourceInstance.getName());
Set<String> diskIds = vm.dataDisks().values().stream().map(VirtualMachineDataDisk::id).collect(Collectors.toSet());
CloudResource volumeSet = buildableResource.stream().filter(cloudResource -> cloudResource.getType().equals(ResourceType.AZURE_VOLUMESET)).filter(cloudResource -> !cloudResourceInstance.getInstanceId().equals(cloudResource.getInstanceId())).findFirst().orElseThrow(() -> new AzureResourceException("Volume set resource not found"));
VolumeSetAttributes volumeSetAttributes = getVolumeSetAttributes(volumeSet);
volumeSetAttributes.getVolumes().forEach(volume -> {
Disk disk = client.getDiskById(volume.getId());
if (!diskIds.contains(disk.id())) {
if (disk.isAttachedToVirtualMachine()) {
detachDiskFromVmByVmId(client, disk);
}
attachDiskToVm(client, disk, vm);
} else {
LOGGER.info("Managed disk {} is already attached to VM {}", disk, vm);
}
});
volumeSet.setInstanceId(cloudResourceInstance.getInstanceId());
volumeSet.setStatus(CommonStatus.CREATED);
LOGGER.info("Volume set {} attached successfully", volumeSet);
return List.of(volumeSet);
}
use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.
the class GcpAttachedDiskResourceBuilder method build.
@Override
public List<CloudResource> build(GcpContext context, CloudInstance instance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> resources, CloudStack cloudStack) throws Exception {
InstanceTemplate template = group.getReferenceInstanceTemplate();
List<String> operations = new ArrayList<>();
List<String> syncedOperations = Collections.synchronizedList(operations);
String projectId = context.getProjectId();
Compute compute = context.getCompute();
Collection<Future<Void>> futures = new ArrayList<>();
List<CloudResource> buildableResource = resources.stream().filter(cloudResource -> CommonStatus.REQUESTED.equals(cloudResource.getStatus())).collect(Collectors.toList());
List<CloudResource> result = new ArrayList<>();
for (CloudResource volumeSetResource : buildableResource) {
VolumeSetAttributes volumeSetAttributes = volumeSetResource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
for (VolumeSetAttributes.Volume volume : volumeSetAttributes.getVolumes()) {
Map<String, String> labels = gcpLabelUtil.createLabelsFromTags(cloudStack);
Disk disk = createDisk(projectId, volume, labels, volumeSetAttributes);
customGcpDiskEncryptionService.addEncryptionKeyToDisk(template, disk);
Future<Void> submit = intermediateBuilderExecutor.submit(() -> {
Insert insDisk = compute.disks().insert(projectId, volumeSetAttributes.getAvailabilityZone(), disk);
try {
Operation operation = insDisk.execute();
syncedOperations.add(operation.getName());
if (operation.getHttpErrorStatusCode() != null) {
throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), disk.getName());
}
} catch (TokenResponseException e) {
throw gcpStackUtil.getMissingServiceAccountKeyError(e, projectId);
} catch (GoogleJsonResponseException e) {
throw new GcpResourceException(checkException(e), resourceType(), disk.getName());
}
return null;
});
futures.add(submit);
}
volumeSetResource.putParameter(OPERATION_ID, operations);
result.add(new Builder().cloudResource(volumeSetResource).status(CommonStatus.CREATED).params(volumeSetResource.getParameters()).build());
}
for (Future<Void> future : futures) {
future.get();
}
result.addAll(resources.stream().filter(cloudResource -> CommonStatus.CREATED.equals(cloudResource.getStatus())).collect(Collectors.toList()));
return result;
}
use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.
the class StackDownscaleServiceTest method finishStackDownscaleAndDontSaveVolumeFQDNTest.
@Test
public void finishStackDownscaleAndDontSaveVolumeFQDNTest() throws TransactionService.TransactionExecutionException {
StackScalingFlowContext stackScalingFlowContext = mock(StackScalingFlowContext.class);
Stack stack = mock(Stack.class);
when(stackScalingFlowContext.getStack()).thenReturn(stack);
when(stackScalingFlowContext.isRepair()).thenReturn(true);
InstanceGroup masterGroup = new InstanceGroup();
masterGroup.setGroupName("master");
when(stack.getInstanceGroups()).thenReturn(Set.of(masterGroup));
InstanceMetaData master = new InstanceMetaData();
master.setInstanceId("i-1111");
master.setPrivateId(1L);
master.setDiscoveryFQDN("master1.cloudera.site");
masterGroup.setInstanceMetaData(Set.of(master));
ArrayList<Resource> volumes = new ArrayList<>();
Resource volume1 = new Resource();
volumes.add(volume1);
volume1.setInstanceId("i-1111");
VolumeSetAttributes volumeSetAttributes = new VolumeSetAttributes("az1", false, "", new ArrayList<>(), 50, "gp2");
volumeSetAttributes.setDiscoveryFQDN("some.fqdn");
volume1.setAttributes(Json.silent(volumeSetAttributes));
doReturn(Optional.of(volumeSetAttributes)).when(resourceAttributeUtil).getTypedAttributes(volume1, VolumeSetAttributes.class);
when(resourceService.findByStackIdAndType(any(), any())).thenReturn(volumes);
stackDownscaleService.finishStackDownscale(stackScalingFlowContext, Set.of(1L));
verify(resourceService).saveAll(resourcesCaptor.capture());
Iterable<Resource> resourcesCaptorValue = resourcesCaptor.getValue();
Json attributes = resourcesCaptorValue.iterator().next().getAttributes();
assertEquals("some.fqdn", attributes.getValue("discoveryFQDN"));
}
Aggregations