From 9696e21c71e585a3f23330b8fbcafebf71bc13c6 Mon Sep 17 00:00:00 2001 From: Caleb Tennis Date: Mon, 26 Jul 2010 00:24:08 +0800 Subject: [PATCH] [ec2] Add the deregister_image request and parser. [ec2] Add ability to call "deregister" directly on an image. [ec2] Add rootDeviceType and rootDeviceName attributes to the image model. --- lib/fog/aws.rb | 22 ++++---- lib/fog/aws/ec2.rb | 1 + lib/fog/aws/models/ec2/image.rb | 24 +++++---- lib/fog/aws/parsers/ec2/deregister_image.rb | 20 ++++++++ lib/fog/aws/parsers/ec2/describe_images.rb | 2 +- lib/fog/aws/requests/ec2/deregister_image.rb | 51 +++++++++++++++++++ lib/fog/aws/requests/ec2/describe_images.rb | 2 + spec/aws/requests/ec2/describe_images_spec.rb | 5 ++ 8 files changed, 107 insertions(+), 20 deletions(-) create mode 100644 lib/fog/aws/parsers/ec2/deregister_image.rb create mode 100644 lib/fog/aws/requests/ec2/deregister_image.rb diff --git a/lib/fog/aws.rb b/lib/fog/aws.rb index 03bc00dc4..db690849c 100644 --- a/lib/fog/aws.rb +++ b/lib/fog/aws.rb @@ -67,16 +67,18 @@ module Fog path << letters(rand(9) + 8) end { - "imageOwnerId" => letters(rand(5) + 4), - "productCodes" => [], - "kernelId" => kernel_id, - "ramdiskId" => ramdisk_id, - "imageState" => "available", - "imageId" => image_id, - "architecture" => "i386", - "isPublic" => true, - "imageLocation" => path.join('/'), - "imageType" => "machine" + "imageOwnerId" => letters(rand(5) + 4), + "productCodes" => [], + "kernelId" => kernel_id, + "ramdiskId" => ramdisk_id, + "imageState" => "available", + "imageId" => image_id, + "architecture" => "i386", + "isPublic" => true, + "imageLocation" => path.join('/'), + "imageType" => "machine", + "rootDeviceType" => ["ebs","instance-store"][rand(2)], + "rootDeviceName" => "/dev/sda1" } end diff --git a/lib/fog/aws/ec2.rb b/lib/fog/aws/ec2.rb index a1e98e972..21eb408a3 100644 --- a/lib/fog/aws/ec2.rb +++ b/lib/fog/aws/ec2.rb @@ -39,6 +39,7 @@ module Fog request 'delete_security_group' request 'delete_snapshot' request 'delete_volume' + request 'deregister_image' request 'describe_addresses' request 'describe_availability_zones' request 'describe_images' diff --git a/lib/fog/aws/models/ec2/image.rb b/lib/fog/aws/models/ec2/image.rb index e08826bf2..38036b3c2 100644 --- a/lib/fog/aws/models/ec2/image.rb +++ b/lib/fog/aws/models/ec2/image.rb @@ -9,18 +9,24 @@ module Fog identity :id, 'imageId' attribute :architecture - attribute :location, 'imageLocation' - attribute :owner_id, 'imageOwnerId' - attribute :state, 'imageState' - attribute :type, 'imageType' - attribute :is_public, 'isPublic' - attribute :kernel_id, 'kernelId' + attribute :location, 'imageLocation' + attribute :owner_id, 'imageOwnerId' + attribute :state, 'imageState' + attribute :type, 'imageType' + attribute :is_public, 'isPublic' + attribute :kernel_id, 'kernelId' attribute :platform - attribute :product_codes, 'productCodes' - attribute :ramdisk_id, 'ramdiskId' + attribute :product_codes, 'productCodes' + attribute :ramdisk_id, 'ramdiskId' + attribute :root_device_type, 'rootDeviceType' + attribute :root_device_name, 'rootDeviceName' + + def deregister + connection.deregister_image(@id) + end end end end -end \ No newline at end of file +end diff --git a/lib/fog/aws/parsers/ec2/deregister_image.rb b/lib/fog/aws/parsers/ec2/deregister_image.rb new file mode 100644 index 000000000..706509975 --- /dev/null +++ b/lib/fog/aws/parsers/ec2/deregister_image.rb @@ -0,0 +1,20 @@ +module Fog + module Parsers + module AWS + module EC2 + + class DeregisterImage < Fog::Parsers::Base + + def end_element(name) + case name + when 'return', 'requestId', 'imageId' + @response[name] = @value + end + end + + end + + end + end + end +end diff --git a/lib/fog/aws/parsers/ec2/describe_images.rb b/lib/fog/aws/parsers/ec2/describe_images.rb index cf548a8f6..548a7f5af 100644 --- a/lib/fog/aws/parsers/ec2/describe_images.rb +++ b/lib/fog/aws/parsers/ec2/describe_images.rb @@ -19,7 +19,7 @@ module Fog def end_element(name) case name - when 'architecture', 'imageId', 'imageLocation', 'imageOwnerId', 'imageState', 'imageType', 'kernelId', 'platform', 'ramdiskId' + when 'architecture', 'imageId', 'imageLocation', 'imageOwnerId', 'imageState', 'imageType', 'kernelId', 'platform', 'ramdiskId', 'rootDeviceType','rootDeviceName' @image[name] = @value when 'isPublic' if @value == 'true' diff --git a/lib/fog/aws/requests/ec2/deregister_image.rb b/lib/fog/aws/requests/ec2/deregister_image.rb new file mode 100644 index 000000000..3abb90aad --- /dev/null +++ b/lib/fog/aws/requests/ec2/deregister_image.rb @@ -0,0 +1,51 @@ +module Fog + module AWS + module EC2 + class Real + + require 'fog/aws/parsers/ec2/deregister_image' + + # Attach an Amazon EBS volume with a running instance, exposing as specified device + # + # ==== Parameters + # * image_id<~String> - Id of image to deregister + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'return'<~Boolean> - Returns true if deregistration succeeded + # * 'requestId'<~String> - Id of request + def deregister_image(image_id) + request( + 'Action' => 'DeregisterImage', + 'ImageId' => image_id, + :parser => Fog::Parsers::AWS::EC2::DeregisterImage.new + ) + end + + end + + class Mock + + def deregister_image(image_id) + response = Excon::Response.new + if image_id + response.status = 200 + response.body = { + 'requestId' => Fog::AWS::Mock.request_id, + 'return' => "true" + } + response + else + message = 'MissingParameter => ' + if !instance_id + message << 'The request must contain the parameter image_id' + end + raise Fog::AWS::EC2::Error.new(message) + end + end + + end + end + end +end diff --git a/lib/fog/aws/requests/ec2/describe_images.rb b/lib/fog/aws/requests/ec2/describe_images.rb index e9fab0d87..c70367493 100644 --- a/lib/fog/aws/requests/ec2/describe_images.rb +++ b/lib/fog/aws/requests/ec2/describe_images.rb @@ -30,6 +30,8 @@ module Fog # * 'platform'<~String> - Operating platform of the image # * 'productCodes'<~Array> - Product codes for the image # * 'ramdiskId'<~String> - Ramdisk id associated with image, if any + # * 'rootDeviceName'<~String> - Root device name, e.g. /dev/sda1 + # * 'rootDeviceType'<~String> - Root device type, ebs or instance-store def describe_images(options = {}) if image_id = options.delete('ImageId') options.merge!(AWS.indexed_param('ImageId', image_id)) diff --git a/spec/aws/requests/ec2/describe_images_spec.rb b/spec/aws/requests/ec2/describe_images_spec.rb index 5e50fdaf5..31d1f21d2 100644 --- a/spec/aws/requests/ec2/describe_images_spec.rb +++ b/spec/aws/requests/ec2/describe_images_spec.rb @@ -17,6 +17,9 @@ describe 'EC2.describe_images' do image['kernelId'].should be_a(String) if image['kernelId'] image['platform'].should be_a(String) if image['platform'] image['ramdiskId'].should be_a(String) if image['ramdiskId'] + image['rootDeviceName'].should be_a(String) if image['rootDeviceName'] + ["ebs","instance-store"].should include(image['rootDeviceType']) + image['rootDeviceName'].should be_a(String) if image['rootDeviceName'] end it "should return proper attributes with params" do @@ -33,6 +36,8 @@ describe 'EC2.describe_images' do image['kernelId'].should be_a(String) if image['kernelId'] image['platform'].should be_a(String) if image['platform'] image['ramdiskId'].should be_a(String) if image['ramdiskId'] + ["ebs","instance-store"].should include(image['rootDeviceType']) + image['rootDeviceName'].should be_a(String) if image['rootDeviceName'] end end