class RbVmomi::VIM

A connection to one vSphere SDK endpoint. @see serviceInstance

Public Class Methods

connect(opts) click to toggle source

Connect to a vSphere SDK endpoint

@param [Hash] opts The options hash. @option opts [String] :host Host to connect to. @option opts [Numeric] :port (443) Port to connect to. @option opts [Boolean] :ssl (true) Whether to use SSL. @option opts [Boolean] :insecure (false) If true, ignore SSL certificate errors. @option opts [String] :cookie If set, use cookie to connect instead of user/password @option opts [String] :user (root) Username. @option opts [String] :password Password. @option opts [String] :path (/sdk) SDK endpoint path. @option opts [Boolean] :debug (false) If true, print SOAP traffic to RbVmomi.logger.debug. @option opts [String] :operation_id If set, use for operationID @option opts [Boolean] :close_on_exit (true) If true, will close connection with at_exit @option opts [RbVmomi::SSO] :sso (nil) Use SSO token to login if set

# 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] = '8.0' 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

Public Instance Methods

close() click to toggle source
Calls superclass method
# File lib/rbvmomi/vim.rb, line 56
def close
  serviceContent.sessionManager.Logout
rescue RbVmomi::Fault => e
  RbVmomi.logger.error(e.message) if debug
ensure
  self.cookie = nil
  super
end
get_log_keys(host=nil) click to toggle source
# 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) click to toggle source
# 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
instanceUuid() click to toggle source
# File lib/rbvmomi/vim.rb, line 105
def instanceUuid
  serviceContent.about.instanceUuid
end
pretty_print(pp) click to toggle source

@private

# File lib/rbvmomi/vim.rb, line 101
def pretty_print pp
  pp.text "VIM(#{@opts[:host]})"
end
propertyCollector() click to toggle source

Alias to serviceContent.propertyCollector

# File lib/rbvmomi/vim.rb, line 91
def propertyCollector
  serviceContent.propertyCollector
end
rev=(x) click to toggle source
Calls superclass method
# File lib/rbvmomi/vim.rb, line 65
def rev= x
  super
  @serviceContent = nil
end
root()
Alias for: rootFolder
rootFolder() click to toggle source

Alias to serviceContent.rootFolder

# File lib/rbvmomi/vim.rb, line 84
def rootFolder
  serviceContent.rootFolder
end
Also aliased as: root
searchIndex() click to toggle source

Alias to serviceContent.searchIndex

# File lib/rbvmomi/vim.rb, line 96
def searchIndex
  serviceContent.searchIndex
end
serviceContent() click to toggle source

Alias to serviceInstance.RetrieveServiceContent

# File lib/rbvmomi/vim.rb, line 79
def serviceContent
  @serviceContent ||= serviceInstance.RetrieveServiceContent
end
serviceInstance() click to toggle source

Return the ServiceInstance

The ServiceInstance is the root of the vSphere inventory. @see www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.ServiceInstance.html

# File lib/rbvmomi/vim.rb, line 74
def serviceInstance
  VIM::ServiceInstance self, 'ServiceInstance'
end