OWASP O2 Platform Blog

New Amazon EC2 ‘image filtering focused’ Extension Methods

I just added a number of extension methods to the O2 Amazon EC2 API that help to easily find images

Here are the new ‘image filtering focused’ extension methods in action

var apiKey = @"C:\O2\_USERDATA\accounts.xml".credential("AmazonAWS");
   
var amazonEC2 = new API_AmazonEC2(apiKey);
var ec2Client =  amazonEC2.getEC2Client();  
 
amazonEC2.CachedImageListRequest = @"C:\O2\_tempDir\4-16-2011\tmp8DC3.tmp.xml"; 
 
 
var images = amazonEC2.getImagesList(ec2Client);         
 
//return images.description("WebMatrix Hosting Server provided by Microsoft"); 
//return images.description_Contains("Amazon Linux");
return images.amazon().description_RegEx("windows.*32");
//return images.name("WebMatrixHostingServer");  
//return images.name_Contains("amzn-ami");   
//return images.name_RegEx("amzn.*i386");  </pre>
 

//return images.imageId(".....");      
//return images.imageId("ami-9d1043d8");  
//return images.@public().amazon().windows();
//return images.@public().amazon()._32Bit(); 
//return images.@public().amazon()._64Bit(); 
//return images.@public().amazon();    
//return images.@public().microsoft();  
//return images.@public()._32Bit();  
//return images.@public()._64Bit();    
//return images.@private();   
//return images.@private();  

And here is the  class added to the O2 Amazon EC2 API

public static class API_AmazonEC2_ExtensionMethods_Images
    {   
        //Visibility
        public static List<Image> @public(this List<Image> images)
        { 
            return images.where(image=>image.Visibility == "Public");
        }
       
        public static List<Image> @private(this List<Image> images)
        { 
            return images.where(image=>image.Visibility == "Private");
        }
       
        //Architecture
        public static List<Image> _32Bit(this List<Image> images)
        { 
            return images.where(image=>image.Architecture == "i386");
        }
       
        public static List<Image> _64Bit(this List<Image> images)
        { 
            return images.where(image=>image.Architecture == "x86_64");
        }
       
        //ImageOwnerAlias
        public static List<Image> amazon(this List<Image> images)
        { 
            return images.where(image=>image.ImageOwnerAlias == "amazon");
        }
       
        public static List<Image> microsoft(this List<Image> images)
        { 
            return images.where(image=>image.ImageOwnerAlias == "microsoft"); 
        }
       
        //Platform
        public static List<Image> windows(this List<Image> images)
        { 
            return images.where(image=>image.Platform == "windows"); 
        }       
       
        //ImageId
        public static Image imageId(this List<Image> images, string imageId)
        { 
            return images.where(image=>image.ImageId == imageId).first(); 
        }       
       
        //Name
       
        public static Image name(this List<Image> images, string name)
        { 
            return images.where(image=>image.Name == name).first();
        }       
       
        public static List<Image> name_Contains(this List<Image> images, string name)
        { 
            return images.where(image=>image.Name.contains(name));
        }       
       
        public static List<Image> name_RegEx(this List<Image> images, string name)
        { 
            return images.where(image=>image.Name.regEx(name));
        }       
       
        //Description
        public static Image description(this List<Image> images, string description)
        { 
            return images.where(image=>image.Description == description).first(); 
        }       
       
        public static List<Image> description_Contains(this List<Image> images, string description)
        { 
            return images.where(image=>image.Description.contains(description));
        }       
       
        public static List<Image> description_RegEx(this List<Image> images, string description)
        { 
            return images.where(image=>image.Description.regEx(description));
        }       
       
        //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;
        }
    }

 

I also made a change in the formula used to populate the TreeView with the available images so that the images with the same name are all grouped together 

April 17, 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: