Add container-update command
Co-Authored-By: Hongbin Lu <hongbin.lu@huawei.com> Depends-On: Id9356c88f995fad6aed33bc21681ee58b2da8ac1 Change-Id: I49fc2a06b003604f74c7e33ab467dadad0ae1fc7
This commit is contained in:
parent
edb07a1be9
commit
b9382727a2
@ -62,21 +62,11 @@ def split_and_deserialize(string):
|
|||||||
return (key, value)
|
return (key, value)
|
||||||
|
|
||||||
|
|
||||||
def args_array_to_patch(op, attributes):
|
def args_array_to_patch(attributes):
|
||||||
patch = []
|
patch = []
|
||||||
for attr in attributes:
|
for attr in attributes:
|
||||||
# Sanitize
|
path, value = split_and_deserialize(attr)
|
||||||
if not attr.startswith('/'):
|
patch.append({path: value})
|
||||||
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)
|
|
||||||
return patch
|
return patch
|
||||||
|
|
||||||
|
|
||||||
|
@ -75,34 +75,13 @@ class ArgsArrayToPatchTest(test_utils.BaseTestCase):
|
|||||||
my_args = {
|
my_args = {
|
||||||
'attributes': ['str=foo', 'int=1', 'bool=true',
|
'attributes': ['str=foo', 'int=1', 'bool=true',
|
||||||
'list=[1, 2, 3]', 'dict={"foo": "bar"}'],
|
'list=[1, 2, 3]', 'dict={"foo": "bar"}'],
|
||||||
'op': 'add',
|
|
||||||
}
|
}
|
||||||
patch = utils.args_array_to_patch(my_args['op'],
|
patch = utils.args_array_to_patch(my_args['attributes'])
|
||||||
my_args['attributes'])
|
self.assertEqual([{'str': 'foo'},
|
||||||
self.assertEqual([{'op': 'add', 'value': 'foo', 'path': '/str'},
|
{'int': 1},
|
||||||
{'op': 'add', 'value': 1, 'path': '/int'},
|
{'bool': True},
|
||||||
{'op': 'add', 'value': True, 'path': '/bool'},
|
{'list': [1, 2, 3]},
|
||||||
{'op': 'add', 'value': [1, 2, 3], 'path': '/list'},
|
{'dict': {"foo": "bar"}}], patch)
|
||||||
{'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)
|
|
||||||
|
|
||||||
|
|
||||||
class FormatArgsTest(test_utils.BaseTestCase):
|
class FormatArgsTest(test_utils.BaseTestCase):
|
||||||
|
@ -152,3 +152,6 @@ class ContainerManager(base.Manager):
|
|||||||
def rename(self, id, name):
|
def rename(self, id, name):
|
||||||
return self._action(id, '/rename',
|
return self._action(id, '/rename',
|
||||||
qparams={'name': name})
|
qparams={'name': name})
|
||||||
|
|
||||||
|
def update(self, id, **patch):
|
||||||
|
return self._update(self._path(id), patch)
|
||||||
|
@ -16,6 +16,7 @@ import json
|
|||||||
|
|
||||||
from zunclient.common import cliutils as utils
|
from zunclient.common import cliutils as utils
|
||||||
from zunclient.common import utils as zun_utils
|
from zunclient.common import utils as zun_utils
|
||||||
|
from zunclient import exceptions as exc
|
||||||
|
|
||||||
|
|
||||||
def _show_container(container):
|
def _show_container(container):
|
||||||
@ -346,3 +347,25 @@ def do_run(cs, args):
|
|||||||
def do_rename(cs, args):
|
def do_rename(cs, args):
|
||||||
"""Rename a container."""
|
"""Rename a container."""
|
||||||
cs.containers.rename(args.container, args.name)
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user