class TrainPlugins::Vsphere::Connection

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/train-vsphere/connection.rb, line 21
def initialize(options)

  options = validate_options(options)
  super(options)

  #force enable caching
  enable_cache :api_call

end

Public Instance Methods

api_client(klass) click to toggle source
# File lib/train-vsphere/connection.rb, line 43
def api_client(klass)

  configuration = VSphereAutomation::Configuration.new.tap do |c|
    c.host = options[:host]
    c.username = options[:user]
    c.password = options[:password]
    c.scheme = 'https'
    c.verify_ssl = !options[:insecure]
    c.verify_ssl_host = !options[:insecure]
  end
  begin
    auth_token = VSphereAutomation::ApiClient.new(configuration)
    auth_token.default_headers['Authorization'] = configuration.basic_auth_token
    session_api = VSphereAutomation::CIS::SessionApi.new(auth_token)
    session_id = session_api.create('').value
    auth_token.default_headers['vmware-api-session-id'] = session_id  
  
  return klass.new(auth_token) unless cache_enabled?(:api_call)
  @cache[:api_call][klass.to_s.to_sym] ||= klass.new(auth_token)

  rescue VSphereAutomation::ApiError => e
    fail Train::ClientError

  #puts "Exception when calling AccessConsolecliApi->get: #{e}"
  end
end
local?() click to toggle source
# File lib/train-vsphere/connection.rb, line 137
def local?
  false
end
uri() click to toggle source

def auth_rbvmomi

#Authenticates to the old API using rbvmomi
return rest_client unless cache_enabled(:api_call)
@cache[:api_call][api_client.to_s.to_sym] ||= rest_client
# File lib/train-vsphere/connection.rb, line 131
def uri
  #Report vsphere URI
  "vsphere://#{options[:host]}"
end
vsphere_client(function) click to toggle source
# File lib/train-vsphere/connection.rb, line 31
def vsphere_client(function)
  begin
    if !defined? @vim 
      @vim = RbVmomi::VIM.connect(host: options[:host], user: options[:user], password: options[:password], insecure: options[:insecure])
    end
    content = @vim.serviceInstance.content
    @cache[:api_call][function.to_s.to_sym] ||= content.public_send(function) if content.respond_to? function
  rescue
    fail Train::ClientError
  end
end

Private Instance Methods

validate_options(options) click to toggle source
# File lib/train-vsphere/connection.rb, line 144
def validate_options(options)
  if options[:user].nil?
    fail Train::ClientError,
        'A user needs to be set'
  end
  if options[:password].nil?
    fail Train::ClientError,
        'A password needs to be set'
  end
  if options[:host].nil?
    fail Train::ClientError,
        'A host needs to be set'
  end


  return options
end