diff --git a/lib/fog/aws/ec2.rb b/lib/fog/aws/ec2.rb index 40eb29f72..e55149aa6 100644 --- a/lib/fog/aws/ec2.rb +++ b/lib/fog/aws/ec2.rb @@ -28,6 +28,8 @@ module Fog load "fog/aws/models/ec2/address.rb" load "fog/aws/models/ec2/addresses.rb" + load "fog/aws/models/ec2/key_pair.rb" + load "fog/aws/models/ec2/key_pairs.rb" load "fog/aws/models/ec2/snapshot.rb" load "fog/aws/models/ec2/snapshots.rb" load "fog/aws/models/ec2/volume.rb" diff --git a/lib/fog/aws/models/ec2/addresses.rb b/lib/fog/aws/models/ec2/addresses.rb index ef51e113c..8ec02bf0e 100644 --- a/lib/fog/aws/models/ec2/addresses.rb +++ b/lib/fog/aws/models/ec2/addresses.rb @@ -10,10 +10,16 @@ module Fog attribute :public_ip + def initialize(attributes) + @public_ip ||= [] + super + end + def all(public_ip = []) data = connection.describe_addresses(public_ip).body addresses = Fog::AWS::EC2::Addresses.new({ - :connection => connection + :connection => connection, + :public_ip => public_ip }.merge!(attributes)) data['addressesSet'].each do |address| addresses << Fog::AWS::EC2::Address.new({ @@ -38,7 +44,7 @@ module Fog def new Fog::AWS::EC2::Address.new( - :addresses => self, + :addresses => self, :connection => connection ) end diff --git a/lib/fog/aws/models/ec2/key_pair.rb b/lib/fog/aws/models/ec2/key_pair.rb index 31c0b3839..9b0216def 100644 --- a/lib/fog/aws/models/ec2/key_pair.rb +++ b/lib/fog/aws/models/ec2/key_pair.rb @@ -25,7 +25,7 @@ module Fog def save data = connection.create_key_pair(@name).body new_attributes = data.reject {|key,value| !['keyFingerprint', 'keyMaterial', 'keyName'].include?(key)} - update_attributes(new_attributes) + merge_attributes(new_attributes) true end diff --git a/lib/fog/aws/models/ec2/key_pairs.rb b/lib/fog/aws/models/ec2/key_pairs.rb index 40bbbabe4..51bf1295e 100644 --- a/lib/fog/aws/models/ec2/key_pairs.rb +++ b/lib/fog/aws/models/ec2/key_pairs.rb @@ -10,10 +10,16 @@ module Fog attribute :key_name + def initialize(attributes) + @key_name ||= [] + super + end + def all(key_name = []) data = connection.describe_key_pairs(key_name).body key_pairs = Fog::AWS::EC2::KeyPairs.new({ - :connection => connection + :connection => connection, + :key_name => key_name }.merge!(attributes)) data['keySet'].each do |key| key_pairs << Fog::AWS::EC2::KeyPair.new({ diff --git a/lib/fog/aws/models/ec2/security_groups.rb b/lib/fog/aws/models/ec2/security_groups.rb index e7aba4a12..b73fe0692 100644 --- a/lib/fog/aws/models/ec2/security_groups.rb +++ b/lib/fog/aws/models/ec2/security_groups.rb @@ -10,10 +10,16 @@ module Fog attribute :group_name + def initialize(attributes) + @group_name ||= [] + super + end + def all(group_name = []) data = connection.describe_security_groups(group_name) security_groups = Fog::AWS::EC2::SecurityGroups.new({ - :connection => connection + :connection => connection, + :group_name => group_name }.merge!(attributes)) data['securityGroupInfo'].each do |security_group| security_groups << Fog::AWS::EC2::SecurityGroup.new({ diff --git a/lib/fog/aws/models/ec2/snapshots.rb b/lib/fog/aws/models/ec2/snapshots.rb index 274001ff6..079ea89a4 100644 --- a/lib/fog/aws/models/ec2/snapshots.rb +++ b/lib/fog/aws/models/ec2/snapshots.rb @@ -11,10 +11,16 @@ module Fog attribute :snapshot_id attribute :volume_id + def initialize(attributes) + @snapshot_id ||= [] + super + end + def all(snapshot_id = []) data = connection.describe_snapshots(snapshot_id).body snapshots = Fog::AWS::EC2::Snapshots.new({ - :connection => connection + :connection => connection, + :snapshot_id => snapshot_id }.merge!(attributes)) data['snapshotSet'].each do |snapshot| snapshots << Fog::AWS::EC2::Snapshot.new({ diff --git a/lib/fog/aws/models/ec2/volumes.rb b/lib/fog/aws/models/ec2/volumes.rb index 90b95be51..c9a10e902 100644 --- a/lib/fog/aws/models/ec2/volumes.rb +++ b/lib/fog/aws/models/ec2/volumes.rb @@ -10,10 +10,16 @@ module Fog attribute :volume_id + def initialize(attributes) + @volume_id ||= [] + super + end + def all(volume_id = []) data = connection.describe_volumes(volume_id).body volumes = Fog::AWS::EC2::Volumes.new({ - :connection => connection + :connection => connection, + :volume_id => volume_id }.merge!(attributes)) data['volumeSet'].each do |volume| volumes << Fog::AWS::EC2::Volume.new({ diff --git a/spec/aws/models/ec2/key_pair_spec.rb b/spec/aws/models/ec2/key_pair_spec.rb new file mode 100644 index 000000000..90f741aca --- /dev/null +++ b/spec/aws/models/ec2/key_pair_spec.rb @@ -0,0 +1,84 @@ +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe 'Fog::AWS::EC2::KeyPair' do + + describe "#initialize" do + + it "should remap attributes from parser" #do + # address = Fog::AWS::EC2::KeyPair.new( + # 'instanceId' => 'i-00000000', + # 'publicIp' => '0.0.0.0' + # ) + # address.instance_id.should == 'i-00000000' + # address.public_ip.should == '0.0.0.0' + # end + + end + + describe "#key_pairs" do + + it "should return a Fog::AWS::EC2::KeyPairs" do + ec2.key_pairs.new.key_pairs.should be_a(Fog::AWS::EC2::KeyPairs) + end + + it "should be the key_pairs the keypair is related to" do + key_pairs = ec2.key_pairs + key_pairs.new.key_pairs.should == key_pairs + end + + end + + describe "#destroy" do + + it "should return true if the key_pair is deleted" do + address = ec2.key_pairs.create(:name => 'keyname') + address.destroy.should be_true + end + + end + + describe "#reload" do + + before(:each) do + @key_pair = ec2.key_pairs.create(:name => 'keyname') + @reloaded = @key_pair.reload + end + + after(:each) do + @key_pair.destroy + end + + it "should return a Fog::AWS::EC2::KeyPair" do + @reloaded.should be_a(Fog::AWS::EC2::KeyPair) + end + + it "should reset attributes to remote state" do + @key_pair.attributes.should == @reloaded.attributes + end + + end + + describe "#save" do + + before(:each) do + @key_pair = ec2.key_pairs.new(:name => 'keyname') + end + + it "should return true when it succeeds" do + @key_pair.save.should be_true + @key_pair.destroy + end + + it "should not exist in key_pairs before save" do + @key_pair.key_pairs.get(@key_pair.name).should be_nil + end + + it "should exist in buckets after save" do + @key_pair.save + @key_pair.key_pairs.get(@key_pair.name).should_not be_nil + @key_pair.destroy + end + + end + +end diff --git a/spec/aws/models/ec2/key_pairs_spec.rb b/spec/aws/models/ec2/key_pairs_spec.rb new file mode 100644 index 000000000..cdef3989a --- /dev/null +++ b/spec/aws/models/ec2/key_pairs_spec.rb @@ -0,0 +1,71 @@ +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe 'Fog::AWS::EC2::KeyPairs' do + + describe "#all" do + + it "should return a Fog::AWS::EC2::KeyPairs" do + ec2.key_pairs.all.should be_a(Fog::AWS::EC2::KeyPairs) + end + + it "should include persisted key_pairs" do + key_pair = ec2.key_pairs.create(:name => 'keyname') + ec2.key_pairs.get(key_pair.name).should_not be_nil + key_pair.destroy + end + + end + + describe "#create" do + + before(:each) do + @key_pair = ec2.key_pairs.create(:name => 'keyname') + end + + after(:each) do + @key_pair.destroy + end + + it "should return a Fog::AWS::EC2::KeyPair" do + @key_pair.should be_a(Fog::AWS::EC2::KeyPair) + end + + it "should exist on ec2" do + ec2.key_pairs.get(@key_pair.name).should_not be_nil + end + + end + + describe "#get" do + + it "should return a Fog::AWS::EC2::KeyPair if a matching key_pair exists" do + key_pair = ec2.key_pairs.create(:name => 'keyname') + get = ec2.key_pairs.get(key_pair.name) + key_pair.attributes[:fingerprint].should == get.attributes[:fingerprint] + key_pair.attributes[:name].should == get.attributes[:name] + key_pair.destroy + end + + it "should return nil if no matching key_pair exists" do + ec2.key_pairs.get('notakeyname').should be_nil + end + + end + + describe "#new" do + + it "should return a Fog::AWS::EC2::KeyPair" do + ec2.key_pairs.new(:name => 'keyname').should be_a(Fog::AWS::EC2::KeyPair) + end + + end + + describe "#reload" do + + it "should return a Fog::AWS::EC2::KeyPairs" do + ec2.key_pairs.all.reload.should be_a(Fog::AWS::EC2::KeyPairs) + end + + end + +end