Skip to content

Commit

Permalink
feat: add workspace_id for Run, RunConfiguration (#302)
Browse files Browse the repository at this point in the history
* fix: add unique email user validation

* feat: add workspace_id for Run, RunConfiguration
  • Loading branch information
Vitalii Melnychuk authored Mar 24, 2022
1 parent b205785 commit e1608f1
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 46 deletions.
2 changes: 2 additions & 0 deletions web/api/maestro_api/api_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
create_run_schema,
update_run_schema,
run_all_schema,
run_configuration_all_schema,
run_configuration_create_schema,
run_metric_all_schema,
run_agent_update_schema,
Expand Down Expand Up @@ -124,6 +125,7 @@ def run_configuration_create_one(*args, **kwargs):
return run_configuration_controller.create_one(*args, **kwargs)

@flask_app.route("/run_configurations", methods=["GET"])
@validate_request(run_configuration_all_schema)
@requires_auth()
def run_configuration_all(*args, **kwargs):
return run_configuration_controller.all(*args, **kwargs)
Expand Down
11 changes: 6 additions & 5 deletions web/api/maestro_api/controllers/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from maestro_api.db.models.run import Run
from maestro_api.db.repo.run import RunRepository
from maestro_api.db.models.run_agent import RunAgent
from maestro_api.db.models.run_metric import RunMetric

from maestro_api.db.models.run_configuration import RunConfiguration

from maestro_api.libs.flask.utils import (
Expand All @@ -25,9 +24,7 @@ def delete_one(self, run_id, user):

run = get_obj_or_404(Run, id=run_id)

RunMetric.objects(run_id=run.id).delete()
RunAgent.objects(run_id=run.id).delete()
run.delete()
self.run_repo.delete_with_related(run)

return jsonify(run.to_dict())

Expand All @@ -41,6 +38,7 @@ def get_one(self, run_id, user):
def all(self, data, user):
"Get all Run objects"

workspace_id = data.get("workspace_id", None)
labels = data.get("labels", None)
run_status = data.get("run_status", None)
skip = int(data.get("skip", 0))
Expand All @@ -52,6 +50,9 @@ def all(self, data, user):
if labels is not None:
filter_query = filter_query & Q(labels__all=str_to_list(labels))

if workspace_id is not None:
filter_query = filter_query & Q(workspace_id=workspace_id)

if run_status is not None:
filter_query = filter_query & Q(run_status__in=str_to_list(run_status))

Expand Down
16 changes: 14 additions & 2 deletions web/api/maestro_api/controllers/run_configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from mongoengine import Q

from maestro_api.db.models.agent import Agent
from maestro_api.db.models.run_plan import RunPlan
from maestro_api.db.models.custom_data import CustomData
from maestro_api.db.models.run_configuration import RunConfiguration
from maestro_api.db.models.workspace import Workspace

from maestro_api.libs.flask.utils import get_obj_or_404, jsonify_list_of_docs, jsonify

Expand All @@ -16,6 +19,7 @@ def _get_create_update_data(self, data):
]

run_plan = get_obj_or_404(RunPlan, id=data.get("run_plan_id"))
workspace = get_obj_or_404(Workspace, id=data.get("workspace_id"))

custom_data_ids = [
get_obj_or_404(CustomData, id=custom_data_id).id
Expand All @@ -34,6 +38,7 @@ def _get_create_update_data(self, data):
"title": title,
"agent_ids": agent_ids,
"run_plan_id": run_plan.id,
"workspace_id": workspace.id,
"hosts": hosts,
"custom_data_ids": custom_data_ids,
"custom_properties": custom_properties,
Expand All @@ -54,10 +59,17 @@ def get_one(self, run_configuration_id, user):

return jsonify(run_configuration.to_dict())

def all(self, user):
def all(self, data, user):
"Get all RunConfiguration objects"

run_configurations = RunConfiguration.objects()
workspace_id = data.get("workspace_id", None)

filter_query = Q()

if workspace_id is not None:
filter_query = filter_query & Q(workspace_id=workspace_id)

run_configurations = RunConfiguration.objects.filter(filter_query)

return jsonify_list_of_docs(run_configurations)

Expand Down
8 changes: 8 additions & 0 deletions web/api/maestro_api/controllers/workspace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from maestro_api.db.models.workspace import Workspace
from maestro_api.db.models.user import User, UserRole
from maestro_api.db.repo.run import RunRepository
from maestro_api.db.models.run import Run
from maestro_api.db.models.run_configuration import RunConfiguration


from maestro_api.libs.flask.utils import (
Expand All @@ -13,6 +16,7 @@
class WorkspaceController:
def __init__(self, flask_app=None):
self.flask_app = flask_app
self.run_repo = RunRepository()

def create_or_update_workspace(self, workspace, data):
"Create Owrkspace and share it with Users"
Expand Down Expand Up @@ -79,6 +83,10 @@ def delete_one(self, workspace_id, user):
User.objects(workspace_ids__in=[workspace.id]).update(
pull__workspace_ids=workspace.id
)
runs = Run.objects(workspace_id=workspace.id)
for run in runs:
self.run_repo.delete_with_related(run)
RunConfiguration.objects(workspace_id=workspace.id).delete()

workspace.delete()

Expand Down
2 changes: 2 additions & 0 deletions web/api/maestro_api/db/models/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Run(CreatedUpdatedDocumentMixin):
choices=RunStatus.list(),
)
run_plan_id = ObjectIdField(required=True)
workspace_id = ObjectIdField(required=True)

agent_ids = ListField(
required=True,
Expand Down Expand Up @@ -91,6 +92,7 @@ def to_dict(self):
"id": str(self.id),
"run_configuration_id": str(self.run_configuration_id),
"run_plan_id": str(self.run_plan_id),
"workspace_id": str(self.workspace_id),
"agent_ids": [str(agent_id) for agent_id in self.agent_ids],
"custom_data_ids": [
str(custom_data_id) for custom_data_id in self.custom_data_ids
Expand Down
2 changes: 2 additions & 0 deletions web/api/maestro_api/db/models/run_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class RunConfigurationLoadProfile(EmbeddedDocument):
class RunConfiguration(CreatedUpdatedDocumentMixin):
title = StringField(required=True)
run_plan_id = ObjectIdField(required=True)
workspace_id = ObjectIdField(required=True)
agent_ids = ListField(
required=True,
field=ObjectIdField(),
Expand All @@ -60,6 +61,7 @@ def to_dict(self):
return {
"id": str(self.id),
"run_plan_id": str(self.run_plan_id),
"workspace_id": str(self.workspace_id),
"agent_ids": [str(agent_id) for agent_id in self.agent_ids],
"custom_data_ids": [
str(custom_data_id) for custom_data_id in self.custom_data_ids
Expand Down
14 changes: 14 additions & 0 deletions web/api/maestro_api/db/repo/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from maestro_api.db.models.run_configuration import RunConfiguration
from maestro_api.db.models.agent import Agent
from maestro_api.db.models.run_agent import RunAgent
from maestro_api.db.models.run_metric import RunMetric
from maestro_api.db.models.run_metric_label import RunMetricLabel


class RunRepository:
Expand All @@ -24,6 +26,7 @@ def create_run(self, run_configuration: RunConfiguration):
new_run = Run(
title=run_configuration.title,
run_configuration_id=run_configuration.id,
workspace_id=run_configuration.workspace_id,
agent_ids=run_configuration.agent_ids,
run_plan_id=run_configuration.run_plan_id,
custom_data_ids=run_configuration.custom_data_ids,
Expand All @@ -46,3 +49,14 @@ def create_run(self, run_configuration: RunConfiguration):
RunAgent.objects.insert(run_agents)

return new_run

def delete_with_related(self, run: Run):
"Delete Run and all related documents"

RunMetric.objects(run_id=run.id).delete()
RunMetricLabel.objects(run_id=run.id).delete()
RunAgent.objects(run_id=run.id).delete()

run.delete()

return run
18 changes: 18 additions & 0 deletions web/api/maestro_api/swagger/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ paths:
produces:
- "application/json"
parameters:
- in: query
name: workspace_id
description: "Wokspace ID"
type: string
- in: query
name: labels
description: "List of Run labels should be included"
Expand Down Expand Up @@ -665,6 +669,9 @@ paths:
run_plan_id:
type: string
description: "Run Plan identifier"
workspace_id:
type: string
description: "Workspace identifier"
agent_ids:
type: array
description: "List of server Agents IDs should be used in test"
Expand Down Expand Up @@ -750,6 +757,7 @@ paths:
description: "Returns list of RunConfiguration objects"
400:
description: "Bad request"

/run_configuration/{run_configuration_id}:
get:
tags:
Expand Down Expand Up @@ -902,6 +910,7 @@ paths:
description: "Returns list of Run objects"
400:
description: "Bad request"

/run_configurations:
get:
tags:
Expand All @@ -913,6 +922,11 @@ paths:
- "application/json"
produces:
- "application/json"
parameters:
- in: query
name: workspace_id
description: "Wokspace ID"
type: string
responses:
200:
schema:
Expand Down Expand Up @@ -1668,6 +1682,8 @@ definitions:
$ref: "#/components/schemas/RunStatus"
run_plan_id:
type: string
workspace_id:
type: string
agent_ids:
type: array
items: string
Expand Down Expand Up @@ -1760,6 +1776,8 @@ definitions:
type: string
run_plan_id:
type: string
workspace_id:
type: string
agent_ids:
type: array
items: string
Expand Down
23 changes: 23 additions & 0 deletions web/api/maestro_api/validation_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
run_all_schema = {
"type": "object",
"properties": {
"workspace_id": {
"type": "string",
"minLength": 12,
"maxLength": 24,
},
"run_status": {"type": "string", "enum": RunStatus.list()},
"notes": {"type": "string"},
"labels": {
Expand All @@ -60,6 +65,18 @@
"additionalProperties": False,
}

run_configuration_all_schema = {
"type": "object",
"properties": {
"workspace_id": {
"type": "string",
"minLength": 12,
"maxLength": 24,
},
},
"additionalProperties": False,
}

run_configuration_create_schema = {
"type": "object",
"properties": {
Expand All @@ -69,6 +86,11 @@
"minLength": 12,
"maxLength": 24,
},
"workspace_id": {
"type": "string",
"minLength": 12,
"maxLength": 24,
},
"agent_ids": {
"type": "array",
"items": {"type": "string", "minLength": 12, "maxLength": 24},
Expand Down Expand Up @@ -148,6 +170,7 @@
},
},
"required": [
"workspace_id",
"run_plan_id",
"agent_ids",
],
Expand Down
Loading

0 comments on commit e1608f1

Please sign in to comment.