@Override public List<Instance> call() { RunInstancesRequest request = new RunInstancesRequest(); request.withInstanceType(this.instanceTemplate.getInstanceType()); request.withImageId(this.instanceTemplate.getAmiId()); InstanceNetworkInterfaceSpecification nic = new InstanceNetworkInterfaceSpecification(); nic.withDeviceIndex(0); // select a subnet at random nic.withSubnetId(randomSubnet()); nic.withAssociatePublicIpAddress(this.instanceTemplate.isAssignPublicIp()); nic.withGroups(this.instanceTemplate.getSecurityGroupIds()); request.withNetworkInterfaces(nic); request.withKeyName(this.instanceTemplate.getKeyPair()); request.withIamInstanceProfile( new IamInstanceProfileSpecification().withArn(this.instanceTemplate.getIamInstanceProfileARN())); request.withUserData(this.instanceTemplate.getEncodedUserData()); request.withEbsOptimized(this.instanceTemplate.isEbsOptimized()); request.withMinCount(this.count).withMaxCount(this.count); if (!this.instanceTemplate.getTags().isEmpty()) { TagSpecification tagSpecifications = new TagSpecification().withResourceType(ResourceType.Instance); tagSpecifications.withTags(tags()); request.withTagSpecifications(tagSpecifications); } RunInstancesResult result = getClient().getApi().runInstances(request); List<Instance> launchedInstances = result.getReservation().getInstances(); List<String> instanceIds = launchedInstances.stream().map(Instance::getInstanceId).collect(Collectors.toList()); return awaitInstances(instanceIds); }
@Test public void getObject_userTagDataAvailable_objectContainsAllAvailableKeys() throws Exception { //Arrange AmazonEC2 amazonEC2 = mock(AmazonEC2.class); InstanceIdProvider instanceIdProvider = mock(InstanceIdProvider.class); when(instanceIdProvider.getCurrentInstanceId()).thenReturn("1234567890"); DescribeTagsRequest describeTagsRequest = new DescribeTagsRequest().withFilters( new Filter("resource-id", Collections.singletonList("1234567890")), new Filter("resource-type", Collections.singletonList("instance"))); DescribeTagsResult describeTagsResult = new DescribeTagsResult().withTags( new TagDescription().withKey("keyA").withResourceType(ResourceType.Instance).withValue("valueA"), new TagDescription().withKey("keyB").withResourceType(ResourceType.Instance).withValue("valueB") ); when(amazonEC2.describeTags(describeTagsRequest)).thenReturn(describeTagsResult); AmazonEc2InstanceUserTagsFactoryBean amazonEc2InstanceUserTagsFactoryBean = new AmazonEc2InstanceUserTagsFactoryBean(amazonEC2, instanceIdProvider); //Act amazonEc2InstanceUserTagsFactoryBean.afterPropertiesSet(); Map<String, String> resultMap = amazonEc2InstanceUserTagsFactoryBean.getObject(); //Assert assertEquals("valueA", resultMap.get("keyA")); assertEquals("valueB", resultMap.get("keyB")); assertFalse(resultMap.containsKey("keyC")); }
/** * Builds a {@code RunInstancesRequest} starting from a template and a virtual instance ID. * Instances will be tagged as they're created. * * @param template the instance template * @param virtualInstanceId the virtual instance IDs * @param userDefinedTags user defined tags to attach to the instance */ @SuppressWarnings("ConstantConditions") private RunInstancesRequest newRunInstancesRequest(EC2InstanceTemplate template, String virtualInstanceId, List<Tag> userDefinedTags) { String image = template.getImage(); String type = template.getType(); InstanceNetworkInterfaceSpecification network = getInstanceNetworkInterfaceSpecification(template); List<BlockDeviceMapping> deviceMappings = getBlockDeviceMappings(template); LOG.info(">> Instance request type: {}, image: {}", type, image); List<Tag> tags = ec2TagHelper.getInstanceTags(template, virtualInstanceId, userDefinedTags); List<TagSpecification> tagSpecifications = Lists.newArrayList( new TagSpecification().withTags(tags).withResourceType(ResourceType.Instance), new TagSpecification().withTags(tags).withResourceType(ResourceType.Volume)); RunInstancesRequest request = new RunInstancesRequest() .withImageId(image) .withInstanceType(type) .withMaxCount(1) .withMinCount(1) .withClientToken(UUID.randomUUID().toString()) .withNetworkInterfaces(network) .withTagSpecifications(tagSpecifications) .withBlockDeviceMappings(deviceMappings) .withEbsOptimized(template.isEbsOptimized()); if (template.getIamProfileName().isPresent()) { request.withIamInstanceProfile(new IamInstanceProfileSpecification() .withName(template.getIamProfileName().get())); } if (template.getKeyName().isPresent()) { request.withKeyName(template.getKeyName().get()); } Placement placement = null; if (template.getAvailabilityZone().isPresent()) { placement = new Placement().withAvailabilityZone(template.getAvailabilityZone().get()); } if (template.getPlacementGroup().isPresent()) { placement = (placement == null) ? new Placement().withGroupName(template.getPlacementGroup().get()) : placement.withGroupName(template.getPlacementGroup().get()); } placement = (placement == null) ? new Placement().withTenancy(template.getTenancy()) : placement.withTenancy(template.getTenancy()); request.withPlacement(placement); Optional<String> userData = template.getUserData(); if (userData.isPresent()) { request.withUserData(userData.get()); } return request; }