Search in sources :

Example 46 with VolumeSetAttributes

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.");
    }
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) ModifyInstanceAttributeRequest(com.amazonaws.services.ec2.model.ModifyInstanceAttributeRequest) InstanceBlockDeviceMappingSpecification(com.amazonaws.services.ec2.model.InstanceBlockDeviceMappingSpecification) ModifyInstanceAttributeResult(com.amazonaws.services.ec2.model.ModifyInstanceAttributeResult) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes)

Example 47 with VolumeSetAttributes

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));
}
Also used : AwsResourceNameService(com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) CREATED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.CREATED) AsyncTaskExecutor(org.springframework.core.task.AsyncTaskExecutor) LoggerFactory(org.slf4j.LoggerFactory) AwsDiskType(com.sequenceiq.common.model.AwsDiskType) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) TagSpecification(com.amazonaws.services.ec2.model.TagSpecification) StringUtils(org.apache.commons.lang3.StringUtils) DeviceNameGenerator(com.sequenceiq.cloudbreak.util.DeviceNameGenerator) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) Future(java.util.concurrent.Future) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) FAILED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.FAILED) CreateVolumeRequest(com.amazonaws.services.ec2.model.CreateVolumeRequest) Map(java.util.Map) EbsInstanceBlockDeviceSpecification(com.amazonaws.services.ec2.model.EbsInstanceBlockDeviceSpecification) Pair(org.springframework.data.util.Pair) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) CloudVolumeUsageType(com.sequenceiq.cloudbreak.cloud.model.CloudVolumeUsageType) IN_PROGRESS(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.IN_PROGRESS) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Optional(java.util.Optional) AwsMethodExecutor(com.sequenceiq.cloudbreak.cloud.aws.common.util.AwsMethodExecutor) NotNull(org.jetbrains.annotations.NotNull) AwsInstanceView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsInstanceView) HashMap(java.util.HashMap) Image(com.sequenceiq.cloudbreak.cloud.model.Image) ResourceRetriever(com.sequenceiq.cloudbreak.cloud.service.ResourceRetriever) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) InstanceBlockDeviceMappingSpecification(com.amazonaws.services.ec2.model.InstanceBlockDeviceMappingSpecification) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) CreateVolumeResult(com.amazonaws.services.ec2.model.CreateVolumeResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsContext(com.sequenceiq.cloudbreak.cloud.aws.common.context.AwsContext) Qualifier(org.springframework.beans.factory.annotation.Qualifier) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Subnet(com.amazonaws.services.ec2.model.Subnet) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) ATTACHED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.ATTACHED) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) DELETED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.DELETED) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) ModifyInstanceAttributeResult(com.amazonaws.services.ec2.model.ModifyInstanceAttributeResult) Component(org.springframework.stereotype.Component) PreserveResourceException(com.sequenceiq.cloudbreak.cloud.template.compute.PreserveResourceException) ModifyInstanceAttributeRequest(com.amazonaws.services.ec2.model.ModifyInstanceAttributeRequest) DeleteVolumeRequest(com.amazonaws.services.ec2.model.DeleteVolumeRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) Collections(java.util.Collections) Volume(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes.Volume) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) DeleteVolumeRequest(com.amazonaws.services.ec2.model.DeleteVolumeRequest) Volume(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes.Volume) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes)

Example 48 with VolumeSetAttributes

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;
}
Also used : CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) DELETED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.DELETED) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) PreserveResourceException(com.sequenceiq.cloudbreak.cloud.template.compute.PreserveResourceException)

Example 49 with VolumeSetAttributes

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())));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Region(com.sequenceiq.cloudbreak.cloud.model.Region) HashMap(java.util.HashMap) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) Map(java.util.Map) Spy(org.mockito.Spy) Location(com.sequenceiq.cloudbreak.cloud.model.Location) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) InjectMocks(org.mockito.InjectMocks) Security(com.sequenceiq.cloudbreak.cloud.model.Security) ResourceType(com.sequenceiq.common.api.type.ResourceType) Collections.emptyMap(java.util.Collections.emptyMap) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ResourceBuilderContext(com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext) Assert.assertTrue(org.junit.Assert.assertTrue) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) TemporaryStorage(com.sequenceiq.cloudbreak.common.type.TemporaryStorage) VolumeMatcher(com.sequenceiq.cloudbreak.cloud.template.context.VolumeMatcher) InstanceGroupType(com.sequenceiq.common.api.type.InstanceGroupType) Test(org.junit.jupiter.api.Test) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Optional(java.util.Optional) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) OutboundInternetTraffic(com.sequenceiq.common.api.type.OutboundInternetTraffic) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Group(com.sequenceiq.cloudbreak.cloud.model.Group) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) ResourceBuilderContext(com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) Test(org.junit.jupiter.api.Test)

Aggregations

VolumeSetAttributes (com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes)49 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)27 ArrayList (java.util.ArrayList)26 Test (org.junit.jupiter.api.Test)18 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)16 List (java.util.List)16 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)14 ResourceType (com.sequenceiq.common.api.type.ResourceType)14 Group (com.sequenceiq.cloudbreak.cloud.model.Group)12 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)12 Volume (com.sequenceiq.cloudbreak.cloud.model.Volume)12 CommonStatus (com.sequenceiq.common.api.type.CommonStatus)12 Collectors (java.util.stream.Collectors)12 Optional (java.util.Optional)11 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)10 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)10 Resource (com.sequenceiq.cloudbreak.domain.Resource)10 InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)10 Inject (javax.inject.Inject)10 Logger (org.slf4j.Logger)10