Skip to content

Instantly share code, notes, and snippets.

@jeffmccune
Created September 8, 2011 20:44
Show Gist options
  • Save jeffmccune/1204635 to your computer and use it in GitHub Desktop.
Save jeffmccune/1204635 to your computer and use it in GitHub Desktop.
rbvmomi responses versus Excon responses
# What I'm doing to convert #
diff --git a/lib/fog/vsphere/requests/compute/find_template_by_instance_uuid.rb b/lib/fog/vsphere/requests/compute/find_template_by_instance_uuid.rb
index 3a1776b..21c289a 100644
--- a/lib/fog/vsphere/requests/compute/find_template_by_instance_uuid.rb
+++ b/lib/fog/vsphere/requests/compute/find_template_by_instance_uuid.rb
@@ -1,27 +1,35 @@
module Fog
module Compute
class Vsphere
- class Real
+
+ module Shared
def find_template_by_instance_uuid(instance_uuid)
- vm = list_virtual_machines.detect(lambda { raise Fog::Vsphere::Errors::NotFound }) do |vm|
- vm.config.instanceUuid == instance_uuid
- end
- unless vm.config.template
- raise Fog::Vsphere::Errors::NotFound, "VM with Instance UUID #{instance_uuid} is not a template."
+ response = Excon::Response.new
+ response.status = 200
+
+ all_vms = list_virtual_machines.body['virtual_machines']
+
+ notfound = lambda { response.status = 404; nil }
+
+ template = all_vms.detect(notfound) do |vm|
+ vm[:instance_uuid] == instance_uuid
end
- vm
+
+ response.body = { 'template' => template }
+ response
end
end
- class Mock
-
- def find_template_by_instance_uuid(instance_uuid)
- Fog::Mock.not_implmented
- end
+ class Real
+ include Shared
+ end
+ class Mock
+ include Shared
end
+
end
end
end

Excon (Seems easier to store and mock )

>> c = Fog::Compute[:vsphere]; c.vsphere_rev
"4.0"
>> c.list_virtual_machines
#<Excon::Response:0x10243d9f8
 @body=
  {"virtual_machines"=>
    [{:connection_state=>"connected",
      :tools_version=>"guestToolsCurrent",
      :instance_uuid=>"50323f93-6835-1178-8b8f-9e2109890e1a",
      :mac_addresses=>{"Network adapter 1"=>"00:50:56:b2:00:a1"},
      :operatingsystem=>nil,
      :hypervisor=>"gunab.puppetlabs.lan",
      :mo_ref=>"vm-698",
      :hostname=>nil,
      :ipaddress=>nil,
      :uuid=>"42322347-d791-cd34-80b9-e25fe28ad37c",
      :is_a_template=>true,
      :name=>"centos56gm",
      :power_state=>"poweredOff",
      :id=>"50323f93-6835-1178-8b8f-9e2109890e1a",
      :tools_state=>"toolsNotRunning"},
     {:connection_state=>"connected",
      :tools_version=>"guestToolsCurrent",
      :instance_uuid=>"5257dee8-050c-cbcd-ae25-db0e582ab530",
      :mac_addresses=>{"Network adapter 1"=>"00:0c:29:3a:00:4d"},
      :operatingsystem=>nil,
      :hypervisor=>"gunab.puppetlabs.lan",
      :mo_ref=>"vm-640",
      :hostname=>nil,
      :ipaddress=>nil,
      :uuid=>"564ddcbe-853a-d29a-b329-a0a3693a004d",
      :is_a_template=>true,
      :name=>"centos56gm2",
      :power_state=>"poweredOff",
      :id=>"5257dee8-050c-cbcd-ae25-db0e582ab530",
      :tools_state=>"toolsNotRunning"},
     {:connection_state=>"connected",
      :tools_version=>"guestToolsCurrent",
      :instance_uuid=>"5032739c-c871-c0d2-034f-9700a0b5383e",
      :mac_addresses=>{"Network adapter 1"=>"00:50:56:b2:00:96"},
      :operatingsystem=>"Red Hat Enterprise Linux 6 (64-bit)",
      :hypervisor=>"gunab.puppetlabs.lan",
      :mo_ref=>"vm-669",
      :hostname=>"compliance.puppetlabs.vm",
      :ipaddress=>"192.168.100.184",
      :uuid=>"564d3f91-3452-a509-a678-1246f7897979",
      :is_a_template=>false,
      :name=>"dashboard_gm",
      :power_state=>"poweredOn",
      :id=>"5032739c-c871-c0d2-034f-9700a0b5383e",
      :tools_state=>"toolsOk"},
     {:connection_state=>"connected",
      :tools_version=>"guestToolsCurrent",
      :instance_uuid=>"5032c8a5-9c5e-ba7a-3804-832a03e16381",
      :mac_addresses=>{"Network adapter 1"=>"00:50:56:b2:00:af"},
      :operatingsystem=>"CentOS 4/5 (32-bit)",
      :hypervisor=>"gunab.puppetlabs.lan",
      :mo_ref=>"vm-715",
      :hostname=>"centos56gm.localdomain",
      :ipaddress=>"192.168.100.187",
      :uuid=>"42329da7-e8ab-29ec-1892-d6a4a964912a",
      :is_a_template=>false,
      :name=>"jefftest",
      :power_state=>"poweredOn",
      :id=>"5032c8a5-9c5e-ba7a-3804-832a03e16381",
      :tools_state=>"toolsOk"}]},
 @headers={},
 @status=200>

