Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frontend)(api): add update datasource functionality #16

Merged
merged 7 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Bug report
description: Create a report to help us improve
title: '[BUG] '
labels: ['bug']
assignees: []
body:
- type: input
attributes:
label: Nesis version
description: What Nesis version are you using?
validations:
required: true
- type: textarea
attributes:
label: Describe the bug
description: A clear and concise description of what the bug is.
validations:
required: true
- type: textarea
attributes:
label: To reproduce
description: Steps to reproduce the behavior
placeholder: |
1. Go to '...'
2. Click on '....'
- type: textarea
attributes:
label: Expected behavior
description: A clear and concise description of what you expected to happen.
- type: textarea
attributes:
label: Screenshots
description: If applicable, add screenshots to help explain the bug.
- type: textarea
attributes:
label: Additional context
description: Add any other context about the bug here.
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: Custom issue template
about: Describe this issue template's objective.
title: ''
labels: ''
assignees: ''

---


14 changes: 14 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Additional context**
Add any other context or screenshots about the feature request.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""migrations-0.1.2-api
"""migrations-0.0.1

Revision ID: 79ec864b571e
Revises:
Expand Down
33 changes: 33 additions & 0 deletions nesis/api/alembic/versions/953b4d309aaa_migrations_0_0_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""migrations-0.0.2

Revision ID: 953b4d309aaa
Revises: 79ec864b571e
Create Date: 2024-04-09 19:11:08.664346

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "953b4d309aaa"
down_revision: Union[str, None] = "79ec864b571e"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint("datasource_name_key", "datasource", type_="unique")
op.create_unique_constraint("uq_datasource_name", "datasource", ["name"])
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint("uq_datasource_name", "datasource", type_="unique")
op.create_unique_constraint("datasource_name_key", "datasource", ["name"])
# ### end Alembic commands ###
8 changes: 4 additions & 4 deletions nesis/api/core/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GET = "GET"
POST = "POST"
DELETE = "DELETE"
PUT = "PUT"
GET: str = "GET"
POST: str = "POST"
DELETE: str = "DELETE"
PUT: str = "PUT"

from .api import app

Expand Down
103 changes: 52 additions & 51 deletions nesis/api/core/controllers/datasources.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import request, jsonify

from . import GET, POST, DELETE
import nesis.api.core.controllers as controllers
from .api import app, error_message

import logging
Expand All @@ -14,80 +14,81 @@
_LOG = logging.getLogger(__name__)


@app.route("/v1/datasources", methods=[POST, GET])
@app.route("/v1/datasources", methods=[controllers.POST, controllers.GET])
def operate_datasources():
token = get_bearer_token(request.headers.get("Authorization"))
try:
if request.method == POST:
result = services.datasource_service.create(
token=token, datasource=request.json
)
return jsonify(result.to_dict())
else:
results = services.datasource_service.get(token=token)
return jsonify({"items": [item.to_dict() for item in results]})
match request.method:
case controllers.POST:
result = services.datasource_service.create(
token=token, datasource=request.json
)
return jsonify(result.to_dict())
case controllers.GET:
results = services.datasource_service.get(token=token)
return jsonify({"items": [item.to_dict() for item in results]})
case _:
raise Exception("Should never be reached")
except util.ConflictException as se:
return jsonify(error_message(str(se))), 409
except util.ServiceException as se:
return jsonify(error_message(str(se))), 400
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except util.ValidationException:
return jsonify(error_message("Unable to validate datasource connection")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500


@app.route("/v1/datasources/<datasource_id>", methods=[GET, DELETE])
@app.route(
"/v1/datasources/<datasource_id>",
methods=[controllers.GET, controllers.DELETE, controllers.PUT],
)
def operate_datasource(datasource_id):
token = get_bearer_token(request.headers.get("Authorization"))
try:
if request.method == GET:
results = services.datasource_service.get(
token=token, datasource_id=datasource_id
)
if len(results) != 0:
return jsonify(results[0].to_dict())
else:
return (
jsonify(
error_message(
f"Datasource {datasource_id} not found", message_type="WARN"
)
),
404,
)
else:
services.datasource_service.delete(token=token, datasource_id=datasource_id)
return jsonify(success=True)
except util.ServiceException as se:
return jsonify(error_message(str(se))), 400
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500


@app.route("/v1/datasources/<datasource>/dataobjects/<dataobject>", methods=[GET])
def operate_dataobject(datasource, dataobject):
token = get_bearer_token(request.headers.get("Authorization"))
try:
if request.method == GET:
results = services.dataobject_service.get(
token=token, datasource=datasource, dataobject=dataobject
)
return jsonify(results.to_dict())

match request.method:
case controllers.GET:
results = services.datasource_service.get(
token=token, datasource_id=datasource_id
)
if len(results) != 0:
return jsonify(results[0].to_dict())
else:
return (
jsonify(
error_message(
f"Datasource {datasource_id} not found",
message_type="WARN",
)
),
404,
)
case controllers.DELETE:
services.datasource_service.delete(
token=token, datasource_id=datasource_id
)
return jsonify(success=True)
case controllers.PUT:
result = services.datasource_service.update(
token=token, datasource=request.json, datasource_id=datasource_id
)
return jsonify(result.to_dict())
case _:
raise Exception("Should never be reached really")
except util.ConflictException as se:
return jsonify(error_message(str(se))), 409
except util.ServiceException as se:
return jsonify(error_message(str(se))), 400
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500
18 changes: 11 additions & 7 deletions nesis/api/core/controllers/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def operate_users():
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500
Expand Down Expand Up @@ -66,7 +66,7 @@ def operate_user(user_id):
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500
Expand All @@ -91,7 +91,7 @@ def operate_user_roles(user_id):
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500
Expand All @@ -108,7 +108,7 @@ def operate_user_role(user_id, role_id):
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500
Expand All @@ -126,12 +126,14 @@ def operate_roles():
else:
results = services.role_service.get(token=token)
return jsonify({"items": [item.to_dict() for item in results]})
except util.ConflictException as se:
return jsonify(error_message(str(se))), 409
except util.ServiceException as se:
return jsonify(error_message(str(se))), 400
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500
Expand Down Expand Up @@ -170,10 +172,12 @@ def operate_role(role_id):
token=token, id=role_id, role=request.json
)
return jsonify(result.to_dict())
except util.ConflictException as se:
return jsonify(error_message(str(se))), 409
except util.ServiceException as se:
return jsonify(error_message(str(se))), 400
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401

Expand All @@ -193,7 +197,7 @@ def operate_sessions():
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except:
_LOG.exception("Error operating session")
return jsonify(error_message("Server error")), 500
2 changes: 1 addition & 1 deletion nesis/api/core/controllers/predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def operate_module_predictions(module):
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500
4 changes: 2 additions & 2 deletions nesis/api/core/controllers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def operate_settings(module):
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500
Expand Down Expand Up @@ -66,7 +66,7 @@ def operate_setting(module, setting_id):
except util.UnauthorizedAccess:
return jsonify(error_message("Unauthorized access")), 401
except util.PermissionException:
return jsonify(error_message("Forbidden resource")), 403
return jsonify(error_message("Forbidden action on resource")), 403
except:
_LOG.exception("Error getting user")
return jsonify(error_message("Server error")), 500
4 changes: 3 additions & 1 deletion nesis/api/core/models/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,13 @@ class Datasource(Base):
id = Column(BigInteger, primary_key=True, nullable=False)
uuid = Column(Unicode(255), unique=True, nullable=False)
type = Column(Enum(DatasourceType, name="datasource_type"), nullable=False)
name = Column(Unicode(255), nullable=False, unique=True)
name = Column(Unicode(255), nullable=False)
enabled = Column(Boolean, default=True, nullable=False)
status = Column(Enum(DatasourceStatus, name="datasource_status"), nullable=False)
connection = Column(JSONB, nullable=False)

__table_args__ = (UniqueConstraint("name", name="uq_datasource_name"),)

def __init__(
self,
source_type: DatasourceType,
Expand Down
Loading
Loading