diff --git a/lib/fog/aws/ec2.rb b/lib/fog/aws/ec2.rb index 5573d279b..49887c0b1 100644 --- a/lib/fog/aws/ec2.rb +++ b/lib/fog/aws/ec2.rb @@ -22,6 +22,8 @@ require "#{parsers_directory}/describe_key_pairs" require "#{parsers_directory}/describe_security_groups" require "#{parsers_directory}/describe_snapshots" require "#{parsers_directory}/describe_volumes" +require "#{parsers_directory}/run_instances" +require "#{parsers_directory}/terminate_instances" requests_directory = "#{current_directory}/requests/ec2" require "#{requests_directory}/allocate_address" @@ -43,6 +45,8 @@ require "#{requests_directory}/describe_snapshots" require "#{requests_directory}/describe_volumes" require "#{requests_directory}/release_address" require "#{requests_directory}/run_instances" +require "#{requests_directory}/terminate_instances" + module Fog module AWS diff --git a/lib/fog/aws/parsers/ec2/describe_security_groups.rb b/lib/fog/aws/parsers/ec2/describe_security_groups.rb index f3a288532..960e8c600 100644 --- a/lib/fog/aws/parsers/ec2/describe_security_groups.rb +++ b/lib/fog/aws/parsers/ec2/describe_security_groups.rb @@ -15,14 +15,12 @@ module Fog end def start_element(name, attrs = []) - if name == 'ipPermissions' - @in_ip_permissions = true - end - if name == 'groups' - @in_groups = true - end - if name == 'ip_ranges' + if name == 'ipRanges' @in_ip_ranges = true + elsif name == 'ipPermissions' + @in_ip_permissions = true + elsif name == 'groups' + @in_groups = true end @value = '' end diff --git a/lib/fog/aws/parsers/ec2/run_instances.rb b/lib/fog/aws/parsers/ec2/run_instances.rb new file mode 100644 index 000000000..9b3f350ac --- /dev/null +++ b/lib/fog/aws/parsers/ec2/run_instances.rb @@ -0,0 +1,87 @@ +module Fog + module Parsers + module AWS + module EC2 + + class RunInstances < Fog::Parsers::Base + + def reset + @instance = { :monitoring => {}, :placement => {}, :product_codes => [] } + @response = { :group_set => [], :instance_set => [] } + end + + def start_element(name, attrs = []) + if name == 'groupSet' + @in_group_set = true + elsif name == 'productCodes' + @in_product_codes = true + end + @value = '' + end + + def end_element(name) + case name + when 'amiLaunchIndex' + @instance[:ami_launch_index] = @value + when 'availabilityZone' + @instance[:placement][:availability_zone] = @value + when 'dnsName' + @instance[:dns_name] = @value + when 'enabled' + if @value == 'true' + @instance[:monitoring][:enabled] = true + else + @instance[:monitoring][:enabled] = false + end + when 'groupId' + @response[:group_set] << @value + when 'groupSet' + @in_group_set = false + when 'keyName' + @instance[:key_name] = @value + when 'imageId' + @instance[:image_id] = @value + when 'instanceId' + @instance[:instance_id] = @value + when 'instanceState' + @instance[:instance_state] = @value + when 'instanceType' + @instance[:instance_type] = @value + when 'item' + unless @in_group_set || @in_product_codes + @response[:instance_set] << @instance + @instance = { :monitoring => {}, :placement => {}, :product_codes => [] } + end + when 'kernelId' + @instance[:kernel_id] = @value + when 'launchTime' + @instance[:launch_time] = Time.parse(@value) + when 'ownerId' + @response[:owner_id] = @value + when 'platform' + @instance[:platform] = @value + when 'privateDnsName' + @instance[:private_dns_name] = @value + when 'product_code' + @instance[:product_codes] << @value + when 'productCodes' + @in_product_codes = false + when 'ramdiskId' + @instance[:ramdisk_id] = @value + when 'reason' + @instance[:reason] = @value + when 'requestId' + @response[:request_id] = @value + when 'requestorId' + @instance[:requestor_id] = @value + when 'reservationId' + @response[:reservation_id] = @value + end + end + + end + + end + end + end +end diff --git a/lib/fog/aws/parsers/ec2/terminate_instances.rb b/lib/fog/aws/parsers/ec2/terminate_instances.rb new file mode 100644 index 000000000..0cf17df4c --- /dev/null +++ b/lib/fog/aws/parsers/ec2/terminate_instances.rb @@ -0,0 +1,53 @@ +module Fog + module Parsers + module AWS + module EC2 + + class TerminateInstances < Fog::Parsers::Base + + def reset + @instance = { :previous_state => {}, :shutdown_state => {} } + @response = { :instance_set => [] } + end + + def start_element(name, attrs = []) + if name == 'previousState' + @in_previous_state = true + elsif name == 'shutdownState' + @in_shutdown_state = true + end + @value = '' + end + + def end_element(name) + case name + when 'item' + @response[:instance_set] << @instance + @instance = { :previous_state => {}, :shutdown_state => {} } + when 'code' + if @in_previous_state + @instance[:previous_state][:code] = @value + elsif @in_shutdown_state + @instance[:shutdown_state][:code] = @value + end + when 'name' + if @in_previous_state + @instance[:previous_state][:name] = @value + elsif @in_shutdown_state + @instance[:shutdown_state][:name] = @value + end + when 'previousState' + @in_previous_state = false + when 'requestId' + @response[:request_id] = @value + when 'shutdownState' + @in_shutdown_state = false + end + end + + end + + end + end + end +end diff --git a/lib/fog/aws/requests/ec2/run_instances.rb b/lib/fog/aws/requests/ec2/run_instances.rb index ce9e47b8e..d64f2495f 100644 --- a/lib/fog/aws/requests/ec2/run_instances.rb +++ b/lib/fog/aws/requests/ec2/run_instances.rb @@ -33,6 +33,7 @@ module Fog # * :virtual_name<~String> - ? # # ==== Returns + # FIXME: docs def run_instances(image_id, min_count, max_count, options = {}) request({ 'Action' => 'RunInstances', @@ -51,7 +52,7 @@ module Fog 'RamdiskId' => options[:ramdisk_id], 'Version' => options[:version], 'VirtualName' => options[:virtual_name] - }, Fog::Parsers::AWS::EC2::Basic.new) + }, Fog::Parsers::AWS::EC2::RunInstances.new) end end diff --git a/lib/fog/aws/requests/ec2/terminate_instances.rb b/lib/fog/aws/requests/ec2/terminate_instances.rb new file mode 100644 index 000000000..18264ca81 --- /dev/null +++ b/lib/fog/aws/requests/ec2/terminate_instances.rb @@ -0,0 +1,21 @@ +module Fog + module AWS + class EC2 + + # Launch specified instances + # + # ==== Parameters + # * instance_id<~Array> - Ids of instances to terminates + # + # ==== Returns + # FIXME: docs + def terminate_instances(instance_id = []) + params = indexed_params('InstanceId', instance_id) + request({ + 'Action' => 'TerminateInstances' + }.merge!(params), Fog::Parsers::AWS::EC2::TerminateInstances.new) + end + + end + end +end diff --git a/spec/aws/ec2/run_instances_spec.rb b/spec/aws/ec2/run_instances_spec.rb index d6254929d..c5b9ed487 100644 --- a/spec/aws/ec2/run_instances_spec.rb +++ b/spec/aws/ec2/run_instances_spec.rb @@ -2,9 +2,13 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe 'EC2.run_instances' do + after(:all) do + p ec2.terminate_instances([@instance_id]) + end + it "should return proper attributes" do - pending actual = ec2.run_instances('ami-5ee70037', 1, 1) + @instance_id = actual.body[:instance_set].first[:instance_id] p actual end diff --git a/spec/aws/ec2/terminate_instances_spec.rb b/spec/aws/ec2/terminate_instances_spec.rb new file mode 100644 index 000000000..f47457f8c --- /dev/null +++ b/spec/aws/ec2/terminate_instances_spec.rb @@ -0,0 +1,14 @@ +require File.dirname(__FILE__) + '/../../spec_helper' + +describe 'EC2.terminate_instances' do + + before(:all) do + @instance_id = ec2.run_instances('ami-5ee70037', 1, 1).body[:instance_set].first[:instance_id] + end + + it "should return proper attributes" do + actual = ec2.terminate_instances([@instance_id]) + p actual + end + +end