mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #1582 from grimme-atix-de/guesttype
[vSphere:] Implementation of Query for Guesttype and NICTypes
This commit is contained in:
commit
907c5eb213
15 changed files with 308 additions and 5 deletions
|
@ -30,6 +30,7 @@ require 'fog/core/scp'
|
|||
require 'fog/core/time'
|
||||
require 'fog/core/timeout'
|
||||
require 'fog/core/wait_for'
|
||||
require 'fog/core/class_from_string'
|
||||
|
||||
# data exchange specific (to be extracted and used on a per provider basis)
|
||||
require 'fog/xml'
|
||||
|
|
26
lib/fog/core/class_from_string.rb
Normal file
26
lib/fog/core/class_from_string.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Fog
|
||||
# get class by string or nil
|
||||
def self.class_from_string classname, defaultpath=""
|
||||
if classname and classname.is_a? String then
|
||||
chain = classname.split("::")
|
||||
klass = Kernel
|
||||
chain.each do |klass_string|
|
||||
klass = klass.const_get klass_string
|
||||
end
|
||||
if klass.is_a? Class then
|
||||
klass
|
||||
elsif defaultpath != nil then
|
||||
Fog.class_from_string((defaultpath.split("::")+chain).join("::"), nil)
|
||||
else
|
||||
nil
|
||||
end
|
||||
elsif classname and classname.is_a? Class then
|
||||
classname
|
||||
else
|
||||
nil
|
||||
end
|
||||
rescue NameError
|
||||
defaultpath != nil ? Fog.class_from_string((defaultpath.split("::")+chain).join("::"), nil) : nil
|
||||
end
|
||||
end
|
||||
|
|
@ -11,10 +11,14 @@ module Fog
|
|||
model_path 'fog/vsphere/models/compute'
|
||||
model :server
|
||||
collection :servers
|
||||
model :servertype
|
||||
collection :servertypes
|
||||
model :datacenter
|
||||
collection :datacenters
|
||||
model :interface
|
||||
collection :interfaces
|
||||
model :interfacetype
|
||||
collection :interfacetypes
|
||||
model :volume
|
||||
collection :volumes
|
||||
model :template
|
||||
|
@ -67,6 +71,10 @@ module Fog
|
|||
request :vm_reconfig_cpus
|
||||
request :vm_config_vnc
|
||||
request :create_folder
|
||||
request :list_server_types
|
||||
request :get_server_type
|
||||
request :list_interface_types
|
||||
request :get_interface_type
|
||||
request :list_vm_customvalues
|
||||
request :list_customfields
|
||||
|
||||
|
@ -96,7 +104,7 @@ module Fog
|
|||
:memory_mb => 'config.hardware.memoryMB',
|
||||
:cpus => 'config.hardware.numCPU',
|
||||
:overall_status => 'overallStatus',
|
||||
:guest_id => 'summary.guest.guestId',
|
||||
:guest_id => 'config.guestId',
|
||||
}
|
||||
|
||||
def convert_vm_view_to_attr_hash(vms)
|
||||
|
|
|
@ -29,6 +29,10 @@ module Fog
|
|||
service.servers({ :datacenter => path.join("/") }.merge(filters))
|
||||
end
|
||||
|
||||
def servertypes filters={}
|
||||
service.servertypes({:datacenter => name }.merge(filters))
|
||||
end
|
||||
|
||||
def customfields filters = {}
|
||||
service.customfields({ :datacenter => path.join("/")}.merge(filters))
|
||||
end
|
||||
|
|
|
@ -15,6 +15,9 @@ module Fog
|
|||
attribute :key
|
||||
|
||||
def initialize(attributes={} )
|
||||
if attributes.has_key? :type and attributes[:type].is_a? String then
|
||||
attributes[:type]=Fog.class_from_string(attributes[:type], "RbVmomi::VIM")
|
||||
end
|
||||
super defaults.merge(attributes)
|
||||
end
|
||||
|
||||
|
@ -25,15 +28,15 @@ module Fog
|
|||
private
|
||||
|
||||
def defaults
|
||||
default_type=Fog.credentials[:default_nic_type] || RbVmomi::VIM::VirtualE1000
|
||||
{
|
||||
:name=>"Network adapter",
|
||||
:network=>"VM Network",
|
||||
:summary=>"VM Network",
|
||||
:type=> RbVmomi::VIM::VirtualE1000,
|
||||
:type=> Fog.class_from_string(default_type, "RbVmomi::VIM"),
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
26
lib/fog/vsphere/models/compute/interfacetype.rb
Normal file
26
lib/fog/vsphere/models/compute/interfacetype.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Vsphere
|
||||
|
||||
class Interfacetype < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
# attribute :class
|
||||
attribute :name
|
||||
attribute :datacenter
|
||||
attribute :servertype
|
||||
|
||||
def initialize(attributes={} )
|
||||
super attributes
|
||||
end
|
||||
|
||||
def to_s
|
||||
name
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
40
lib/fog/vsphere/models/compute/interfacetypes.rb
Normal file
40
lib/fog/vsphere/models/compute/interfacetypes.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/vsphere/models/compute/interfacetype'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Vsphere
|
||||
|
||||
class Interfacetypes < Fog::Collection
|
||||
|
||||
model Fog::Compute::Vsphere::Interfacetype
|
||||
attr_accessor :datacenter
|
||||
attr_accessor :servertype
|
||||
|
||||
def all(filters = { })
|
||||
requires :servertype
|
||||
case servertype
|
||||
when Fog::Compute::Vsphere::Servertype
|
||||
load service.list_interface_types(filters.merge({
|
||||
:datacenter => datacenter,
|
||||
:servertype => servertype.id
|
||||
}))
|
||||
else
|
||||
raise 'interfacetypes should have a servertype'
|
||||
end
|
||||
end
|
||||
|
||||
def get(id)
|
||||
requires :servertype
|
||||
requires :datacenter
|
||||
new service.get_interface_type id, servertype, datacenter
|
||||
rescue Fog::Compute::Vsphere::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -231,7 +231,7 @@ module Fog
|
|||
{
|
||||
:cpus => 1,
|
||||
:memory_mb => 512,
|
||||
:guest_id => 'otherGuest',
|
||||
# :guest_id => 'otherGuest',
|
||||
:path => '/'
|
||||
}
|
||||
end
|
||||
|
|
39
lib/fog/vsphere/models/compute/servertype.rb
Normal file
39
lib/fog/vsphere/models/compute/servertype.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Vsphere
|
||||
|
||||
class Servertype < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :family
|
||||
attribute :fullname
|
||||
attribute :datacenter
|
||||
attribute :interfacetypes
|
||||
|
||||
def initialize(attributes={} )
|
||||
super defaults.merge(attributes)
|
||||
end
|
||||
|
||||
def to_s
|
||||
id
|
||||
end
|
||||
|
||||
def interfacetypes filters={}
|
||||
attributes[:interfacetypes] ||= service.interfacetypes({ :datacenter => datacenter, :servertype => self }.merge(filters))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def defaults
|
||||
{
|
||||
:id=>"otherGuest64",
|
||||
:family=>"otherGuestFamily",
|
||||
:interfacetypes => nil,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
29
lib/fog/vsphere/models/compute/servertypes.rb
Normal file
29
lib/fog/vsphere/models/compute/servertypes.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/vsphere/models/compute/servertype'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Vsphere
|
||||
|
||||
class Servertypes < Fog::Collection
|
||||
|
||||
model Fog::Compute::Vsphere::Servertype
|
||||
attr_accessor :datacenter, :id, :fullname
|
||||
|
||||
def all(filters = { })
|
||||
load service.list_server_types(filters.merge({:datacenter => datacenter}))
|
||||
end
|
||||
|
||||
def get(id)
|
||||
requires :datacenter
|
||||
new service.get_server_type(id, datacenter)
|
||||
rescue Fog::Compute::Vsphere::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
15
lib/fog/vsphere/requests/compute/get_interface_type.rb
Normal file
15
lib/fog/vsphere/requests/compute/get_interface_type.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Vsphere
|
||||
class Real
|
||||
def get_interface_type(id, servertype, datacenter, filter={})
|
||||
interfacetype=list_interface_types(filters={:id => id,
|
||||
:datacenter => datacenter,
|
||||
:servertype => servertype.id }).first
|
||||
raise(Fog::Compute::Vsphere::NotFound) unless interfacetype
|
||||
interfacetype
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
lib/fog/vsphere/requests/compute/get_server_type.rb
Normal file
32
lib/fog/vsphere/requests/compute/get_server_type.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Vsphere
|
||||
class Real
|
||||
def get_server_type(id, datacenter, filter={})
|
||||
server_type=get_raw_server_type(id, datacenter)
|
||||
raise(Fog::Compute::Vsphere::NotFound) unless server_type
|
||||
server_type_attributes(server_type, datacenter)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def get_raw_server_type(id, datacenter, filter={})
|
||||
types=raw_server_types(datacenter)
|
||||
raise(Fog::Compute::Vsphere::NotFound) unless types
|
||||
types=types.select{ | servertype | servertype.id == id }.first
|
||||
raise(Fog::Compute::Vsphere::NotFound) unless types
|
||||
types
|
||||
end
|
||||
end
|
||||
class Mock
|
||||
def get_server_type(id)
|
||||
{:id=>"rhel6Guest",
|
||||
:name=>"rhel6Guest",
|
||||
:family=>"linuxGuest",
|
||||
:fullname=>"Red Hat Enterprise Linux 6 (32-Bit)",
|
||||
:datacenter=>"Solutions"}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
25
lib/fog/vsphere/requests/compute/list_interface_types.rb
Normal file
25
lib/fog/vsphere/requests/compute/list_interface_types.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Vsphere
|
||||
class Real
|
||||
def list_interface_types(filters={})
|
||||
datacenter_name = filters[:datacenter]
|
||||
servertype_name = filters[:servertype]
|
||||
get_raw_server_type(servertype_name, datacenter_name)[:supportedEthernetCard].map do | nictype |
|
||||
next if filters.has_key?(:id) and filters[:id] != nictype
|
||||
interface_type_attributes(nictype, servertype_name, datacenter_name)
|
||||
end.compact
|
||||
end
|
||||
def interface_type_attributes(nic, servertype, datacenter)
|
||||
{
|
||||
:id => nic,
|
||||
:name => nic,
|
||||
:datacenter => datacenter,
|
||||
:servertype => servertype,
|
||||
# :class => Fog.class_from_string(nic)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
55
lib/fog/vsphere/requests/compute/list_server_types.rb
Normal file
55
lib/fog/vsphere/requests/compute/list_server_types.rb
Normal file
|
@ -0,0 +1,55 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Vsphere
|
||||
class Real
|
||||
def list_server_types(filters={})
|
||||
datacenter_name = filters[:datacenter]
|
||||
servertypes=raw_server_types(datacenter_name)
|
||||
if servertypes
|
||||
servertypes.map do | servertype |
|
||||
server_type_attributes(servertype, datacenter_name)
|
||||
end.compact
|
||||
else
|
||||
nil
|
||||
end
|
||||
#select{ | guestdesc | guestdesc.select{ | k, v | filter.has_key?(k) and filter[k] == v }==filter }
|
||||
end
|
||||
|
||||
def raw_server_types(datacenter_name, filter={})
|
||||
datacenter=find_raw_datacenter(datacenter_name)
|
||||
environmentBrowser=datacenter.hostFolder.childEntity.grep(RbVmomi::VIM::ComputeResource).first.environmentBrowser
|
||||
if environmentBrowser
|
||||
environmentBrowser.QueryConfigOption[:guestOSDescriptor]
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def server_type_attributes(servertype, datacenter)
|
||||
{
|
||||
:id => servertype.id,
|
||||
:name => servertype.id,
|
||||
:family => servertype.family,
|
||||
:fullname => servertype.fullName,
|
||||
:datacenter => datacenter,
|
||||
}
|
||||
end
|
||||
end
|
||||
class Mock
|
||||
def list_server_types(datacenter_name)
|
||||
[{:id=>"rhel6Guest",
|
||||
:name=>"rhel6Guest",
|
||||
:family=>"linuxGuest",
|
||||
:fullname=>"Red Hat Enterprise Linux 6 (32-Bit)",
|
||||
:datacenter=>"Solutions"},
|
||||
{:id=>"rhel5_64Guest",
|
||||
:name=>"rhel5_64Guest",
|
||||
:family=>"linuxGuest",
|
||||
:fullname=>"Red Hat Enterprise Linux 5 (64-Bit)",
|
||||
:datacenter=>"Solutions"}]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue