Permalink
Browse files

Adding support for describe/modify/reset instance attributes with tests.

  • Loading branch information...
nstielau committed Oct 23, 2010
1 parent c1fb8b2 commit bdbdfc5600df519897c4412bc852fe457a2eacae
Showing with 126 additions and 10 deletions.
  1. +37 −10 lib/AWS/EC2/instances.rb
  2. +89 −0 test/test_EC2_instances.rb
View
@@ -36,7 +36,7 @@ def run_instances( options = {} )
raise ArgumentError, ":image_id must be provided" if options[:image_id].nil? || options[:image_id].empty?
raise ArgumentError, ":min_count is not valid" unless options[:min_count].to_i > 0
raise ArgumentError, ":max_count is not valid or must be >= :min_count" unless options[:max_count].to_i > 0 && options[:max_count].to_i >= options[:min_count].to_i
- raise ArgumentError, ":instance_type must specify a valid instance type" unless options[:instance_type].nil? || ["m1.small", "m1.large", "m1.xlarge", "m2.xlarge", "c1.medium", "c1.xlarge", "m2.2xlarge", "m2.4xlarge", "cc1.4xlarge"].include?(options[:instance_type])
+ raise ArgumentError, ":instance_type must specify a valid instance type" unless options[:instance_type].nil? || ["t1.micro", "m1.small", "m1.large", "m1.xlarge", "m2.xlarge", "c1.medium", "c1.xlarge", "m2.2xlarge", "m2.4xlarge", "cc1.4xlarge"].include?(options[:instance_type])
raise ArgumentError, ":monitoring_enabled must be 'true' or 'false'" unless options[:monitoring_enabled].nil? || [true, false].include?(options[:monitoring_enabled])
raise ArgumentError, ":disable_api_termination must be 'true' or 'false'" unless options[:disable_api_termination].nil? || [true, false].include?(options[:disable_api_termination])
raise ArgumentError, ":instance_initiated_shutdown_behavior must be 'stop' or 'terminate'" unless options[:instance_initiated_shutdown_behavior].nil? || ["stop", "terminate"].include?(options[:instance_initiated_shutdown_behavior])
@@ -92,30 +92,57 @@ def describe_instances( options = {} )
end
- # Not yet implemented
+ # Returns information about an attribute of an instance.
#
- # @todo Implement this method
+ # @option options [String] :instance_id (nil) ID of the instance on which the attribute will be queried.
+ # @option options [String] :attribute (nil) Specifies the attribute to query..
#
def describe_instance_attribute( options = {} )
- raise "Not yet implemented"
+ raise ArgumentError, "No :instance_id provided" if options[:instance_id].nil? || options[:instance_id].empty?
+ raise ArgumentError, "No :attribute provided" if options[:attribute].nil? || options[:attribute].empty?
+ valid_attributes = %w(instanceType kernel ramdisk userData disableApiTermination instanceInitiatedShutdownBehavior rootDevice blockDeviceMapping)
+ raise ArgumentError, "Invalid :attribute provided" unless valid_attributes.include?(options[:attribute].to_s)
+ params = {}
+ params["InstanceId"] = options[:instance_id]
+ params["Attribute"] = options[:attribute]
+ return response_generator(:action => "DescribeInstanceAttribute", :params => params)
end
- # Not yet implemented
+ # Modifies an attribute of an instance.
#
- # @todo Implement this method
+ # @option options [String] :instance_id (nil) ID of the instance on which the attribute will be modified.
+ # @option options [String] :attribute (nil) Specifies the attribute to modify.
+ # @option options [String] :value (nil) The value of the attribute being modified.
#
def modify_instance_attribute( options = {} )
- raise "Not yet implemented"
+ raise ArgumentError, "No :instance_id provided" if options[:instance_id].nil? || options[:instance_id].empty?
+ raise ArgumentError, "No :attribute provided" if options[:attribute].nil? || options[:attribute].empty?
+ raise ArgumentError, "No :value provided" if options[:value].nil?
+ valid_attributes = %w(instanceType kernel ramdisk userData disableApiTermination instanceInitiatedShutdownBehavior rootDevice blockDeviceMapping)
+ raise ArgumentError, "Invalid :attribute provided" unless valid_attributes.include?(options[:attribute].to_s)
+ params = {}
+ params["InstanceId"] = options[:instance_id]
+ params["Attribute"] = options[:attribute]
+ params["Value"] = options[:value].to_s
+ return response_generator(:action => "ModifyInstanceAttribute", :params => params)
end
- # Not yet implemented
+ # Resets an attribute of an instance to its default value.
#
- # @todo Implement this method
+ # @option options [String] :instance_id (nil) ID of the instance on which the attribute will be reset.
+ # @option options [String] :attribute (nil) The instance attribute to reset to the default value.
#
def reset_instance_attribute( options = {} )
- raise "Not yet implemented"
+ raise ArgumentError, "No :instance_id provided" if options[:instance_id].nil? || options[:instance_id].empty?
+ raise ArgumentError, "No :attribute provided" if options[:attribute].nil? || options[:attribute].empty?
+ valid_attributes = %w(kernel ramdisk)
+ raise ArgumentError, "Invalid :attribute provided" unless valid_attributes.include?(options[:attribute].to_s)
+ params = {}
+ params["InstanceId"] = options[:instance_id]
+ params["Attribute"] = options[:attribute]
+ return response_generator(:action => "ResetInstanceAttribute", :params => params)
end
View
@@ -217,6 +217,27 @@
</UnmonitorInstancesResponse>
RESPONSE
+
+ @describe_instance_attribute_response_body = <<-RESPONSE
+ <DescribeInstanceAttributeResponse xmlns="http://ec2.amazonaws.com/doc/2010-08-31/">
+ <instanceId>i-10a64379</instanceId>
+ <kernel>
+ <value>aki-f70657b2</value>
+ </kernel>
+ </DescribeInstanceAttributeResponse>
+ RESPONSE
+
+ @modify_instance_attribute_response_body = <<-RESPONSE
+ <ModifyInstanceAttributeResponse xmlns="http://ec2.amazonaws.com/doc/2010-08-31/">
+ <return>true</return>
+ </ModifyInstanceAttributeResponse>
+ RESPONSE
+
+ @reset_instance_attribute_response_body = <<-RESPONSE
+ <ResetInstanceAttributeResponse xmlns="http://ec2.amazonaws.com/doc/2010-08-31/">
+ <return>true</return>
+ </ResetInstanceAttributeResponse>
+ RESPONSE
end
@@ -608,4 +629,72 @@
@response.instancesSet.item[1].monitoring.state.should.equal "disabling"
end
+ specify "should get an ArgumentError when trying to describe/modify/reset an instance attribute without an istance id" do
+ lambda { @ec2.describe_instance_attribute(:attribute => "ramdisk") }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.modify_instance_attribute(:attribute => "ramdisk") }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.reset_instance_attribute(:attribute => "ramdisk") }.should.raise(AWS::ArgumentError)
+
+ lambda { @ec2.describe_instance_attribute(:attribute => "ramdisk", :instance_id => nil) }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.modify_instance_attribute(:attribute => "ramdisk", :instance_id => nil) }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.reset_instance_attribute(:attribute => "ramdisk", :instance_id => nil) }.should.raise(AWS::ArgumentError)
+
+ lambda { @ec2.describe_instance_attribute(:attribute => "ramdisk", :instance_id => "") }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.modify_instance_attribute(:attribute => "ramdisk", :instance_id => "") }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.reset_instance_attribute(:attribute => "ramdisk", :instance_id => "") }.should.raise(AWS::ArgumentError)
+ end
+
+ specify "should get an ArgumentError when trying to describe/modify/reset an instance attribute without an attribute" do
+ lambda { @ec2.describe_instance_attribute(:instance_id => "i-33457a5a") }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.modify_instance_attribute(:instance_id => "i-33457a5a") }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.reset_instance_attribute(:instance_id => "i-33457a5a") }.should.raise(AWS::ArgumentError)
+
+ lambda { @ec2.describe_instance_attribute(:instance_id => "i-33457a5a", :attribute => nil) }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.modify_instance_attribute(:instance_id => "i-33457a5a", :attribute => nil) }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.reset_instance_attribute(:instance_id => "i-33457a5a", :attribute => nil) }.should.raise(AWS::ArgumentError)
+
+ lambda { @ec2.describe_instance_attribute(:instance_id => "i-33457a5a", :attribute => "") }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.modify_instance_attribute(:instance_id => "i-33457a5a", :attribute => "") }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.reset_instance_attribute(:instance_id => "i-33457a5a", :attribute => "") }.should.raise(AWS::ArgumentError)
+ end
+
+ specify "should get an ArgumentError when trying to describe/modify/reset an instance attribute without a valid attribute" do
+ lambda { @ec2.describe_instance_attribute(:instance_id => "i-33457a5a", :attribute => 'party') }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.modify_instance_attribute(:instance_id => "i-33457a5a", :attribute => 'party') }.should.raise(AWS::ArgumentError)
+ lambda { @ec2.reset_instance_attribute(:instance_id => "i-33457a5a", :attribute => 'party') }.should.raise(AWS::ArgumentError)
+ end
+
+ specify "should not get an ArgumentError when trying to describe/modify/reset an instance attribute with a valid attribute" do
+ @ec2.stubs(:make_request).returns stub(:body => @describe_instance_attribute_response_body, :is_a? => true)
+ %w(instanceType kernel ramdisk userData disableApiTermination instanceInitiatedShutdownBehavior rootDevice blockDeviceMapping).each do |a|
+ lambda { @ec2.describe_instance_attribute(:instance_id => "i-33457a5a", :attribute => a) }.should.not.raise(AWS::ArgumentError)
+ lambda { @ec2.modify_instance_attribute(:instance_id => "i-33457a5a", :attribute => 'party') }.should.raise(AWS::ArgumentError)
+ end
+ %w(kernel ramdisk).each do |a|
+ lambda { @ec2.reset_instance_attribute(:instance_id => "i-33457a5a", :attribute => 'party') }.should.raise(AWS::ArgumentError)
+ end
+ end
+
+ specify "should successfully describe instance attribute" do
+ @ec2.stubs(:make_request).with('DescribeInstanceAttribute', {"InstanceId"=>"i-33457a5a", "Attribute" => "kernel"}).
+ returns stub(:body => @describe_instance_attribute_response_body, :is_a? => true)
+ @response = @ec2.describe_instance_attribute(:instance_id => "i-33457a5a", :attribute => 'kernel')
+ @response.class.should.equal Hash
+ @response.kernel.value.should.equal "aki-f70657b2"
+ end
+
+ specify "should successfully modify instance attribute" do
+ @ec2.stubs(:make_request).with('ModifyInstanceAttribute', {"InstanceId"=>"i-33457a5a", "Attribute" => "disableApiTermination", "Value" => "true"}).
+ returns stub(:body => @modify_instance_attribute_response_body, :is_a? => true)
+ @response = @ec2.modify_instance_attribute(:instance_id => "i-33457a5a", :attribute => 'disableApiTermination', :value => true)
+ @response.class.should.equal Hash
+ @response.return.should.equal "true"
+ end
+
+ specify "should successfully reset instance attribute" do
+ @ec2.stubs(:make_request).with('ResetInstanceAttribute', {"InstanceId"=>"i-33457a5a", "Attribute" => "kernel"}).
+ returns stub(:body => @reset_instance_attribute_response_body, :is_a? => true)
+ @response = @ec2.reset_instance_attribute(:instance_id => "i-33457a5a", :attribute => 'kernel')
+ @response.class.should.equal Hash
+ @response.return.should.equal "true"
+ end
end

0 comments on commit bdbdfc5

Please sign in to comment.