Enable to rename container in update action

This patch enables to rename container in update action.

Change-Id: I2c58d65355965f512ff89ba0b689d735b7f36522
This commit is contained in:
Shu Muto 2017-06-14 17:26:03 +09:00
parent 6ab8f75e59
commit f42c24e1e8
4 changed files with 46 additions and 8 deletions

View File

@ -50,6 +50,10 @@ def _cleanup_params(attrs, check, **params):
for (key, value) in params.items():
if key == "run":
run = value
elif key == "cpu":
args["cpu"] = float(value)
elif key == "memory":
args["memory"] = int(value)
elif key == "interactive":
args["interactive"] = value
elif key == "restart_policy":
@ -72,6 +76,19 @@ def _cleanup_params(attrs, check, **params):
return args, run
def _delete_attributes_with_same_value(old, new):
'''Delete attributes with same value from new dict
If new dict has same value in old dict, remove the attributes
from new dict.
'''
for k in old.keys():
if k in new:
if old[k] == new[k]:
del new[k]
return new
def container_create(request, **kwargs):
args, run = _cleanup_params(CONTAINER_CREATE_ATTRS, True, **kwargs)
response = None
@ -83,8 +100,32 @@ def container_create(request, **kwargs):
def container_update(request, id, **kwargs):
'''Update Container
Get current Container attributes and check updates.
And update with "rename" for "name", then use "update" for
"cpu" and "memory".
'''
# get current data
container = zunclient(request).containers.get(id).to_dict()
if container["memory"] is not None:
container["memory"] = int(container["memory"].replace("M", ""))
args, run = _cleanup_params(CONTAINER_CREATE_ATTRS, True, **kwargs)
return zunclient(request).containers.update(id, **args)
# remove same values from new params
_delete_attributes_with_same_value(container, args)
# do rename
name = args.pop("name", None)
if len(args):
zunclient(request).containers.update(id, **args)
# do update
if name:
zunclient(request).containers.rename(id, name)
args["name"] = name
return args
def container_delete(request, id, force=False):

View File

@ -52,10 +52,8 @@ class Container(generic.View):
Returns the Container object on success.
"""
container = client.container_update(request, id, **request.DATA)
return rest_utils.CreatedResponse(
'/api/zun/containers/%s' % id,
container.to_dict())
args = client.container_update(request, id, **request.DATA)
return args
@urls.register

View File

@ -121,7 +121,7 @@
// only "cpu" and "memory" fields are editable.
for (var key in model) {
if (model.hasOwnProperty(key) && model[key] === null || model[key] === "" ||
(key !== "cpu" && key !== "memory")) {
(key !== "name" && key !== "cpu" && key !== "memory")) {
delete model[key];
}
}

View File

@ -135,8 +135,7 @@
items: [
{
key: "name",
placeholder: gettext("Name of the container to create."),
readonly: action === "update"
placeholder: gettext("Name of the container to create.")
},
{
key: "image",