From b9382727a26554e4fdd64e6ab49ffa7ff00e26f8 Mon Sep 17 00:00:00 2001 From: Madhuri Kumari Date: Tue, 27 Dec 2016 05:43:46 +0000 Subject: [PATCH] Add container-update command Co-Authored-By: Hongbin Lu Depends-On: Id9356c88f995fad6aed33bc21681ee58b2da8ac1 Change-Id: I49fc2a06b003604f74c7e33ab467dadad0ae1fc7 --- zunclient/common/utils.py | 16 +++-------- zunclient/tests/unit/common/test_utils.py | 33 +++++------------------ zunclient/v1/containers.py | 3 +++ zunclient/v1/containers_shell.py | 23 ++++++++++++++++ 4 files changed, 35 insertions(+), 40 deletions(-) diff --git a/zunclient/common/utils.py b/zunclient/common/utils.py index 1c2fbc38..6941de5d 100644 --- a/zunclient/common/utils.py +++ b/zunclient/common/utils.py @@ -62,21 +62,11 @@ def split_and_deserialize(string): return (key, value) -def args_array_to_patch(op, attributes): +def args_array_to_patch(attributes): patch = [] for attr in attributes: - # Sanitize - if not attr.startswith('/'): - attr = '/' + attr - if op in ['add', 'replace']: - path, value = split_and_deserialize(attr) - patch.append({'op': op, 'path': path, 'value': value}) - - elif op == "remove": - # For remove only the key is needed - patch.append({'op': op, 'path': attr}) - else: - raise exc.CommandError(_('Unknown PATCH operation: %s') % op) + path, value = split_and_deserialize(attr) + patch.append({path: value}) return patch diff --git a/zunclient/tests/unit/common/test_utils.py b/zunclient/tests/unit/common/test_utils.py index 94aa0dc3..e2c93925 100644 --- a/zunclient/tests/unit/common/test_utils.py +++ b/zunclient/tests/unit/common/test_utils.py @@ -75,34 +75,13 @@ class ArgsArrayToPatchTest(test_utils.BaseTestCase): my_args = { 'attributes': ['str=foo', 'int=1', 'bool=true', 'list=[1, 2, 3]', 'dict={"foo": "bar"}'], - 'op': 'add', } - patch = utils.args_array_to_patch(my_args['op'], - my_args['attributes']) - self.assertEqual([{'op': 'add', 'value': 'foo', 'path': '/str'}, - {'op': 'add', 'value': 1, 'path': '/int'}, - {'op': 'add', 'value': True, 'path': '/bool'}, - {'op': 'add', 'value': [1, 2, 3], 'path': '/list'}, - {'op': 'add', 'value': {"foo": "bar"}, - 'path': '/dict'}], patch) - - def test_args_array_to_patch_format_error(self): - my_args = { - 'attributes': ['foobar'], - 'op': 'add', - } - self.assertRaises(exc.CommandError, utils.args_array_to_patch, - my_args['op'], my_args['attributes']) - - def test_args_array_to_patch_remove(self): - my_args = { - 'attributes': ['/foo', 'extra/bar'], - 'op': 'remove', - } - patch = utils.args_array_to_patch(my_args['op'], - my_args['attributes']) - self.assertEqual([{'op': 'remove', 'path': '/foo'}, - {'op': 'remove', 'path': '/extra/bar'}], patch) + patch = utils.args_array_to_patch(my_args['attributes']) + self.assertEqual([{'str': 'foo'}, + {'int': 1}, + {'bool': True}, + {'list': [1, 2, 3]}, + {'dict': {"foo": "bar"}}], patch) class FormatArgsTest(test_utils.BaseTestCase): diff --git a/zunclient/v1/containers.py b/zunclient/v1/containers.py index 1ed5bbf8..06f26b76 100644 --- a/zunclient/v1/containers.py +++ b/zunclient/v1/containers.py @@ -152,3 +152,6 @@ class ContainerManager(base.Manager): def rename(self, id, name): return self._action(id, '/rename', qparams={'name': name}) + + def update(self, id, **patch): + return self._update(self._path(id), patch) diff --git a/zunclient/v1/containers_shell.py b/zunclient/v1/containers_shell.py index ca935099..33d4ec40 100644 --- a/zunclient/v1/containers_shell.py +++ b/zunclient/v1/containers_shell.py @@ -16,6 +16,7 @@ import json from zunclient.common import cliutils as utils from zunclient.common import utils as zun_utils +from zunclient import exceptions as exc def _show_container(container): @@ -346,3 +347,25 @@ def do_run(cs, args): def do_rename(cs, args): """Rename a container.""" cs.containers.rename(args.container, args.name) + + +@utils.arg('container', + metavar='', + help="ID or name of the container to udate.") +@utils.arg('--cpu', + metavar='', + help='The number of virtual cpus.') +@utils.arg('-m', '--memory', + metavar='', + help='The container memory size in MiB') +def do_update(cs, args): + """Updates one or more container attributes""" + opts = {} + if args.memory is not None: + opts['memory'] = args.memory + if args.cpu is not None: + opts['cpu'] = args.cpu + if not opts: + raise exc.CommandError("You must update at least one property") + container = cs.containers.update(args.container, **opts) + _show_container(container)