use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.
the class AwsVolumeResourceBuilder method turnOnDeleteOnterminationOnAttachedVolumes.
private void turnOnDeleteOnterminationOnAttachedVolumes(CloudResource resource, List<CloudResourceStatus> cloudResourceStatuses, AmazonEc2Client client) {
String instanceId = resource.getInstanceId();
if (StringUtils.isNotEmpty(instanceId)) {
List<InstanceBlockDeviceMappingSpecification> deviceMappingSpecifications = cloudResourceStatuses.stream().filter(cloudResourceStatus -> ATTACHED.equals(cloudResourceStatus.getStatus())).map(CloudResourceStatus::getCloudResource).map(cloudResource -> cloudResource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class)).map(VolumeSetAttributes::getVolumes).flatMap(List::stream).map(toInstanceBlockDeviceMappingSpecification).collect(Collectors.toList());
ModifyInstanceAttributeRequest modifyInstanceAttributeRequest = new ModifyInstanceAttributeRequest().withInstanceId(instanceId).withBlockDeviceMappings(deviceMappingSpecifications);
ModifyInstanceAttributeResult modifyIdentityIdFormatResult = awsMethodExecutor.execute(() -> client.modifyInstanceAttribute(modifyInstanceAttributeRequest), null);
String result = instanceId + " not found on the provider.";
if (modifyIdentityIdFormatResult != null) {
result = modifyIdentityIdFormatResult.toString();
}
LOGGER.info("Delete on termination set to true on instance '{}'. {}", instanceId, result);
} else {
LOGGER.info("No instance id found for volume set resource, skipping the modify instance attributes call to AWS.");
}
}
use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.
the class AwsVolumeResourceBuilder method deleteOrphanedVolumes.
private void deleteOrphanedVolumes(List<CloudResourceStatus> cloudResourceStatuses, AmazonEc2Client client) {
LOGGER.debug("Deleting orphan volumes");
cloudResourceStatuses.stream().filter(cloudResourceStatus -> CREATED.equals(cloudResourceStatus.getStatus())).map(CloudResourceStatus::getCloudResource).map(cloudResource -> cloudResource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class)).map(VolumeSetAttributes::getVolumes).flatMap(List::stream).map(VolumeSetAttributes.Volume::getId).map(volumeId -> new DeleteVolumeRequest().withVolumeId(volumeId)).forEach(request -> deleteVolumeByDeleteVolumeRequest(client, request));
}
use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.
the class AwsVolumeResourceBuilder method delete.
@Override
public CloudResource delete(AwsContext context, AuthenticatedContext auth, CloudResource resource) throws PreserveResourceException {
LOGGER.debug("Set delete on termination to true, on instances");
VolumeSetAttributes volumeSetAttributes = resource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
List<CloudResourceStatus> cloudResourceStatuses = checkResources(ResourceType.AWS_VOLUMESET, context, auth, List.of(resource));
boolean anyDeleted = cloudResourceStatuses.stream().map(CloudResourceStatus::getStatus).anyMatch(DELETED::equals);
if (!volumeSetAttributes.getDeleteOnTermination() && !anyDeleted) {
LOGGER.debug("Volumes will be preserved.");
resource.setStatus(CommonStatus.DETACHED);
volumeSetAttributes.setDeleteOnTermination(Boolean.TRUE);
resource.putParameter(CloudResource.ATTRIBUTES, volumeSetAttributes);
resourceNotifier.notifyUpdate(resource, auth.getCloudContext());
throw new PreserveResourceException("Resource will be preserved for later reattachment.");
}
AmazonEc2Client client = getAmazonEC2Client(auth);
deleteOrphanedVolumes(cloudResourceStatuses, client);
turnOnDeleteOnterminationOnAttachedVolumes(resource, cloudResourceStatuses, client);
return null;
}
use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.
the class AwsContextServiceTest method addResourcesToContextTest.
@Test
void addResourcesToContextTest() {
List<CloudResource> resources = new ArrayList<>();
resources.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.REQUESTED).name("worker1").params(Map.of("privateId", 1L)).group("worker").build());
resources.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.REQUESTED).name("worker2").params(Map.of("privateId", 2L)).group("worker").build());
resources.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.REQUESTED).name("worker3").params(Map.of("privateId", 3L)).group("worker").build());
resources.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.REQUESTED).name("compute2").params(Map.of("privateId", 5L)).group("compute").build());
resources.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.REQUESTED).name("compute3").params(Map.of("privateId", 6L)).group("compute").build());
resources.add(CloudResource.builder().type(ResourceType.AWS_INSTANCE).status(CommonStatus.REQUESTED).name("compute4").params(Map.of("privateId", 7L)).group("compute").build());
VolumeSetAttributes volume1attributes = new VolumeSetAttributes("az1", false, "fstab", new ArrayList<>(), 100, "general");
volume1attributes.setDiscoveryFQDN("worker3.example.com");
resources.add(CloudResource.builder().type(ResourceType.AWS_VOLUMESET).status(CommonStatus.REQUESTED).name("volume1").group("worker").params(Map.of("attributes", volume1attributes)).build());
VolumeSetAttributes volume2attributes = new VolumeSetAttributes("az1", false, "fstab", new ArrayList<>(), 100, "general");
volume2attributes.setDiscoveryFQDN("compute2.example.com");
resources.add(CloudResource.builder().type(ResourceType.AWS_VOLUMESET).status(CommonStatus.REQUESTED).name("volume3").group("compute").params(Map.of("attributes", volume2attributes)).build());
VolumeSetAttributes volume3attributes = new VolumeSetAttributes("az1", false, "fstab", new ArrayList<>(), 100, "general");
volume3attributes.setDiscoveryFQDN("compute3.example.com");
resources.add(CloudResource.builder().type(ResourceType.AWS_VOLUMESET).status(CommonStatus.REQUESTED).name("volume4").group("compute").params(Map.of("attributes", volume3attributes)).build());
List<Group> groups = new ArrayList<>();
List<CloudInstance> workerInstances = new ArrayList<>();
workerInstances.add(new CloudInstance("W1", getInstanceTemplate(1L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker1.example.com")));
workerInstances.add(new CloudInstance(null, getInstanceTemplate(2L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker2.example.com")));
workerInstances.add(new CloudInstance(null, getInstanceTemplate(3L, "worker"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "worker3.example.com")));
groups.add(new Group("worker", InstanceGroupType.CORE, workerInstances, mock(Security.class), mock(CloudInstance.class), mock(InstanceAuthentication.class), "admin", "ssh", 100, Optional.empty(), createGroupNetwork(), emptyMap()));
List<CloudInstance> computeInstances = new ArrayList<>();
computeInstances.add(new CloudInstance("C1", getInstanceTemplate(4L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute1.example.com")));
computeInstances.add(new CloudInstance(null, getInstanceTemplate(5L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute2.example.com")));
computeInstances.add(new CloudInstance(null, getInstanceTemplate(6L, "compute"), mock(InstanceAuthentication.class), "subnet-1", "az1", Map.of(CloudInstance.FQDN, "compute3.example.com")));
groups.add(new Group("compute", InstanceGroupType.CORE, computeInstances, mock(Security.class), mock(CloudInstance.class), mock(InstanceAuthentication.class), "admin", "ssh", 100, Optional.empty(), createGroupNetwork(), emptyMap()));
ResourceBuilderContext context = new ResourceBuilderContext("context", Location.location(Region.region("us-west-1")), 0);
awsContextService.addResourcesToContext(resources, context, groups);
assertNull(context.getComputeResources(1L));
List<CloudResource> worker2 = context.getComputeResources(2L);
assertEquals(1, worker2.size());
assertTrue(worker2.stream().anyMatch(cloudResource -> "worker2".equals(cloudResource.getName())));
List<CloudResource> worker3 = context.getComputeResources(3L);
assertEquals(2, worker3.size());
assertTrue(worker3.stream().anyMatch(cloudResource -> "worker3".equals(cloudResource.getName())));
assertTrue(worker3.stream().anyMatch(cloudResource -> "volume1".equals(cloudResource.getName())));
assertNull(context.getComputeResources(4L));
List<CloudResource> compute2 = context.getComputeResources(5L);
assertEquals(2, compute2.size());
assertTrue(compute2.stream().anyMatch(cloudResource -> "compute2".equals(cloudResource.getName())));
assertTrue(compute2.stream().anyMatch(cloudResource -> "volume3".equals(cloudResource.getName())));
List<CloudResource> compute3 = context.getComputeResources(6L);
assertEquals(2, compute3.size());
assertTrue(compute3.stream().anyMatch(cloudResource -> "compute3".equals(cloudResource.getName())));
assertTrue(compute3.stream().anyMatch(cloudResource -> "volume4".equals(cloudResource.getName())));
}
Aggregations