From b14697ea3fada6e9f6725fa14e9c669beebd8875 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Thu, 12 Dec 2013 13:18:11 +1100 Subject: [PATCH] Add support for resource_types This adds the following commands: $ heat resource-type-list $ heat resource-type-show Change-Id: Ifa70da5bc56a5f2979697a9ce2c41fa9a5c1f947 Closes-bug: #1260130 --- heatclient/tests/test_resource_types.py | 45 +++++++++++++++++++++++ heatclient/v1/client.py | 3 ++ heatclient/v1/resource_types.py | 47 +++++++++++++++++++++++++ heatclient/v1/shell.py | 20 +++++++++++ 4 files changed, 115 insertions(+) create mode 100644 heatclient/tests/test_resource_types.py create mode 100644 heatclient/v1/resource_types.py diff --git a/heatclient/tests/test_resource_types.py b/heatclient/tests/test_resource_types.py new file mode 100644 index 0000000..4030fd8 --- /dev/null +++ b/heatclient/tests/test_resource_types.py @@ -0,0 +1,45 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock +import testtools + +from heatclient.v1.resource_types import ResourceTypeManager + + +class ResourceTypeManagerTest(testtools.TestCase): + + def test_list_types(self): + manager = ResourceTypeManager(None) + manager._list = mock.MagicMock() + manager.list() + manager._list.assert_called_once_with('/resource_types', + 'resource_types') + + def test_get(self): + resource_type = u'OS::Nova::KeyPair' + + class FakeAPI(object): + """Fake API and ensure request url is correct.""" + def __init__(self, *args, **kwargs): + self.requests = [] + + def json_request(self, *args, **kwargs): + self.requests.append(args) + return {}, {'attributes': [], 'properties': []} + + test_api = FakeAPI() + manager = ResourceTypeManager(test_api) + manager.get(resource_type) + expect = ('GET', '/resource_types/OS%3A%3ANova%3A%3AKeyPair') + self.assertIn(expect, test_api.requests) diff --git a/heatclient/v1/client.py b/heatclient/v1/client.py index 5b757d0..39fbe6e 100644 --- a/heatclient/v1/client.py +++ b/heatclient/v1/client.py @@ -16,6 +16,7 @@ from heatclient.common import http from heatclient.v1 import actions from heatclient.v1 import events +from heatclient.v1 import resource_types from heatclient.v1 import resources from heatclient.v1 import stacks @@ -35,5 +36,7 @@ class Client(object): self.http_client = http.HTTPClient(*args, **kwargs) self.stacks = stacks.StackManager(self.http_client) self.resources = resources.ResourceManager(self.http_client) + self.resource_types = resource_types.ResourceTypeManager( + self.http_client) self.events = events.EventManager(self.http_client) self.actions = actions.ActionManager(self.http_client) diff --git a/heatclient/v1/resource_types.py b/heatclient/v1/resource_types.py new file mode 100644 index 0000000..2600197 --- /dev/null +++ b/heatclient/v1/resource_types.py @@ -0,0 +1,47 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from heatclient.common import base +from heatclient.openstack.common.py3kcompat import urlutils +from heatclient.openstack.common import strutils + + +class ResourceType(base.Resource): + def __repr__(self): + return "" % self._info + + def data(self, **kwargs): + return self.manager.data(self, **kwargs) + + def _add_details(self, info): + self.resource_type = info + + +class ResourceTypeManager(base.Manager): + resource_class = ResourceType + + def list(self): + """Get a list of resource types. + :rtype: list of :class:`ResourceType` + """ + return self._list('/resource_types', 'resource_types') + + def get(self, resource_type): + """Get the details for a specific resource_type. + + :param resource_type: name of the resource type to get the details for + """ + url_str = '/resource_types/%s' % ( + urlutils.quote(strutils.safe_encode(resource_type), '')) + resp, body = self.api.json_request('GET', url_str) + return body diff --git a/heatclient/v1/shell.py b/heatclient/v1/shell.py index f445f1c..5a76950 100644 --- a/heatclient/v1/shell.py +++ b/heatclient/v1/shell.py @@ -293,6 +293,26 @@ def do_stack_list(hc, args={}): utils.print_list(stacks, fields, sortby=3) +def do_resource_type_list(hc, args={}): + '''List the available resource types.''' + kwargs = {} + types = hc.resource_types.list(**kwargs) + utils.print_list(types, ['resource_type']) + + +@utils.arg('resource_type', metavar='', + help='Resource Type to get the details for.') +def do_resource_type_show(hc, args={}): + '''Show the resource type.''' + try: + resource_type = hc.resource_types.get(args.resource_type) + except exc.HTTPNotFound: + raise exc.CommandError( + 'Resource Type not found: %s' % args.resource_type) + else: + print(json.dumps(resource_type, indent=2)) + + @utils.arg('id', metavar='', help='Name or ID of stack to get the template for.') def do_gettemplate(hc, args):