package org.ut.cmldep.amazon.request;

import java.util.ArrayList;
import java.util.List;

import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;

public class AmazonRequest {
	
	public static List<String> getAllTags(){
		
		System.out.println("Geting tags from Amazon");
		
		List<String> tags = new ArrayList<String>();
		
		AmazonEC2Client ec2 = AWSClients.getEC2Client();
		
        DescribeInstancesResult result = ec2.describeInstances();
        
        List<Reservation> reservations = result.getReservations();

        for (Reservation reservation : reservations) {
            
        	List<Instance> instances = reservation.getInstances();

            for (Instance instance : instances) {
            	
            	if (instance.getTags().iterator().hasNext()){
            		
            		String tag = instance.getTags().iterator().next().getValue();
            		if (!tag.isEmpty()) tags.add(instance.getTags().iterator().next().getValue());
            	}

            }
        }
        
        return tags;
		
	}
	
}
