Pass region_name to SwiftAPI

Currently when SwiftAPI(common/swift.py) is initialized, region_name
is not specified; with multi-region setup, ironic may pick wrong swift
endpoint.

This fix loads region_name from keystone_authtoken/region_name, and
pass the value to SwiftAPI

Closes-Bug: #1554933

Change-Id: I7295fc96c3b9ed50214576e80723d1d9db1a7e38
This commit is contained in:
Xiaobin Qu 2016-03-10 01:05:11 -08:00
parent ec76063870
commit 11db34a48a
3 changed files with 34 additions and 9 deletions

View File

@ -50,6 +50,8 @@ CONF.import_opt('insecure', 'keystonemiddleware.auth_token',
group='keystone_authtoken')
CONF.import_opt('cafile', 'keystonemiddleware.auth_token',
group='keystone_authtoken')
CONF.import_opt('region_name', 'keystonemiddleware.auth_token',
group='keystone_authtoken')
class SwiftAPI(object):
@ -60,7 +62,8 @@ class SwiftAPI(object):
tenant_name=None,
key=None,
auth_url=None,
auth_version=None):
auth_version=None,
region_name=None):
"""Constructor for creating a SwiftAPI object.
:param user: the name of the user for Swift account
@ -68,6 +71,7 @@ class SwiftAPI(object):
:param key: the 'password' or key to authenticate with
:param auth_url: the url for authentication
:param auth_version: the version of api to use for authentication
:param region_name: the region used for getting endpoints of swift
"""
user = user or CONF.keystone_authtoken.admin_user
tenant_name = tenant_name or CONF.keystone_authtoken.admin_tenant_name
@ -83,6 +87,9 @@ class SwiftAPI(object):
'key': key,
'authurl': auth_url,
'auth_version': auth_version}
region_name = region_name or CONF.keystone_authtoken.region_name
if region_name:
params['os_options'] = {'region_name': region_name}
self.connection = swift_client.Connection(**params)

View File

@ -47,17 +47,30 @@ class SwiftTestCase(base.TestCase):
self.config(swift_max_retries=2, group='swift')
self.config(insecure=0, group='keystone_authtoken')
self.config(cafile='/path/to/ca/file', group='keystone_authtoken')
self.expected_params = {'retries': 2,
'insecure': 0,
'user': 'admin',
'tenant_name': 'tenant',
'key': 'password',
'authurl': 'http://authurl/v2.0',
'cacert': '/path/to/ca/file',
'auth_version': '2'}
def test___init__(self, connection_mock):
swift.SwiftAPI()
params = {'retries': 2,
'insecure': 0,
'user': 'admin',
'tenant_name': 'tenant',
'key': 'password',
'authurl': 'http://authurl/v2.0',
'cacert': '/path/to/ca/file',
'auth_version': '2'}
connection_mock.assert_called_once_with(**self.expected_params)
def test__init__with_region_from_config(self, connection_mock):
self.config(region_name='region1', group='keystone_authtoken')
swift.SwiftAPI()
params = self.expected_params.copy()
params['os_options'] = {'region_name': 'region1'}
connection_mock.assert_called_once_with(**params)
def test__init__with_region_from_constructor(self, connection_mock):
swift.SwiftAPI(region_name='region1')
params = self.expected_params.copy()
params['os_options'] = {'region_name': 'region1'}
connection_mock.assert_called_once_with(**params)
@mock.patch.object(__builtin__, 'open', autospec=True)

View File

@ -0,0 +1,5 @@
---
fixes:
- Fixes a bug where the keystone_authtoken/region_name wasn't passed
to Swift when instantiating its client, in a multi-region environment
this is needed so the client can choose the correct swift endpoint.