Skip to content

Commit

Permalink
Normalize OpenStack CPI options
Browse files Browse the repository at this point in the history
Remove keys with nil values

Signed-off-by: Karl Isenberg <[email protected]>
  • Loading branch information
mariash committed Oct 3, 2014
1 parent 2696cdb commit 905949f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
40 changes: 36 additions & 4 deletions bosh_openstack_cpi/lib/cloud/openstack/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Bosh::OpenStackCloud
class Cloud < Bosh::Cloud
include Helpers

OPTION_KEYS = ['openstack', 'registry', 'agent']

BOSH_APP_DIR = '/var/vcap/bosh'
FIRST_DEVICE_NAME_LETTER = 'b'

Expand All @@ -24,7 +26,7 @@ class Cloud < Bosh::Cloud
# @option options [Hash] agent agent options
# @option options [Hash] registry agent options
def initialize(options)
@options = options.dup
@options = normalize_options(options)

validate_options
initialize_registry
Expand Down Expand Up @@ -907,7 +909,7 @@ def validate_options
'username' => String,
'api_key' => String,
'tenant' => String,
optional('region') => enum(String, nil),
optional('region') => String,
optional('endpoint_type') => String,
optional('state_timeout') => Numeric,
optional('stemcell_public_visibility') => enum(String, bool),
Expand All @@ -916,13 +918,14 @@ def validate_options
optional('default_key_name') => String,
optional('default_security_groups') => [String],
optional('wait_resource_poll_interval') => Integer,
optional('config_drive') => enum('disk', 'cdrom', nil),
optional('config_drive') => enum('disk', 'cdrom'),
},
'registry' => {
'endpoint' => String,
'user' => String,
'password' => String,
}
},
optional('agent') => Hash,
}
end
schema.validate(@options)
Expand All @@ -941,5 +944,34 @@ def initialize_registry
registry_password)
end

def normalize_options(options)
unless options.kind_of?(Hash)
raise ArgumentError, "Invalid OpenStack cloud properties: Hash expected, received #{options}"
end
# we only care about two top-level fields
options = hash_filter(options.dup) { |key| OPTION_KEYS.include?(key) }
# nil values should be treated the same as missing keys (makes validating optional fields easier)
delete_entries_with_nil_keys(options)
end

def hash_filter(hash)
copy = {}
hash.each do |key, value|
copy[key] = value if yield(key)
end
copy
end

def delete_entries_with_nil_keys(options)
options.each do |key, value|
if value == nil
options.delete(key)
elsif value.kind_of?(Hash)
options[key] = delete_entries_with_nil_keys(value.dup)
end
end
options
end

end
end
20 changes: 16 additions & 4 deletions bosh_openstack_cpi/spec/unit/cloud_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,30 @@
end
end

context 'connection_options' do
before { options['openstack']['connection_options'] = 'connection_options' }
context 'when connection_options are specified' do
it 'expects connection_options to be a hash' do
options['openstack']['connection_options'] = { 'any-key' => 'any-value' }

expect { subject }.to_not raise_error
end

it 'raises an error if connection_options is not a Hash' do
options['openstack']['connection_options'] = 'connection_options'

expect { subject }.to raise_error(ArgumentError, /Invalid OpenStack cloud properties/)
end
end

context 'boot_from_volume' do
before { options['openstack']['boot_from_volume'] = 'boot_from_volume' }
context 'when boot_from_volume is specified' do
it 'expects boot_from_volume to be a boolean' do
options['openstack']['boot_from_volume'] = true

expect { subject }.to_not raise_error
end

it 'raises an error if boot_from_volume is not a boolean' do
options['openstack']['boot_from_volume'] = 'boot_from_volume'

expect { subject }.to raise_error(ArgumentError, /Invalid OpenStack cloud properties/)
end
end
Expand Down

0 comments on commit 905949f

Please sign in to comment.