mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #1763 from grimme-atix-de/customfields
[VSphere] Added VMware customvalue and customfields to read the annotatitions for each VM
This commit is contained in:
commit
d681420552
9 changed files with 166 additions and 0 deletions
|
@ -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
|
||||
|
@ -63,6 +67,8 @@ module Fog
|
|||
request :vm_reconfig_cpus
|
||||
request :vm_config_vnc
|
||||
request :create_folder
|
||||
request :list_vm_customvalues
|
||||
request :list_customfields
|
||||
|
||||
module Shared
|
||||
|
||||
|
|
19
lib/fog/vsphere/models/compute/customfield.rb
Normal file
19
lib/fog/vsphere/models/compute/customfield.rb
Normal file
|
@ -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
|
27
lib/fog/vsphere/models/compute/customfields.rb
Normal file
27
lib/fog/vsphere/models/compute/customfields.rb
Normal file
|
@ -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
|
17
lib/fog/vsphere/models/compute/customvalue.rb
Normal file
17
lib/fog/vsphere/models/compute/customvalue.rb
Normal file
|
@ -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
|
37
lib/fog/vsphere/models/compute/customvalues.rb
Normal file
37
lib/fog/vsphere/models/compute/customvalues.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
@ -186,6 +188,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
|
||||
|
@ -237,6 +243,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
|
||||
|
|
22
lib/fog/vsphere/requests/compute/list_customfields.rb
Normal file
22
lib/fog/vsphere/requests/compute/list_customfields.rb
Normal file
|
@ -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
|
22
lib/fog/vsphere/requests/compute/list_vm_customvalues.rb
Normal file
22
lib/fog/vsphere/requests/compute/list_vm_customvalues.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue