Class: RbVmomi::VIM

Inherits:
Connection
  • Object
show all
Defined in:
lib/rbvmomi/vim.rb

Overview

A connection to one vSphere SDK endpoint.

See Also:

Defined Under Namespace

Classes: ComputeResource, Datacenter, Datastore, DynamicTypeMgrAllTypeInfo, DynamicTypeMgrDataTypeInfo, DynamicTypeMgrManagedTypeInfo, EsxcliCommand, EsxcliNamespace, Folder, HostSystem, ManagedEntity, ManagedObject, ObjectContent, ObjectUpdate, OvfManager, PerfCounterInfo, PerformanceManager, PropertyCollector, ReflectManagedMethodExecuter, ResourcePool, ServiceInstance, Task, VirtualMachine

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connect(opts) ⇒ Object

Connect to a vSphere SDK endpoint

Parameters:

  • opts (Hash)

    The options hash.

Options Hash (opts):

  • :host (String)

    Host to connect to.

  • :port (Numeric) — default: 443

    Port to connect to.

  • :ssl (Boolean) — default: true

    Whether to use SSL.

  • :insecure (Boolean) — default: false

    If true, ignore SSL certificate errors.

  • :cookie (String)

    If set, use cookie to connect instead of user/password

  • :user (String) — default: root

    Username.

  • :password (String)

    Password.

  • :path (String) — default: /sdk

    SDK endpoint path.

  • :debug (Boolean) — default: false

    If true, print SOAP traffic to stderr.

  • :operation_id (String)

    If set, use for operationID

  • :close_on_exit (Boolean) — default: true

    If true, will close connection with at_exit

  • :sso (RbVmomi::SSO) — default: nil

    Use SSO token to login if set


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rbvmomi/vim.rb', line 25

def self.connect opts
  raise unless opts.is_a? Hash
  raise 'host option required' unless opts[:host]

  opts[:cookie] ||= nil
  opts[:user] ||= 'root'
  opts[:password] ||= ''
  opts[:ssl] = true unless opts.member? :ssl or opts[:"no-ssl"]
  opts[:insecure] ||= false
  opts[:port] ||= (opts[:ssl] ? 443 : 80)
  opts[:path] ||= '/sdk'
  opts[:ns] ||= 'urn:vim25'
  opts[:rev] = '6.7' if opts[:rev].nil?
  opts[:debug] = (!ENV['RBVMOMI_DEBUG'].empty? rescue false) unless opts.member? :debug

  conn = new(opts).tap do |vim|
    unless opts[:cookie]
      if opts[:sso]
        vim.serviceContent.sessionManager.LoginByToken
      else
        vim.serviceContent.sessionManager.Login userName: opts[:user], password: opts[:password]
      end
    end
    rev = vim.serviceContent.about.apiVersion
    vim.rev = [rev, opts[:rev]].min { |a, b| Gem::Version.new(a) <=> Gem::Version.new(b) }
  end

  at_exit { conn.close } if opts.fetch(:close_on_exit, true)
  conn
end

Instance Method Details

#closeObject


56
57
58
59
60
61
62
63
# File 'lib/rbvmomi/vim.rb', line 56

def close
  serviceContent.sessionManager.Logout
rescue RbVmomi::Fault => e
  $stderr.puts(e.message) if debug
ensure
  self.cookie = nil
  super
end

#get_log_keys(host = nil) ⇒ Object


125
126
127
128
129
130
131
132
# File 'lib/rbvmomi/vim.rb', line 125

def get_log_keys host=nil
  diagMgr = self.serviceContent.diagnosticManager
  keys = []
  diagMgr.QueryDescriptions(host: host).each do |desc|
    keys << "#{desc.key}"
  end
  keys
end

#get_log_lines(logKey, lines = 5, start = nil, host = nil) ⇒ Object


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rbvmomi/vim.rb', line 109

def get_log_lines logKey, lines=5, start=nil, host=nil
  diagMgr = self.serviceContent.diagnosticManager
  if !start
    log = diagMgr.BrowseDiagnosticLog(host: host, key: logKey, start: 999999999)
    lineEnd = log.lineEnd
    start = lineEnd - lines
  end
  start = start < 0 ? 0 : start
  log = diagMgr.BrowseDiagnosticLog(host: host, key: logKey, start: start)
  if log.lineText.size > 0
    [log.lineText.slice(-lines, log.lineText.size), log.lineEnd]
  else
    [log.lineText, log.lineEnd]
  end
end

#instanceUuidObject


105
106
107
# File 'lib/rbvmomi/vim.rb', line 105

def instanceUuid
  serviceContent.about.instanceUuid
end

#propertyCollectorObject

Alias to serviceContent.propertyCollector


91
92
93
# File 'lib/rbvmomi/vim.rb', line 91

def propertyCollector
  serviceContent.propertyCollector
end

#rev=(x) ⇒ Object


65
66
67
68
# File 'lib/rbvmomi/vim.rb', line 65

def rev= x
  super
  @serviceContent = nil
end

#rootFolderObject Also known as: root

Alias to serviceContent.rootFolder


84
85
86
# File 'lib/rbvmomi/vim.rb', line 84

def rootFolder
  serviceContent.rootFolder
end

#searchIndexObject

Alias to serviceContent.searchIndex


96
97
98
# File 'lib/rbvmomi/vim.rb', line 96

def searchIndex
  serviceContent.searchIndex
end

#serviceContentObject

Alias to serviceInstance.RetrieveServiceContent


79
80
81
# File 'lib/rbvmomi/vim.rb', line 79

def serviceContent
  @serviceContent ||= serviceInstance.RetrieveServiceContent
end

#serviceInstanceObject

Return the ServiceInstance

The ServiceInstance is the root of the vSphere inventory.


74
75
76
# File 'lib/rbvmomi/vim.rb', line 74

def serviceInstance
  VIM::ServiceInstance self, 'ServiceInstance'
end