Class: Cronenberg::Config
- Inherits:
-
Object
- Object
- Cronenberg::Config
- Defined in:
- lib/cronenberg/config.rb
Constant Summary collapse
- REQUIRED_VALUES =
{ names: [:host, :user, :password], env_vars: ['VCENTER_SERVER', 'VCENTER_USER', 'VCENTER_PASSWORD'], }
Instance Attribute Summary collapse
-
#datacenter ⇒ Object
readonly
Returns the value of attribute datacenter.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#insecure ⇒ Object
readonly
Returns the value of attribute insecure.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#ssl ⇒ Object
readonly
Returns the value of attribute ssl.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #default_config_location ⇒ Object
-
#initialize(config_file_location = nil) ⇒ Config
constructor
A new instance of Config.
- #process_config_file(file_path) ⇒ Object
- #process_environment_variables ⇒ Object
Constructor Details
#initialize(config_file_location = nil) ⇒ Config
Returns a new instance of Config.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cronenberg/config.rb', line 16 def initialize(config_file_location=nil) settings = process_environment_variables || process_config_file(config_file_location || default_config_location) if settings.nil? raise 'You must provide credentials in either environment variables or a config file.' else settings = settings.delete_if { |k, v| v.nil? } missing = REQUIRED_VALUES[:names] - settings.keys unless missing.empty? = 'To use this module you must provide the following settings:' missing.each do |var| += " #{var}" end raise end @host = settings[:host] @user = settings[:user] @password = settings[:password] @datacenter = settings[:datacenter_name] @insecure = settings[:insecure].nil? ? false : settings[:insecure] @ssl = settings[:ssl].nil? ? true : settings[:ssl] @port = settings[:port] end end |
Instance Attribute Details
#datacenter ⇒ Object (readonly)
Returns the value of attribute datacenter.
10 11 12 |
# File 'lib/cronenberg/config.rb', line 10 def datacenter @datacenter end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
10 11 12 |
# File 'lib/cronenberg/config.rb', line 10 def host @host end |
#insecure ⇒ Object (readonly)
Returns the value of attribute insecure.
10 11 12 |
# File 'lib/cronenberg/config.rb', line 10 def insecure @insecure end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
10 11 12 |
# File 'lib/cronenberg/config.rb', line 10 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
10 11 12 |
# File 'lib/cronenberg/config.rb', line 10 def port @port end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
10 11 12 |
# File 'lib/cronenberg/config.rb', line 10 def ssl @ssl end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
10 11 12 |
# File 'lib/cronenberg/config.rb', line 10 def user @user end |
Instance Method Details
#default_config_location ⇒ Object
12 13 14 |
# File 'lib/cronenberg/config.rb', line 12 def default_config_location File.join(Dir.home, '.vcenter.conf') end |
#process_config_file(file_path) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/cronenberg/config.rb', line 61 def process_config_file(file_path) file_present = File.file?(file_path) unless file_present nil else begin configuration = YAML.load_file(file_path) rescue Psych::SyntaxError => e raise "Your configuration file at #{file_path} is invalid. The error from the YAML parser is:\n #{e.}" end vsphere_config = configuration['vcenter'] raise "Invalid configuration file, missing vcenter key." unless vsphere_config.keys.first == 'vcenter' required = REQUIRED_VALUES[:names].map { |var| var.to_s } missing = required - vsphere_config.keys if missing.size < required.size { host: vsphere_config['host'], user: vsphere_config['user'], password: vsphere_config['password'], datacenter_name: vsphere_config['datacenter'], insecure: vsphere_config['insecure'], port: vsphere_config['port'], ssl: vsphere_config['ssl'], } else nil end end end |
#process_environment_variables ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cronenberg/config.rb', line 41 def process_environment_variables required = REQUIRED_VALUES[:env_vars] missing = required - ENV.keys if missing.size < required.size { host: ENV['VCENTER_SERVER'], user: ENV['VCENTER_USER'], password: ENV['VCENTER_PASSWORD'], datacenter_name: ENV['VCENTER_DATACENTER'], insecure: ENV['VCENTER_INSECURE'], port: ENV['VCENTER_PORT'], ssl: ENV['VCENTER_SSL'], } else nil end end |