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

[vsphere|compute] Implemented idiomatic interface creation.

This commit is contained in:
Kevin Menard 2013-11-01 15:28:37 -04:00
parent 035129e40f
commit e334e545ef
3 changed files with 32 additions and 2 deletions

View file

@ -4,6 +4,8 @@ module Fog
class Interface < Fog::Model
SAVE_MUTEX = Mutex.new
identity :mac
alias :id :mac
@ -44,6 +46,34 @@ module Fog
service.destroy_vm_interface(server_id, :key => key, :type => type)
end
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
requires :server_id, :type, :network
# Our approach of finding the newly created interface is rough. We assume that the :key value always increments
# and thus the highest :key value must correspond to the created interface. Since this has an inherent race
# condition we need to gate the saves.
SAVE_MUTEX.synchronize do
data = service.add_vm_interface(server_id, attributes)
if data['task_state'] == 'success'
# We have to query vSphere to get the interface attributes since the task handle doesn't include that info.
created = server.interfaces.all.sort_by(&:key).last
self.mac = created.mac
self.name = created.name
self.status = created.status
self.summary = created.summary
self.key = created.key
self.virtualswitch = created.virtualswitch
true
else
false
end
end
end
private
def defaults

View file

@ -48,7 +48,7 @@ module Fog
def new(attributes = {})
if server
super({ :server => server }.merge(attributes))
super({ :server_id => server.id }.merge(attributes))
else
super
end

View file

@ -6,7 +6,7 @@ module Fog
def add_vm_interface(vmid, options = {})
raise ArgumentError, "instance id is a required parameter" unless vmid
interface = get_interface_from_options(vmid, options)
interface = get_interface_from_options(vmid, options.merge(:server_id => vmid))
vm_reconfig_hardware('instance_uuid' => vmid, 'hardware_spec' => {'deviceChange'=>[create_interface(interface)]})
end