Environment Template CLI

Change-Id: I74ddffcf876f6122d693449a52de8180b0cbeec2
This commit is contained in:
Henar Muñoz Frutos 2015-03-03 16:23:05 +01:00
parent 4cfac3cbf2
commit d50225a152
5 changed files with 267 additions and 2 deletions

View File

@ -22,6 +22,7 @@ from muranoclient.v1 import actions
import muranoclient.v1.environments as environments
from muranoclient.v1 import packages
import muranoclient.v1.sessions as sessions
import muranoclient.v1.templates as templates
def my_mock(*a, **b):
@ -231,3 +232,76 @@ class UnitTestsForClassesAndFunctions(testtools.TestCase):
manager = actions.ActionManager(api_mock)
result = manager.get_result('testEnvId', '1234')
self.assertEqual({'a': 'b'}, result)
def test_env_template_manager_list(self):
"""It tests the list of environment templates.
"""
manager = templates.EnvTemplateManager(api)
result = manager.list()
self.assertEqual([], result)
def test_env_template_manager_create(self):
manager = templates.EnvTemplateManager(api)
result = manager.create({'name': 'test'})
self.assertEqual({'name': 'test'}, result.data)
def test_env_template_manager_create_with_named_parameters(self):
manager = templates.EnvTemplateManager(api)
result = manager.create(data={'name': 'test'})
self.assertEqual({'name': 'test'}, result.data)
def test_env_template_manager_create_negative_without_parameters(self):
manager = templates.EnvTemplateManager(api)
self.assertRaises(TypeError, manager.create)
def test_env_template_manager_delete(self):
manager = templates.EnvTemplateManager(api)
result = manager.delete('test')
self.assertIsNone(result)
def test_env_template_manager_delete_with_named_parameters(self):
manager = templates.EnvTemplateManager(api)
result = manager.delete(env_template_id='1')
self.assertIsNone(result)
def test_env_template_manager_delete_negative_without_parameters(self):
manager = templates.EnvTemplateManager(api)
self.assertRaises(TypeError, manager.delete)
def test_env_template_manager_update(self):
manager = templates.EnvTemplateManager(api)
result = manager.update('1', 'test')
self.assertEqual({'name': 'test'}, result.data)
def test_env_template_manager_update_with_named_parameters(self):
manager = templates.EnvTemplateManager(api)
result = manager.update(env_template_id='1',
name='test')
self.assertEqual({'name': 'test'}, result.data)
def test_env_template_manager_update_negative_with_one_parameter(self):
manager = templates.EnvTemplateManager(api)
self.assertRaises(TypeError, manager.update, 'test')
def test_env_template_manager_update_negative_without_parameters(self):
manager = templates.EnvTemplateManager(api)
self.assertRaises(TypeError, manager.update)
def test_env_template_manager_get(self):
manager = templates.EnvTemplateManager(api)
result = manager.get('test')
self.assertIsNotNone(result.manager)

View File

@ -229,6 +229,29 @@ class ShellTest(base.TestCaseShell):
name='env-id-or-name')
self.client.environments.get.assert_called_once()
@mock.patch('muranoclient.v1.templates.EnvTemplateManager')
def test_env_template_delete(self, mock_manager):
self.client.env_templates = mock_manager()
self.make_env()
self.shell('env-template-delete env1 env2')
self.client.env_templates.delete.assert_has_calls([
mock.call('env1'), mock.call('env2')])
@mock.patch('muranoclient.v1.templates.EnvTemplateManager')
def test_env_template_create(self, mock_manager):
self.client.env_templates = mock_manager()
self.make_env()
self.shell('env-template-create env-name')
self.client.env_templates.create.assert_called_once_with(
{'name': 'env-name'})
@mock.patch('muranoclient.v1.templates.EnvTemplateManager')
def test_env_template_show(self, mock_manager):
self.client.env_templates = mock_manager()
self.make_env()
self.shell('env-template-show env-id')
self.client.env_templates.get.assert_called_once_with('env-id')
@mock.patch('muranoclient.v1.environments.EnvironmentManager')
@mock.patch('muranoclient.v1.deployments.DeploymentManager')
def test_deployments_show(self, mock_deployment_manager, mock_env_manager):

View File

@ -22,6 +22,7 @@ from muranoclient.v1 import packages
from muranoclient.v1 import request_statistics
from muranoclient.v1 import services
from muranoclient.v1 import sessions
from muranoclient.v1 import templates
class Client(http.HTTPClient):
@ -38,6 +39,7 @@ class Client(http.HTTPClient):
self.glance_client = kwargs.pop('glance_client', None)
super(Client, self).__init__(*args, **kwargs)
self.environments = environments.EnvironmentManager(self)
self.env_templates = templates.EnvTemplateManager(self)
self.sessions = sessions.SessionManager(self)
self.services = services.ServiceManager(self)
self.deployments = deployments.DeploymentManager(self)

View File

