diff options
author | chenjiankun <chenjiankun1@huawei.com> | 2017-07-13 08:20:10 +0000 |
---|---|---|
committer | chenjiankun <chenjiankun1@huawei.com> | 2017-07-13 08:20:10 +0000 |
commit | 28443ae43c2896efd287cea8c2e699b09010ab4b (patch) | |
tree | 58d997938766cbaaa53e46d4aaf55c4bcaf53a21 | |
parent | 2b2cfebd2383e38fc375eb308c3f96ef20d75881 (diff) |
Add API to delete single environment
JIRA: YARDSTICK-718
API: /api/v2/yardstick/environments/<environment_id>
METHOD: DELETE
Change-Id: I2e96b0f786440dea46f6b851d3f10f6cc5a0ab40
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
-rw-r--r-- | api/resources/v2/environments.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/api/resources/v2/environments.py b/api/resources/v2/environments.py index f83c0a941..e4679b0d6 100644 --- a/api/resources/v2/environments.py +++ b/api/resources/v2/environments.py @@ -2,9 +2,13 @@ import uuid import logging from oslo_serialization import jsonutils +from docker import Client from api import ApiResource from api.database.v2.handlers import V2EnvironmentHandler +from api.database.v2.handlers import V2OpenrcHandler +from api.database.v2.handlers import V2PodHandler +from api.database.v2.handlers import V2ContainerHandler from yardstick.common.utils import result_handler from yardstick.common.utils import change_obj_to_dict from yardstick.common import constants as consts @@ -69,3 +73,45 @@ class V2Environment(ApiResource): container_id = environment['container_id'] environment['container_id'] = jsonutils.loads(container_id) if container_id else {} return result_handler(consts.API_SUCCESS, {'environment': environment}) + + def delete(self, environment_id): + try: + uuid.UUID(environment_id) + except ValueError: + return result_handler(consts.API_ERROR, 'invalid environment id') + + environment_handler = V2EnvironmentHandler() + try: + environment = environment_handler.get_by_uuid(environment_id) + except ValueError: + return result_handler(consts.API_ERROR, 'no such environment id') + + if environment.openrc_id: + LOG.info('delete openrc: %s', environment.openrc_id) + openrc_handler = V2OpenrcHandler() + openrc_handler.delete_by_uuid(environment.openrc_id) + + if environment.pod_id: + LOG.info('delete pod: %s', environment.pod_id) + pod_handler = V2PodHandler() + pod_handler.delete_by_uuid(environment.pod_id) + + if environment.container_id: + LOG.info('delete containers') + container_info = jsonutils.loads(environment.container_id) + + container_handler = V2ContainerHandler() + client = Client(base_url=consts.DOCKER_URL) + for k, v in container_info.items(): + LOG.info('start delete: %s', k) + container = container_handler.get_by_uuid(v) + LOG.debug('container name: %s', container.name) + try: + client.remove_container(container.name, force=True) + except Exception: + LOG.exception('remove container failed') + container_handler.delete_by_uuid(v) + + environment_handler.delete_by_uuid(environment_id) + + return result_handler(consts.API_SUCCESS, {'environment': environment_id}) |