Class: VcoWorkflows::VcoSession

Inherits:
Object
  • Object
show all
Defined in:
lib/vcoworkflows/vcosession.rb

Overview

VcoSession is a simple wrapper for RestClient::Resource, and supports GET and POST operations against the vCO API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, uri: nil, user: nil, password: nil, verify_ssl: true) ⇒ VcoSession

Initialize the session

When specifying a config, do not provide other parameters. Likewise, if providing uri, user, and password, a config object is not necessary.

Parameters:

  • config (VcoWorkflows::Config) (defaults to: nil)

    Configuration object for the connection

  • uri (String) (defaults to: nil)

    URI for the vCenter Orchestrator API endpoint

  • user (String) (defaults to: nil)

    User name for vCO

  • password (String) (defaults to: nil)

    Password for vCO

  • verify_ssl (Boolean) (defaults to: true)

    Whether or not to verify SSL certificates



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vcoworkflows/vcosession.rb', line 25

def initialize(config: nil, uri: nil, user: nil, password: nil, verify_ssl: true)
  # If a configuration object was provided, use it.
  # If we got a URL and no config, build a new config with the URL and any
  # other options that passed in.
  # Otherwise, load the default config file if possible...
  if config
    config = config
  elsif uri && config.nil?
    config = VcoWorkflows::Config.new(url:        uri,
                                      username:   user,
                                      password:   password,
                                      verify_ssl: verify_ssl)
  elsif uri.nil? && config.nil?
    config = VcoWorkflows::Config.new
  end

  RestClient.proxy = ENV['http_proxy'] # Set a proxy if present
  @rest_resource = RestClient::Resource.new(config.url,
                                            user:       config.username,
                                            password:   config.password,
                                            verify_ssl: config.verify_ssl)
end

Instance Attribute Details

#rest_resourceObject (readonly)

Accessor for rest-client object, primarily for testing purposes



11
12
13
# File 'lib/vcoworkflows/vcosession.rb', line 11

def rest_resource
  @rest_resource
end

Instance Method Details

#get(endpoint, headers = {}) ⇒ String

Perform a REST GET operation against the specified endpoint

Parameters:

  • endpoint (String)

    REST endpoint to use

  • headers (Hash) (defaults to: {})

    Optional headers to use in request

Returns:

  • (String)

    JSON response body



54
55
56
57
# File 'lib/vcoworkflows/vcosession.rb', line 54

def get(endpoint, headers = {})
  headers = { accept: :json }.merge(headers)
  @rest_resource[endpoint].get headers
end

#post(endpoint, body, headers = {}) ⇒ String

Perform a REST POST operation against the specified endpoint with the given data body

Parameters:

  • endpoint (String)

    REST endpoint to use

  • body (String)

    JSON data body to post

  • headers (Hash) (defaults to: {})

    Optional headers to use in request

Returns:

  • (String)

    JSON response body



66
67
68
69
# File 'lib/vcoworkflows/vcosession.rb', line 66

def post(endpoint, body, headers = {})
  headers = { accept: :json, content_type: :json }.merge(headers)
  @rest_resource[endpoint].post body, headers
end