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

nicer error messages for missing required params on models

This commit is contained in:
Wesley Beary 2009-11-21 13:56:39 -08:00
parent 9e87d99a17
commit 7636b845b6
11 changed files with 86 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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)