From d1235d762b96e36b67180ee06929e36c81efd617 Mon Sep 17 00:00:00 2001 From: Clayton O'Neill Date: Thu, 12 Jun 2014 19:49:34 +0000 Subject: [PATCH] Add multi-region support If only one region is available then the Neutron CLI will just pick the only one available. However, if more than one is available, then it will error out about ambiguous region. This patch extends the base provider code to parse out the nova_region_name when it's parsing out the keystone credentials and define the OS_REGION_NAME environment variable when calling neutron, if the nova_region_name variable exists in the config file. Closes-Bug: #1329552 Change-Id: Ic0d59dcb02a11e93e8fad1e0e2ea830fb0dcccc7 --- lib/puppet/provider/neutron.rb | 9 ++++++++- spec/unit/provider/neutron_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/puppet/provider/neutron.rb b/lib/puppet/provider/neutron.rb index f683d2877..c640f3cc8 100644 --- a/lib/puppet/provider/neutron.rb +++ b/lib/puppet/provider/neutron.rb @@ -31,8 +31,12 @@ def self.get_neutron_credentials conf = neutron_conf if conf and conf['keystone_authtoken'] and auth_keys.all?{|k| !conf['keystone_authtoken'][k].nil?} - return Hash[ auth_keys.map \ + creds = Hash[ auth_keys.map \ { |k| [k, conf['keystone_authtoken'][k].strip] } ] + if conf['DEFAULT'] and !conf['DEFAULT']['nova_region_name'].nil? + creds['nova_region_name'] = conf['DEFAULT']['nova_region_name'] + end + return creds else raise(Puppet::Error, "File: #{conf_filename} does not contain all \ required sections. Neutron types will not work if neutron is not \ @@ -68,6 +72,9 @@ def self.auth_neutron(*args) :OS_TENANT_NAME => q['admin_tenant_name'], :OS_PASSWORD => q['admin_password'] } + if q.key?('nova_region_name') + authenv[:OS_REGION_NAME] = q['nova_region_name'] + end begin withenv authenv do neutron(args) diff --git a/spec/unit/provider/neutron_spec.rb b/spec/unit/provider/neutron_spec.rb index e522f10fa..35a3ff381 100644 --- a/spec/unit/provider/neutron_spec.rb +++ b/spec/unit/provider/neutron_spec.rb @@ -64,6 +64,15 @@ def klass klass.get_auth_endpoint.should == auth_endpoint end + it 'should find region_name if specified' do + conf = { + 'keystone_authtoken' => credential_hash, + 'DEFAULT' => { 'nova_region_name' => 'REGION_NAME' } + } + klass.expects(:neutron_conf).returns(conf) + klass.neutron_credentials['nova_region_name'] == 'REGION_NAME' + end + end describe 'when invoking the neutron cli' do @@ -80,6 +89,21 @@ def klass klass.auth_neutron('test_retries') end + it 'should set region in the environment if needed' do + authenv = { + :OS_AUTH_URL => auth_endpoint, + :OS_USERNAME => credential_hash['admin_user'], + :OS_TENANT_NAME => credential_hash['admin_tenant_name'], + :OS_PASSWORD => credential_hash['admin_password'], + :OS_REGION_NAME => 'REGION_NAME', + } + + cred_hash = credential_hash.merge({'nova_region_name' => 'REGION_NAME'}) + klass.expects(:get_neutron_credentials).with().returns(cred_hash) + klass.expects(:withenv).with(authenv) + klass.auth_neutron('test_retries') + end + ['[Errno 111] Connection refused', '(HTTP 400)'].reverse.each do |valid_message| it "should retry when neutron cli returns with error #{valid_message}" do