@Test public void tesTagRootVolumesForInstancesMoreThanSingleRequestLimit() { int instanceCount = 1200; CloudResource instance = CloudResource.builder() .type(ResourceType.AWS_INSTANCE).instanceId(INSTANCE_ID).name(INSTANCE_ID).status(CommonStatus.CREATED).build(); Instance awsInstance = new Instance() .withInstanceId(INSTANCE_ID) .withBlockDeviceMappings(new InstanceBlockDeviceMapping() .withDeviceName("/dev/sda1") .withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID))) .withRootDeviceName("/dev/sda1"); List<CloudResource> instanceList = new ArrayList<>(instanceCount); List<Instance> awsInstances = new ArrayList<>(instanceCount); for (int i = 0; i < instanceCount; i++) { instanceList.add(instance); awsInstances.add(awsInstance); } DescribeInstancesResult describeResult = new DescribeInstancesResult() .withReservations(new Reservation().withInstances(awsInstances)); AmazonEC2Client ec2Client = mock(AmazonEC2Client.class); when(ec2Client.describeInstances(any())).thenReturn(describeResult); awsTaggingService.tagRootVolumes(authenticatedContext(), ec2Client, instanceList, Map.of()); verify(ec2Client, times(2)).createTags(tagRequestCaptor.capture()); List<CreateTagsRequest> requests = tagRequestCaptor.getAllValues(); assertEquals(1000, requests.get(0).getResources().size()); assertEquals(200, requests.get(1).getResources().size()); }
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);  }