diff --git a/lib/fog/aws/ec2.rb b/lib/fog/aws/ec2.rb index 413a8f14c..c1b199e2d 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/volume.rb" + load "fog/aws/models/ec2/volumes.rb" load "fog/aws/parsers/ec2/allocate_address.rb" load "fog/aws/parsers/ec2/attach_volume.rb" diff --git a/lib/fog/aws/models/ec2/volume.rb b/lib/fog/aws/models/ec2/volume.rb index bb1ee5ed8..9b5171464 100644 --- a/lib/fog/aws/models/ec2/volume.rb +++ b/lib/fog/aws/models/ec2/volume.rb @@ -16,7 +16,7 @@ module Fog def initialize(attributes = {}) if attributes['attachmentSet'] - attributes.merge!(attributes.delete('attachmentSet')) + attributes.merge!(attributes.delete('attachmentSet').first || {}) end super end @@ -34,7 +34,7 @@ module Fog def save data = connection.create_volume(@availability_zone, @size, @snapshot_id).body new_attributes = data.reject {|key,value| key == 'requestId'} - update_attributes(new_attributes) + merge_attributes(new_attributes) true end diff --git a/lib/fog/aws/models/ec2/volumes.rb b/lib/fog/aws/models/ec2/volumes.rb index fb739370f..ce23e15cf 100644 --- a/lib/fog/aws/models/ec2/volumes.rb +++ b/lib/fog/aws/models/ec2/volumes.rb @@ -8,9 +8,14 @@ module Fog class Volumes < Fog::Collection + attribute :volume_id + def all(volume_id = []) - data = connection.describe_volumes(volume_id) - volumes = Fog::AWS::EC2::Volumes.new(:connection => connection) + data = connection.describe_volumes(volume_id).body + volumes = Fog::AWS::EC2::Volumes.new( + :connection => connection, + :volume_id => volume_id + ) data['volumeSet'].each do |volume| volumes << Fog::AWS::EC2::Volume.new({ :connection => connection, @@ -26,6 +31,12 @@ module Fog volume end + def get(volume_id) + all(volume_id).first + rescue Fog::Errors::BadRequest + nil + end + def new(attributes = {}) Fog::AWS::EC2::Volume.new( attributes.merge!( @@ -35,6 +46,10 @@ module Fog ) end + def reload + all(volume_id) + end + end end diff --git a/spec/aws/models/ec2/volume_spec.rb b/spec/aws/models/ec2/volume_spec.rb new file mode 100644 index 000000000..ef31e0b50 --- /dev/null +++ b/spec/aws/models/ec2/volume_spec.rb @@ -0,0 +1,84 @@ +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe 'Fog::AWS::EC2::Volume' do + + describe "#initialize" do + + it "should remap attributes from parser" # do + # volume = Fog::AWS::EC2::Volume.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 "#volumes" do + + it "should return a Fog::AWS::EC2::Volumes" do + ec2.volumes.new.volumes.should be_a(Fog::AWS::EC2::Volumes) + end + + it "should be the volumes the volume is related to" do + volumes = ec2.volumes + volumes.new.volumes.should == volumes + end + + end + + describe "#destroy" do + + it "should return true if the volume is deleted" do + volume = ec2.volumes.create + volume.destroy.should be_true + end + + end + + describe "#reload" do + + before(:each) do + @volume = ec2.volumes.create + @reloaded = @volume.reload + end + + after(:each) do + @volume.destroy + end + + it "should return a Fog::AWS::EC2::Volume" do + @reloaded.should be_a(Fog::AWS::EC2::Volume) + end + + it "should reset attributes to remote state" do + @volume.attributes.should == @reloaded.attributes + end + + end + + describe "#save" do + + before(:each) do + @volume = ec2.volumes.new + end + + it "should return true when it succeeds" do + @volume.save.should be_true + @volume.destroy + end + + it "should not exist in addresses before save" do + @volume.volumes.get(@volume.volume_id).should be_nil + end + + it "should exist in buckets after save" do + @volume.save + @volume.volumes.get(@volume.volume_id).should_not be_nil + @volume.destroy + end + + end + +end diff --git a/spec/aws/models/ec2/volumes_spec.rb b/spec/aws/models/ec2/volumes_spec.rb new file mode 100644 index 000000000..038523df2 --- /dev/null +++ b/spec/aws/models/ec2/volumes_spec.rb @@ -0,0 +1,70 @@ +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe 'Fog::AWS::EC2::Volumes' do + + describe "#all" do + + it "should return a Fog::AWS::EC2::Volumes" do + ec2.volumes.all.should be_a(Fog::AWS::EC2::Volumes) + end + + it "should include persisted volumes" do + volume = ec2.volumes.create + ec2.volumes.get(volume.volume_id).should_not be_nil + volume.destroy + end + + end + + describe "#create" do + + before(:each) do + @volume = ec2.volumes.create + end + + after(:each) do + @volume.destroy + end + + it "should return a Fog::AWS::EC2::Volume" do + @volume.should be_a(Fog::AWS::EC2::Volume) + end + + it "should exist on ec2" do + ec2.volumes.get(@volume.volume_id).should_not be_nil + end + + end + + describe "#get" do + + it "should return a Fog::AWS::EC2::Volume if a matching volume exists" do + volume = ec2.volumes.create + get = ec2.volumes.get(volume.volume_id) + volume.attributes.should == get.attributes + volume.destroy + end + + it "should return nil if no matching address exists" do + ec2.volumes.get('vol-00000000').should be_nil + end + + end + + describe "#new" do + + it "should return a Fog::AWS::EC2::Volume" do + ec2.volumes.new.should be_a(Fog::AWS::EC2::Volume) + end + + end + + describe "#reload" do + + it "should return a Fog::AWS::EC2::Volumes" do + ec2.volumes.all.reload.should be_a(Fog::AWS::EC2::Volumes) + end + + end + +end