public void testFilterByTags() throws InterruptedException { int nodes = randomIntBetween(5, 10); Settings nodeSettings = Settings.builder() .put(DISCOVERY_EC2.TAG_SETTING.getKey() + "stage", "prod") .build(); int prodInstances = 0; List<List<Tag>> tagsList = new ArrayList<>(); for (int node = 0; node < nodes; node++) { List<Tag> tags = new ArrayList<>(); if (randomBoolean()) { tags.add(new Tag("stage", "prod")); prodInstances++; } else { tags.add(new Tag("stage", "dev")); } tagsList.add(tags); } logger.info("started [{}] instances with [{}] stage=prod tag", nodes, prodInstances); List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes, tagsList); assertThat(discoveryNodes, hasSize(prodInstances)); }
public AmazonEC2Mock(int nodes, List<List<Tag>> tagsList) { if (tagsList != null) { assert tagsList.size() == nodes; } for (int node = 1; node < nodes + 1; node++) { String instanceId = "node" + node; Instance instance = new Instance() .withInstanceId(instanceId) .withState(new InstanceState().withName(InstanceStateName.Running)) .withPrivateDnsName(PREFIX_PRIVATE_DNS + instanceId + SUFFIX_PRIVATE_DNS) .withPublicDnsName(PREFIX_PUBLIC_DNS + instanceId + SUFFIX_PUBLIC_DNS) .withPrivateIpAddress(PREFIX_PRIVATE_IP + node) .withPublicIpAddress(PREFIX_PUBLIC_IP + node); if (tagsList != null) { instance.setTags(tagsList.get(node-1)); } instances.add(instance); } }
private List<Server> convertInstanceToServer(List<Instance> instances) { List<Server> servers = new ArrayList<>(); for (Instance instance : instances) { Server server = new Server(instance.getInstanceId()); for (Tag tag : instance.getTags()) { if (tag != null && tag.getKey() != null && tag.getKey().equals("Name")) { server.setName(tag.getValue()); } } server.setStatus(instance.getState().getName()); server.setType(instance.getInstanceType()); server.setPublicIP(Arrays.asList(instance.getPublicIpAddress())); server.setPrivateIP(Arrays.asList(instance.getPrivateIpAddress())); servers.add(server); } return servers; }
/** * Update tags for one all more instance * @param instanceIds * @param tags * @throws Exception */ @Override public void setTagsForInstances(List<String> instanceIds, List<Tag> tags) throws Exception { Preconditions.checkNotNull(instanceIds); Preconditions.checkNotNull(tags); awsRateLimiter.acquire(); OperationStats op = new OperationStats("ec2InstanceStore", "setTagsForInstances"); try { if (tags.size() > 0) { CreateTagsRequest req = new CreateTagsRequest(instanceIds, tags); defaultClient.createTags(req); } op.succeed(); } catch (Exception ex) { op.failed(); throw ex; } }
/** * Return a list of tags that need to be updated to the Ec2Instance. * The tags are either not in Ec2Instance tags or having different * values * @param ec2Instance * @param esInstance * @return A list of tags */ public List<Tag> getUpdateTags(Instance ec2Instance, EsInstance esInstance) { Preconditions.checkNotNull(ec2Instance); Preconditions.checkNotNull(esInstance); List<Tag> updateTags = new ArrayList<>(); List<Tag> currentEc2Tag = ec2Instance.getTags(); List<Tag> esUploadTags = getUpdateTags(esInstance); for (Tag tag : esUploadTags) { boolean shouldUpdate = true; for (Tag ec2Tag : currentEc2Tag) { if (ec2Tag.getKey().equals(tag.getKey()) && ec2Tag.getValue().equals(tag.getValue())) { shouldUpdate = false; break; } } if (shouldUpdate) { updateTags.add(tag); } } return updateTags; }
private List<Instance> processResponse(DescribeInstancesResult response) { List<Instance> returnedInstances = new ArrayList<>(); for (Reservation reservation : response.getReservations()) { for (com.amazonaws.services.ec2.model.Instance instance : reservation.getInstances()) { // Don't show dead servers or servers not tagged as belonging to Swordfish if (instance.getState().getName().equals("terminated") || !instance.getTags().contains(new Tag().withKey("Swordfish").withValue("true"))) { continue; } Instance instanceDetails = getInstanceDetails(instance); returnedInstances.add(instanceDetails); instanceRepository.save(instanceDetails); refreshClient(instanceDetails); } } return returnedInstances; }
void attachSnapshotTags(AmazonEC2Async client, String sourceSnapshotId, String snapshotId) { DescribeSnapshotsResult result = client .describeSnapshots(new DescribeSnapshotsRequest().withSnapshotIds(sourceSnapshotId)); List<Snapshot> snapshots = result.getSnapshots(); if (snapshots.size() != 1) { throw new RuntimeException("snapshot can not found. sourceSnapshotId[" + snapshotId + "]"); } List<Tag> sourceSnapshotTags = snapshots.get(0).getTags(); List<Tag> tags = new ArrayList<Tag>(); tags.addAll(sourceSnapshotTags); tags.add(new Tag("SourceSnapshotId", sourceSnapshotId)); tags.add(new Tag("BackupType", "copy-snapshot")); // overwrite CreateTagsRequest snapshotTagsRequest = new CreateTagsRequest().withResources(snapshotId); snapshotTagsRequest.setTags(tags); client.createTags(snapshotTagsRequest); }
public DeferredResult<Void> createNameTagAsync(String resourceId, String name) { Tag nameTag = new Tag().withKey(AWS_TAG_NAME).withValue(name); CreateTagsRequest request = new CreateTagsRequest() .withResources(resourceId) .withTags(nameTag); String message = "Name tag AWS resource with id [" + resourceId + "] with name [" + name + "]."; AWSDeferredResultAsyncHandler<CreateTagsRequest, CreateTagsResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message); this.client.createTagsAsync(request, handler); return handler.toDeferredResult() .thenApply(result -> (Void) null); }
private void setResourceTags(ResourceState resourceState, List<Tag> tags) { if (tags != null && !tags.isEmpty()) { Map<String, String> awsResourceTags = tags.stream().collect( Collectors.toMap(tag -> tag.getKey(), tag -> tag.getValue())); // The name of the compute state is the value of the AWS_TAG_NAME tag String nameTag = awsResourceTags.get(AWSConstants.AWS_TAG_NAME); if (nameTag != null) { resourceState.name = nameTag; awsResourceTags.remove(AWSConstants.AWS_TAG_NAME); } // add tag links setTagLinksToResourceState(resourceState, awsResourceTags, true); } }
/** * Tags provisioned Spot instances. Expects that the instances already exists or are in the * process of being created. * * @throws InterruptedException if the operation is interrupted */ @VisibleForTesting @SuppressWarnings("PMD.UselessParentheses") protected void tagSpotInstances() throws InterruptedException { // Pre-compute user-defined tags for efficiency List<Tag> userDefinedTags = ec2TagHelper.getUserDefinedTags(template); for (SpotAllocationRecord spotAllocationRecord : spotAllocationRecordsByVirtualInstanceId.values()) { if ((spotAllocationRecord.ec2InstanceId != null) && !spotAllocationRecord.instanceTagged && tagSpotInstance(template, userDefinedTags, spotAllocationRecord.virtualInstanceId, spotAllocationRecord.ec2InstanceId)) { spotAllocationRecord.instanceTagged = true; } } }
/** * Tags an EC2 instance. Expects that the instance already exists or is in the process of * being created. This may also tag EBS volumes depending on template configurations. * * @param template the instance template * @param userDefinedTags the user-defined tags * @param virtualInstanceId the virtual instance id * @param ec2InstanceId the EC2 instance id * @return true if the instance was successfully tagged, false otherwise * @throws InterruptedException if the operation is interrupted */ private boolean tagSpotInstance(EC2InstanceTemplate template, List<Tag> userDefinedTags, String virtualInstanceId, String ec2InstanceId) throws InterruptedException { LOG.info(">> Tagging instance {} / {}", ec2InstanceId, virtualInstanceId); // We have to individually tag the spot instance and it's associated volumes // since AWS doesn't allow specifying tags as part of the launch request for // spot instances. // Wait for the instance to be started. If it is terminating, skip tagging. if (!waitUntilInstanceHasStarted(ec2InstanceId)) { return false; } List<Tag> tags = ec2TagHelper.getInstanceTags(template, virtualInstanceId, userDefinedTags); client.createTags(new CreateTagsRequest().withTags(tags).withResources(ec2InstanceId)); // Tag EBS volumes if they were part of instance launch request if (EBSAllocationStrategy.get(template) == EBSAllocationStrategy.AS_INSTANCE_REQUEST) { tagSpotEbsVolumes(ec2InstanceId, virtualInstanceId, tags); } return true; }
/** * Search Ec2 Instance by Name tag. * * @param ec2 * @param targetName * Search Keyword for Name tag * @return Instance with Name tag equals targetName. If it does't found, * then return null. */ public static Instance findInstanceByName(AmazonEC2 ec2, String targetName) { DescribeInstancesResult instanceResult = ec2.describeInstances(); List<Reservation> reservations = instanceResult.getReservations(); for (Reservation reservation : reservations) { List<Instance> instances = reservation.getInstances(); for (Instance instance : instances) { List<Tag> tagList = instance.getTags(); String name = ""; for (Tag tag : tagList) { String tagKey = tag.getKey(); String tagValue = tag.getValue(); if (tagKey.contains("Name")) { name = tagValue; if (targetName.equals(name)) { return instance; } break; } } } } return null; }
public static VolumeDto convert(Volume volume) { VolumeDto volumeDto = new VolumeDto(); BeanUtils.copyProperties(volume, volumeDto); List<Tag> tags = new ArrayList<>(); for (Tag tag : volume.getTags()) { // check whether volume has Name tag if (tag.getKey().equals("Name")) { volumeDto.setVolumeName(tag.getValue()); } else { tags.add(tag); } } volumeDto.setTags(tags); // check whether volume is attached to instance if (volume.getAttachments().size() > 0) { volumeDto.setInstanceID(volume.getAttachments().get(0).getInstanceId()); } return volumeDto; }
@Test public void convertVolumeList() { List<Volume> volumeList = new ArrayList<>(); //with tags volumeList.add(createVolume(VOLUME_ID, SNAPSHOT_ID, CREATE_TIME, AVAILABILITY_ZONE, SIZE, STATE, tags, INSTANCE_ID)); // without tags volumeList.add(createVolume(VOLUME_ID + "_", SNAPSHOT_ID + "_", CREATE_TIME, AVAILABILITY_ZONE + "_", SIZE + 1, STATE + "_", new ArrayList<Tag>(), INSTANCE_ID + "_")); Set<VolumeDto> volumeDtoList = VolumeDtoConverter.convert(volumeList); Assert.assertTrue(volumeDtoList.size() == 2); assertVolumeDtoFields((VolumeDto) volumeDtoList.toArray()[0], VOLUME_ID, SNAPSHOT_ID, CREATE_TIME, AVAILABILITY_ZONE, SIZE, STATE, tags, INSTANCE_ID); assertVolumeDtoFields((VolumeDto) volumeDtoList.toArray()[1], VOLUME_ID + "_", SNAPSHOT_ID + "_", CREATE_TIME, AVAILABILITY_ZONE + "_", SIZE + 1, STATE + "_", new ArrayList<Tag>(), INSTANCE_ID + "_"); }
private Volume createVolume(String volId, String snapId, Date createDate, String zone, int size, String state, List<Tag> tags, String instance_id) { Volume volume = new Volume(); volume.setVolumeId(volId); volume.setSnapshotId(snapId); volume.setCreateTime(createDate); volume.setAvailabilityZone(zone); volume.setState(state); volume.setSize(size); volume.setTags(tags); if (instance_id != null) { List<VolumeAttachment> volumeAttachmentList = new ArrayList(); VolumeAttachment volumeAttachment = new VolumeAttachment(); volumeAttachment.setInstanceId(instance_id); volumeAttachmentList.add(volumeAttachment); volume.setAttachments(volumeAttachmentList); } return volume; }
public SubnetDTO(final Subnet subnet) { this.subnetId = subnet.getSubnetId(); this.vpcId = subnet.getVpcId(); this.state = subnet.getState(); this.availabilityZone = subnet.getAvailabilityZone(); this.cidrBlock = subnet.getCidrBlock(); this.tags.addAll( subnet.getTags() .stream() .map(TagDTO::new) .collect(Collectors.toList())); this.name = subnet.getTags() .stream() .filter(t -> t.getKey().equals("Name")) .findFirst() .map(Tag::getValue) .orElse("n/a"); }
public VpcDTO(final Vpc vpc) { this.vpcId = vpc.getVpcId(); this.cidrBlock = vpc.getCidrBlock(); this.state = vpc.getState(); this.tags.addAll( vpc.getTags() .stream() .map(TagDTO::new) .collect(Collectors.toList())); this.name = vpc.getTags() .stream() .filter(t -> t.getKey().equals("Name")) .findFirst() .map(Tag::getValue) .orElse("n/a"); }
public List<Instance> runInstances(RunInstancesRequest request, Tag... tags) throws Exception { logger.info("create ec2 instance, request={}", request); RunInstancesResult result = new Runner<RunInstancesResult>() .maxAttempts(3) .retryInterval(Duration.ofSeconds(20)) .retryOn(this::retryOnRunInstance) .run(() -> ec2.runInstances(request)); Threads.sleepRoughly(Duration.ofSeconds(5)); // wait little bit to make sure instance is visible to tag service List<String> instanceIds = result.getReservation().getInstances().stream().map(Instance::getInstanceId).collect(Collectors.toList()); CreateTagsRequest tagsRequest = new CreateTagsRequest() .withResources(instanceIds) .withTags(tags); createTags(tagsRequest); waitUntilRunning(instanceIds); return describeInstances(instanceIds); }
private List<Instance> runningInstances(String resourceId) { Tag tag = new EC2TagHelper(env).resourceId(resourceId); DescribeTagsRequest request = new DescribeTagsRequest() .withFilters(new Filter("key").withValues(tag.getKey()), new Filter("value").withValues(tag.getValue()), new Filter("resource-type").withValues("instance")); List<TagDescription> remoteTags = AWS.ec2.describeTags(request); List<String> instanceIds = remoteTags.stream().map(TagDescription::getResourceId).collect(Collectors.toList()); if (instanceIds.isEmpty()) { com.amazonaws.services.autoscaling.model.AutoScalingGroup asGroup = AWS.as.describeASGroup(env.name + "-" + this.resourceId); if (asGroup == null) throw new Error("can not find any running instance or asGroup, id=" + this.resourceId); instanceIds = asGroup.getInstances().stream() .map(com.amazonaws.services.autoscaling.model.Instance::getInstanceId) .collect(Collectors.toList()); } logger.info("find instanceId, {} => {}", resourceId, instanceIds); List<Instance> instances = AWS.ec2.describeInstances(instanceIds) .stream().filter(instance -> "running".equals(instance.getState().getName())).collect(Collectors.toList()); if (instances.isEmpty()) throw new Error("can not find any running instance, id=" + resourceId); return instances; }
@Override public boolean tagEnvironmentWithVersion(Region region, DeployJobVariables jobVariables) { String searchTag = jobVariables.getEnvironment(); String version = jobVariables.getVersion(); LOGGER.info("tagEnvironmentWithVersion " + region + " Tag " + searchTag + " version " + version); boolean environmentSuccessfulTagged = false; ec2.setRegion(region); DescribeInstancesResult instances = ec2.describeInstances(); for (Reservation reservation : instances.getReservations()) { for (Instance instance : reservation.getInstances()) { for (Tag tag : instance.getTags()) { if (tag.getValue().equalsIgnoreCase(searchTag)) { CreateTagsRequest createTagsRequest = new CreateTagsRequest(); createTagsRequest.withResources(instance.getInstanceId()).withTags(new Tag(VERSION_TAG, version)); LOGGER.info("Create Tag " + version + " for instance " + instance.getInstanceId()); ec2.createTags(createTagsRequest); environmentSuccessfulTagged = true; } } } } return environmentSuccessfulTagged; }
/** * Sets tags for the specified instance * @param instanceId * @return */ private void setTagsForInstance(String instanceId) { Set<Object> keys = awsProperties.keySet(); List<Tag> tags = new ArrayList<>(); for(Object o : keys) { if(o instanceof String && ((String)o).startsWith("tag")) { String values = (String)awsProperties.get(o); String[] splitValues = values.split(","); String key = splitValues[0]; String value = splitValues[1]; Tag tagToAdd = new Tag(key,value); log.info("Adding tag: " + tagToAdd); tags.add(tagToAdd); } } // Including a hard coded tag here so we can track which resources originate from this plugin Tag nodeTag = new Tag("LaunchSource","SeleniumGridScalerPlugin"); log.info("Adding hard-coded tag: " + nodeTag); tags.add(nodeTag); CreateTagsRequest ctr = new CreateTagsRequest(Arrays.asList(instanceId),tags); ec2Client.createTags(ctr); }
public int getInstanceNumWithTag(Tag t) { assert (t != null); int num = 0; List<Reservation> reservations = ec2.describeInstances() .getReservations(); for (Reservation reservation : reservations) { List<Instance> instances = reservation.getInstances(); for (Instance instance : instances) { if (!instance.getState().getName().equals("pending") && !instance.getState().getName().equals("running")) continue; List<Tag> tags = instance.getTags(); for (Tag tag : tags) { if (tag.getKey().equals(t.getKey()) && tag.getValue().equals(t.getValue())) { num++; break; } } } } return num; }
@Override public void attachMachine(String machineId) throws NotFoundException, CloudPoolDriverException { checkState(isConfigured(), "attempt to use unconfigured Ec2PoolDriver"); // verify that machine exists this.client.getInstanceMetadata(machineId); try { Tag tag = new Tag(ScalingTags.CLOUD_POOL_TAG, getPoolName()); tagInstance(machineId, tag); } catch (Exception e) { Throwables.throwIfInstanceOf(e, NotFoundException.class); throw new CloudPoolDriverException( String.format("failed to attach '%s' to cloud pool: %s", machineId, e.getMessage()), e); } }
@Override public void setMembershipStatus(String machineId, MembershipStatus membershipStatus) throws NotFoundException, CloudPoolDriverException { checkState(isConfigured(), "attempt to use unconfigured Ec2PoolDriver"); // verify that machine exists in group getMachineOrFail(machineId); try { Tag tag = new Tag(ScalingTags.MEMBERSHIP_STATUS_TAG, JsonUtils.toString(toJson(membershipStatus))); tagInstance(machineId, tag); } catch (Exception e) { Throwables.throwIfInstanceOf(e, NotFoundException.class); throw new CloudPoolDriverException( String.format("failed to set membership status for instance %s: %s", machineId, e.getMessage()), e); } }
private void replaceTags(Instance instance, List<Tag> tags) { List<Tag> filteredTags = new ArrayList<>(instance.getTags()); // first remove any old occurrences of updated tags Iterator<Tag> iterator = filteredTags.iterator(); while (iterator.hasNext()) { Tag instanceTag = iterator.next(); for (Tag updatedTag : tags) { if (instanceTag.getKey().equals(updatedTag.getKey())) { iterator.remove(); } } } instance.setTags(filteredTags); // ... then add updated tags instance.getTags().addAll(tags); }
@Override public void setServiceState(String spotRequestId, ServiceState serviceState) throws NotFoundException, CloudPoolDriverException { checkState(isConfigured(), "attempt to use unconfigured driver"); try { verifyPoolMember(spotRequestId); Tag serviceStateTag = new Tag().withKey(SERVICE_STATE_TAG).withValue(serviceState.name()); this.client.tagResource(spotRequestId, asList(serviceStateTag)); } catch (Exception e) { Throwables.throwIfInstanceOf(e, NotFoundException.class); throw new CloudPoolDriverException( String.format("failed to set service state for %s: %s", spotRequestId, e.getMessage()), e); } }
@Override public void setMembershipStatus(String spotRequestId, MembershipStatus membershipStatus) throws NotFoundException, CloudPoolDriverException { checkState(isConfigured(), "attempt to use unconfigured driver"); try { verifyPoolMember(spotRequestId); Tag membershipStatusTag = new Tag().withKey(MEMBERSHIP_STATUS_TAG).withValue(membershipStatus.toString()); this.client.tagResource(spotRequestId, asList(membershipStatusTag)); } catch (Exception e) { Throwables.throwIfInstanceOf(e, NotFoundException.class); throw new CloudPoolDriverException( String.format("failed to set membership status for %s: %s", spotRequestId, e.getMessage()), e); } }
@Override public List<Instance> launchInstances(Ec2ProvisioningTemplate template, int count) throws AmazonClientException { List<Instance> launchedInstances = new ArrayList<>(); for (int i = 0; i < count; i++) { int idNum = ++this.idSequencer; String id = "i-" + idNum; LOG.info("launching instance {} into fake account ...", id); Instance newInstance = new Instance().withInstanceId(id).withState(new InstanceState().withName("pending")) .withPublicIpAddress("1.2.3." + idNum).withImageId(template.getAmiId()) .withInstanceType(template.getInstanceType()); if (!template.getTags().isEmpty()) { List<Tag> tags = new ArrayList<>(); for (Entry<String, String> tag : template.getTags().entrySet()) { tags.add(new Tag(tag.getKey(), tag.getValue())); } newInstance.withTags(tags); } this.instances.put(newInstance.getInstanceId(), newInstance); launchedInstances.add(newInstance); } return launchedInstances; }
@Override public void tagResource(String resourceId, List<Tag> tags) throws AmazonClientException { if (!this.instances.containsKey(resourceId) && !this.spotRequests.containsKey(resourceId)) { throw new AmazonServiceException(String.format("The ID '%s' is not valid (Service: AmazonEC2; " + "Status Code: 400; Error Code: InvalidID; " + "Request ID: 05ee0a2b-737b-4749-9b83-eb7eec710c68)", resourceId)); } if (this.instances.containsKey(resourceId)) { List<Tag> instanceTags = this.instances.get(resourceId).getTags(); instanceTags.addAll(tags); } if (this.spotRequests.containsKey(resourceId)) { List<Tag> spotTags = this.spotRequests.get(resourceId).getTags(); spotTags.addAll(tags); } }
/** * Verify that the group membership tag is removed from the server when * detaching a group member. */ @Test public void testDetach() { this.driver = new SpotPoolDriver(this.fakeClient, this.executor, this.mockEventBus); this.driver.configure(config()); this.fakeClient.setupFakeAccount( asList(spotRequest("sir-1", "open", null, POOL1_TAG), spotRequest("sir-2", "active", "i-2", POOL1_TAG), spotRequest("sir-3", "cancelled", "i-3", POOL1_TAG)), asList(instance("i-2", Running, "sir-2"), instance("i-3", Running, "sir-3"))); // pool membership tag should exist Tag poolTag = new Tag(CLOUD_POOL_TAG, POOL_NAME); assertTrue(this.fakeClient.getSpotInstanceRequest("sir-1").getTags().contains(poolTag)); this.driver.detachMachine("sir-1"); // pool membership tag should be gone assertFalse(this.fakeClient.getSpotInstanceRequest("sir-1").getTags().contains(poolTag)); }
@Test public void testHasTag() { Predicate<Instance> hasTag = InstancePredicates .hasTag(new Tag().withKey("expectedKey").withValue("expectedValue")); Map<String, String> noTags = ImmutableMap.of(); Instance i0 = makeInstance("i-0", "running", noTags); Instance i1 = makeInstance("i-1", "running", ImmutableMap.of("k1", "v1")); Instance i2 = makeInstance("i-2", "running", ImmutableMap.of("expectedKey", "expectedValue")); Instance i3 = makeInstance("i-3", "running", ImmutableMap.of("k1", "v1", // "expectedKey", "expectedValue")); assertFalse(hasTag.test(i0)); assertFalse(hasTag.test(i1)); assertTrue(hasTag.test(i2)); assertTrue(hasTag.test(i3)); }
@Override public void setServiceState(String machineId, ServiceState serviceState) throws NotFoundException, CloudPoolDriverException { checkState(isConfigured(), "attempt to use unconfigured driver"); // verify that machine exists in group getMachineOrFail(machineId); try { Tag tag = new Tag().withKey(ScalingTags.SERVICE_STATE_TAG).withValue(serviceState.name()); this.client.tagInstance(machineId, Arrays.asList(tag)); } catch (Exception e) { Throwables.throwIfInstanceOf(e, CloudPoolDriverException.class); String message = format("failed to tag service state on server \"%s\": %s", machineId, e.getMessage()); throw new CloudPoolDriverException(message, e); } }
@Override public void setMembershipStatus(String machineId, MembershipStatus membershipStatus) throws NotFoundException, CloudPoolDriverException { checkState(isConfigured(), "attempt to use unconfigured driver"); // verify that machine exists in group getMachineOrFail(machineId); try { Tag tag = new Tag().withKey(ScalingTags.MEMBERSHIP_STATUS_TAG) .withValue(JsonUtils.toString(toJson(membershipStatus))); this.client.tagInstance(machineId, Arrays.asList(tag)); } catch (Exception e) { Throwables.throwIfInstanceOf(e, CloudPoolDriverException.class); String message = format("failed to tag membership status on server \"%s\": %s", machineId, e.getMessage()); throw new CloudPoolDriverException(message, e); } }
/** * A {@link CloudPoolDriverException} should be thrown on failure to tag the * membership status of a pool member. */ @Test(expected = CloudPoolDriverException.class) public void setMembershipStatusOnError() { int desiredCapacity = 1; setUpMockedAutoScalingGroup(GROUP_NAME, ONDEMAND_LAUNCH_CONFIG, desiredCapacity, ec2Instances(ec2Instance("i-1", "running"))); MembershipStatus status = MembershipStatus.awaitingService(); String tagValue = JsonUtils.toString(JsonUtils.toJson(status)); List<Tag> statusTag = asList(new Tag().withKey(ScalingTags.MEMBERSHIP_STATUS_TAG).withValue(tagValue)); doThrow(new RuntimeException("API unreachable")).when(this.mockAwsClient).tagInstance("i-1", statusTag); this.driver.setMembershipStatus("i-1", status); this.driver.setServiceState("i-1", IN_SERVICE); }
public void tagInstance(String instanceId, String tag, String value, AmazonEC2 ec2Client) { System.out.println(instanceId); //quick fix try { Thread.sleep(1000); } catch (InterruptedException e) { // swallow } CreateTagsRequest request = new CreateTagsRequest(); request = request.withResources(instanceId) .withTags(new Tag(tag, value)); ec2Client.createTags(request); }
protected List<DiscoveryNode> buildDynamicNodes(Settings nodeSettings, int nodes, List<List<Tag>> tagsList) { AwsEc2Service awsEc2Service = new AwsEc2ServiceMock(nodeSettings, nodes, tagsList); AwsEc2UnicastHostsProvider provider = new AwsEc2UnicastHostsProvider(nodeSettings, transportService, awsEc2Service); List<DiscoveryNode> discoveryNodes = provider.buildDynamicNodes(); logger.debug("--> nodes found: {}", discoveryNodes); return discoveryNodes; }
public void testFilterByMultipleTags() throws InterruptedException { int nodes = randomIntBetween(5, 10); Settings nodeSettings = Settings.builder() .putArray(DISCOVERY_EC2.TAG_SETTING.getKey() + "stage", "prod", "preprod") .build(); int prodInstances = 0; List<List<Tag>> tagsList = new ArrayList<>(); for (int node = 0; node < nodes; node++) { List<Tag> tags = new ArrayList<>(); if (randomBoolean()) { tags.add(new Tag("stage", "prod")); if (randomBoolean()) { tags.add(new Tag("stage", "preprod")); prodInstances++; } } else { tags.add(new Tag("stage", "dev")); if (randomBoolean()) { tags.add(new Tag("stage", "preprod")); } } tagsList.add(tags); } logger.info("started [{}] instances with [{}] stage=prod tag", nodes, prodInstances); List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes, tagsList); assertThat(discoveryNodes, hasSize(prodInstances)); }
public void testReadHostFromTag() throws InterruptedException, UnknownHostException { int nodes = randomIntBetween(5, 10); String[] addresses = new String[nodes]; for (int node = 0; node < nodes; node++) { addresses[node] = "192.168.0." + (node + 1); poorMansDNS.put("node" + (node + 1), new TransportAddress(InetAddress.getByName(addresses[node]), 9300)); } Settings nodeSettings = Settings.builder() .put(DISCOVERY_EC2.HOST_TYPE_SETTING.getKey(), "tag:foo") .build(); List<List<Tag>> tagsList = new ArrayList<>(); for (int node = 0; node < nodes; node++) { List<Tag> tags = new ArrayList<>(); tags.add(new Tag("foo", "node" + (node + 1))); tagsList.add(tags); } logger.info("started [{}] instances", nodes); List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes, tagsList); assertThat(discoveryNodes, hasSize(nodes)); for (DiscoveryNode discoveryNode : discoveryNodes) { TransportAddress address = discoveryNode.getAddress(); TransportAddress expected = poorMansDNS.get(discoveryNode.getName()); assertEquals(address, expected); } }
private void createTags(PropertyHandler ph) throws APPlatformException { List<Tag> tags = new ArrayList<Tag>(); tags.add(new Tag(PropertyHandler.TAG_NAME, ph.getInstanceName())); tags.add(new Tag(PropertyHandler.TAG_SUBSCRIPTION_ID, ph.getSettings() .getSubscriptionId())); tags.add(new Tag(PropertyHandler.TAG_ORGANIZATION_ID, ph.getSettings() .getOrganizationId())); CreateTagsRequest ctr = new CreateTagsRequest(); LOGGER.debug("attach tags to resource " + ph.getAWSInstanceId()); ctr.withResources(ph.getAWSInstanceId()).setTags(tags); getEC2().createTags(ctr); }