diff --git a/lib/fog/vsphere.rb b/lib/fog/vsphere.rb index ae5591d0c..104e15740 100644 --- a/lib/fog/vsphere.rb +++ b/lib/fog/vsphere.rb @@ -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') diff --git a/lib/fog/vsphere/compute.rb b/lib/fog/vsphere/compute.rb index 133805842..2850a66f3 100644 --- a/lib/fog/vsphere/compute.rb +++ b/lib/fog/vsphere/compute.rb @@ -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 diff --git a/lib/fog/vsphere/models/compute/server.rb b/lib/fog/vsphere/models/compute/server.rb new file mode 100644 index 000000000..2ed281ee7 --- /dev/null +++ b/lib/fog/vsphere/models/compute/server.rb @@ -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 diff --git a/lib/fog/vsphere/models/compute/servers.rb b/lib/fog/vsphere/models/compute/servers.rb new file mode 100644 index 000000000..55a627243 --- /dev/null +++ b/lib/fog/vsphere/models/compute/servers.rb @@ -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 diff --git a/lib/fog/vsphere/requests/compute/list_virtual_machines.rb b/lib/fog/vsphere/requests/compute/list_virtual_machines.rb new file mode 100644 index 000000000..a8187faf8 --- /dev/null +++ b/lib/fog/vsphere/requests/compute/list_virtual_machines.rb @@ -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 +