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

Cleaned code and fixed issue with getting a resource too soon after request for creation.

This commit is contained in:
jordangbull 2013-07-11 10:30:54 -07:00
parent 50f54278d7
commit a3368acb13
5 changed files with 34 additions and 30 deletions

View file

@ -137,6 +137,22 @@ module Fog
response
end
def backoff_if_unfound(&block)
retries_remaining = 5
begin
result = block.call
rescue Exception => msg
if msg.to_s.include? 'was not found' and retries_remaining > 0
retries_remaining -= 1
sleep 0.1
retry
else
raise msg
end
end
result
end
end
RUNNING = 'RUNNING'

View file

@ -20,7 +20,7 @@ module Fog
def save
data = service.insert_disk(name, size_gb, zone_name, image_name).body
data = service.get_disk(self.name, zone_name).body
data = service.backoff_if_unfound {service.get_disk(name, zone_name).body}
service.disks.merge_attributes(data)
end

View file

@ -83,14 +83,7 @@ module Fog
}
options.delete_if {|key, value| value.nil?}
service.insert_server(name, zone_name, options)
begin
data = service.get_server(self.name, self.zone_name).body
rescue Exception
puts "waiting for server to be callable..."
sleep 0.1
retry
end
data = service.backoff_if_unfound {service.get_server(self.name, self.zone_name).body}
service.servers.merge_attributes(data)
end

View file

@ -12,19 +12,17 @@ module Fog
class Real
def insert_disk(disk_name, disk_size, zone_name=@default_zone, image=nil)
def insert_disk(disk_name, disk_size, zone_name=@default_zone, image_name=nil)
api_method = @compute.disks.insert
parameters = {
'project' => @project,
'zone' => zone_name
}
if image
# We need to check if the image is owned by the user or a global image.
if get_image(image, @project).data[:status] == 200
parameters['sourceImage'] = @api_url + @project + "/global/images/#{image}"
else
parameters['sourceImage'] = @api_url + "google/global/images/#{image}"
end
if image_name
# We don't know the owner of the image.
image = images.create({:name => image_name})
@image_url = @api_url + image.resource_url
parameters['sourceImage'] = @image_url
end
body_object = {
'name' => disk_name,
@ -33,8 +31,7 @@ module Fog
result = self.build_result(api_method, parameters,
body_object)
disk_name = MultiJson.load(result.body)["targetLink"].split('/')[-1]
return get_disk(disk_name, zone_name)
response = self.build_response(result)
end
end

View file

@ -17,8 +17,9 @@ module Fog
end
def insert_server(server_name, zone_name, options={}, *deprecated_args)
if (deprecated_args.length > 0)
raise "Too many parameters specified. This may be the cause of code written for an outdated version of fog. Usage: "#TODO
if deprecated_args.length > 0 or not options.is_a? Hash
raise ArgumentError.new 'Too many parameters specified. This may be the cause of code written for an outdated'\
' version of fog. Usage: server_name, zone_name, [options]'
end
api_method = @compute.instances.insert
parameters = {
@ -29,13 +30,10 @@ module Fog
if options.has_key? 'image'
image_name = options.delete 'image'
# We need to check if the image is owned by the user or a global image.
if get_image(image_name, @project).data[:status] == 200
image_url = @api_url + @project + "/global/images/#{image_name}"
else
image_url = @api_url + "google/global/images/#{image_name}"
end
body_object['image'] = image_url
# We don't know the owner of the image.
image = images.create({:name => image_name})
@image_url = @api_url + image.resource_url
body_object['image'] = @image_url
end
body_object['machineType'] = @api_url + @project + "/zones/#{zone_name}/machineTypes/#{options.delete 'machineType'}"
networkInterfaces = []
@ -47,7 +45,7 @@ module Fog
]
}
end
#TODO add other networks
# TODO: add other networks
body_object['networkInterfaces'] = networkInterfaces
if options['disks']
@ -65,7 +63,7 @@ module Fog
if options['kernel']
body_object['kernel'] = @api_url + "google/global/kernels/#{options.delete 'kernel'}"
end
body_object.merge! options #adds in all remaining options that weren't explicitly handled
body_object.merge! options # Adds in all remaining options that weren't explicitly handled.
result = self.build_result(api_method, parameters,
body_object=body_object)