Deprecate misleading cli command 'resource-template'
Current 'resource-template' command is misleading as it's providing template snippet for the 'resource-type' and not the 'resource'. This has been deprecated and a new command 'resource-type-template' added. ex. $heat resource-type-template -F json OS::Nova::KeyPair Change-Id: I373e2fa3c7891d2488704975ffdfb6d9afa5059a Closes-Bug: #1336306
This commit is contained in:
@@ -11,7 +11,6 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
import testtools
|
||||
|
||||
from heatclient.v1.resource_types import ResourceTypeManager
|
||||
@@ -19,27 +18,48 @@ 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'
|
||||
def _base_test(self, expect, key):
|
||||
|
||||
class FakeAPI(object):
|
||||
"""Fake API and ensure request url is correct."""
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.requests = []
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
assert ('GET', args[0]) == expect
|
||||
|
||||
def json_request(self, *args, **kwargs):
|
||||
self.requests.append(args)
|
||||
return {}, {'attributes': [], 'properties': []}
|
||||
assert args == expect
|
||||
ret = key and {key: []} or {}
|
||||
return {}, {key: ret}
|
||||
|
||||
test_api = FakeAPI()
|
||||
manager = ResourceTypeManager(test_api)
|
||||
manager.get(resource_type)
|
||||
manager = ResourceTypeManager(FakeAPI())
|
||||
return manager
|
||||
|
||||
def test_list_types(self):
|
||||
key = 'resource_types'
|
||||
expect = ('GET', '/resource_types')
|
||||
|
||||
class FakeResponse(object):
|
||||
def json(self):
|
||||
return {key: {}}
|
||||
|
||||
class FakeClient(object):
|
||||
def get(self, *args, **kwargs):
|
||||
assert ('GET', args[0]) == expect
|
||||
return FakeResponse()
|
||||
|
||||
manager = ResourceTypeManager(FakeClient())
|
||||
manager.list()
|
||||
|
||||
def test_get(self):
|
||||
key = 'resource_types'
|
||||
resource_type = 'OS::Nova::KeyPair'
|
||||
expect = ('GET', '/resource_types/OS%3A%3ANova%3A%3AKeyPair')
|
||||
self.assertIn(expect, test_api.requests)
|
||||
manager = self._base_test(expect, key)
|
||||
manager.get(resource_type)
|
||||
|
||||
def test_generate_template(self):
|
||||
key = 'resource_types'
|
||||
resource_type = 'OS::Nova::KeyPair'
|
||||
expect = ('GET', '/resource_types/OS%3A%3ANova%3A%3AKeyPair/template')
|
||||
manager = self._base_test(expect, key)
|
||||
manager.generate_template(resource_type)
|
||||
|
||||
@@ -1572,6 +1572,85 @@ class ShellTestResources(ShellBase):
|
||||
self.assertEqual("", text)
|
||||
|
||||
|
||||
class ShellTestResourceTypes(ShellBase):
|
||||
def setUp(self):
|
||||
super(ShellTestResourceTypes, self).setUp()
|
||||
self._set_fake_env()
|
||||
|
||||
# Patch os.environ to avoid required auth info.
|
||||
def _set_fake_env(self):
|
||||
fake_env = {
|
||||
'OS_USERNAME': 'username',
|
||||
'OS_PASSWORD': 'password',
|
||||
'OS_TENANT_NAME': 'tenant_name',
|
||||
'OS_AUTH_URL': 'http://no.where',
|
||||
}
|
||||
self.set_fake_env(fake_env)
|
||||
|
||||
def _script_keystone_client(self):
|
||||
fakes.script_keystone_client()
|
||||
|
||||
def test_resource_type_template_yaml(self):
|
||||
self._script_keystone_client()
|
||||
resp_dict = {"heat_template_version": "2013-05-23",
|
||||
"parameters": {},
|
||||
"resources": {},
|
||||
"outputs": {}}
|
||||
resp = fakes.FakeHTTPResponse(
|
||||
200,
|
||||
'OK',
|
||||
{'content-type': 'application/json'},
|
||||
jsonutils.dumps(resp_dict))
|
||||
|
||||
http.HTTPClient.json_request(
|
||||
'GET', '/resource_types/OS%3A%3ANova%3A%3AKeyPair/template'
|
||||
).AndReturn((resp, resp_dict))
|
||||
|
||||
self.m.ReplayAll()
|
||||
|
||||
show_text = self.shell(
|
||||
'resource-type-template -F yaml OS::Nova::KeyPair')
|
||||
required = [
|
||||
"heat_template_version: '2013-05-23'",
|
||||
"outputs: {}",
|
||||
"parameters: {}",
|
||||
"resources: {}"
|
||||
]
|
||||
for r in required:
|
||||
self.assertRegexpMatches(show_text, r)
|
||||
|
||||
def test_resource_type_template_json(self):
|
||||
self._script_keystone_client()
|
||||
resp_dict = {"AWSTemplateFormatVersion": "2013-05-23",
|
||||
"Parameters": {},
|
||||
"Resources": {},
|
||||
"Outputs": {}}
|
||||
resp = fakes.FakeHTTPResponse(
|
||||
200,
|
||||
'OK',
|
||||
{'content-type': 'application/json'},
|
||||
jsonutils.dumps(resp_dict))
|
||||
|
||||
http.HTTPClient.json_request(
|
||||
'GET', '/resource_types/OS%3A%3ANova%3A%3AKeyPair/template'
|
||||
).AndReturn((resp, resp_dict))
|
||||
|
||||
self.m.ReplayAll()
|
||||
|
||||
show_text = self.shell(
|
||||
'resource-type-template -F json OS::Nova::KeyPair')
|
||||
required = [
|
||||
'{',
|
||||
' "AWSTemplateFormatVersion": "2013-05-23"',
|
||||
' "Outputs": {}',
|
||||
' "Resources": {}',
|
||||
' "Parameters": {}',
|
||||
'}'
|
||||
]
|
||||
for r in required:
|
||||
self.assertRegexpMatches(show_text, r)
|
||||
|
||||
|
||||
class ShellTestBuildInfo(ShellBase):
|
||||
def setUp(self):
|
||||
super(ShellTestBuildInfo, self).setUp()
|
||||
|
||||
@@ -46,3 +46,9 @@ class ResourceTypeManager(base.BaseManager):
|
||||
parse.quote(strutils.safe_encode(resource_type), ''))
|
||||
resp, body = self.client.json_request('GET', url_str)
|
||||
return body
|
||||
|
||||
def generate_template(self, resource_type):
|
||||
url_str = '/resource_types/%s/template' % (
|
||||
parse.quote(strutils.safe_encode(resource_type), ''))
|
||||
resp, body = self.client.json_request('GET', url_str)
|
||||
return body
|
||||
|
||||
@@ -86,6 +86,9 @@ class ResourceManager(stacks.StackChildManager):
|
||||
return body
|
||||
|
||||
def generate_template(self, resource_name):
|
||||
"""DEPRECATED! Use `generate_template` of `ResourceTypeManager`
|
||||
instead.
|
||||
"""
|
||||
url_str = '/resource_types/%s/template' % (
|
||||
parse.quote(strutils.safe_encode(resource_name), ''))
|
||||
resp, body = self.client.json_request('GET', url_str)
|
||||
|
||||
@@ -492,6 +492,26 @@ def do_resource_type_show(hc, args={}):
|
||||
print(jsonutils.dumps(resource_type, indent=2))
|
||||
|
||||
|
||||
@utils.arg('resource_type', metavar='<RESOURCE_TYPE>',
|
||||
help='Resource type to generate a template for.')
|
||||
@utils.arg('-F', '--format', metavar='<FORMAT>',
|
||||
help="The template output format, one of: %s."
|
||||
% ', '.join(utils.supported_formats.keys()))
|
||||
def do_resource_type_template(hc, args):
|
||||
'''Generate a template based on a resource type.'''
|
||||
fields = {'resource_type': args.resource_type}
|
||||
try:
|
||||
template = hc.resource_types.generate_template(**fields)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError(
|
||||
'Resource Type %s not found.' % args.resource_type)
|
||||
else:
|
||||
if args.format:
|
||||
print(utils.format_output(template, format=args.format))
|
||||
else:
|
||||
print(utils.format_output(template))
|
||||
|
||||
|
||||
@utils.arg('id', metavar='<NAME or ID>',
|
||||
help='Name or ID of stack to get the template for.')
|
||||
def do_gettemplate(hc, args):
|
||||
@@ -610,23 +630,15 @@ def do_resource_show(hc, args):
|
||||
utils.print_dict(resource.to_dict(), formatters=formatters)
|
||||
|
||||
|
||||
@utils.arg('resource', metavar='<RESOURCE>',
|
||||
help='Name of the resource to generate a template for.')
|
||||
@utils.arg('resource_type', metavar='<RESOURCE_TYPE>',
|
||||
help='Resource type to generate a template for.')
|
||||
@utils.arg('-F', '--format', metavar='<FORMAT>',
|
||||
help="The template output format, one of: %s."
|
||||
% ', '.join(utils.supported_formats.keys()))
|
||||
def do_resource_template(hc, args):
|
||||
'''Generate a template based on a resource.'''
|
||||
fields = {'resource_name': args.resource}
|
||||
try:
|
||||
template = hc.resources.generate_template(**fields)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Resource %s not found.' % args.resource)
|
||||
else:
|
||||
if args.format:
|
||||
print(utils.format_output(template, format=args.format))
|
||||
else:
|
||||
print(utils.format_output(template))
|
||||
'''DEPRECATED! Use resource-type-template instead.'''
|
||||
logger.warning('DEPRECATED! Use resource-type-template instead.')
|
||||
do_resource_type_template(hc, args)
|
||||
|
||||
|
||||
@utils.arg('id', metavar='<NAME or ID>',
|
||||
|
||||
Reference in New Issue
Block a user