Class: VcoWorkflows::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vcoworkflows/config.rb,
lib/vcoworkflows/constants.rb

Overview

Class Config

Constant Summary collapse

DEFAULT_CONFIG_DIR =

Where we nominally expect configuration files to be

File.join((ENV['HOME'] || ENV['HOMEDRIVE']), '.vcoworkflows')
DEFAULT_CONFIG_FILE =

Default configuration file

File.join(DEFAULT_CONFIG_DIR, 'config.json')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file: nil, url: nil, username: nil, password: nil, verify_ssl: true) ⇒ VcoWorkflows::Config

Constructor

Parameters:

  • config_file (String) (defaults to: nil)

    Path to config file to load

  • url (String) (defaults to: nil)

    URL for vCO server

  • username (String) (defaults to: nil)

    Username for vCO server

  • password (String) (defaults to: nil)

    Password for vCO server

  • verify_ssl (Boolean) (defaults to: true)

    Verify SSL Certificates?



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vcoworkflows/config.rb', line 29

def initialize(config_file: nil, url: nil, username: nil, password: nil, verify_ssl: true)
  # If we're given a URL and no config_file, then build the configuration
  # from the values we're given (or can scrape from the environment).
  # Otherwise, load the given config file or the default config file if no
  # config file was given.
  if url && config_file.nil?
    self.url    = url
    @username   = username.nil? ? ENV['VCO_USER'] : username
    @password   = password.nil? ? ENV['VCO_PASSWD'] : password
    @verify_ssl = verify_ssl
  else
    # First, check if an explicit config file was given
    config_file = DEFAULT_CONFIG_FILE if config_file.nil?
    load_config(config_file)
  end

  # Fail if we don't have both a username and a password.
  fail(IOError, ERR[:url_unset]) if @url.nil?
  fail(IOError, ERR[:username_unset]) if @username.nil?
  fail(IOError, ERR[:password_unset]) if @password.nil?
end

Instance Attribute Details

#passwordObject

Password to authenticate to vCenter Orchestrator



15
16
17
# File 'lib/vcoworkflows/config.rb', line 15

def password
  @password
end

#urlObject

URL for the vCenter Orchestrator REST API



9
10
11
# File 'lib/vcoworkflows/config.rb', line 9

def url
  @url
end

#usernameObject

User name to authenticate to vCenter Orchestrator



12
13
14
# File 'lib/vcoworkflows/config.rb', line 12

def username
  @username
end

#verify_sslObject

Whether or not to do SSL/TLS Certificate verification



18
19
20
# File 'lib/vcoworkflows/config.rb', line 18

def verify_ssl
  @verify_ssl
end

Instance Method Details

#load_config(config_file) ⇒ Object

load config file

Parameters:

  • config_file (String)

    Path for the configuration file to load



65
66
67
68
69
70
71
72
# File 'lib/vcoworkflows/config.rb', line 65

def load_config(config_file)
  config_data = JSON.parse(File.read(config_file))
  return if config_data.nil?
  self.url    = config_data['url']
  @username   = config_data['username']
  @password   = config_data['password']
  @verify_ssl = config_data['verify_ssl']
end

#to_jsonObject

Return a JSON document for this object



84
85
86
87
88
89
90
91
# File 'lib/vcoworkflows/config.rb', line 84

def to_json
  config = {}
  config['url'] = @url
  config['username'] = @username
  config['password'] = @password
  config['verify_ssl'] = @verify_ssl
  puts JSON.pretty_generate(config)
end

#to_sString

Return a String representation of this object

Returns:

  • (String)


76
77
78
79
80
81
# File 'lib/vcoworkflows/config.rb', line 76

def to_s
  puts "url:        #{@url}"
  puts "username:   #{@username}"
  puts "password:   #{@password}"
  puts "verify_ssl: #{@verify_ssl}"
end