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

(#9241) Add model for Fog::Compute[:vsphere].servers

This patch adds a request list_virtual_machines which is responsible for
making an API connection and returning a raw "response" object from the
API.

Model instances of a Server (compute resource) are returned as a
collection through the "all" method.  The Fog framework calls all on the
instance of the collection.
This commit is contained in:
Jeff McCune 2011-09-01 15:10:57 -07:00
parent cf75fb1358
commit 8bc5c768ec
5 changed files with 134 additions and 0 deletions

View file

@ -8,6 +8,7 @@ module Fog
module Errors
class ServiceError < Fog::Errors::Error; end
class SecurityError < ServiceError; end
class NotFound < ServiceError; end
end
service(:compute, 'vsphere/compute')

View file

@ -8,8 +8,13 @@ module Fog
recognizes :vsphere_port, :vsphere_path, :vsphere_ns
recognizes :vsphere_rev, :vsphere_ssl, :vsphere_expected_pubkey_hash
model_path 'fog/vsphere/models/compute'
model :server
collection :servers
request_path 'fog/vsphere/requests/compute'
request :current_time
request :list_virtual_machines
class Mock

View file

@ -0,0 +1,60 @@
require 'fog/compute/models/server'
module Fog
module Compute
class Vsphere
class Server < Fog::Compute::Server
# This will be the instance uuid which is globally unique across
# a vSphere deployment.
identity :id
# JJM REVISIT (Extend the model of a vmware server)
# SEE: http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.VirtualMachine.html
# (Take note of the See also section.)
# In particular:
# GuestInfo: information about the guest operating system
# VirtualMachineConfigInfo: Access to the VMX file and configuration
attribute :name
# UUID may be the same from VM to VM if the user does not select (I copied it)
attribute :uuid
# Instance UUID should be unique across a vCenter deployment.
attribute :instance_uuid
attribute :hostname
attribute :operatingsystem
attribute :ipaddress
attribute :power_state, :aliases => 'power'
attribute :tools_state, :aliases => 'tools'
attribute :tools_version
attribute :mac_addresses, :aliases => 'macs'
attribute :hypervisor, :aliases => 'host'
attribute :is_a_template
# Create an instance of the server model from a
# vSphere Managed Object (mob) instance.
def self.attribute_hash_from_mob(vm)
{
:id => vm.config.instanceUuid || vm._ref,
:name => vm.name,
:uuid => vm.config.uuid,
:instance_uuid => vm.config.instanceUuid,
:hostname => vm.summary.guest.hostName,
:operatingsystem => vm.summary.guest.guestFullName,
:ipaddress => vm.summary.guest.ipAddress,
:power_state => vm.runtime.powerState,
:connection_state => vm.runtime.connectionState,
:hypervisor => vm.runtime.host.name,
:tools_state => vm.summary.guest.toolsStatus,
:tools_version => vm.summary.guest.toolsVersionStatus,
:mac_addresses => vm.macs,
:is_a_template => vm.config.template
}
end
end
end
end
end

View file

@ -0,0 +1,35 @@
require 'fog/core/collection'
require 'fog/vsphere/models/compute/server'
module Fog
module Compute
class Vsphere
class Servers < Fog::Collection
model Fog::Compute::Vsphere::Server
def all
# Virtual Machine Managed Objects (vm_mobs)
vm_mobs = connection.list_virtual_machines
vm_attributes = vm_mobs.collect do |vm_mob|
model.attribute_hash_from_mob(vm_mob)
end
load(vm_attributes)
end
def get(server_id)
raise NotImplementedError
# FIXME: (TODO) Make a raw API call
# Massage the data into an attribute hash
# Pass the attribute hash to new() to create a new instance of the model.
rescue Fog::Compute::Vsphere::NotFound
nil
end
end
end
end
end

View file

@ -0,0 +1,33 @@
module Fog
module Compute
class Vsphere
class Real
def list_virtual_machines
virtual_machines = Array.new
datacenters = @connection.rootFolder.children.find_all do |child|
child.kind_of? RbVmomi::VIM::Datacenter
end
# Next, look in the "vmFolder" of each data center:
datacenters.each do |dc|
dc.vmFolder.children.each do |vm|
virtual_machines << vm
end
end
virtual_machines
end
end
class Mock
def list_virtual_machines
Fog::Mock.not_implmented
end
end
end
end
end