Merge "Enable vnf/tg instantiate as blocking call."
[yardstick.git] / api / resources / v2 / pods.py
index ffb8a60..d98238c 100644 (file)
@@ -1,3 +1,11 @@
+##############################################################################
+# 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 yaml
 import logging
@@ -10,6 +18,7 @@ from api.database.v2.handlers import V2EnvironmentHandler
 from yardstick.common import constants as consts
 from yardstick.common.utils import result_handler
 from yardstick.common.task_template import TaskTemplate
+from yardstick.common.yaml_loader import yaml_load
 
 LOG = logging.getLogger(__name__)
 LOG.setLevel(logging.DEBUG)
@@ -40,7 +49,7 @@ class V2Pods(ApiResource):
         upload_file.save(consts.POD_FILE)
 
         with open(consts.POD_FILE) as f:
-            data = yaml.safe_load(TaskTemplate.render(f.read()))
+            data = yaml_load(TaskTemplate.render(f.read()))
         LOG.debug('pod content is: %s', data)
 
         LOG.info('create pod in database')
@@ -77,3 +86,24 @@ class V2Pod(ApiResource):
         content = jsonutils.loads(pod.content)
 
         return result_handler(consts.API_SUCCESS, {'pod': content})
+
+    def delete(self, pod_id):
+        try:
+            uuid.UUID(pod_id)
+        except ValueError:
+            return result_handler(consts.API_ERROR, 'invalid pod id')
+
+        pod_handler = V2PodHandler()
+        try:
+            pod = pod_handler.get_by_uuid(pod_id)
+        except ValueError:
+            return result_handler(consts.API_ERROR, 'no such pod')
+
+        LOG.info('update pod in environment')
+        environment_handler = V2EnvironmentHandler()
+        environment_handler.update_attr(pod.environment_id, {'pod_id': None})
+
+        LOG.info('delete pod in database')
+        pod_handler.delete_by_uuid(pod_id)
+
+        return result_handler(consts.API_SUCCESS, {'pod': pod_id})