Skip to content

Commit b14697e

Browse files
committed
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
1 parent 89282eb commit b14697e

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
14+
import mock
15+
import testtools
16+
17+
from heatclient.v1.resource_types import ResourceTypeManager
18+
19+
20+
class ResourceTypeManagerTest(testtools.TestCase):
21+
22+
def test_list_types(self):
23+
manager = ResourceTypeManager(None)
24+
manager._list = mock.MagicMock()
25+
manager.list()
26+
manager._list.assert_called_once_with('/resource_types',
27+
'resource_types')
28+
29+
def test_get(self):
30+
resource_type = u'OS::Nova::KeyPair'
31+
32+
class FakeAPI(object):
33+
"""Fake API and ensure request url is correct."""
34+
def __init__(self, *args, **kwargs):
35+
self.requests = []
36+
37+
def json_request(self, *args, **kwargs):
38+
self.requests.append(args)
39+
return {}, {'attributes': [], 'properties': []}
40+
41+
test_api = FakeAPI()
42+
manager = ResourceTypeManager(test_api)
43+
manager.get(resource_type)
44+
expect = ('GET', '/resource_types/OS%3A%3ANova%3A%3AKeyPair')
45+
self.assertIn(expect, test_api.requests)

‎heatclient/v1/client.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from heatclient.common import http
1717
from heatclient.v1 import actions
1818
from heatclient.v1 import events
19+
from heatclient.v1 import resource_types
1920
from heatclient.v1 import resources
2021
from heatclient.v1 import stacks
2122

@@ -35,5 +36,7 @@ def __init__(self, *args, **kwargs):
3536
self.http_client = http.HTTPClient(*args, **kwargs)
3637
self.stacks = stacks.StackManager(self.http_client)
3738
self.resources = resources.ResourceManager(self.http_client)
39+
self.resource_types = resource_types.ResourceTypeManager(
40+
self.http_client)
3841
self.events = events.EventManager(self.http_client)
3942
self.actions = actions.ActionManager(self.http_client)

‎heatclient/v1/resource_types.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
14+
from heatclient.common import base
15+
from heatclient.openstack.common.py3kcompat import urlutils
16+
from heatclient.openstack.common import strutils
17+
18+
19+
class ResourceType(base.Resource):
20+
def __repr__(self):
21+
return "<ResourceType %s>" % self._info
22+
23+
def data(self, **kwargs):
24+
return self.manager.data(self, **kwargs)
25+
26+
def _add_details(self, info):
27+
self.resource_type = info
28+
29+
30+
class ResourceTypeManager(base.Manager):
31+
resource_class = ResourceType
32+
33+
def list(self):
34+
"""Get a list of resource types.
35+
:rtype: list of :class:`ResourceType`
36+
"""
37+
return self._list('/resource_types', 'resource_types')
38+
39+
def get(self, resource_type):
40+
"""Get the details for a specific resource_type.
41+
42+
:param resource_type: name of the resource type to get the details for
43+
"""
44+
url_str = '/resource_types/%s' % (
45+
urlutils.quote(strutils.safe_encode(resource_type), ''))
46+
resp, body = self.api.json_request('GET', url_str)
47+
return body

‎heatclient/v1/shell.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,26 @@ def do_stack_list(hc, args={}):
293293
utils.print_list(stacks, fields, sortby=3)
294294

295295

296+
def do_resource_type_list(hc, args={}):
297+
'''List the available resource types.'''
298+
kwargs = {}
299+
types = hc.resource_types.list(**kwargs)
300+
utils.print_list(types, ['resource_type'])
301+
302+
303+
@utils.arg('resource_type', metavar='<RESOURCE_TYPE>',
304+
help='Resource Type to get the details for.')
305+
def do_resource_type_show(hc, args={}):
306+
'''Show the resource type.'''
307+
try:
308+
resource_type = hc.resource_types.get(args.resource_type)
309+
except exc.HTTPNotFound:
310+
raise exc.CommandError(
311+
'Resource Type not found: %s' % args.resource_type)
312+
else:
313+
print(json.dumps(resource_type, indent=2))
314+
315+
296316
@utils.arg('id', metavar='<NAME or ID>',
297317
help='Name or ID of stack to get the template for.')
298318
def do_gettemplate(hc, args):

0 commit comments

Comments
 (0)