From b73ab8ce03e5ed758d1ece109900159ddaa97ecc Mon Sep 17 00:00:00 2001 From: Marc Grimme Date: Fri, 27 Sep 2013 11:48:31 +0200 Subject: [PATCH] [vSphere:] Implementation of Query for Guesttype and NICTypes --- lib/fog/core.rb | 1 + lib/fog/core/class_from_string.rb | 26 +++++++++ lib/fog/vsphere/compute.rb | 10 +++- lib/fog/vsphere/models/compute/datacenter.rb | 4 ++ lib/fog/vsphere/models/compute/interface.rb | 7 ++- .../vsphere/models/compute/interfacetype.rb | 26 +++++++++ .../vsphere/models/compute/interfacetypes.rb | 40 ++++++++++++++ lib/fog/vsphere/models/compute/server.rb | 2 +- lib/fog/vsphere/models/compute/servertype.rb | 39 +++++++++++++ lib/fog/vsphere/models/compute/servertypes.rb | 29 ++++++++++ lib/fog/vsphere/requests/compute/create_vm.rb | 2 +- .../requests/compute/get_interface_type.rb | 15 +++++ .../requests/compute/get_server_type.rb | 32 +++++++++++ .../requests/compute/list_interface_types.rb | 25 +++++++++ .../requests/compute/list_server_types.rb | 55 +++++++++++++++++++ 15 files changed, 308 insertions(+), 5 deletions(-) create mode 100644 lib/fog/core/class_from_string.rb create mode 100644 lib/fog/vsphere/models/compute/interfacetype.rb create mode 100644 lib/fog/vsphere/models/compute/interfacetypes.rb create mode 100644 lib/fog/vsphere/models/compute/servertype.rb create mode 100644 lib/fog/vsphere/models/compute/servertypes.rb create mode 100644 lib/fog/vsphere/requests/compute/get_interface_type.rb create mode 100644 lib/fog/vsphere/requests/compute/get_server_type.rb create mode 100644 lib/fog/vsphere/requests/compute/list_interface_types.rb create mode 100644 lib/fog/vsphere/requests/compute/list_server_types.rb diff --git a/lib/fog/core.rb b/lib/fog/core.rb index f6eda6cdc..b12e54e64 100644 --- a/lib/fog/core.rb +++ b/lib/fog/core.rb @@ -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' diff --git a/lib/fog/core/class_from_string.rb b/lib/fog/core/class_from_string.rb new file mode 100644 index 000000000..79276047c --- /dev/null +++ b/lib/fog/core/class_from_string.rb @@ -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 + diff --git a/lib/fog/vsphere/compute.rb b/lib/fog/vsphere/compute.rb index 607d7fe7f..e86175510 100644 --- a/lib/fog/vsphere/compute.rb +++ b/lib/fog/vsphere/compute.rb @@ -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) diff --git a/lib/fog/vsphere/models/compute/datacenter.rb b/lib/fog/vsphere/models/compute/datacenter.rb index f3d798c77..9ce3fd1cb 100644 --- a/lib/fog/vsphere/models/compute/datacenter.rb +++ b/lib/fog/vsphere/models/compute/datacenter.rb @@ -28,6 +28,10 @@ module Fog service.servers({ :datacenter => name }.merge(filters)) end + def servertypes filters={} + service.servertypes({:datacenter => name }.merge(filters)) + end + def customfields filters = {} service.customfields({ :datacenter => name}.merge(filters)) end diff --git a/lib/fog/vsphere/models/compute/interface.rb b/lib/fog/vsphere/models/compute/interface.rb index 19cab3781..a3f505ff2 100644 --- a/lib/fog/vsphere/models/compute/interface.rb +++ b/lib/fog/vsphere/models/compute/interface.rb @@ -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 diff --git a/lib/fog/vsphere/models/compute/interfacetype.rb b/lib/fog/vsphere/models/compute/interfacetype.rb new file mode 100644 index 000000000..68a2578a9 --- /dev/null +++ b/lib/fog/vsphere/models/compute/interfacetype.rb @@ -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 diff --git a/lib/fog/vsphere/models/compute/interfacetypes.rb b/lib/fog/vsphere/models/compute/interfacetypes.rb new file mode 100644 index 000000000..d090f4b20 --- /dev/null +++ b/lib/fog/vsphere/models/compute/interfacetypes.rb @@ -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 + diff --git a/lib/fog/vsphere/models/compute/server.rb b/lib/fog/vsphere/models/compute/server.rb index 691f51630..8aa673d43 100644 --- a/lib/fog/vsphere/models/compute/server.rb +++ b/lib/fog/vsphere/models/compute/server.rb @@ -231,7 +231,7 @@ module Fog { :cpus => 1, :memory_mb => 512, - :guest_id => 'otherGuest', +# :guest_id => 'otherGuest', :path => '/' } end diff --git a/lib/fog/vsphere/models/compute/servertype.rb b/lib/fog/vsphere/models/compute/servertype.rb new file mode 100644 index 000000000..ebfc1acd7 --- /dev/null +++ b/lib/fog/vsphere/models/compute/servertype.rb @@ -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 diff --git a/lib/fog/vsphere/models/compute/servertypes.rb b/lib/fog/vsphere/models/compute/servertypes.rb new file mode 100644 index 000000000..3dcdd2c58 --- /dev/null +++ b/lib/fog/vsphere/models/compute/servertypes.rb @@ -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 + diff --git a/lib/fog/vsphere/requests/compute/create_vm.rb b/lib/fog/vsphere/requests/compute/create_vm.rb index 3244b83a6..b5139087f 100644 --- a/lib/fog/vsphere/requests/compute/create_vm.rb +++ b/lib/fog/vsphere/requests/compute/create_vm.rb @@ -126,4 +126,4 @@ module Fog end end end -end \ No newline at end of file +end diff --git a/lib/fog/vsphere/requests/compute/get_interface_type.rb b/lib/fog/vsphere/requests/compute/get_interface_type.rb new file mode 100644 index 000000000..27d3c8668 --- /dev/null +++ b/lib/fog/vsphere/requests/compute/get_interface_type.rb @@ -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 diff --git a/lib/fog/vsphere/requests/compute/get_server_type.rb b/lib/fog/vsphere/requests/compute/get_server_type.rb new file mode 100644 index 000000000..2af41bb35 --- /dev/null +++ b/lib/fog/vsphere/requests/compute/get_server_type.rb @@ -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 diff --git a/lib/fog/vsphere/requests/compute/list_interface_types.rb b/lib/fog/vsphere/requests/compute/list_interface_types.rb new file mode 100644 index 000000000..3af73e048 --- /dev/null +++ b/lib/fog/vsphere/requests/compute/list_interface_types.rb @@ -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 diff --git a/lib/fog/vsphere/requests/compute/list_server_types.rb b/lib/fog/vsphere/requests/compute/list_server_types.rb new file mode 100644 index 000000000..46a3f106c --- /dev/null +++ b/lib/fog/vsphere/requests/compute/list_server_types.rb @@ -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