Add API(v2) to update openrc 43/37443/2
authorchenjiankun <chenjiankun1@huawei.com>
Fri, 14 Jul 2017 01:01:19 +0000 (01:01 +0000)
committerchenjiankun <chenjiankun1@huawei.com>
Fri, 14 Jul 2017 04:22:03 +0000 (04:22 +0000)
JIRA: YARDSTICK-720

API: /api/v2/yardstick/environments/openrcs/action
METHOD: POST
PARAMS:
{
    'action': 'update_openrc',
        'args': {
            'openrc': {
                "EXTERNAL_NETWORK": "ext-net",
                "OS_AUTH_URL": "http://192.168.23.51:5000/v3",
                "OS_IDENTITY_API_VERSION": "3",
                "OS_IMAGE_API_VERSION": "2",
                "OS_PASSWORD": "console",
                "OS_PROJECT_DOMAIN_NAME": "default",
                "OS_PROJECT_NAME": "admin",
                "OS_TENANT_NAME": "admin",
                "OS_USERNAME": "admin",
                "OS_USER_DOMAIN_NAME": "default"
            },
            'environment_id': environment_id
        }
}

Change-Id: Ie9a1614190a01456fd0896f0bdfd05f9d0724fc6
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
api/resources/v2/openrcs.py

index b78c696..cbe8946 100644 (file)
@@ -59,6 +59,47 @@ class V2Openrc(ApiResource):
 
         return result_handler(consts.API_SUCCESS, {'openrc': openrc_data, 'uuid': openrc_id})
 
+    def update_openrc(self, args):
+        try:
+            openrc_vars = args['openrc']
+        except KeyError:
+            return result_handler(consts.API_ERROR, 'openrc must be provided')
+
+        try:
+            environment_id = args['environment_id']
+        except KeyError:
+            return result_handler(consts.API_ERROR, 'environment_id must be provided')
+
+        LOG.info('writing openrc: %s', consts.OPENRC)
+        makedirs(consts.CONF_DIR)
+
+        lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()]
+        LOG.debug('writing: %s', ''.join(lines))
+        with open(consts.OPENRC, 'w') as f:
+            f.writelines(lines)
+        LOG.info('writing openrc: Done')
+
+        LOG.info('source openrc: %s', consts.OPENRC)
+        try:
+            source_env(consts.OPENRC)
+        except Exception:
+            LOG.exception('source openrc failed')
+            return result_handler(consts.API_ERROR, 'source openrc failed')
+        LOG.info('source openrc: Done')
+
+        openrc_id = str(uuid.uuid4())
+        self._write_into_database(environment_id, openrc_id, openrc_vars)
+
+        LOG.info('writing ansible cloud conf')
+        try:
+            self._generate_ansible_conf_file(openrc_vars)
+        except Exception:
+            LOG.exception('write cloud conf failed')
+            return result_handler(consts.API_ERROR, 'genarate ansible conf failed')
+        LOG.info('finish writing ansible cloud conf')
+
+        return result_handler(consts.API_SUCCESS, {'openrc': openrc_vars, 'uuid': openrc_id})
+
     def _write_into_database(self, environment_id, openrc_id, openrc_data):
         LOG.info('writing openrc to database')
         openrc_handler = V2OpenrcHandler()