Merge "Add container-update command"

This commit is contained in:
Jenkins 2017-01-23 03:49:19 +00:00 committed by Gerrit Code Review
commit d426faa8c7
4 changed files with 35 additions and 40 deletions

View File

@ -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

View File

@ -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):

View File

@ -155,3 +155,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)

View File

@ -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):
@ -404,3 +405,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='<container>',
help="ID or name of the container to udate.")
@utils.arg('--cpu',
metavar='<cpu>',
help='The number of virtual cpus.')
@utils.arg('-m', '--memory',
metavar='<memory>',
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)