summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-04-25 21:53:42 +0200
committerGitHub <noreply@github.com>2021-04-25 21:53:42 +0200
commit128f76f7fd7c8abb41a3bbbd9f1980cbc20ae7a5 (patch)
treeadd513890005233a7784466797bfe6f5052e9eeb /opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios
parent128a1db017545597a5c035b7960eb3fd36b5f987 (diff)
parent57b54b59ed74ec37338ae26b3864d051255aba49 (diff)
build: Flatten project structure
This change updates the project structure to become flattened. Previously, the simulator, frontend and API each lived into their own directory. With this change, all modules of the project live in the top-level directory of the repository.
Diffstat (limited to 'opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios')
-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
3 files changed, 174 insertions, 0 deletions
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
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/__init__.py
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
new file mode 100644
index 00000000..2f042e06
--- /dev/null
+++ b/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/endpoint.py
@@ -0,0 +1,49 @@
+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.google_id, True)
+
+ scenario = Scenario(request.params_body['scenario'])
+
+ topology = Topology.from_id(scenario.obj['topology']['topologyId'])
+ topology.check_exists()
+ topology.check_user_access(request.google_id, 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
new file mode 100644
index 00000000..e5982b7f
--- /dev/null
+++ b/opendc-web/opendc-web-api/opendc/api/v2/portfolios/portfolioId/scenarios/test_endpoint.py
@@ -0,0 +1,125 @@
+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': [{
+ 'projectId': test_id,
+ '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': [{
+ 'projectId': test_id,
+ '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