Add API(v2) to upload pod file 55/37455/2
authorchenjiankun <chenjiankun1@huawei.com>
Fri, 14 Jul 2017 03:27:43 +0000 (03:27 +0000)
committerchenjiankun <chenjiankun1@huawei.com>
Fri, 14 Jul 2017 05:08:08 +0000 (05:08 +0000)
JIRA: YARDSTICK-723

API: /api/v2/yardstick/pods/action
METHOD: POST
PARAMS:
{
    'action': 'upload_pod_file',
    'args': {
        'file': file,
        'environment_id': environment_id
    }
}

Change-Id: I5eb065d8b46080a94c989ec9b8217dc54900bd06
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
api/resources/v2/pods.py [new file with mode: 0644]
api/urls.py

diff --git a/api/resources/v2/pods.py b/api/resources/v2/pods.py
new file mode 100644 (file)
index 0000000..f5cdc3b
--- /dev/null
@@ -0,0 +1,60 @@
+import uuid
+import yaml
+import logging
+
+from oslo_serialization import jsonutils
+
+from api import ApiResource
+from api.database.v2.handlers import V2PodHandler
+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
+
+LOG = logging.getLogger(__name__)
+LOG.setLevel(logging.DEBUG)
+
+
+class V2Pods(ApiResource):
+
+    def post(self):
+        return self._dispatch_post()
+
+    def upload_pod_file(self, args):
+        try:
+            upload_file = args['file']
+        except KeyError:
+            return result_handler(consts.API_ERROR, 'file must be provided')
+
+        try:
+            environment_id = args['environment_id']
+        except KeyError:
+            return result_handler(consts.API_ERROR, 'environment_id must be provided')
+
+        try:
+            uuid.UUID(environment_id)
+        except ValueError:
+            return result_handler(consts.API_ERROR, 'invalid environment id')
+
+        LOG.info('writing pod file: %s', consts.POD_FILE)
+        upload_file.save(consts.POD_FILE)
+
+        with open(consts.POD_FILE) as f:
+            data = yaml.safe_load(TaskTemplate.render(f.read()))
+        LOG.debug('pod content is: %s', data)
+
+        LOG.info('create pod in database')
+        pod_id = str(uuid.uuid4())
+        pod_handler = V2PodHandler()
+        pod_init_data = {
+            'uuid': pod_id,
+            'environment_id': environment_id,
+            'content': jsonutils.dumps(data)
+        }
+        pod_handler.insert(pod_init_data)
+
+        LOG.info('update pod in environment')
+        environment_handler = V2EnvironmentHandler()
+        environment_handler.update_attr(environment_id, {'pod_id': pod_id})
+
+        return result_handler(consts.API_SUCCESS, {'uuid': pod_id, 'pod': data})
index 7fe64b2..be453a6 100644 (file)
@@ -28,4 +28,6 @@ urlpatterns = [
 
     Url('/api/v2/yardstick/environments/openrcs/action', 'v2_openrcs'),
     Url('/api/v2/yardstick/environments/openrcs/<openrc_id>', 'v2_openrc'),
+
+    Url('/api/v2/yardstick/pods/action', 'v2_pods'),
 ]