aboutsummaryrefslogtreecommitdiffstats
path: root/api/resources/v2/containers.py
diff options
context:
space:
mode:
authorchenjiankun <chenjiankun1@huawei.com>2017-07-17 08:15:57 +0000
committerchenjiankun <chenjiankun1@huawei.com>2017-07-17 08:15:57 +0000
commitcf0f0e355fe3b242ba33638958ce431180010e41 (patch)
tree014f3829838f9674ed5c6a079b9f3ea4e081519e /api/resources/v2/containers.py
parent41742db0c3528f86b93cff532e5f0b571b607fc2 (diff)
Add API(v2) to delete container
JIRA: YARDSTICK-729 API: /api/v2/yardstick/containers/<container_id> METHOD: DELETE Change-Id: I4cda76329edd7b988a357f1fcb2b5041bf48be64 Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
Diffstat (limited to 'api/resources/v2/containers.py')
-rw-r--r--api/resources/v2/containers.py40
1 files changed, 38 insertions, 2 deletions
diff --git a/api/resources/v2/containers.py b/api/resources/v2/containers.py
index 556477df7..ce7130376 100644
--- a/api/resources/v2/containers.py
+++ b/api/resources/v2/containers.py
@@ -326,12 +326,12 @@ class V2Container(ApiResource):
try:
uuid.UUID(container_id)
except ValueError:
- result_handler(consts.API_ERROR, 'invalid container id')
+ return result_handler(consts.API_ERROR, 'invalid container id')
try:
container = container_handler.get_by_uuid(container_id)
except ValueError:
- result_handler(consts.API_ERROR, 'no such container id')
+ return result_handler(consts.API_ERROR, 'no such container id')
name = container.name
client = Client(base_url=consts.DOCKER_URL)
@@ -345,3 +345,39 @@ class V2Container(ApiResource):
}
return result_handler(consts.API_SUCCESS, {'container': data})
+
+ def delete(self, container_id):
+ try:
+ uuid.UUID(container_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'invalid container id')
+
+ try:
+ container = container_handler.get_by_uuid(container_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'no such container id')
+
+ environment_id = container.environment_id
+
+ client = Client(base_url=consts.DOCKER_URL)
+ LOG.info('delete container: %s', container.name)
+ try:
+ client.remove_container(container.name, force=True)
+ except Exception:
+ LOG.exception('delete container failed')
+ return result_handler(consts.API_ERROR, 'delete container failed')
+
+ LOG.info('delete container in database')
+ container_handler.delete_by_uuid(container_id)
+
+ LOG.info('update container in environment')
+ environment = environment_handler.get_by_uuid(environment_id)
+ container_info = jsonutils.loads(environment.container_id)
+ key = next((k for k, v in container_info.items() if v == container_id))
+ container_info.pop(key)
+ environment_delete_data = {
+ 'container_id': jsonutils.dumps(container_info)
+ }
+ environment_handler.update_attr(environment_id, environment_delete_data)
+
+ return result_handler(consts.API_SUCCESS, {'container': container_id})