@Test public void tesTagRootVolumesForSingleInstance() { CloudResource instance = CloudResource.builder() .type(ResourceType.AWS_INSTANCE).instanceId(INSTANCE_ID).name(INSTANCE_ID).status(CommonStatus.CREATED).build(); DescribeInstancesResult describeResult = new DescribeInstancesResult() .withReservations(new Reservation() .withInstances(new Instance() .withInstanceId(INSTANCE_ID) .withBlockDeviceMappings(new InstanceBlockDeviceMapping() .withDeviceName("/dev/sda1") .withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID))) .withRootDeviceName("/dev/sda1")) ); AmazonEC2Client ec2Client = mock(AmazonEC2Client.class); when(ec2Client.describeInstances(any())).thenReturn(describeResult); Map<String, String> userTags = Map.of("key1", "val1", "key2", "val2"); awsTaggingService.tagRootVolumes(authenticatedContext(), ec2Client, List.of(instance), userTags); verify(ec2Client, times(1)).createTags(tagRequestCaptor.capture()); CreateTagsRequest request = tagRequestCaptor.getValue(); assertEquals(1, request.getResources().size()); assertEquals(VOLUME_ID, request.getResources().get(0)); List<com.amazonaws.services.ec2.model.Tag> tags = request.getTags(); assertThat(tags, containsInAnyOrder( hasProperty("key", Matchers.is("key1")), hasProperty("key", Matchers.is("key2")) )); assertThat(tags, containsInAnyOrder( hasProperty("value", Matchers.is("val1")), hasProperty("value", Matchers.is("val2")) )); }
public void tagRootVolumes(AuthenticatedContext ac, AmazonEC2Client ec2Client, List<CloudResource> instanceResources, Map<String, String> userDefinedTags) { String stackName = ac.getCloudContext().getName(); LOGGER.debug("Fetch AWS instances to collect all root volume ids for stack: {}", stackName); List<String> instanceIds = instanceResources.stream().map(CloudResource::getInstanceId).collect(Collectors.toList()); DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds)); List<Instance> instances = describeInstancesResult.getReservations().stream().flatMap(res -> res.getInstances().stream()).collect(Collectors.toList()); List<String> rootVolumeIds = instances.stream() .map(this::getRootVolumeId) .filter(Optional::isPresent) .map(blockDeviceMapping -> blockDeviceMapping.get().getEbs().getVolumeId()) .collect(Collectors.toList()); int instanceCount = instances.size(); int volumeCount = rootVolumeIds.size(); if (instanceCount != volumeCount) { LOGGER.debug("Did not find all root volumes, instanceResources: {}, found root volumes: {} for stack: {}", instanceCount, volumeCount, stackName); } else { LOGGER.debug("Found all ({}) root volumes for stack: {}", volumeCount, stackName); } AtomicInteger counter = new AtomicInteger(); Collection<List<String>> volumeIdChunks = rootVolumeIds.stream() .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / MAX_RESOURCE_PER_REQUEST)).values(); Collection<Tag> tags = prepareEc2Tags(ac, userDefinedTags); for (List<String> volumeIds : volumeIdChunks) { LOGGER.debug("Tag {} root volumes for stack: {}", volumeIds.size(), stackName); ec2Client.createTags(new CreateTagsRequest().withResources(volumeIds).withTags(tags)); } }
AwsTaggingService { public void tagRootVolumes(AuthenticatedContext ac, AmazonEC2Client ec2Client, List<CloudResource> instanceResources, Map<String, String> userDefinedTags) { String stackName = ac.getCloudContext().getName(); LOGGER.debug("Fetch AWS instances to collect all root volume ids for stack: {}", stackName); List<String> instanceIds = instanceResources.stream().map(CloudResource::getInstanceId).collect(Collectors.toList()); DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds)); List<Instance> instances = describeInstancesResult.getReservations().stream().flatMap(res -> res.getInstances().stream()).collect(Collectors.toList()); List<String> rootVolumeIds = instances.stream() .map(this::getRootVolumeId) .filter(Optional::isPresent) .map(blockDeviceMapping -> blockDeviceMapping.get().getEbs().getVolumeId()) .collect(Collectors.toList()); int instanceCount = instances.size(); int volumeCount = rootVolumeIds.size(); if (instanceCount != volumeCount) { LOGGER.debug("Did not find all root volumes, instanceResources: {}, found root volumes: {} for stack: {}", instanceCount, volumeCount, stackName); } else { LOGGER.debug("Found all ({}) root volumes for stack: {}", volumeCount, stackName); } AtomicInteger counter = new AtomicInteger(); Collection<List<String>> volumeIdChunks = rootVolumeIds.stream() .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / MAX_RESOURCE_PER_REQUEST)).values(); Collection<Tag> tags = prepareEc2Tags(ac, userDefinedTags); for (List<String> volumeIds : volumeIdChunks) { LOGGER.debug("Tag {} root volumes for stack: {}", volumeIds.size(), stackName); ec2Client.createTags(new CreateTagsRequest().withResources(volumeIds).withTags(tags)); } } }
AwsTaggingService { public void tagRootVolumes(AuthenticatedContext ac, AmazonEC2Client ec2Client, List<CloudResource> instanceResources, Map<String, String> userDefinedTags) { String stackName = ac.getCloudContext().getName(); LOGGER.debug("Fetch AWS instances to collect all root volume ids for stack: {}", stackName); List<String> instanceIds = instanceResources.stream().map(CloudResource::getInstanceId).collect(Collectors.toList()); DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds)); List<Instance> instances = describeInstancesResult.getReservations().stream().flatMap(res -> res.getInstances().stream()).collect(Collectors.toList()); List<String> rootVolumeIds = instances.stream() .map(this::getRootVolumeId) .filter(Optional::isPresent) .map(blockDeviceMapping -> blockDeviceMapping.get().getEbs().getVolumeId()) .collect(Collectors.toList()); int instanceCount = instances.size(); int volumeCount = rootVolumeIds.size(); if (instanceCount != volumeCount) { LOGGER.debug("Did not find all root volumes, instanceResources: {}, found root volumes: {} for stack: {}", instanceCount, volumeCount, stackName); } else { LOGGER.debug("Found all ({}) root volumes for stack: {}", volumeCount, stackName); } AtomicInteger counter = new AtomicInteger(); Collection<List<String>> volumeIdChunks = rootVolumeIds.stream() .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / MAX_RESOURCE_PER_REQUEST)).values(); Collection<Tag> tags = prepareEc2Tags(ac, userDefinedTags); for (List<String> volumeIds : volumeIdChunks) { LOGGER.debug("Tag {} root volumes for stack: {}", volumeIds.size(), stackName); ec2Client.createTags(new CreateTagsRequest().withResources(volumeIds).withTags(tags)); } }  }
AwsTaggingService { public void tagRootVolumes(AuthenticatedContext ac, AmazonEC2Client ec2Client, List<CloudResource> instanceResources, Map<String, String> userDefinedTags) { String stackName = ac.getCloudContext().getName(); LOGGER.debug("Fetch AWS instances to collect all root volume ids for stack: {}", stackName); List<String> instanceIds = instanceResources.stream().map(CloudResource::getInstanceId).collect(Collectors.toList()); DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds)); List<Instance> instances = describeInstancesResult.getReservations().stream().flatMap(res -> res.getInstances().stream()).collect(Collectors.toList()); List<String> rootVolumeIds = instances.stream() .map(this::getRootVolumeId) .filter(Optional::isPresent) .map(blockDeviceMapping -> blockDeviceMapping.get().getEbs().getVolumeId()) .collect(Collectors.toList()); int instanceCount = instances.size(); int volumeCount = rootVolumeIds.size(); if (instanceCount != volumeCount) { LOGGER.debug("Did not find all root volumes, instanceResources: {}, found root volumes: {} for stack: {}", instanceCount, volumeCount, stackName); } else { LOGGER.debug("Found all ({}) root volumes for stack: {}", volumeCount, stackName); } AtomicInteger counter = new AtomicInteger(); Collection<List<String>> volumeIdChunks = rootVolumeIds.stream() .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / MAX_RESOURCE_PER_REQUEST)).values(); Collection<Tag> tags = prepareEc2Tags(ac, userDefinedTags); for (List<String> volumeIds : volumeIdChunks) { LOGGER.debug("Tag {} root volumes for stack: {}", volumeIds.size(), stackName); ec2Client.createTags(new CreateTagsRequest().withResources(volumeIds).withTags(tags)); } }  Collection<com.amazonaws.services.cloudformation.model.Tag> prepareCloudformationTags(AuthenticatedContext ac, Map<String, String> userDefinedTags); Collection<com.amazonaws.services.ec2.model.Tag> prepareEc2Tags(AuthenticatedContext ac, Map<String, String> userDefinedTags); void tagRootVolumes(AuthenticatedContext ac, AmazonEC2Client ec2Client, List<CloudResource> instanceResources, Map<String, String> userDefinedTags); }
AwsTaggingService { public void tagRootVolumes(AuthenticatedContext ac, AmazonEC2Client ec2Client, List<CloudResource> instanceResources, Map<String, String> userDefinedTags) { String stackName = ac.getCloudContext().getName(); LOGGER.debug("Fetch AWS instances to collect all root volume ids for stack: {}", stackName); List<String> instanceIds = instanceResources.stream().map(CloudResource::getInstanceId).collect(Collectors.toList()); DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds)); List<Instance> instances = describeInstancesResult.getReservations().stream().flatMap(res -> res.getInstances().stream()).collect(Collectors.toList()); List<String> rootVolumeIds = instances.stream() .map(this::getRootVolumeId) .filter(Optional::isPresent) .map(blockDeviceMapping -> blockDeviceMapping.get().getEbs().getVolumeId()) .collect(Collectors.toList()); int instanceCount = instances.size(); int volumeCount = rootVolumeIds.size(); if (instanceCount != volumeCount) { LOGGER.debug("Did not find all root volumes, instanceResources: {}, found root volumes: {} for stack: {}", instanceCount, volumeCount, stackName); } else { LOGGER.debug("Found all ({}) root volumes for stack: {}", volumeCount, stackName); } AtomicInteger counter = new AtomicInteger(); Collection<List<String>> volumeIdChunks = rootVolumeIds.stream() .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / MAX_RESOURCE_PER_REQUEST)).values(); Collection<Tag> tags = prepareEc2Tags(ac, userDefinedTags); for (List<String> volumeIds : volumeIdChunks) { LOGGER.debug("Tag {} root volumes for stack: {}", volumeIds.size(), stackName); ec2Client.createTags(new CreateTagsRequest().withResources(volumeIds).withTags(tags)); } }  Collection<com.amazonaws.services.cloudformation.model.Tag> prepareCloudformationTags(AuthenticatedContext ac, Map<String, String> userDefinedTags); Collection<com.amazonaws.services.ec2.model.Tag> prepareEc2Tags(AuthenticatedContext ac, Map<String, String> userDefinedTags); void tagRootVolumes(AuthenticatedContext ac, AmazonEC2Client ec2Client, List<CloudResource> instanceResources, Map<String, String> userDefinedTags);  }