Make account prefix of Swift confgurable

Account prefix is configurable in Swift and can be different. This
change introduces a configuration parameter to customize the prefix
so that users can customize the prefix consistently.

To allow setting a consistent value in Ironic and Swift, the prefix
value is automatically suffixed by '_' like;
 - AUTH  => AUTH_
 - AUTH_ => AUTH_

Story: 2009778
Task: 44259
Change-Id: I4862efa3af452f89f1dc4e15d2c3cc43b857f86d
This commit is contained in:
Takashi Kajinami 2022-01-09 22:35:48 +09:00
parent 1a03e61329
commit cde42678b9
4 changed files with 74 additions and 2 deletions

View File

@ -303,15 +303,20 @@ class GlanceImageService(object):
'but it was not found in the service catalog. You must '
'provide "swift_endpoint_url" as a config option.'))
swift_account_prefix = CONF.glance.swift_account_prefix
if swift_account_prefix and not swift_account_prefix.endswith('_'):
swift_account_prefix = '%s_' % swift_account_prefix
# Strip /v1/AUTH_%(tenant_id)s, if present
endpoint_url = re.sub('/v1/AUTH_[^/]+/?$', '', endpoint_url)
endpoint_url = re.sub('/v1/%s[^/]+/?$' % swift_account_prefix, '',
endpoint_url)
key = CONF.glance.swift_temp_url_key
account = CONF.glance.swift_account
if not account:
swift_session = swift.get_swift_session()
auth_ref = swift_session.auth.get_auth_ref(swift_session)
account = 'AUTH_%s' % auth_ref.project_id
account = '%s%s' % (swift_account_prefix, auth_ref.project_id)
if not key:
swift_api = swift.SwiftAPI()

View File

@ -90,6 +90,11 @@ opts = [
'of the project used to access Swift (as set in the [swift] '
'section). Swift temporary URL format: '
'"endpoint_url/api_version/account/container/object_id"')),
cfg.StrOpt(
'swift_account_prefix',
default='AUTH',
help=_('The prefix added to the project uuid to determine the swift '
'account.')),
cfg.StrOpt(
'swift_container',
default='glance',

View File

@ -603,6 +603,62 @@ class TestGlanceSwiftTempURL(base.TestCase):
method='GET')
swift_mock.assert_called_once_with()
@mock.patch('ironic.common.swift.get_swift_session', autospec=True)
@mock.patch('swiftclient.utils.generate_temp_url', autospec=True)
def test_swift_temp_url_account_detected_with_prefix(self, tempurl_mock,
swift_mock):
self.config(swift_account=None, group='glance')
self.config(swift_account_prefix='SWIFTPREFIX', group='glance')
path = ('/v1/SWIFTPREFIX_42/glance'
'/757274c4-2856-4bd2-bb20-9a4a231e187b')
tempurl_mock.return_value = (
path + '?temp_url_sig=hmacsig&temp_url_expires=1400001200')
auth_ref = swift_mock.return_value.auth.get_auth_ref.return_value
auth_ref.project_id = '42'
self.service._validate_temp_url_config = mock.Mock()
temp_url = self.service.swift_temp_url(image_info=self.fake_image)
self.assertEqual(CONF.glance.swift_endpoint_url
+ tempurl_mock.return_value,
temp_url)
tempurl_mock.assert_called_with(
path=path,
seconds=CONF.glance.swift_temp_url_duration,
key=CONF.glance.swift_temp_url_key,
method='GET')
swift_mock.assert_called_once_with()
@mock.patch('ironic.common.swift.get_swift_session', autospec=True)
@mock.patch('swiftclient.utils.generate_temp_url', autospec=True)
def test_swift_temp_url_account_detected_with_prefix_underscore(
self, tempurl_mock, swift_mock):
self.config(swift_account=None, group='glance')
self.config(swift_account_prefix='SWIFTPREFIX_', group='glance')
path = ('/v1/SWIFTPREFIX_42/glance'
'/757274c4-2856-4bd2-bb20-9a4a231e187b')
tempurl_mock.return_value = (
path + '?temp_url_sig=hmacsig&temp_url_expires=1400001200')
auth_ref = swift_mock.return_value.auth.get_auth_ref.return_value
auth_ref.project_id = '42'
self.service._validate_temp_url_config = mock.Mock()
temp_url = self.service.swift_temp_url(image_info=self.fake_image)
self.assertEqual(CONF.glance.swift_endpoint_url
+ tempurl_mock.return_value,
temp_url)
tempurl_mock.assert_called_with(
path=path,
seconds=CONF.glance.swift_temp_url_duration,
key=CONF.glance.swift_temp_url_key,
method='GET')
swift_mock.assert_called_once_with()
@mock.patch('ironic.common.swift.SwiftAPI', autospec=True)
@mock.patch('swiftclient.utils.generate_temp_url', autospec=True)
def test_swift_temp_url_key_detected(self, tempurl_mock, swift_mock):

View File

@ -0,0 +1,6 @@
---
features:
- |
The new ``[glance] swift_account_prefix`` parameter has been added. This
parameter be set according to the ``reseller_prefix`` parameter in
``proxy-server.conf`` of Swift.