Leverage on Xtesting
[functest.git] / functest / api / resources / v1 / creds.py
index 45e4559..ce83edd 100644 (file)
@@ -13,27 +13,40 @@ Resources to handle openstack related requests
 
 import collections
 import logging
+import socket
 
 from flask import jsonify
+from flasgger.utils import swag_from
+import pkg_resources
+from xtesting.ci import run_tests
 
 from functest.api.base import ApiResource
 from functest.api.common import api_utils
 from functest.cli.commands.cli_os import OpenStack
-from functest.utils import openstack_utils as os_utils
-from functest.utils.constants import CONST
+from functest.utils import constants
 
 LOGGER = logging.getLogger(__name__)
 
+ADDRESS = socket.gethostbyname(socket.gethostname())
+ENDPOINT_CREDS = ('http://{}:5000/api/v1/functest/openstack'.format(ADDRESS))
+
 
 class V1Creds(ApiResource):
     """ V1Creds Resource class"""
 
+    @swag_from(
+        pkg_resources.resource_filename('functest', 'api/swagger/creds.yaml'),
+        endpoint='{0}/credentials'.format(ENDPOINT_CREDS))
     def get(self):  # pylint: disable=no-self-use
         """ Get credentials """
-        os_utils.source_credentials(CONST.__getattribute__('openstack_creds'))
+        run_tests.Runner.source_envfile(constants.ENV_FILE)
         credentials_show = OpenStack.show_credentials()
         return jsonify(credentials_show)
 
+    @swag_from(
+        pkg_resources.resource_filename('functest',
+                                        'api/swagger/creds_action.yaml'),
+        endpoint='{0}/action'.format(ENDPOINT_CREDS))
     def post(self):
         """ Used to handle post request """
         return self._dispatch_post()
@@ -52,13 +65,13 @@ class V1Creds(ApiResource):
 
         lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()]
 
-        rc_file = CONST.__getattribute__('openstack_creds')
+        rc_file = constants.ENV_FILE
         with open(rc_file, 'w') as creds_file:
             creds_file.writelines(lines)
 
         LOGGER.info("Sourcing the OpenStack RC file...")
         try:
-            os_utils.source_credentials(rc_file)
+            run_tests.Runner.source_envfile(rc_file)
         except Exception as err:  # pylint: disable=broad-except
             LOGGER.exception('Failed to source the OpenStack RC file')
             return api_utils.result_handler(status=0, data=str(err))