From d68806965af9342337bb241e626934a13aa28aa4 Mon Sep 17 00:00:00 2001 From: Marc Grimme Date: Fri, 15 Feb 2013 10:14:50 +0100 Subject: [PATCH] [VSphere] Added VMware customvalue and customfields to read the annotations for each VM. --- lib/fog/vsphere/compute.rb | 6 +++ lib/fog/vsphere/models/compute/customfield.rb | 19 ++++++++++ .../vsphere/models/compute/customfields.rb | 27 ++++++++++++++ lib/fog/vsphere/models/compute/customvalue.rb | 17 +++++++++ .../vsphere/models/compute/customvalues.rb | 37 +++++++++++++++++++ lib/fog/vsphere/models/compute/datacenter.rb | 4 ++ lib/fog/vsphere/models/compute/server.rb | 12 ++++++ .../requests/compute/list_customfields.rb | 22 +++++++++++ .../requests/compute/list_vm_customvalues.rb | 22 +++++++++++ 9 files changed, 166 insertions(+) create mode 100644 lib/fog/vsphere/models/compute/customfield.rb create mode 100644 lib/fog/vsphere/models/compute/customfields.rb create mode 100644 lib/fog/vsphere/models/compute/customvalue.rb create mode 100644 lib/fog/vsphere/models/compute/customvalues.rb create mode 100644 lib/fog/vsphere/requests/compute/list_customfields.rb create mode 100644 lib/fog/vsphere/requests/compute/list_vm_customvalues.rb diff --git a/lib/fog/vsphere/compute.rb b/lib/fog/vsphere/compute.rb index cca0696d0..cc09c9858 100644 --- a/lib/fog/vsphere/compute.rb +++ b/lib/fog/vsphere/compute.rb @@ -29,6 +29,10 @@ module Fog collection :datastores model :folder collection :folders + model :customvalue + collection :customvalues + model :customfield + collection :customfields request_path 'fog/vsphere/requests/compute' request :current_time @@ -62,6 +66,8 @@ module Fog request :vm_reconfig_cpus request :vm_config_vnc request :create_folder + request :list_vm_customvalues + request :list_customfields module Shared diff --git a/lib/fog/vsphere/models/compute/customfield.rb b/lib/fog/vsphere/models/compute/customfield.rb new file mode 100644 index 000000000..f757a0002 --- /dev/null +++ b/lib/fog/vsphere/models/compute/customfield.rb @@ -0,0 +1,19 @@ +module Fog + module Compute + class Vsphere + + class Customfield < Fog::Model + + identity :key + + attribute :name + attribute :type + + def to_s + name + end + end + + end + end +end diff --git a/lib/fog/vsphere/models/compute/customfields.rb b/lib/fog/vsphere/models/compute/customfields.rb new file mode 100644 index 000000000..c20b58303 --- /dev/null +++ b/lib/fog/vsphere/models/compute/customfields.rb @@ -0,0 +1,27 @@ +require 'fog/core/collection' +require 'fog/vsphere/models/compute/customfield' + +module Fog + module Compute + class Vsphere + + class Customfields < Fog::Collection + + model Fog::Compute::Vsphere::Customfield + + attr_accessor :vm + + def all(filters = {}) + load service.list_customfields() + end + + def get(key) + load(service.list_customfields()).find do | cv | + cv.key == ((key.is_a? String) ? key.to_i : key) + end + end + + end + end + end +end diff --git a/lib/fog/vsphere/models/compute/customvalue.rb b/lib/fog/vsphere/models/compute/customvalue.rb new file mode 100644 index 000000000..1d8da4315 --- /dev/null +++ b/lib/fog/vsphere/models/compute/customvalue.rb @@ -0,0 +1,17 @@ +module Fog + module Compute + class Vsphere + + class Customvalue < Fog::Model + + attribute :value + attribute :key + + def to_s + value + end + end + + end + end +end diff --git a/lib/fog/vsphere/models/compute/customvalues.rb b/lib/fog/vsphere/models/compute/customvalues.rb new file mode 100644 index 000000000..b0de1aa31 --- /dev/null +++ b/lib/fog/vsphere/models/compute/customvalues.rb @@ -0,0 +1,37 @@ +require 'fog/core/collection' +require 'fog/vsphere/models/compute/customvalue' + +module Fog + module Compute + class Vsphere + + class Customvalues < Fog::Collection + + model Fog::Compute::Vsphere::Customvalue + + attr_accessor :vm + + def all(filters = {}) + requires :vm + case vm + when Fog::Compute::Vsphere::Server + load service.list_vm_customvalues(vm.id) + else + raise 'customvalues should have vm' + end + end + + def get(key) + requires :vm + case vm + when Fog::Compute::Vsphere::Server + load service.list_vm_customvalues(vm.id) + else + raise 'customvalues should have vm' + end.find { | cv | cv.key == key } + end + + end + end + end +end diff --git a/lib/fog/vsphere/models/compute/datacenter.rb b/lib/fog/vsphere/models/compute/datacenter.rb index 2282e9a85..f3d798c77 100644 --- a/lib/fog/vsphere/models/compute/datacenter.rb +++ b/lib/fog/vsphere/models/compute/datacenter.rb @@ -27,6 +27,10 @@ module Fog def virtual_machines filters = {} service.servers({ :datacenter => name }.merge(filters)) end + + def customfields filters = {} + service.customfields({ :datacenter => name}.merge(filters)) + end def to_s name diff --git a/lib/fog/vsphere/models/compute/server.rb b/lib/fog/vsphere/models/compute/server.rb index 086a90260..a9d1d406e 100644 --- a/lib/fog/vsphere/models/compute/server.rb +++ b/lib/fog/vsphere/models/compute/server.rb @@ -38,6 +38,7 @@ module Fog attribute :cpus attribute :interfaces attribute :volumes + attribute :customvalues attribute :overall_status, :aliases => 'status' attribute :cluster attribute :datacenter @@ -50,6 +51,7 @@ module Fog self.instance_uuid ||= id # TODO: remvoe instance_uuid as it can be replaced with simple id initialize_interfaces initialize_volumes + initialize_customvalues end # Lazy Loaded Attributes @@ -167,6 +169,10 @@ module Fog def volumes attributes[:volumes] ||= id.nil? ? [] : service.volumes( :vm => self ) end + + def customvalues + attributes[:customvalues] ||= id.nil? ? [] : service.customvalues( :vm => self ) + end def folder return nil unless datacenter and path @@ -218,6 +224,12 @@ module Fog self.attributes[:volumes].map! { |vol| vol.is_a?(Hash) ? service.volumes.new(vol) : vol } end end + + def initialize_customvalues + if attributes[:customvalues] and attributes[:customvalues].is_a?(Array) + self.attributes[:customvalues].map { |cfield| cfield.is_a?(Hash) ? service.customvalue.new(cfield) : cfield} + end + end end end diff --git a/lib/fog/vsphere/requests/compute/list_customfields.rb b/lib/fog/vsphere/requests/compute/list_customfields.rb new file mode 100644 index 000000000..d86e75c9b --- /dev/null +++ b/lib/fog/vsphere/requests/compute/list_customfields.rb @@ -0,0 +1,22 @@ +module Fog + module Compute + class Vsphere + class Real + def list_customfields() + @connection.serviceContent.customFieldsManager.field.map do |customfield| + { + :key => customfield.key.to_i, + :name => customfield.name, + :type => customfield.type + } + end + end + + end + class Mock + def list_vm_customfields() + end + end + end + end +end diff --git a/lib/fog/vsphere/requests/compute/list_vm_customvalues.rb b/lib/fog/vsphere/requests/compute/list_vm_customvalues.rb new file mode 100644 index 000000000..fab12c9c1 --- /dev/null +++ b/lib/fog/vsphere/requests/compute/list_vm_customvalues.rb @@ -0,0 +1,22 @@ +module Fog + module Compute + class Vsphere + class Real + def list_vm_customvalues(vm_id) + get_vm_ref(vm_id).summary.customValue.map do |customvalue| + { + :key => customvalue.key.to_i, + :value => customvalue.value, + } + end + + end + + end + class Mock + def list_vm_customfields(vm_id) + end + end + end + end +end