summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-05-15 13:09:06 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-05-18 15:46:40 +0200
commit2281d3265423d01e60f8cc088de5a5730bb8a910 (patch)
tree8dc81338cfd30845717f1b9025176d26c82fe930 /opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId
parent05d2318538eba71ac0555dc5ec146499d9cb0592 (diff)
api: Migrate to Flask Restful
This change updates the API to use Flask Restful instead of our own in-house REST library. This change reduces the maintenance effort and allows us to drastically simplify the API implementation needed for the OpenDC v2 API.
Diffstat (limited to 'opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId')
-rw-r--r--opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/__init__.py0
-rw-r--r--opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/endpoint.py67
-rw-r--r--opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/__init__.py0
-rw-r--r--opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/endpoint.py49
-rw-r--r--opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/test_endpoint.py125
-rw-r--r--opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/test_endpoint.py149
6 files changed, 0 insertions, 390 deletions
diff --git a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/__init__.py b/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/__init__.py
deleted file mode 100644
index e69de29b..00000000
--- a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/__init__.py
+++ /dev/null
diff --git a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/endpoint.py b/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/endpoint.py
deleted file mode 100644
index c856f4ce..00000000
--- a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/endpoint.py
+++ /dev/null
@@ -1,67 +0,0 @@
-from opendc.models.portfolio import Portfolio
-from opendc.models.project import Project
-from opendc.util.rest import Response
-
-
-def GET(request):
- """Get this Portfolio."""
-
- request.check_required_parameters(path={'portfolioId': 'string'})
-
- portfolio = Portfolio.from_id(request.params_path['portfolioId'])
-
- portfolio.check_exists()
- portfolio.check_user_access(request.current_user['sub'], False)
-
- return Response(200, 'Successfully retrieved portfolio.', portfolio.obj)
-
-
-def PUT(request):
- """Update this Portfolio."""
-
- request.check_required_parameters(path={'portfolioId': 'string'}, body={'portfolio': {
- 'name': 'string',
- 'targets': {
- 'enabledMetrics': 'list',
- 'repeatsPerScenario': 'int',
- },
- }})
-
- portfolio = Portfolio.from_id(request.params_path['portfolioId'])
-
- portfolio.check_exists()
- portfolio.check_user_access(request.current_user['sub'], True)
-
- portfolio.set_property('name',
- request.params_body['portfolio']['name'])
- portfolio.set_property('targets.enabledMetrics',
- request.params_body['portfolio']['targets']['enabledMetrics'])
- portfolio.set_property('targets.repeatsPerScenario',
- request.params_body['portfolio']['targets']['repeatsPerScenario'])
-
- portfolio.update()
-
- return Response(200, 'Successfully updated portfolio.', portfolio.obj)
-
-
-def DELETE(request):
- """Delete this Portfolio."""
-
- request.check_required_parameters(path={'portfolioId': 'string'})
-
- portfolio = Portfolio.from_id(request.params_path['portfolioId'])
-
- portfolio.check_exists()
- portfolio.check_user_access(request.current_user['sub'], True)
-
- portfolio_id = portfolio.get_id()
-
- project = Project.from_id(portfolio.obj['projectId'])
- project.check_exists()
- if portfolio_id in project.obj['portfolioIds']:
- project.obj['portfolioIds'].remove(portfolio_id)
- project.update()
-
- old_object = portfolio.delete()
-
- return Response(200, 'Successfully deleted portfolio.', old_object)
diff --git a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/__init__.py b/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/__init__.py
deleted file mode 100644
index e69de29b..00000000
--- a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/__init__.py
+++ /dev/null
diff --git a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/endpoint.py b/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/endpoint.py
deleted file mode 100644
index b12afce3..00000000
--- a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/endpoint.py
+++ /dev/null
@@ -1,49 +0,0 @@
-from opendc.models.portfolio import Portfolio
-from opendc.models.scenario import Scenario
-from opendc.models.topology import Topology
-from opendc.util.rest import Response
-
-
-def POST(request):
- """Add a new Scenario for this Portfolio."""
-
- request.check_required_parameters(path={'portfolioId': 'string'},
- body={
- 'scenario': {
- 'name': 'string',
- 'trace': {
- 'traceId': 'string',
- 'loadSamplingFraction': 'float',
- },
- 'topology': {
- 'topologyId': 'string',
- },
- 'operational': {
- 'failuresEnabled': 'bool',
- 'performanceInterferenceEnabled': 'bool',
- 'schedulerName': 'string',
- },
- }
- })
-
- portfolio = Portfolio.from_id(request.params_path['portfolioId'])
-
- portfolio.check_exists()
- portfolio.check_user_access(request.current_user['sub'], True)
-
- scenario = Scenario(request.params_body['scenario'])
-
- topology = Topology.from_id(scenario.obj['topology']['topologyId'])
- topology.check_exists()
- topology.check_user_access(request.current_user['sub'], True)
-
- scenario.set_property('portfolioId', portfolio.get_id())
- scenario.set_property('simulation', {'state': 'QUEUED'})
- scenario.set_property('topology.topologyId', topology.get_id())
-
- scenario.insert()
-
- portfolio.obj['scenarioIds'].append(scenario.get_id())
- portfolio.update()
-
- return Response(200, 'Successfully added Scenario.', scenario.obj)
diff --git a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/test_endpoint.py b/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/test_endpoint.py
deleted file mode 100644
index ff1666c0..00000000
--- a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/test_endpoint.py
+++ /dev/null
@@ -1,125 +0,0 @@
-from opendc.util.database import DB
-
-test_id = 24 * '1'
-
-
-def test_add_scenario_missing_parameter(client):
- assert '400' in client.post('/v2/portfolios/1/scenarios').status
-
-
-def test_add_scenario_non_existing_portfolio(client, mocker):
- mocker.patch.object(DB, 'fetch_one', return_value=None)
- assert '404' in client.post(f'/v2/portfolios/{test_id}/scenarios',
- json={
- 'scenario': {
- 'name': 'test',
- 'trace': {
- 'traceId': test_id,
- 'loadSamplingFraction': 1.0,
- },
- 'topology': {
- 'topologyId': test_id,
- },
- 'operational': {
- 'failuresEnabled': True,
- 'performanceInterferenceEnabled': False,
- 'schedulerName': 'DEFAULT',
- },
- }
- }).status
-
-
-def test_add_scenario_not_authorized(client, mocker):
- mocker.patch.object(DB,
- 'fetch_one',
- return_value={
- '_id': test_id,
- 'projectId': test_id,
- 'portfolioId': test_id,
- 'authorizations': [{
- 'userId': 'test',
- 'authorizationLevel': 'VIEW'
- }]
- })
- assert '403' in client.post(f'/v2/portfolios/{test_id}/scenarios',
- json={
- 'scenario': {
- 'name': 'test',
- 'trace': {
- 'traceId': test_id,
- 'loadSamplingFraction': 1.0,
- },
- 'topology': {
- 'topologyId': test_id,
- },
- 'operational': {
- 'failuresEnabled': True,
- 'performanceInterferenceEnabled': False,
- 'schedulerName': 'DEFAULT',
- },
- }
- }).status
-
-
-def test_add_scenario(client, mocker):
- mocker.patch.object(DB,
- 'fetch_one',
- return_value={
- '_id': test_id,
- 'projectId': test_id,
- 'portfolioId': test_id,
- 'portfolioIds': [test_id],
- 'scenarioIds': [test_id],
- 'authorizations': [{
- 'userId': 'test',
- 'authorizationLevel': 'EDIT'
- }],
- 'simulation': {
- 'state': 'QUEUED',
- },
- })
- mocker.patch.object(DB,
- 'insert',
- return_value={
- '_id': test_id,
- 'name': 'test',
- 'trace': {
- 'traceId': test_id,
- 'loadSamplingFraction': 1.0,
- },
- 'topology': {
- 'topologyId': test_id,
- },
- 'operational': {
- 'failuresEnabled': True,
- 'performanceInterferenceEnabled': False,
- 'schedulerName': 'DEFAULT',
- },
- 'portfolioId': test_id,
- 'simulationState': {
- 'state': 'QUEUED',
- },
- })
- mocker.patch.object(DB, 'update', return_value=None)
- res = client.post(
- f'/v2/portfolios/{test_id}/scenarios',
- json={
- 'scenario': {
- 'name': 'test',
- 'trace': {
- 'traceId': test_id,
- 'loadSamplingFraction': 1.0,
- },
- 'topology': {
- 'topologyId': test_id,
- },
- 'operational': {
- 'failuresEnabled': True,
- 'performanceInterferenceEnabled': False,
- 'schedulerName': 'DEFAULT',
- },
- }
- })
- assert 'portfolioId' in res.json['content']
- assert 'simulation' in res.json['content']
- assert '200' in res.status
diff --git a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/test_endpoint.py b/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/test_endpoint.py
deleted file mode 100644
index 1a44c63d..00000000
--- a/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/test_endpoint.py
+++ /dev/null
@@ -1,149 +0,0 @@
-from opendc.util.database import DB
-
-test_id = 24 * '1'
-test_id_2 = 24 * '2'
-
-
-def test_get_portfolio_non_existing(client, mocker):
- mocker.patch.object(DB, 'fetch_one', return_value=None)
- assert '404' in client.get(f'/v2/portfolios/{test_id}').status
-
-
-def test_get_portfolio_no_authorizations(client, mocker):
- mocker.patch.object(DB, 'fetch_one', return_value={'projectId': test_id, 'authorizations': []})
- res = client.get(f'/v2/portfolios/{test_id}')
- assert '403' in res.status
-
-
-def test_get_portfolio_not_authorized(client, mocker):
- mocker.patch.object(DB,
- 'fetch_one',
- return_value={
- 'projectId': test_id,
- '_id': test_id,
- 'authorizations': []
- })
- res = client.get(f'/v2/portfolios/{test_id}')
- assert '403' in res.status
-
-
-def test_get_portfolio(client, mocker):
- mocker.patch.object(DB,
- 'fetch_one',
- return_value={
- 'projectId': test_id,
- '_id': test_id,
- 'authorizations': [{
- 'userId': 'test',
- 'authorizationLevel': 'EDIT'
- }]
- })
- res = client.get(f'/v2/portfolios/{test_id}')
- assert '200' in res.status
-
-
-def test_update_portfolio_missing_parameter(client):
- assert '400' in client.put(f'/v2/portfolios/{test_id}').status
-
-
-def test_update_portfolio_non_existing(client, mocker):
- mocker.patch.object(DB, 'fetch_one', return_value=None)
- assert '404' in client.put(f'/v2/portfolios/{test_id}', json={
- 'portfolio': {
- 'name': 'test',
- 'targets': {
- 'enabledMetrics': ['test'],
- 'repeatsPerScenario': 2
- }
- }
- }).status
-
-
-def test_update_portfolio_not_authorized(client, mocker):
- mocker.patch.object(DB,
- 'fetch_one',
- return_value={
- '_id': test_id,
- 'projectId': test_id,
- 'authorizations': [{
- 'userId': 'test',
- 'authorizationLevel': 'VIEW'
- }]
- })
- mocker.patch.object(DB, 'update', return_value={})
- assert '403' in client.put(f'/v2/portfolios/{test_id}', json={
- 'portfolio': {
- 'name': 'test',
- 'targets': {
- 'enabledMetrics': ['test'],
- 'repeatsPerScenario': 2
- }
- }
- }).status
-
-
-def test_update_portfolio(client, mocker):
- mocker.patch.object(DB,
- 'fetch_one',
- return_value={
- '_id': test_id,
- 'projectId': test_id,
- 'authorizations': [{
- 'userId': 'test',
- 'authorizationLevel': 'OWN'
- }],
- 'targets': {
- 'enabledMetrics': [],
- 'repeatsPerScenario': 1
- }
- })
- mocker.patch.object(DB, 'update', return_value={})
-
- res = client.put(f'/v2/portfolios/{test_id}', json={'portfolio': {
- 'name': 'test',
- 'targets': {
- 'enabledMetrics': ['test'],
- 'repeatsPerScenario': 2
- }
- }})
- assert '200' in res.status
-
-
-def test_delete_project_non_existing(client, mocker):
- mocker.patch.object(DB, 'fetch_one', return_value=None)
- assert '404' in client.delete(f'/v2/portfolios/{test_id}').status
-
-
-def test_delete_project_different_user(client, mocker):
- mocker.patch.object(DB,
- 'fetch_one',
- return_value={
- '_id': test_id,
- 'projectId': test_id,
- 'googleId': 'other_test',
- 'authorizations': [{
- 'userId': 'test',
- 'authorizationLevel': 'VIEW'
- }]
- })
- mocker.patch.object(DB, 'delete_one', return_value=None)
- assert '403' in client.delete(f'/v2/portfolios/{test_id}').status
-
-
-def test_delete_project(client, mocker):
- mocker.patch.object(DB,
- 'fetch_one',
- return_value={
- '_id': test_id,
- 'projectId': test_id,
- 'googleId': 'test',
- 'portfolioIds': [test_id],
- 'authorizations': [{
- 'userId': 'test',
- 'authorizationLevel': 'OWN'
- }]
- })
- mocker.patch.object(DB, 'delete_one', return_value={})
- mocker.patch.object(DB, 'update', return_value=None)
- res = client.delete(f'/v2/portfolios/{test_id}')
- assert '200' in res.status