1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

make ec2 models use collection/identity abstraction, fix a couple mocks

This commit is contained in:
Wesley Beary 2009-10-23 22:23:55 -07:00
parent 0adeb99650
commit 45ef403102
22 changed files with 79 additions and 165 deletions

View file

@ -4,12 +4,9 @@ module Fog
class Address < Fog::Model
attribute :instance_id, 'instanceId'
attribute :public_ip, 'publicIp'
identity :public_ip, 'publicIp'
def addresses
@addresses
end
attribute :instance_id, 'instanceId'
def initialize(new_attributes = {})
new_attributes = {
@ -33,12 +30,6 @@ module Fog
end
end
def reload
if new_address = addresses.get(@public_ip)
merge_attributes(new_address.attributes)
end
end
def save
data = connection.allocate_address
@public_ip = data.body['publicIp']
@ -48,12 +39,6 @@ module Fog
true
end
private
def addresses=(new_addresses)
@addresses = new_addresses
end
end
end

View file

@ -26,7 +26,7 @@ module Fog
}.merge!(attributes))
data['addressesSet'].each do |address|
addresses << Fog::AWS::EC2::Address.new({
:addresses => addresses,
:collection => addresses,
:connection => connection
}.merge!(address))
end
@ -52,7 +52,7 @@ module Fog
def new
Fog::AWS::EC2::Address.new(
:addresses => self,
:collection => self,
:connection => connection,
:instance => instance
)

View file

@ -4,12 +4,13 @@ module Fog
class Instance < Fog::Model
identity :instance_id, 'instanceId'
attribute :ami_launch_index, 'amiLaunchIndex'
attribute :availability_zone, 'availabilityZone'
attribute :dns_name, 'dnsName'
attribute :group_id, 'groupId'
attribute :image_id, 'imageId'
attribute :instance_id, 'instanceId'
attribute :instance_state, 'instanceState'
attribute :instance_type, 'instanceType'
attribute :kernel_id, 'kernelId'
@ -47,10 +48,6 @@ module Fog
@key_name = new_keypair.name
end
def instances
@instances
end
def monitoring=(new_monitoring)
if new_monitoring.is_a?(Hash)
@monitoring = new_monitoring['state']
@ -67,12 +64,6 @@ module Fog
end
end
def reload
if new_instance = instances.get(@instance_id)
merge_attributes(new_instance.attributes)
end
end
def save
options = {}
if @availability_zone
@ -118,10 +109,6 @@ module Fog
end
end
def instances=(new_instances)
@instances = new_instances
end
end
end

View file

@ -24,8 +24,8 @@ module Fog
data['reservationSet'].each do |reservation|
reservation['instancesSet'].each do |instance|
instances << Fog::AWS::EC2::Instance.new({
:connection => connection,
:instances => self
:collection => instances,
:connection => connection
}.merge!(instance))
end
end
@ -49,8 +49,8 @@ module Fog
def new(attributes = {})
Fog::AWS::EC2::Instance.new(
attributes.merge!(
:connection => connection,
:instances => self
:collection => self,
:connection => connection
)
)
end

View file

@ -4,25 +4,16 @@ module Fog
class KeyPair < Fog::Model
identity :name, 'keyName'
attribute :fingerprint, 'keyFingerprint'
attribute :material, 'keyMaterial'
attribute :name, 'keyName'
def destroy
connection.delete_key_pair(@name)
true
end
def key_pairs
@key_pairs
end
def reload
if new_key_pair = key_pairs.get(@name)
merge_attributes(new_key_pair.attributes)
end
end
def save
data = connection.create_key_pair(@name).body
new_attributes = data.reject {|key,value| !['keyFingerprint', 'keyMaterial', 'keyName'].include?(key)}
@ -30,12 +21,6 @@ module Fog
true
end
private
def key_pairs=(new_key_pairs)
@key_pairs = new_key_pairs
end
end
end

View file

@ -23,8 +23,8 @@ module Fog
}.merge!(attributes))
data['keySet'].each do |key|
key_pairs << Fog::AWS::EC2::KeyPair.new({
:connection => connection,
:key_pairs => self
:collection => key_pairs,
:connection => connection
}.merge!(key))
end
key_pairs
@ -47,8 +47,8 @@ module Fog
def new(attributes = {})
Fog::AWS::EC2::KeyPair.new(
attributes.merge!(
:connection => connection,
:key_pairs => self
:collection => self,
:connection => connection
)
)
end

View file

@ -4,8 +4,9 @@ module Fog
class SecurityGroup < Fog::Model
identity :group_name, 'groupName'
attribute :group_description, 'groupDescription'
attribute :group_name, 'groupName'
attribute :ip_permissions, 'ipPermissions'
attribute :owner_id, 'ownerId'
@ -21,27 +22,11 @@ module Fog
true
end
def reload
if new_security_group = security_groups.get(@group_name)
merge_attributes(new_security_group.attributes)
end
end
def save
data = connection.create_security_group(@group_name, @group_description).body
true
end
def security_groups
@security_groups
end
private
def security_groups=(new_security_groups)
@security_groups = new_security_groups
end
end
end

View file

@ -23,8 +23,8 @@ module Fog
}.merge!(attributes))
data['securityGroupInfo'].each do |security_group|
security_groups << Fog::AWS::EC2::SecurityGroup.new({
:connection => connection,
:security_groups => self
:collection => security_groups,
:connection => connection
}.merge!(security_group))
end
security_groups
@ -47,8 +47,8 @@ module Fog
def new(attributes = {})
Fog::AWS::EC2::SecurityGroup.new(
attributes.merge!(
:connection => connection,
:security_groups => self
:collection => self,
:connection => connection
)
)
end

View file

@ -4,8 +4,9 @@ module Fog
class Snapshot < Fog::Model
identity :snapshot_id, 'snapshotId'
attribute :progress
attribute :snapshot_id, 'snapshotId'
attribute :start_time, 'startTime'
attribute :status
attribute :volume_id, 'volumeId'
@ -15,12 +16,6 @@ module Fog
true
end
def reload
if new_snapshot = snapshots.get(@snapshot_id)
merge_attributes(new_snapshot.attributes)
end
end
def save
data = connection.create_snapshot(volume_id).body
new_attributes = data.reject {|key,value| key == 'requestId'}
@ -28,20 +23,12 @@ module Fog
true
end
def snapshots
@snapshots
end
def volume
connection.describe_volumes(@volume_id)
end
private
def snapshots=(new_snapshots)
@snapshots = new_snapshots
end
def volume=(new_volume)
@volume_id = new_volume.volume_id
end

View file

@ -26,8 +26,8 @@ module Fog
}.merge!(attributes))
data['snapshotSet'].each do |snapshot|
snapshots << Fog::AWS::EC2::Snapshot.new({
:connection => connection,
:snapshots => self
:collection => snapshots,
:connection => connection
}.merge!(snapshot))
end
if volume_id
@ -53,8 +53,8 @@ module Fog
def new(attributes = {})
snapshot = Fog::AWS::EC2::Snapshot.new(
attributes.merge!(
:connection => connection,
:snapshots => self
:collection => self,
:connection => connection
)
)
if volume_id

View file

@ -4,6 +4,8 @@ module Fog
class Volume < Fog::Model
identity :volume_id, 'volumeId'
attribute :attach_time, 'attachTime'
attribute :availability_zone, 'availabilityZone'
attribute :create_time, 'createTime'
@ -12,7 +14,6 @@ module Fog
attribute :size
attribute :snapshot_id, 'snapshotId'
attribute :status
attribute :volume_id, 'volumeId'
def initialize(attributes = {})
if attributes['attachmentSet']
@ -39,12 +40,6 @@ module Fog
end
end
def reload
if new_volume = volumes.get(@volume_id)
merge_attributes(new_volume.attributes)
end
end
def save
data = connection.create_volume(@availability_zone, @size, @snapshot_id).body
new_attributes = data.reject {|key,value| key == 'requestId'}
@ -59,16 +54,6 @@ module Fog
connection.snapshots(:volume_id => volume_id)
end
def volumes
@volumes
end
private
def volumes=(new_volumes)
@volumes = new_volumes
end
end
end

View file

@ -26,8 +26,8 @@ module Fog
}.merge!(attributes))
data['volumeSet'].each do |volume|
volumes << Fog::AWS::EC2::Volume.new({
:connection => connection,
:volumes => self
:collection => volumes,
:connection => connection
}.merge!(volume))
end
if instance
@ -53,9 +53,9 @@ module Fog
def new(attributes = {})
volume = Fog::AWS::EC2::Volume.new(
attributes.merge!(
:connection => connection,
:instance => instance,
:volumes => self
:collection => self,
:connection => connection,
:instance => instance
)
)
end

View file

@ -63,9 +63,9 @@ else
'Code' => 'MissingParameter'
}
unless availability_zone
response['Message'] = 'The request must contain the parameter availability_zone'
response.body['Message'] = 'The request must contain the parameter availability_zone'
else
response['Message'] = 'The request must contain the parameter size'
response.body['Message'] = 'The request must contain the parameter size'
end
end
response

View file

@ -115,7 +115,7 @@ else
'ramdiskId' => options['RamdiskId'] || Fog::AWS::Mock.ramdisk_id,
'reason' => '',
'reservationId' => reservation_id,
'state' => 'pending'
'instanceState' => 'pending'
}
Fog::AWS::EC2.data[:instances][instance_id] = data
instances_set << data.reject{|key,value| !['amiLaunchIndex', 'dnsName', 'imageId', 'instanceId', 'instanceState', 'instanceType', 'kernelId', 'keyName', 'launchTime', 'monitoring', 'placement', 'privateDnsName', 'productCodes', 'ramdiskId', 'reason'].include?(key)}

View file

@ -18,12 +18,12 @@ describe 'Fog::AWS::EC2::Address' do
describe "#addresses" do
it "should return a Fog::AWS::EC2::Addresses" do
ec2.addresses.new.addresses.should be_a(Fog::AWS::EC2::Addresses)
ec2.addresses.new.collection.should be_a(Fog::AWS::EC2::Addresses)
end
it "should be the addresses the address is related to" do
addresses = ec2.addresses
addresses.new.addresses.should == addresses
addresses.new.collection.should == addresses
end
end
@ -101,12 +101,12 @@ describe 'Fog::AWS::EC2::Address' do
end
it "should not exist in addresses before save" do
@address.addresses.get(@address.public_ip).should be_nil
@address.collection.get(@address.public_ip).should be_nil
end
it "should exist in buckets after save" do
@address.save
@address.addresses.get(@address.public_ip).should_not be_nil
@address.collection.get(@address.public_ip).should_not be_nil
@address.destroy
end

View file

@ -62,15 +62,15 @@ describe 'Fog::AWS::EC2::Instance' do
end
end
describe "#instances" do
describe "#collection" do
it "should return a Fog::AWS::EC2::Instances" do
ec2.instances.new.instances.should be_a(Fog::AWS::EC2::Instances)
ec2.instances.new.collection.should be_a(Fog::AWS::EC2::Instances)
end
it "should be the instances the instance is related to" do
instances = ec2.instances
instances.new.instances.should == instances
instances.new.collection.should == instances
end
end
@ -136,12 +136,12 @@ describe 'Fog::AWS::EC2::Instance' do
end
it "should not exist in instances before save" do
@instance.instances.get(@instance.instance_id).should be_nil
ec2.instances.get(@instance.instance_id).should be_nil
end
it "should exist in buckets after save" do
@instance.save
@instance.instances.get(@instance.instance_id).should_not be_nil
ec2.instances.get(@instance.instance_id).should_not be_nil
@instance.destroy
end

View file

@ -17,15 +17,15 @@ describe 'Fog::AWS::EC2::KeyPair' do
end
describe "#key_pairs" do
describe "#collection" do
it "should return a Fog::AWS::EC2::KeyPairs" do
ec2.key_pairs.new.key_pairs.should be_a(Fog::AWS::EC2::KeyPairs)
ec2.key_pairs.new.collection.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
key_pairs.new.collection.should == key_pairs
end
end
@ -72,12 +72,12 @@ describe 'Fog::AWS::EC2::KeyPair' do
end
it "should not exist in key_pairs before save" do
@key_pair.key_pairs.get(@key_pair.name).should be_nil
ec2.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
ec2.key_pairs.get(@key_pair.name).should_not be_nil
@key_pair.destroy
end

View file

@ -19,15 +19,15 @@ describe 'Fog::AWS::EC2::SecurityGroup' do
end
describe "#security_groups" do
describe "#collection" do
it "should return a Fog::AWS::EC2::SecurityGroups" do
ec2.security_groups.new.security_groups.should be_a(Fog::AWS::EC2::SecurityGroups)
ec2.security_groups.new.collection.should be_a(Fog::AWS::EC2::SecurityGroups)
end
it "should be the security_groups the keypair is related to" do
security_groups = ec2.security_groups
security_groups.new.security_groups.should == security_groups
security_groups.new.collection.should == security_groups
end
end
@ -74,12 +74,12 @@ describe 'Fog::AWS::EC2::SecurityGroup' do
end
it "should not exist in security_groups before save" do
@security_group.security_groups.get(@security_group.group_name).should be_nil
ec2.security_groups.get(@security_group.group_name).should be_nil
end
it "should exist in buckets after save" do
@security_group.save
@security_group.security_groups.get(@security_group.group_name).should_not be_nil
ec2.security_groups.get(@security_group.group_name).should_not be_nil
@security_group.destroy
end

View file

@ -17,15 +17,15 @@ describe 'Fog::AWS::EC2::Snapshots' do
end
describe "#snapshots" do
describe "#collection" do
it "should return a Fog::AWS::EC2::Snapshots" do
ec2.snapshots.new.snapshots.should be_a(Fog::AWS::EC2::Snapshots)
ec2.snapshots.new.collection.should be_a(Fog::AWS::EC2::Snapshots)
end
it "should be the snapshots the snapshot is related to" do
snapshots = ec2.snapshots
snapshots.new.snapshots.should == snapshots
snapshots.new.collection.should == snapshots
end
end
@ -33,7 +33,7 @@ describe 'Fog::AWS::EC2::Snapshots' do
describe "#destroy" do
it "should return true if the snapshot is deleted" do
volume = ec2.volumes.create
volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
snapshot = volume.snapshots.create
snapshot.destroy.should be_true
volume.destroy
@ -44,7 +44,7 @@ describe 'Fog::AWS::EC2::Snapshots' do
describe "#reload" do
before(:each) do
@volume = ec2.volumes.create
@volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
@snapshot = @volume.snapshots.create
@reloaded = @snapshot.reload
end
@ -67,7 +67,7 @@ describe 'Fog::AWS::EC2::Snapshots' do
describe "#save" do
before(:each) do
@volume = ec2.volumes.create
@volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
@snapshot = @volume.snapshots.new
end
@ -81,12 +81,12 @@ describe 'Fog::AWS::EC2::Snapshots' do
end
it "should not exist in addresses before save" do
@snapshot.snapshots.get(@snapshot.snapshot_id).should be_nil
ec2.snapshots.get(@snapshot.snapshot_id).should be_nil
end
it "should exist in buckets after save" do
@snapshot.save
@snapshot.snapshots.get(@snapshot.snapshot_id).should_not be_nil
ec2.snapshots.get(@snapshot.snapshot_id).should_not be_nil
@snapshot.destroy
end

View file

@ -9,7 +9,7 @@ describe 'Fog::AWS::EC2::Snapshots' do
end
it "should include persisted snapshots" do
volume = ec2.volumes.create
volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
snapshot = volume.snapshots.create
ec2.snapshots.all.map {|snapshot| snapshot.snapshot_id}.should include(snapshot.snapshot_id)
snapshot.destroy
@ -17,8 +17,8 @@ describe 'Fog::AWS::EC2::Snapshots' do
end
it "should limit snapshots by volume_id if present" do
volume = ec2.volumes.create
other_volume = ec2.volumes.create
volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
other_volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
snapshot = volume.snapshots.create
other_volume.snapshots.all.map {|snapshot| snapshot.snapshot_id}.should_not include(snapshot.snapshot_id)
snapshot.destroy
@ -31,7 +31,7 @@ describe 'Fog::AWS::EC2::Snapshots' do
describe "#create" do
before(:each) do
@volume = ec2.volumes.create
@volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
@snapshot = @volume.snapshots.create
end
@ -53,7 +53,7 @@ describe 'Fog::AWS::EC2::Snapshots' do
describe "#get" do
it "should return a Fog::AWS::EC2::Snapshot if a matching snapshot exists" do
volume = ec2.volumes.create
volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
snapshot = volume.snapshots.create
get = ec2.snapshots.get(snapshot.snapshot_id)
snapshot.attributes.should == get.attributes

View file

@ -23,15 +23,15 @@ describe 'Fog::AWS::EC2::Volume' do
end
describe "#volumes" do
describe "#collection" do
it "should return a Fog::AWS::EC2::Volumes" do
ec2.volumes.new.volumes.should be_a(Fog::AWS::EC2::Volumes)
ec2.volumes.new.collection.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
volumes.new.collection.should == volumes
end
end
@ -126,12 +126,12 @@ describe 'Fog::AWS::EC2::Volume' do
end
it "should not exist in volumes before save" do
@volume.volumes.get(@volume.volume_id).should be_nil
ec2.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
ec2.volumes.get(@volume.volume_id).should_not be_nil
@volume.destroy
end

View file

@ -9,7 +9,7 @@ describe 'Fog::AWS::EC2::Volumes' do
end
it "should include persisted volumes" do
volume = ec2.volumes.create
volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
ec2.volumes.get(volume.volume_id).should_not be_nil
volume.destroy
end
@ -19,7 +19,7 @@ describe 'Fog::AWS::EC2::Volumes' do
describe "#create" do
before(:each) do
@volume = ec2.volumes.create
@volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
end
after(:each) do
@ -39,9 +39,9 @@ describe 'Fog::AWS::EC2::Volumes' do
describe "#get" do
it "should return a Fog::AWS::EC2::Volume if a matching volume exists" do
volume = ec2.volumes.create
volume = ec2.volumes.create(:availability_zone => 'us-east-1a', :size => 1, :device => 'dev/sdz1')
get = ec2.volumes.get(volume.volume_id)
volume.attributes.should == get.attributes
volume.attributes.reject { |key, value| key == :device }.should == get.attributes.reject { |key, value| key == :device }
volume.destroy
end