rbvmomi objects

>> c.list_virtual_machines.to_yaml
--- 
- !ruby/object:RbVmomi::VIM::VirtualMachine 
  connection: &id002 !ruby/object:RbVmomi::VIM 
    cookie: vmware_soap_session="D87D49E2-2785-46E5-BA81-719C4F4C0AE1"; Path=/;
    debug: 
    http: !ruby/object:Net::HTTP 
      address: vc01.puppetlabs.lan
      close_on_empty_response: false
      curr_http_version: "1.1"
      debug_output: 
      newimpl: true
      open_timeout: 5
      port: 443
      read_timeout: 1000000
      seems_1_0_server: false
      socket: !ruby/object:Net::BufferedIO 
        debug_output: 
        io: !ruby/object:OpenSSL::SSL::SSLSocket 
          callback_state: 
          context: &id001 !ruby/object:OpenSSL::SSL::SSLContext 
            ca_file: 
            ca_path: 
            cert: 
            cert_store: 
            client_ca: 
            client_cert_cb: 
            extra_chain_cert: 
            key: 
            options: 
            session_get_cb: 
            session_id_context: 
            session_new_cb: 
            session_remove_cb: 
            timeout: 
            tmp_dh_callback: 
            verify_callback: 
            verify_depth: 
            verify_mode: 0
          eof: false
          io: !ruby/object:TCPSocket {}

          rbuffer: ""
          sync: true
          sync_close: true
          wbuffer: ""
        rbuf: ""
        read_timeout: 1000000
        written_bytes: 
      ssl_context: *id001
      started: true
      use_ssl: true
    lock: !ruby/object:Mutex {}

    ns: urn:vim25
    opts: 
      :path: /sdk
      :rev: "4.0"
      :insecure: true
      :ns: urn:vim25
      :host: vc01.puppetlabs.lan
      :ssl: true
      :port: 443
    rev: "4.1"
    serviceContent: !ruby/object:RbVmomi::VIM::ServiceContent 
      props: 
        :viewManager: !ruby/object:RbVmomi::VIM::ViewManager 
          connection: *id002
          ref: ViewManager
          soap: *id002
        :customFieldsManager: !ruby/object:RbVmomi::VIM::CustomFieldsManager 
          connection: *id002
          ref: CustomFieldsManager
          soap: *id002
        :ipPoolManager: !ruby/object:RbVmomi::VIM::IpPoolManager 
          connection: *id002
          ref: IpPoolManager
          soap: *id002
        :rootFolder: !ruby/object:RbVmomi::VIM::Folder 
          connection: *id002
          ref: group-d1
          soap: *id002
        :about: !ruby/object:RbVmomi::VIM::AboutInfo 
          props: 
            :productLineId: vpx
            :build: "345043"
            :apiVersion: "4.1"
            :licenseProductName: VMware VirtualCenter Server
            :fullName: VMware vCenter Server 4.1.0 build-345043
            :instanceUuid: EAAC5152-90C3-43B8-8EDC-CB0A0059CFA9
            :licenseProductVersion: "4.0"
            :localeVersion: INTL
            :vendor: VMware, Inc.
            :apiType: VirtualCenter
            :name: VMware vCenter Server
            :localeBuild: "000"
            :dynamicProperty: []

            :version: 4.1.0
            :osType: win32-x86
        :alarmManager: !ruby/object:RbVmomi::VIM::AlarmManager 
          connection: *id002
          ref: AlarmManager
          soap: *id002
        :setting: !ruby/object:RbVmomi::VIM::OptionManager 
          connection: *id002
          ref: VpxSettings
          soap: *id002
        :searchIndex: !ruby/object:RbVmomi::VIM::SearchIndex 
          connection: *id002
          ref: SearchIndex
          soap: *id002
        :dvSwitchManager: !ruby/object:RbVmomi::VIM::DistributedVirtualSwitchManager 
          connection: *id002
          ref: DVSManager
          soap: *id002
        :propertyCollector: !ruby/object:RbVmomi::VIM::PropertyCollector 
          connection: *id002
          ref: propertyCollector
          soap: *id002
        :eventManager: !ruby/object:RbVmomi::VIM::EventManager 
          connection: *id002
          ref: EventManager
          soap: *id002
        :snmpSystem: !ruby/object:RbVmomi::VIM::HostSnmpSystem 
          connection: *id002
          ref: SnmpSystem
          soap: *id002
        :userDirectory: !ruby/object:RbVmomi::VIM::UserDirectory 
          connection: *id002
          ref: UserDirectory
          soap: *id002
        :diagnosticManager: !ruby/object:RbVmomi::VIM::DiagnosticManager 
          connection: *id002
          ref: DiagMgr
          soap: *id002
        :hostProfileManager: !ruby/object:RbVmomi::VIM::HostProfileManager 
          connection: *id002
          ref: HostProfileManager
          soap: *id002
        :taskManager: !ruby/object:RbVmomi::VIM::TaskManager 
          connection: *id002
          ref: TaskManager
          soap: *id002
        :vmProvisioningChecker: !ruby/object:RbVmomi::VIM::VirtualMachineProvisioningChecker 
          connection: *id002
          ref: ProvChecker
          soap: *id002
        :authorizationManager: !ruby/object:RbVmomi::VIM::AuthorizationManager 
          connection: *id002
          ref: AuthorizationManager
          soap: *id002
        :licenseManager: !ruby/object:RbVmomi::VIM::LicenseManager 
          connection: *id002
          ref: LicenseManager
          soap: *id002
        :clusterProfileManager: !ruby/object:RbVmomi::VIM::ClusterProfileManager 
          connection: *id002
          ref: ClusterProfileManager
          soap: *id002
        :extensionManager: !ruby/object:RbVmomi::VIM::ExtensionManager 
          connection: *id002
          ref: ExtensionManager
          soap: *id002
        :vmCompatibilityChecker: !ruby/object:RbVmomi::VIM::VirtualMachineCompatibilityChecker 
          connection: *id002
          ref: CompatChecker
          soap: *id002
        :perfManager: !ruby/object:RbVmomi::VIM::PerformanceManager 
          connection: *id002
          ref: PerfMgr
          soap: *id002
        :fileManager: !ruby/object:RbVmomi::VIM::FileManager 
          connection: *id002
          ref: FileManager
          soap: *id002
        :complianceManager: !ruby/object:RbVmomi::VIM::ProfileComplianceManager 
          connection: *id002
          ref: MoComplianceManager
          soap: *id002
        :dynamicProperty: []

        :sessionManager: !ruby/object:RbVmomi::VIM::SessionManager 
          connection: *id002
          ref: SessionManager
          soap: *id002
        :customizationSpecManager: !ruby/object:RbVmomi::VIM::CustomizationSpecManager 
          connection: *id002
          ref: CustomizationSpecManager
          soap: *id002
        :ovfManager: !ruby/object:RbVmomi::VIM::OvfManager 
          connection: *id002
          ref: OvfManager
          soap: *id002
        :scheduledTaskManager: !ruby/object:RbVmomi::VIM::ScheduledTaskManager 
          connection: *id002
          ref: ScheduledTaskManager
          soap: *id002
        :virtualDiskManager: !ruby/object:RbVmomi::VIM::VirtualDiskManager 
          connection: *id002
          ref: VirtualDiskManager
          soap: *id002
        :localizationManager: !ruby/object:RbVmomi::VIM::LocalizationManager 
          connection: *id002
          ref: LocalizationManager
          soap: *id002
  ref: vm-698
  soap: *id002
- !ruby/object:RbVmomi::VIM::VirtualMachine 
  connection: *id002
  ref: vm-640
  soap: *id002
- !ruby/object:RbVmomi::VIM::VirtualMachine 
  connection: *id002
  ref: vm-669
  soap: *id002
- !ruby/object:RbVmomi::VIM::VirtualMachine 
  connection: *id002
  ref: vm-715
  soap: *id002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment