OWASP O2 Platform Blog

Script to view Amazon EC2 Images List

I just added a couple new methods to the O2’s Amazon EC2 API which provide details about the available AMIs (Amazon Machine Images).

The new capabilities are now avaialble on the Amazon EC2 Browser:

 

… click on the View Available Images (AMIs) link a

…which will popup a new window:

… that will load the data from Amazon (the blue color indicates that it is fetching data):

To help to navigate through the large amount of AMIs available, a couple filters were added:

When expanded will show the list of Images on that category , and when clicked on a AMI node,  its details will show on the right:

Source code

Here is how to consume directly this feature

var apiKey = @"C:\O2\_USERDATA\accounts.xml".credential("AmazonAWS");
   
var amazonEC2 = new API_AmazonEC2(apiKey);
var ec2Client =  amazonEC2.getEC2Client(); </pre>
 

amazonEC2.show_ImagesList_In_TreeView(ec2Client); 
 
//O2File:API_AmazonEC2.cs
//O2Ref:AWSSDK.dll

the code above has no cache support, which is show below:

var apiKey = @"C:\O2\_USERDATA\accounts.xml".credential("AmazonAWS");
   
var amazonEC2 = new API_AmazonEC2(apiKey);
var ec2Client =  amazonEC2.getEC2Client(); </pre>
 

amazonEC2.CachedImageListRequest = @"C:\O2\_tempDir\4-16-2011\tmp8DC3.tmp.xml"; 
amazonEC2.show_ImagesList_In_TreeView(ec2Client); 
 
//O2File:API_AmazonEC2.cs
//O2Ref:AWSSDK.dll

Here are the new methods thare were added to O2’s Amazon EC2 API :

        //this quite an expensive operation (3M of data retrieved) - so I added caching support
        public static List<Image> getImagesList(this API_AmazonEC2 amazonEC2, AmazonEC2Client ec2Client)
        {                                       
            if (amazonEC2.CachedImageListRequest.fileExists())
                return amazonEC2.CachedImageListRequest.load<List<Amazon.EC2.Model.Image>>();
               
            var describeImagesRequest = new DescribeImagesRequest();                
            "Retrieving ImagesList from Amazon..".info();   
            var images = ec2Client.DescribeImages(describeImagesRequest)
                      .DescribeImagesResult.Image;
            if (images.isNull() || images.size()==0)
            {
                "in getImagesList, there was an error retrieving list (are we online?)".error();               
            }
            else
            {
                amazonEC2.CachedImageListRequest = images.save();
                "The Image List was saved to : {0}".info(amazonEC2.CachedImageListRequest); 
            }
            return images;
        }</pre>
 

        public static List<Image> show_ImagesList_In_TreeView(this API_AmazonEC2 amazonEC2)
        {
            return amazonEC2.show_ImagesList_In_TreeView(amazonEC2.getEC2Client());
        }
       
        public static List<Image> show_ImagesList_In_TreeView(this API_AmazonEC2 amazonEC2, AmazonEC2Client ec2Client)
        {
            return amazonEC2.show_ImagesList_In_TreeView(ec2Client, "Amazon EC2 Images List".popupWindow());
        }               
       
        public static List<Image> show_ImagesList_In_TreeView(this API_AmazonEC2 amazonEC2, AmazonEC2Client ec2Client, Control control)
        {               
            var treeView = control.clear().add_TreeView_with_PropertyGrid(false).sort();      
            treeView.backColor(System.Drawing.Color.Azure);
            Application.DoEvents();
            var imagesList = amazonEC2.getImagesList(ec2Client);
            Func<Amazon.EC2.Model.Image,string> imageName =
                (image)=> (image.Description.valid())
                                    ? "{0} - {1}".format(image.Description, image.Name)
                                    : "{0}".format(image.Name);
            Action<string> mapByProperty  =
                (propertyName)=>{               
                                    var visibilityNode = treeView.add_Node("by {0}".format(propertyName));
                                    foreach(var visibility in imagesList.Select((image)=>image.property(propertyName).str()).Distinct())
                                        visibilityNode.add_Node(visibility)
                                                      .add_Nodes(imagesList.Where((image) => image.property(propertyName).str() == visibility),
                                                                   imageName);
                                };
            mapByProperty("Visibility");
            mapByProperty("ImageOwnerAlias");
            mapByProperty("Platform");
            mapByProperty("Architecture");
            "Completed processing show_ImagesList_In_TreeView".info();
            if (treeView.nodes().size()>0)
                treeView.backColor(System.Drawing.Color.White);
            return imagesList;
        }

April 16, 2011 - Posted by | EC2

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: