diff --git a/lib/fog/aws/models/ec2/address.rb b/lib/fog/aws/models/ec2/address.rb index 1472cd1c5..de7e75ecb 100644 --- a/lib/fog/aws/models/ec2/address.rb +++ b/lib/fog/aws/models/ec2/address.rb @@ -9,11 +9,15 @@ module Fog attribute :instance_id, 'instanceId' def destroy + requires :public_ip + connection.release_address(@public_ip) true end def instance=(new_instance) + requires :public_ip + if new_instance associate(new_instance) else diff --git a/lib/fog/aws/models/ec2/instance.rb b/lib/fog/aws/models/ec2/instance.rb index 4f314bbd1..2b5b6977b 100644 --- a/lib/fog/aws/models/ec2/instance.rb +++ b/lib/fog/aws/models/ec2/instance.rb @@ -24,10 +24,14 @@ module Fog attribute :user_data def addresses + requires :instance_id + connection.addresses(:instance => self) end def destroy + requires :instance_id + connection.terminate_instances(@instance_id) true end @@ -41,6 +45,8 @@ module Fog # end def key_pair + requires :key_name + connection.keypairs.all(@key_name).first end @@ -65,11 +71,15 @@ module Fog end def reboot + requires :instance_id + connection.reboot_instances(@instance_id) true end def save + requires :image_id + options = {} if @availability_zone options['Placement.AvailabilityZone'] = @availability_zone @@ -101,6 +111,8 @@ module Fog end def volumes + requires :instance_id + connection.volumes(:instance => self) end diff --git a/lib/fog/aws/models/ec2/key_pair.rb b/lib/fog/aws/models/ec2/key_pair.rb index 4c1bf56d5..83f8fb841 100644 --- a/lib/fog/aws/models/ec2/key_pair.rb +++ b/lib/fog/aws/models/ec2/key_pair.rb @@ -10,11 +10,15 @@ module Fog attribute :material, 'keyMaterial' def destroy + requires :name + connection.delete_key_pair(@name) true end def save + requires :name + data = connection.create_key_pair(@name).body new_attributes = data.reject {|key,value| !['keyFingerprint', 'keyMaterial', 'keyName'].include?(key)} merge_attributes(new_attributes) diff --git a/lib/fog/aws/models/ec2/security_group.rb b/lib/fog/aws/models/ec2/security_group.rb index e29ec2342..c01c107b2 100644 --- a/lib/fog/aws/models/ec2/security_group.rb +++ b/lib/fog/aws/models/ec2/security_group.rb @@ -11,6 +11,8 @@ module Fog attribute :owner_id, 'ownerId' def authorize_group_and_owner(group, owner) + requires :group_name + connection.authorize_security_group_ingress( 'GroupName' => @group_name, 'SourceSecurityGroupName' => group, @@ -19,6 +21,8 @@ module Fog end def authorize_port_range(range, options = {}) + requires :group_name + connection.authorize_security_group_ingress( 'CidrIp' => options[:cidr_ip] || '0.0.0.0/0', 'FromPort' => range.min, @@ -29,11 +33,15 @@ module Fog end def destroy + requires :group_name + connection.delete_security_group(@group_name) true end def save + requires :group_name + data = connection.create_security_group(@group_name, @group_description).body true end diff --git a/lib/fog/aws/models/ec2/snapshot.rb b/lib/fog/aws/models/ec2/snapshot.rb index 76474248b..8bf3ba0d7 100644 --- a/lib/fog/aws/models/ec2/snapshot.rb +++ b/lib/fog/aws/models/ec2/snapshot.rb @@ -12,18 +12,24 @@ module Fog attribute :volume_id, 'volumeId' def destroy + requires :snapshot_id + connection.delete_snapshot(@snapshot_id) true end def save - data = connection.create_snapshot(volume_id).body + requires :volume_id + + data = connection.create_snapshot(@volume_id).body new_attributes = data.reject {|key,value| key == 'requestId'} merge_attributes(new_attributes) true end def volume + requires :snapshot_id + connection.describe_volumes(@volume_id) end diff --git a/lib/fog/aws/models/ec2/volume.rb b/lib/fog/aws/models/ec2/volume.rb index c68c0bf05..82bd6988d 100644 --- a/lib/fog/aws/models/ec2/volume.rb +++ b/lib/fog/aws/models/ec2/volume.rb @@ -23,11 +23,15 @@ module Fog end def destroy + requires :volume_id + connection.delete_volume(@volume_id) true end def instance=(new_instance) + requires :volume_id + if new_instance attach(new_instance) else @@ -36,6 +40,8 @@ module Fog end def save + requires :availability_zone, :size, :snapshot_id + data = connection.create_volume(@availability_zone, @size, @snapshot_id).body new_attributes = data.reject {|key,value| key == 'requestId'} merge_attributes(new_attributes) @@ -46,7 +52,9 @@ module Fog end def snapshots - connection.snapshots(:volume_id => volume_id) + requires :volume_id + + connection.snapshots(:volume_id => @volume_id) end private diff --git a/lib/fog/aws/models/s3/bucket.rb b/lib/fog/aws/models/s3/bucket.rb index bc21da31e..beb387931 100644 --- a/lib/fog/aws/models/s3/bucket.rb +++ b/lib/fog/aws/models/s3/bucket.rb @@ -10,6 +10,8 @@ module Fog attribute :owner def destroy + requires :name + connection.delete_bucket(@name) true rescue Excon::Errors::NotFound @@ -17,6 +19,8 @@ module Fog end def location + requires :name + data = connection.get_bucket_location(@name) data.body['LocationConstraint'] end @@ -35,16 +39,22 @@ module Fog end def payer + requires :name + data = connection.get_request_payment(@name) data.body['Payer'] end def payer=(new_payer) + requires :name + connection.put_request_payment(@name, new_payer) @payer = new_payer end def save + requires :name + options = {} if @location options['LocationConstraint'] = @location diff --git a/lib/fog/aws/models/s3/object.rb b/lib/fog/aws/models/s3/object.rb index e632c4ee8..e80c9b948 100644 --- a/lib/fog/aws/models/s3/object.rb +++ b/lib/fog/aws/models/s3/object.rb @@ -20,6 +20,8 @@ module Fog end def copy(target_bucket_name, target_object_key) + requires :bucket, :key + data = connection.copy_object(bucket.name, @key, target_bucket_name, target_object_key).body target_bucket = connection.buckets.new(:name => target_bucket_name) target_object = target_bucket.objects.new(attributes.merge!(:key => target_object_key)) @@ -34,11 +36,14 @@ module Fog end def destroy + requires :bucket, :key + connection.delete_object(bucket.name, @key) true end def save(options = {}) + requires :body, :bucket, :key data = connection.put_object(bucket.name, @key, @body, options) @etag = data.headers['ETag'] true diff --git a/lib/fog/model.rb b/lib/fog/model.rb index 12e85e22e..95aa136e1 100644 --- a/lib/fog/model.rb +++ b/lib/fog/model.rb @@ -89,6 +89,20 @@ module Fog merge_attributes(new_attributes) end + def requires(*args) + missing = [] + for arg in [:connection] | args + missing << arg unless send("#{arg}") + end + unless missing.empty? + if missing.length == 1 + raise(ArgumentError, "#{missing.first} is required for this operation") + else + raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation") + end + end + end + private def collection=(new_collection) diff --git a/lib/fog/rackspace/models/servers/image.rb b/lib/fog/rackspace/models/servers/image.rb index 5ffb2ff85..e4ba92cfd 100644 --- a/lib/fog/rackspace/models/servers/image.rb +++ b/lib/fog/rackspace/models/servers/image.rb @@ -13,15 +13,21 @@ module Fog attribute :server_id, 'serverId' def server=(new_server) + requires :id + @server_id = new_server.id end def destroy + requires :id + connection.delete_image(@id) true end def save + requires :server_id + data = connection.create_server(@server_id) merge_attributes(data.body['image']) true diff --git a/lib/fog/rackspace/models/servers/server.rb b/lib/fog/rackspace/models/servers/server.rb index dad39fcca..e01fcd8b5 100644 --- a/lib/fog/rackspace/models/servers/server.rb +++ b/lib/fog/rackspace/models/servers/server.rb @@ -18,20 +18,27 @@ module Fog attribute :metadata def destroy + requires :id + connection.delete_server(@id) true end def images + requires :id + connection.images(:server => self) end def reboot(type = 'SOFT') + requires :id + connection.reboot_server(@id, type) true end def save + requires :flavor_id, :image_id, :name options = { 'metadata' => @metadata, 'personality' => @personality } options = options.reject {|key, value| value.nil?} data = connection.create_server(@flavor_id, @image_id, @name, options)