Java Code Examples for com.amazonaws.services.ec2.model.Instance#getTags()

The following examples show how to use com.amazonaws.services.ec2.model.Instance#getTags() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UploadTagsGenerator.java    From soundwave with Apache License 2.0 6 votes vote down vote up
/**
 * 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;

}
 
Example 2
Source File: EC2Processor.java    From development with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 6 votes vote down vote up
public List<ServerEnvironment> getEnvironmentsByTag(Region region, String searchTag) {
    LOGGER.info("getEnvironmentsByTag " + region + " tag: " + searchTag);
    List<ServerEnvironment> environments = new ArrayList<ServerEnvironment>();

    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)) {
                    environments.add(getEnvironmentFromInstance(instance));
                }
            }
        }
    }
    return environments;
}
 
Example 4
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 6 votes vote down vote up
@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;
}
 
Example 5
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 5 votes vote down vote up
private ServerEnvironment getEnvironmentFromInstance(Instance instance) {
    ServerEnvironment env = new ServerEnvironment(instance.getInstanceId(), instance.getInstanceType());

    List<EnvironmentTag> tags = new ArrayList<EnvironmentTag>();
    for (Tag tag : instance.getTags()) {
        EnvironmentTag envTag = new EnvironmentTag(tag.getKey(), tag.getValue());
        tags.add(envTag);
        if (tag.getKey().equalsIgnoreCase(DEFAULT_INSTANCE_NAME_TAG)) {
            env.setEnvironmentTag(tag.getValue());
            if (tag.getValue().contains(PROD_VALUE)) {
                env.setType(ENVIRONMENT_TYPES.PRODUCTION);
            } else if (tag.getValue().contains(STAGING_VALUE)) {
                env.setType(ENVIRONMENT_TYPES.STAGING);
            } else if (tag.getValue().contains(JENKINS_VALUE)) {
                env.setType(ENVIRONMENT_TYPES.JENKINS);
            }
        }
        if (tag.getKey().equalsIgnoreCase(VERSION_TAG)) {
            env.setVersion(tag.getValue());
        }
    }
    env.setState(instance.getState());
    env.setLaunchTime(instance.getLaunchTime());
    env.setPublicIpAddress(instance.getPublicIpAddress());
    env.setTags(tags);
    return env;
}
 
Example 6
Source File: InstanceLookupTable.java    From graylog-plugin-aws with Apache License 2.0 5 votes vote down vote up
private String getNameOfInstance(Instance x) {
    for (Tag tag : x.getTags()) {
        if("Name".equals(tag.getKey())) {
            return tag.getValue();
        }
    }

    return null;
}
 
Example 7
Source File: AwsUtilities.java    From soundwave with Apache License 2.0 4 votes vote down vote up
public static Tag getAwsTag(Instance awsInstance, String tagName) {
  List<Tag> tags = awsInstance.getTags();
  java.util.Optional<Tag> tag =
      tags.stream().filter(t -> StringUtils.equals(t.getKey(), tagName)).findFirst();
  return tag.isPresent() ? tag.get() : null;
}
 
Example 8
Source File: EC2Instance.java    From billow with Apache License 2.0 4 votes vote down vote up
public EC2Instance(Instance instance) {
    this.id = instance.getInstanceId();
    this.type = instance.getInstanceType();
    this.lifecycle = instance.getInstanceLifecycle();
    this.hypervisor = instance.getHypervisor();
    this.az = instance.getPlacement().getAvailabilityZone();
    this.group = instance.getPlacement().getGroupName();
    this.tenancy = instance.getPlacement().getTenancy();
    this.vpc = instance.getVpcId();
    this.platform = instance.getPlatform();
    this.kernel = instance.getKernelId();
    this.key = instance.getKeyName();
    this.image = instance.getImageId();
    this.privateIP = instance.getPrivateIpAddress();
    this.publicIP = instance.getPublicIpAddress();
    this.publicHostname = instance.getPublicDnsName();
    this.privateHostname = instance.getPrivateDnsName();
    this.architecture = instance.getArchitecture();
    this.state = instance.getState().getName();
    this.ramdisk = instance.getRamdiskId();
    this.subnet = instance.getSubnetId();
    this.rootDeviceName = instance.getRootDeviceName();
    this.rootDeviceType = instance.getRootDeviceType();
    this.stateTransitionReason = instance.getStateTransitionReason();
    this.spotInstanceRequest = instance.getSpotInstanceRequestId();
    this.virtualizationType = instance.getVirtualizationType();
    this.sourceDestCheck = instance.getSourceDestCheck();
    this.launchTime = new DateTime(instance.getLaunchTime());

    if (instance.getIamInstanceProfile() != null) {
        this.iamInstanceProfile = instance.getIamInstanceProfile().getArn().toString();
    } else {
        this.iamInstanceProfile = null;
    }

    final StateReason stateReason = instance.getStateReason();
    if (stateReason != null)
        this.stateReason = stateReason.getMessage();
    else
        this.stateReason = null;

    this.securityGroups = new ArrayList<>();
    for (GroupIdentifier identifier : instance.getSecurityGroups()) {
        this.securityGroups.add(new SecurityGroup(identifier));
    }

    this.tags = new HashMap<>();
    for (Tag tag : instance.getTags()) {
        this.tags.put(tag.getKey(), tag.getValue());
    }
}