Blob Blame History Raw
From 66bdba1971ef0ca48d2f8c5da63d34baf8a2fdb5 Mon Sep 17 00:00:00 2001
From: Angus Salkeld <asalkeld@redhat.com>
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 <resource type>

Change-Id: Ifa70da5bc56a5f2979697a9ce2c41fa9a5c1f947
Closes-bug: #1260130
(cherry picked from commit b14697ea3fada6e9f6725fa14e9c669beebd8875)

Conflicts:
	heatclient/v1/client.py
---
 heatclient/tests/test_resource_types.py | 45 +++++++++++++++++++++++++++++++
 heatclient/v1/client.py                 | 13 +++++----
 heatclient/v1/resource_types.py         | 47 +++++++++++++++++++++++++++++++++
 heatclient/v1/shell.py                  | 20 ++++++++++++++
 4 files changed, 120 insertions(+), 5 deletions(-)
 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 3ec6be6..940a94a 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
 
@@ -32,8 +33,10 @@ class Client(http.HTTPClient):
 
     def __init__(self, *args, **kwargs):
         """Initialize a new client for the Heat v1 API."""
-        super(Client, self).__init__(*args, **kwargs)
-        self.stacks = stacks.StackManager(self)
-        self.resources = resources.ResourceManager(self)
-        self.events = events.EventManager(self)
-        self.actions = actions.ActionManager(self)
+        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 "<ResourceType %s>" % 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 d98b6a7..c3e6396 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='<RESOURCE_TYPE>',
+           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='<NAME or ID>',
            help='Name or ID of stack to get the template for.')
 def do_gettemplate(hc, args):