Use configured value for import-methods header

In Image API v2.6, the image-create response returns an
'OpenStack-image-import-methods' header containing a list of
methods available in that cloud.  This patch populates that
list from the enabled_import_methods configuration setting.
Following RFC 7230, if the enabled_import_methods is empty,
the header is not returned with the response.

Change-Id: I3066da36a0fbb83fe492bddb193975e42ffc5084
Closes-bug: #1748559
This commit is contained in:
Brian Rosmaita 2018-02-09 17:12:31 -05:00
parent 8fdb9537a7
commit 3712dccfdb
2 changed files with 53 additions and 5 deletions

View File

@ -892,12 +892,15 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
response.status_int = http.CREATED
self.show(response, image)
response.location = self._get_image_href(image)
# TODO(jokke): make this configurable when swift-local is implemented
# and remove the if statement with the config option.
# TODO(rosmaita): remove the outer 'if' statement when the
# enable_image_import config option is removed
if CONF.enable_image_import:
import_methods = ("OpenStack-image-import-methods",
"glance-direct")
response.headerlist.append(import_methods)
# according to RFC7230, headers should not have empty fields
# see http://httpwg.org/specs/rfc7230.html#field.components
if CONF.enabled_import_methods:
import_methods = ("OpenStack-image-import-methods",
','.join(CONF.enabled_import_methods))
response.headerlist.append(import_methods)
def show(self, response, image):
image_view = self._format_image(image)

View File

@ -3567,6 +3567,51 @@ class TestImagesSerializer(test_utils.BaseTestCase):
self.assertEqual('application/json', response.content_type)
self.assertEqual('/v2/images/%s' % UUID1, response.location)
def test_create_has_import_methods_header(self):
# NOTE(rosmaita): enabled_import_methods is defined as type
# oslo.config.cfg.ListOpt, so it is stored internally as a list
# but is converted to a string for output in the HTTP header
header_name = 'OpenStack-image-import-methods'
# check multiple methods
enabled_methods = ['one', 'two', 'three']
self.config(enabled_import_methods=enabled_methods)
response = webob.Response()
self.serializer.create(response, self.fixtures[0])
self.assertEqual(http.CREATED, response.status_int)
header_value = response.headers.get(header_name)
self.assertIsNotNone(header_value)
self.assertItemsEqual(enabled_methods, header_value.split(','))
# check single method
self.config(enabled_import_methods=['swift-party-time'])
response = webob.Response()
self.serializer.create(response, self.fixtures[0])
self.assertEqual(http.CREATED, response.status_int)
header_value = response.headers.get(header_name)
self.assertIsNotNone(header_value)
self.assertEqual('swift-party-time', header_value)
# no header for empty config value
self.config(enabled_import_methods=[])
response = webob.Response()
self.serializer.create(response, self.fixtures[0])
self.assertEqual(http.CREATED, response.status_int)
headers = response.headers.keys()
self.assertNotIn(header_name, headers)
# TODO(rosmaita): remove this test when the enable_image_import
# option is removed
def test_create_has_no_import_methods_header(self):
header_name = 'OpenStack-image-import-methods'
self.config(enable_image_import=False)
response = webob.Response()
self.serializer.create(response, self.fixtures[0])
self.assertEqual(http.CREATED, response.status_int)
headers = response.headers.keys()
self.assertNotIn(header_name, headers)
def test_update(self):
expected = {
'id': UUID1,