X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=api%2Fresources%2Fv2%2Fenvironments.py;h=158e98be763ddd8a6a9549c8d53c251768ba03b7;hb=efb4f088f14aee394599bea21973f82f1867c4fe;hp=4b27c45b852f5a763053a9390f9f267d63f10973;hpb=6acd5d156ba25d1eed49763fb220a117a43d5ef8;p=yardstick.git diff --git a/api/resources/v2/environments.py b/api/resources/v2/environments.py index 4b27c45b8..158e98be7 100644 --- a/api/resources/v2/environments.py +++ b/api/resources/v2/environments.py @@ -1,10 +1,22 @@ +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## 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 @@ -23,6 +35,9 @@ class V2Environments(ApiResource): container_info = e['container_id'] e['container_id'] = jsonutils.loads(container_info) if container_info else {} + image_id = e['image_id'] + e['image_id'] = image_id.split(',') if image_id else [] + data = { 'environments': environments } @@ -49,3 +64,70 @@ class V2Environments(ApiResource): environment_handler.insert(env_init_data) return result_handler(consts.API_SUCCESS, {'uuid': env_id}) + + +class V2Environment(ApiResource): + + def get(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') + + environment = change_obj_to_dict(environment) + + container_id = environment['container_id'] + environment['container_id'] = jsonutils.loads(container_id) if container_id else {} + + image_id = environment['image_id'] + environment['image_id'] = image_id.split(',') if image_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})