[Launch Instance Fix] Glance rest API fix

Adds support for resource_type, so retrieved namespaces will have
properties with proper prefixes.

Closes-Bug: 1439312
Change-Id: I4c2b450db8189c99624290a1d678fcaf509e1de6
This commit is contained in:
Szymon Wroblewski 2015-04-01 19:33:22 +02:00
parent b2fc84d7f6
commit 9579280bf5
4 changed files with 40 additions and 71 deletions

View File

@ -103,6 +103,9 @@ limitations under the License.
* @param {Object} params
* Query parameters. Optional.
*
* @param {string} params.resource_type
* Namespace resource type.
*
* @param {boolean} params.paginate
* True to paginate automatically.
*
@ -134,20 +137,6 @@ limitations under the License.
});
};
/**
* @name hz.api.glanceAPI.getImages
* @description
* Get a specific namespace.
*
* http://docs.openstack.org/developer/glance/metadefs-concepts.html
*/
this.getNamespace = function(namespace) {
return apiService.get('/api/glance/metadefs/namespaces/' + namespace)
.error(function () {
horizon.alert('error', gettext('Unable to retrieve namespace.'));
});
};
}
// Register it with the API module so that anybody using the

View File

@ -199,6 +199,7 @@ class Namespace(BaseGlanceMetadefAPIResourceWrapper):
return False
@memoized
def metadefs_namespace_get(request, namespace, resource_type=None, wrap=False):
namespace = glanceclient(request, '2').\
metadefs_namespace.get(namespace, resource_type=resource_type)
@ -279,6 +280,18 @@ def metadefs_namespace_list(request,
return namespaces, has_more_data, has_prev_data
def metadefs_namespace_full_list(request, resource_type, filters={},
*args, **kwargs):
filters['resource_types'] = [resource_type]
namespaces, has_more_data, has_prev_data = metadefs_namespace_list(
request, filters, *args, **kwargs
)
return [
metadefs_namespace_get(request, x.namespace, resource_type)
for x in namespaces
], has_more_data, has_prev_data
def metadefs_namespace_create(request, namespace):
return glanceclient(request, '2').metadefs_namespace.create(**namespace)

View File

@ -15,6 +15,7 @@
"""API for the glance service.
"""
from itertools import izip
from django.views import generic
from openstack_dashboard import api
@ -22,7 +23,7 @@ from openstack_dashboard.api.rest import utils as rest_utils
from openstack_dashboard.api.rest import urls
CLIENT_KEYWORDS = {'marker', 'sort_dir', 'sort_key', 'paginate'}
CLIENT_KEYWORDS = {'resource_type', 'marker', 'sort_dir', 'sort_key', 'paginate'}
@urls.register
@ -88,26 +89,6 @@ class Images(generic.View):
}
@urls.register
class MetadefsNamespace(generic.View):
"""API for Glance Metadata Definitions.
http://docs.openstack.org/developer/glance/metadefs-concepts.html
"""
url_regex = r'glance/metadefs/namespaces/(?P<namespace>.+|default)$'
@rest_utils.ajax()
def get(self, request, namespace):
"""Get a specific metadata definition namespaces.
Returns the namespace. GET params are passed through.
Example GET:
http://localhost/api/glance/metadefs/namespaces/OS::Compute::Watchdog
"""
return api.glance.metadefs_namespace_get(request, namespace)
@urls.register
class MetadefsNamespaces(generic.View):
"""API for Single Glance Metadata Definitions.
@ -129,6 +110,9 @@ class MetadefsNamespaces(generic.View):
The following get parameters may be passed in the GET
request:
:param resource_type: Namespace resource type.
If specified returned namespace properties will have prefixes
proper for selected resource type.
:param paginate: If true will perform pagination based on settings.
:param marker: Specifies the namespace of the last-seen namespace.
The typical pattern of limit and marker is to make an
@ -147,14 +131,11 @@ class MetadefsNamespaces(generic.View):
filters.
"""
filters, kwargs = rest_utils.parse_filters_kwargs(request,
CLIENT_KEYWORDS)
filters, kwargs = rest_utils.parse_filters_kwargs(
request, CLIENT_KEYWORDS
)
namespaces, has_more, has_prev = api.glance.metadefs_namespace_list(
request, filters=filters, **kwargs)
return {
'items': [n.to_dict() for n in namespaces],
'has_more_data': has_more,
'has_prev_data': has_prev,
}
names = ('items', 'has_more_data', 'has_prev_data')
return dict(izip(names, api.glance.metadefs_namespace_full_list(
request, filters=filters, **kwargs
)))

View File

@ -56,19 +56,18 @@ class ImagesRestTestCase(test.TestCase):
@mock.patch.object(glance.api, 'glance')
def test_namespace_get_list(self, gc):
request = self.mock_rest_request(**{'GET': {}})
gc.metadefs_namespace_list.return_value = ([
mock.Mock(**{'to_dict.return_value': {'namespace': '1'}}),
mock.Mock(**{'to_dict.return_value': {'namespace': '2'}})
], False, False)
gc.metadefs_namespace_full_list.return_value = (
[{'namespace': '1'}, {'namespace': '2'}], False, False
)
response = glance.MetadefsNamespaces().get(request)
self.assertStatusCode(response, 200)
self.assertEqual(response.content,
'{"items": [{"namespace": "1"}, {"namespace": "2"}]'
', "has_more_data": false, "has_prev_data": false}')
gc.metadefs_namespace_list.assert_called_once_with(request,
filters={},
**{})
gc.metadefs_namespace_full_list.assert_called_once_with(
request, filters={}
)
@mock.patch.object(glance.api, 'glance')
def test_namespace_get_list_kwargs_and_filters(self, gc):
@ -81,28 +80,15 @@ class ImagesRestTestCase(test.TestCase):
filters = {'resource_types': 'type'}
request = self.mock_rest_request(
**{'GET': dict(kwargs, **filters)})
gc.metadefs_namespace_list.return_value = ([
mock.Mock(**{'to_dict.return_value': {'namespace': '1'}}),
mock.Mock(**{'to_dict.return_value': {'namespace': '2'}})
], False, False)
gc.metadefs_namespace_full_list.return_value = (
[{'namespace': '1'}, {'namespace': '2'}], False, False
)
response = glance.MetadefsNamespaces().get(request)
self.assertStatusCode(response, 200)
self.assertEqual(response.content,
'{"items": [{"namespace": "1"}, {"namespace": "2"}]'
', "has_more_data": false, "has_prev_data": false}')
gc.metadefs_namespace_list.assert_called_once_with(request,
filters=filters,
**kwargs)
@mock.patch.object(glance.api, 'glance')
def test_namespace_get_namespace(self, gc):
kwargs = {'resource_type': ['OS::Nova::Flavor']}
request = self.mock_rest_request(**{'GET': dict(kwargs)})
gc.metadefs_namespace_get.return_value\
.to_dict.return_value = {'namespace': '1'}
response = glance.MetadefsNamespace().get(request, "1")
self.assertStatusCode(response, 200)
gc.metadefs_namespace_get.assert_called_once_with(request,
"1")
gc.metadefs_namespace_full_list.assert_called_once_with(
request, filters=filters, **kwargs
)