From 2d23d4a90e33cbb4592c87e59cd2232bf6cf7e49 Mon Sep 17 00:00:00 2001 From: Caleb Tennis Date: Fri, 30 Jul 2010 13:41:15 -0400 Subject: [PATCH] [ec2] Add register_image functionality --- lib/fog/aws/ec2.rb | 1 + lib/fog/aws/parsers/ec2/register_image.rb | 20 +++++ lib/fog/aws/requests/ec2/register_image.rb | 89 ++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 lib/fog/aws/parsers/ec2/register_image.rb create mode 100644 lib/fog/aws/requests/ec2/register_image.rb diff --git a/lib/fog/aws/ec2.rb b/lib/fog/aws/ec2.rb index 6794273f3..678a905c5 100644 --- a/lib/fog/aws/ec2.rb +++ b/lib/fog/aws/ec2.rb @@ -57,6 +57,7 @@ request 'modify_snapshot_attribute' request 'reboot_instances' request 'release_address' + request 'register_image' request 'revoke_security_group_ingress' request 'run_instances' request 'terminate_instances' diff --git a/lib/fog/aws/parsers/ec2/register_image.rb b/lib/fog/aws/parsers/ec2/register_image.rb new file mode 100644 index 000000000..c5bfb7c51 --- /dev/null +++ b/lib/fog/aws/parsers/ec2/register_image.rb @@ -0,0 +1,20 @@ +module Fog + module Parsers + module AWS + module EC2 + + class RegisterImage < Fog::Parsers::Base + + def end_element(name) + case name + when 'requestId', 'imageId' + @response[name] = @value + end + end + + end + + end + end + end +end diff --git a/lib/fog/aws/requests/ec2/register_image.rb b/lib/fog/aws/requests/ec2/register_image.rb new file mode 100644 index 000000000..d5e3cabee --- /dev/null +++ b/lib/fog/aws/requests/ec2/register_image.rb @@ -0,0 +1,89 @@ +module Fog + module AWS + module EC2 + class Real + + require 'fog/aws/parsers/ec2/register_image' + + # register an image + # + # ==== Parameters + # * Name<~String> - Name of the AMI to be registered + # * Description<~String> - AMI description + # * Location<~String> - S3 manifest location (for S3 backed AMIs) + # or + # * RootDeviceName<~String> - Name of Root Device (for EBS snapshot backed AMIs) + # * BlockDevices<~Array>: + # * BlockDeviceOptions<~Hash>: + # * DeviceName<~String> - Name of the Block Device + # * VirtualName<~String> - Name of the Virtual Device + # * SnapshotId<~String> - id of the EBS Snapshot + # * VolumeSize<~Integer> - Size of the snapshot (optional) + # * NoDevice<~Boolean> - Do not use an ebs device (def: true) + # * DeleteOnTermation<~Boolean> - Delete EBS volume on instance term (def: true) + # * Options<~Hash>: + # * Architecture<~String> - i386 or x86_64 + # * KernelId<~String> - kernelId + # * RamdiskId<~String> - ramdiskId + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'return'<~Boolean> - Returns true if deregistration succeeded + # * 'imageId'<~String> - Id of newly created AMI + + def register_image(name, description, location, block_devices=[], options={}) + common_options = { + 'Action' => 'RegisterImage', + 'Name' => name, + 'Description' => description, + } + + # This determines if we are doing a snapshot or a S3 backed AMI. + if(location =~ /^\/dev\/sd[a-p]\d{0,2}$/) + common_options['RootDeviceName'] = location + else + common_options['ImageLocation'] = location + end + + bdi = 0 + block_devices.each do |bd| + bdi += 1 + ["DeviceName","VirtualName"].each do |n| + common_options["BlockDeviceMapping.#{bdi}.#{n}"] = bd["#{n}"] if bd["#{n}"] + end + ["SnapshotId","VolumeSize","NoDevice","DeleteOnTermination"].each do |n| + common_options["BlockDeviceMapping.#{bdi}.Ebs.#{n}"] = bd["#{n}"] if bd["#{n}"] + end + + end + + request(common_options.merge!(options)) + end + + end + + class Mock + + def register_image(name, description, location, block_devices=[], options={}) + response = Excon::Response.new + if !name.empty? + response.status = 200 + response.body = { + 'requestId' => Fog::AWS::Mock.request_id, + 'imageId' => Fog::AWS::Mock.image_id + } + response + else + message = 'MissingParameter => ' + if name.empty? + message << 'The request must contain the parameter name' + end + raise Fog::AWS::EC2::Error.new(message) + end + end + + end + end + end +end