Example usage for com.amazonaws.services.ec2.model Tag withKey

List of usage examples for com.amazonaws.services.ec2.model Tag withKey

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model Tag withKey.

Prototype


public Tag withKey(String key) 

Source Link

Document

The key of the tag.

Usage

From source file:Aws.Instances.java

public void DescribeInstances(AmazonEC2Client ec2) {
    DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
    List<Reservation> reservations = describeInstancesRequest.getReservations();
    Set<Instance> instances = new HashSet<>();

    for (Iterator<Reservation> it = reservations.iterator(); it.hasNext();) {
        Reservation reservationId = it.next();
        instances.addAll(reservationId.getInstances());
    }/*from  w w  w  .  j a  v a 2  s.  c om*/

    Set<String> InstancesIds = new HashSet<>();
    for (Iterator<Instance> Iteratore = instances.iterator(); Iteratore.hasNext();) {
        Instance instanceId = Iteratore.next();
        List<Tag> tags = instanceId.getTags();
        InstancesIds.add(instanceId.getInstanceId());
        System.out.println("Instance ID " + instanceId.getInstanceId() + " ; Instance Tags " + tags);
        for (Iterator<Tag> tag = tags.iterator(); tag.hasNext();) {
            Tag tagkey = tag.next();
            System.out.println(tagkey.withKey("Name").getValue());
        }

    }
}

From source file:com.liferay.amazontools.AsgardAMIDeployer.java

License:Open Source License

protected String createAutoScalingGroup() throws Exception {
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = amazonAutoScalingClient
            .describeAutoScalingGroups();

    List<AutoScalingGroup> autoScalingGroups = describeAutoScalingGroupsResult.getAutoScalingGroups();

    int oldAutoScalingGroupsSize = autoScalingGroups.size();

    String availabilityZone = properties.getProperty("availability.zone");

    Map<String, String> parameters = new HashMap<String, String>();

    parameters.put("checkHealth", "true");
    parameters.put("imageId", getImageId(_imageName));

    String asgardClusterName = properties.getProperty("asgard.cluster.name");

    parameters.put("name", asgardClusterName);

    parameters.put("trafficAllowed", "true");

    if (!_parallelDeployment) {
        parameters.put("desiredCapacity", "1");
        parameters.put("min", "1");
    }/* ww  w.  java  2  s.  com*/

    _jsonWebServiceClient.doPost("/" + availabilityZone + "/cluster/createNextGroup", parameters);

    for (int i = 0; i < 30; i++) {
        describeAutoScalingGroupsResult = amazonAutoScalingClient.describeAutoScalingGroups();

        autoScalingGroups = describeAutoScalingGroupsResult.getAutoScalingGroups();

        int newAutoScalingGroupsSize = autoScalingGroups.size();

        if (oldAutoScalingGroupsSize == newAutoScalingGroupsSize) {
            sleep(15);
        } else {
            break;
        }
    }

    String autoScalingGroupName = null;
    boolean created = false;
    int maxSize = 0;

    for (int i = 0; i < 30; i++) {
        String json = _jsonWebServiceClient.doGet(
                "/" + availabilityZone + "/cluster/show/" + asgardClusterName + ".json",
                Collections.<String, String>emptyMap());

        JSONArray autoScalingGroupsJSONArray = new JSONArray(json);

        JSONObject autoScalingGroupJSONObject = autoScalingGroupsJSONArray
                .getJSONObject(autoScalingGroupsJSONArray.length() - 1);

        autoScalingGroupName = autoScalingGroupJSONObject.getString("autoScalingGroupName");
        maxSize = autoScalingGroupJSONObject.getInt("maxSize");

        List<String> instanceIds = new ArrayList<String>();

        JSONArray instancesJSONArray = autoScalingGroupJSONObject.getJSONArray("instances");

        for (int j = 0; j < instancesJSONArray.length(); j++) {
            JSONObject instanceJSONObject = instancesJSONArray.getJSONObject(j);

            instanceIds.add(instanceJSONObject.getString("instanceId"));
        }

        if (instanceIds.isEmpty() || !isInService(autoScalingGroupJSONObject)) {

            sleep(15);
        } else {
            CreateTagsRequest createTagsRequest = new CreateTagsRequest();

            createTagsRequest.setResources(instanceIds);

            List<Tag> tags = new ArrayList<Tag>();

            Tag tag = new Tag();

            tag.withKey("Name");
            tag.withValue(properties.getProperty("instance.name"));

            tags.add(tag);

            createTagsRequest.setTags(tags);

            amazonEC2Client.createTags(createTagsRequest);

            CreateOrUpdateTagsRequest createOrUpdateTagsRequest = new CreateOrUpdateTagsRequest();

            com.amazonaws.services.autoscaling.model.Tag autoScalingTag = new com.amazonaws.services.autoscaling.model.Tag();

            autoScalingTag.setKey("Name");
            autoScalingTag.setPropagateAtLaunch(true);
            autoScalingTag.setResourceId(autoScalingGroupName);
            autoScalingTag.setResourceType("auto-scaling-group");
            autoScalingTag.setValue(properties.getProperty("instance.name"));

            createOrUpdateTagsRequest.withTags(autoScalingTag);

            amazonAutoScalingClient.createOrUpdateTags(createOrUpdateTagsRequest);

            created = true;

            break;
        }
    }

    if (!created) {
        throw new RuntimeException("Unable to create Auto Scaling Group " + autoScalingGroupName);
    }

    if (!_parallelDeployment) {
        int minSize = Integer.parseInt(properties.getProperty("instance.min.size"));

        if (minSize > 1) {
            checkAutoScalingGroup(autoScalingGroupName, 1);

            parameters.clear();

            parameters.put("maxSize", String.valueOf(maxSize));
            parameters.put("minSize", String.valueOf(minSize));
            parameters.put("name", autoScalingGroupName);

            _jsonWebServiceClient.doPost("/" + availabilityZone + "/cluster/resize", parameters);

            for (int i = 0; i < 30; i++) {
                String json = _jsonWebServiceClient.doGet(
                        "/" + availabilityZone + "/cluster/show/" + asgardClusterName + ".json",
                        Collections.<String, String>emptyMap());

                JSONArray autoScalingGroupsJSONArray = new JSONArray(json);

                JSONObject autoScalingGroupJSONObject = autoScalingGroupsJSONArray
                        .getJSONObject(autoScalingGroupsJSONArray.length() - 1);

                JSONArray instancesJSONArray = autoScalingGroupJSONObject.getJSONArray("instances");

                if (instancesJSONArray.length() == 1) {
                    sleep(15);
                } else {
                    break;
                }
            }
        }
    }

    return autoScalingGroupName;
}