Class: Cronenberg::Config

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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?
			message = 'To use this module you must provide the following settings:'
			missing.each do |var|
				message += " #{var}"
			end
			raise message
		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

#datacenterObject (readonly)

Returns the value of attribute datacenter.



10
11
12
# File 'lib/cronenberg/config.rb', line 10

def datacenter
  @datacenter
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/cronenberg/config.rb', line 10

def host
  @host
end

#insecureObject (readonly)

Returns the value of attribute insecure.



10
11
12
# File 'lib/cronenberg/config.rb', line 10

def insecure
  @insecure
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/cronenberg/config.rb', line 10

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/cronenberg/config.rb', line 10

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



10
11
12
# File 'lib/cronenberg/config.rb', line 10

def ssl
  @ssl
end

#userObject (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_locationObject



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.message}"
		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_variablesObject



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