Pass session directly to swiftclient

since v3.2.0 swiftclient supports instantiating the API client from
keystonauth session, and we already require this version as minimal in
ironic.

This patch removes existing workarounds in SwiftAPI code which were there
only to accomodate for absence of session handling in swiftclient,
and now the keystoneauth session built from the [swift] config section
is used directly to instantiate the swift API client.

Change-Id: I593156d122db62b49e2d383d3dbbbae691b7b904
Related-Bug: #1518938
Co-Authored-By: Pavlo Shchelokovskyy <shchelokovskyy@gmail.com>
This commit is contained in:
Anton Arefiev 2017-02-10 17:40:08 +02:00
parent f9915931b2
commit 7e8cdc0d0f
2 changed files with 3 additions and 40 deletions

View File

@ -16,7 +16,6 @@
import json
from oslo_config import cfg
import six
from swiftclient import client as swift_client
from swiftclient import exceptions as swift_exceptions
@ -79,30 +78,8 @@ class SwiftAPI(object):
global SWIFT_SESSION
if not SWIFT_SESSION:
SWIFT_SESSION = keystone.get_session(SWIFT_GROUP)
# TODO(pas-ha): swiftclient does not support keystone sessions ATM.
# Must be reworked when LP bug #1518938 is fixed.
swift_url = SWIFT_SESSION.get_endpoint(
service_type=CONF.swift.os_service_type,
endpoint_type=CONF.swift.os_endpoint_type,
region_name=CONF.swift.os_region
)
token = SWIFT_SESSION.get_token()
params = dict(retries=CONF.swift.max_retries,
preauthurl=swift_url,
preauthtoken=token)
# NOTE(pas-ha):session.verify is for HTTPS urls and can be
# - False (do not verify)
# - True (verify but try to locate system CA certificates)
# - Path (verify using specific CA certificate)
# This is normally handled inside the Session instance,
# but swiftclient still does not support sessions,
# so we need to reconstruct these options from Session here.
verify = SWIFT_SESSION.verify
params['insecure'] = not verify
if verify and isinstance(verify, six.string_types):
params['cacert'] = verify
self.connection = swift_client.Connection(**params)
self.connection = swift_client.Connection(session=SWIFT_SESSION)
def create_object(self, object, data, container=CONF.swift.container,
headers=None):

View File

@ -64,23 +64,9 @@ class SwiftTestCase(BaseTest):
self.addCleanup(swift.reset_swift_session)
def test___init__(self, connection_mock, load_mock, opts_mock):
swift_url = 'http://swiftapi'
token = 'secret_token'
mock_sess = mock.Mock()
mock_sess.get_token.return_value = token
mock_sess.get_endpoint.return_value = swift_url
mock_sess.verify = False
load_mock.return_value = mock_sess
swift.SwiftAPI()
params = {'retries': 2,
'preauthurl': swift_url,
'preauthtoken': token,
'insecure': True}
connection_mock.assert_called_once_with(**params)
mock_sess.get_endpoint.assert_called_once_with(
service_type='object-store',
endpoint_type='internalURL',
region_name='somewhere')
connection_mock.assert_called_once_with(
session=load_mock.return_value)
def test_create_object(self, connection_mock, load_mock, opts_mock):
swiftapi = swift.SwiftAPI()