aboutsummaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/__init__.py1
-rw-r--r--api/database/v2/handlers.py3
-rw-r--r--api/resources/v2/__init__.py0
-rw-r--r--api/resources/v2/environments.py117
-rw-r--r--api/resources/v2/openrcs.py211
-rw-r--r--api/resources/v2/pods.py79
-rw-r--r--api/server.py13
-rw-r--r--api/urls.py35
8 files changed, 453 insertions, 6 deletions
diff --git a/api/__init__.py b/api/__init__.py
index 4622802df..3195c971b 100644
--- a/api/__init__.py
+++ b/api/__init__.py
@@ -34,6 +34,7 @@ class ApiResource(Resource):
except KeyError:
pass
+ args.update({k: v for k, v in request.form.items()})
LOG.debug('Input args is: action: %s, args: %s', action, args)
return action, args
diff --git a/api/database/v2/handlers.py b/api/database/v2/handlers.py
index eb732817d..095ad724c 100644
--- a/api/database/v2/handlers.py
+++ b/api/database/v2/handlers.py
@@ -24,6 +24,9 @@ class V2EnvironmentHandler(object):
db_session.commit()
return environment
+ def list_all(self):
+ return V2Environment.query.all()
+
def get_by_uuid(self, uuid):
environment = V2Environment.query.filter_by(uuid=uuid).first()
if not environment:
diff --git a/api/resources/v2/__init__.py b/api/resources/v2/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/api/resources/v2/__init__.py
diff --git a/api/resources/v2/environments.py b/api/resources/v2/environments.py
new file mode 100644
index 000000000..e4679b0d6
--- /dev/null
+++ b/api/resources/v2/environments.py
@@ -0,0 +1,117 @@
+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
+
+LOG = logging.getLogger(__name__)
+LOG.setLevel(logging.DEBUG)
+
+
+class V2Environments(ApiResource):
+
+ def get(self):
+ environment_handler = V2EnvironmentHandler()
+ environments = [change_obj_to_dict(e) for e in environment_handler.list_all()]
+
+ for e in environments:
+ container_info = e['container_id']
+ e['container_id'] = jsonutils.loads(container_info) if container_info else {}
+
+ data = {
+ 'environments': environments
+ }
+
+ return result_handler(consts.API_SUCCESS, data)
+
+ def post(self):
+ return self._dispatch_post()
+
+ def create_environment(self, args):
+ try:
+ name = args['name']
+ except KeyError:
+ return result_handler(consts.API_ERROR, 'name must be provided')
+
+ env_id = str(uuid.uuid4())
+
+ environment_handler = V2EnvironmentHandler()
+
+ env_init_data = {
+ 'name': name,
+ 'uuid': env_id
+ }
+ 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 {}
+ 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})
diff --git a/api/resources/v2/openrcs.py b/api/resources/v2/openrcs.py
new file mode 100644
index 000000000..5f3b9382f
--- /dev/null
+++ b/api/resources/v2/openrcs.py
@@ -0,0 +1,211 @@
+import uuid
+import logging
+import re
+import os
+
+import yaml
+from oslo_serialization import jsonutils
+
+from api import ApiResource
+from api.database.v2.handlers import V2OpenrcHandler
+from api.database.v2.handlers import V2EnvironmentHandler
+from yardstick.common import constants as consts
+from yardstick.common.utils import result_handler
+from yardstick.common.utils import makedirs
+from yardstick.common.utils import source_env
+
+LOG = logging.getLogger(__name__)
+LOG.setLevel(logging.DEBUG)
+
+
+class V2Openrcs(ApiResource):
+
+ def post(self):
+ return self._dispatch_post()
+
+ def upload_openrc(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 openrc: %s', consts.OPENRC)
+ makedirs(consts.CONF_DIR)
+ upload_file.save(consts.OPENRC)
+ source_env(consts.OPENRC)
+
+ LOG.info('parsing openrc')
+ try:
+ openrc_data = self._get_openrc_dict()
+ except Exception:
+ LOG.exception('parse openrc failed')
+ return result_handler(consts.API_ERROR, 'parse openrc failed')
+
+ openrc_id = str(uuid.uuid4())
+ self._write_into_database(environment_id, openrc_id, openrc_data)
+
+ LOG.info('writing ansible cloud conf')
+ try:
+ self._generate_ansible_conf_file(openrc_data)
+ 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_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')
+
+ try:
+ uuid.UUID(environment_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'invalid environment id')
+
+ 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()
+ openrc_init_data = {
+ 'uuid': openrc_id,
+ 'environment_id': environment_id,
+ 'content': jsonutils.dumps(openrc_data)
+ }
+ openrc_handler.insert(openrc_init_data)
+
+ LOG.info('binding openrc to environment: %s', environment_id)
+ environment_handler = V2EnvironmentHandler()
+ environment_handler.update_attr(environment_id, {'openrc_id': openrc_id})
+
+ def _get_openrc_dict(self):
+ with open(consts.OPENRC) as f:
+ content = f.readlines()
+
+ result = {}
+ for line in content:
+ m = re.search(r'(\ .*)=(.*)', line)
+ if m:
+ try:
+ value = os.environ[m.group(1).strip()]
+ except KeyError:
+ pass
+ else:
+ result.update({m.group(1).strip(): value})
+
+ return result
+
+ def _generate_ansible_conf_file(self, openrc_data):
+ ansible_conf = {
+ 'clouds': {
+ 'opnfv': {
+ 'auth': {
+ }
+ }
+ }
+ }
+ black_list = ['OS_IDENTITY_API_VERSION', 'OS_IMAGE_API_VERSION']
+
+ for k, v in openrc_data.items():
+ if k.startswith('OS') and k not in black_list:
+ key = k[3:].lower()
+ ansible_conf['clouds']['opnfv']['auth'][key] = v
+
+ try:
+ value = openrc_data['OS_IDENTITY_API_VERSION']
+ except KeyError:
+ pass
+ else:
+ ansible_conf['clouds']['opnfv']['identity_api_version'] = value
+
+ makedirs(consts.OPENSTACK_CONF_DIR)
+ with open(consts.CLOUDS_CONF, 'w') as f:
+ yaml.dump(ansible_conf, f, default_flow_style=False)
+
+
+class V2Openrc(ApiResource):
+
+ def get(self, openrc_id):
+ try:
+ uuid.UUID(openrc_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'invalid openrc id')
+
+ LOG.info('Geting openrc: %s', openrc_id)
+ openrc_handler = V2OpenrcHandler()
+ try:
+ openrc = openrc_handler.get_by_uuid(openrc_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'no such openrc id')
+
+ LOG.info('load openrc content')
+ content = jsonutils.loads(openrc.content)
+
+ return result_handler(consts.API_ERROR, {'openrc': content})
+
+ def delete(self, openrc_id):
+ try:
+ uuid.UUID(openrc_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'invalid openrc id')
+
+ LOG.info('Geting openrc: %s', openrc_id)
+ openrc_handler = V2OpenrcHandler()
+ try:
+ openrc = openrc_handler.get_by_uuid(openrc_id)
+ except ValueError:
+ return result_handler(consts.API_ERROR, 'no such openrc id')
+
+ LOG.info('update openrc in environment')
+ environment_handler = V2EnvironmentHandler()
+ environment_handler.update_attr(openrc.environment_id, {'openrc_id': None})
+
+ openrc_handler.delete_by_uuid(openrc_id)
+
+ return result_handler(consts.API_SUCCESS, {'openrc': openrc_id})
diff --git a/api/resources/v2/pods.py b/api/resources/v2/pods.py
new file mode 100644
index 000000000..ffb8a6046
--- /dev/null
+++ b/api/resources/v2/pods.py
@@ -0,0 +1,79 @@
+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})
+
+
+class V2Pod(ApiResource):
+
+ def get(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')
+
+ content = jsonutils.loads(pod.content)
+
+ return result_handler(consts.API_SUCCESS, {'pod': content})
diff --git a/api/server.py b/api/server.py
index c1548ca10..158b8a508 100644
--- a/api/server.py
+++ b/api/server.py
@@ -32,7 +32,7 @@ try:
except ImportError:
from urllib.parse import urljoin
-logger = logging.getLogger(__name__)
+LOG = logging.getLogger(__name__)
app = Flask(__name__)
@@ -62,7 +62,7 @@ def init_db():
return False
subclses = filter(func, inspect.getmembers(models, inspect.isclass))
- logger.debug('Import models: %s', [a[1] for a in subclses])
+ LOG.debug('Import models: %s', [a[1] for a in subclses])
Base.metadata.create_all(bind=engine)
@@ -77,12 +77,15 @@ def get_endpoint(url):
for u in urlpatterns:
- api.add_resource(get_resource(u.target), u.url, endpoint=get_endpoint(u.url))
+ try:
+ api.add_resource(get_resource(u.target), u.url, endpoint=get_endpoint(u.url))
+ except StopIteration:
+ LOG.error('url resource not found: %s', u.url)
if __name__ == '__main__':
_init_logging()
- logger.setLevel(logging.DEBUG)
- logger.info('Starting server')
+ LOG.setLevel(logging.DEBUG)
+ LOG.info('Starting server')
init_db()
app.run(host='0.0.0.0')
diff --git a/api/urls.py b/api/urls.py
index 4b9d4d191..5c7e9f7b5 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -19,5 +19,38 @@ urlpatterns = [
Url('/yardstick/testcases/<case_name>/docs', 'v1_case_docs'),
Url('/yardstick/testsuites/action', 'v1_test_suite'),
Url('/yardstick/results', 'v1_result'),
- Url('/yardstick/env/action', 'v1_env')
+ Url('/yardstick/env/action', 'v1_env'),
+
+ # api v2
+ Url('/api/v2/yardstick/environments', 'v2_environments'),
+ Url('/api/v2/yardstick/environments/action', 'v2_environments'),
+ Url('/api/v2/yardstick/environments/<environment_id>', 'v2_environment'),
+
+ Url('/api/v2/yardstick/openrcs/action', 'v2_openrcs'),
+ Url('/api/v2/yardstick/openrcs/<openrc_id>', 'v2_openrc'),
+
+ Url('/api/v2/yardstick/pods/action', 'v2_pods'),
+ Url('/api/v2/yardstick/pods/<pod_id>', 'v2_pod'),
+
+ Url('/api/v2/yardstick/images', 'v2_images'),
+ Url('/api/v2/yardstick/images/action', 'v2_images'),
+
+ Url('/api/v2/yardstick/containers/action', 'v2_containers'),
+ Url('/api/v2/yardstick/containers/<container_id>', 'v2_container'),
+
+ Url('/api/v2/yardstick/projects', 'v2_projects'),
+ Url('/api/v2/yardstick/projects/action', 'v2_projects'),
+ Url('/api/v2/yardstick/projects/<project_id>', 'v2_project'),
+
+ Url('/api/v2/yardstick/tasks', 'v2_tasks'),
+ Url('/api/v2/yardstick/tasks/action', 'v2_tasks'),
+ Url('/api/v2/yardstick/tasks/<task_id>', 'v2_task'),
+
+ Url('/api/v2/yardstick/testcases', 'v2_testcases'),
+ Url('/api/v2/yardstick/testcases/action', 'v2_testcases'),
+ Url('/api/v2/yardstick/testcases/<case_name>', 'v2_testcase'),
+
+ Url('/api/v2/yardstick/testsuites', 'v2_testsuites'),
+ Url('/api/v2/yardstick/testsuites/action', 'v2_testsuites'),
+ Url('/api/v2/yardstick/testsuites/<suite_name>', 'v2_testsuites')
]