@ -95,8 +95,95 @@ def do_environment_show(mc, args):
utils.print_dict(environment.to_dict(), formatters=formatters)
@utils.arg("id", metavar="<NAME or ID>",
help="Environment id or name for which to list deployments")
def do_env_template_list(mc, args={}):
"""List the environments templates."""
env_templates = mc.env_templates.list()
field_labels = ['ID', 'Name', 'Created', 'Updated']
fields = ['id', 'name', 'created', 'updated']
utils.print_list(env_templates, fields, field_labels, sortby=0)
@utils.arg("name", metavar="<ENV_TEMPLATE_NAME>",
help="Environment Template name")
def do_env_template_create(mc, args):
"""Create an environment template."""
mc.env_templates.create({"name": args.name})
do_env_template_list(mc)
@utils.arg("id", metavar="<ID>",
help="Environment Template id")
def do_env_template_show(mc, args):
"""Display environment template details."""
try:
env_template = mc.env_templates.get(args.id)
except exceptions.NotFound:
raise exceptions.CommandError("Environment template %s not found"
% args.id)
else:
formatters = {
"id": utils.text_wrap_formatter,
"created": utils.text_wrap_formatter,
"name": utils.text_wrap_formatter,
"tenant_id": utils.text_wrap_formatter,
"services": utils.json_formatter,
}
utils.print_dict(env_template.to_dict(), formatters=formatters)
@utils.arg("name", metavar="<ENV_TEMPLATE_NAME>",
help="Environment Template name")
@utils.arg('app_template_file', metavar='<FILE>',
help='Path to the template.')
def do_env_template_add_app(mc, args):
"""Add application to the environment template."""
with open(args.app_template_file, "r") as myfile:
app_file = myfile.readlines()
mc.env_templates.create_app(args.name, app_file)
do_env_template_list(mc)
@utils.arg("id", metavar="<ENV_TEMPLATE_ID>",
help="Environment Template ID")
@utils.arg("service_id", metavar="<ENV_TEMPLATE_APP_ID>",
help="Application Id")
def do_env_template_del_app(mc, args):
"""Delete application to the environment template."""
mc.env_templates.delete_app(args.name, args.service_id)
do_env_template_list(mc)
@utils.arg("id", metavar="<ID>",
help="Environment Template id")
@utils.arg("name", metavar="<ENV_TEMPLATE_NAME>",
help="Environment Template name")
def do_env_template_update(mc, args):
"""Update an environment template."""
mc.env_templates.update(args.id, args.name)
do_env_template_list(mc)
@utils.arg("id", metavar="<ID>",
nargs="+", help="Id of environment(s) template to delete")
def do_env_template_delete(mc, args):
"""Delete an environment template."""
failure_count = 0
for env_template_id in args.id:
try:
mc.env_templates.delete(env_template_id)
except exceptions.NotFound:
failure_count += 1
mns = "Failed to delete '{0}'; environment template not found".\
format(env_template_id)
if failure_count == len(args.id):
raise exceptions.CommandError(mns)
do_env_template_list(mc)
@utils.arg("id", metavar="<ID>",
help="Environment id for which to list deployments")
def do_deployment_list(mc, args):
"""List deployments for an environment."""
try:

View File

@ -0,0 +1,79 @@
# Copyright (c) 2013 Mirantis, Inc.
#
# 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 muranoclient.common import base
class Template(base.Resource):
"""It involves the template resource.
"""
def __repr__(self):
return "<Template %s>" % self._info
def data(self, **kwargs):
return self.manager.data(self, **kwargs)
class EnvTemplateManager(base.Manager):
"""It involves the template manager.
"""
resource_class = Template
def list(self):
"""It lists the environment templates.
"""
return self._list('/v1/templates', 'templates')
def create(self, data):
"""It creates a environment template
:param data: The environment template information.
"""
return self._create('/v1/templates', data)
def update(self, env_template_id, name):
"""It updates the environment template name.
:param env_template_id: The environment template ID.
:param name: The name to be updated.
"""
return self._update('/v1/templates/{id}'.format(id=env_template_id),
data={'name': name})
def delete(self, env_template_id):
"""It deletes an environment template name.
:param env_template_id: The environment template ID.
"""
return self._delete('/v1/templates/{id}'.format(id=env_template_id))
def get(self, env_template_id):
"""It gets information about an environment template name.
:param env_template_id: The environment template ID.
"""
return self._get("/v1/templates/{id}".format(id=env_template_id))
def create_app(self, env_template_id, data):
"""It creates an application in an environment template.
:param env_template_id: The environment template ID.
:param data: the application information.
"""
return self.\
_create('/v1/templates/{id}/services'.
format(id=env_template_id), data)
def delete_app(self, env_template_id, app_id):
"""It deletes an application in an environment template.
:param env_template_id: The environment template ID.
:param app_id: the application ID to be deleted.
"""
return self._delete('/v1/templates/{id}/services/{app_id}'.
format(id=env_template_id, service_id=app_id))