-
Notifications
You must be signed in to change notification settings - Fork 82
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
Refactoring env stack part4 #1181
Merged
SofiaSazonova
merged 14 commits into
data-dot-all:main
from
SofiaSazonova:refactoring-env-stack-part4
Apr 16, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
476f8fb
move get_stack to stack_service.py
be74e4b
move logic from stack_resolvers to stack_service
5045fe1
move logic from KeyValueTagRepository to service
124f5f9
Move logic from repositories to services
1ceecd3
Move logic from repositories to services
5ddf7b3
RP changes
5387baf
get_stack_logs: check target resource permissions
8edee5c
find_environment_by_uri don't need session
2988c86
find_environment_by_uri don't need session
88eca80
move CloudWatch query to resolvers cause it's assync
aad4637
lint
e34e50b
typing List
be99f3e
remove unused get_environmental_stack_by_uri
f305528
remove unused import
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 15 additions & 69 deletions
84
backend/dataall/core/stacks/db/keyvaluetag_repositories.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,42 @@ | ||
import logging | ||
|
||
from dataall.base.context import get_context | ||
from dataall.core.permissions.services.resource_policy_service import ResourcePolicyService | ||
from dataall.core.stacks.db import stack_models as models | ||
from dataall.core.stacks.db.target_type_repositories import TargetType | ||
from dataall.base.db import exceptions | ||
from dataall.core.stacks.db.stack_models import KeyValueTag | ||
from typing import List | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class KeyValueTag: | ||
class KeyValueTagRepository: | ||
@staticmethod | ||
def update_key_value_tags(session, uri: str, data: dict = None) -> [models.KeyValueTag]: | ||
if not uri: | ||
raise exceptions.RequiredParameter('targetUri') | ||
if not data: | ||
raise exceptions.RequiredParameter('data') | ||
if not data.get('targetType'): | ||
raise exceptions.RequiredParameter('targetType') | ||
|
||
context = get_context() | ||
ResourcePolicyService.check_user_resource_permission( | ||
session=session, | ||
username=context.username, | ||
groups=context.groups, | ||
resource_uri=uri, | ||
permission_name=TargetType.get_resource_update_permission_name(data['targetType']), | ||
) | ||
|
||
tag_keys = [tag['key'].lower() for tag in data.get('tags', [])] | ||
if tag_keys and len(tag_keys) != len(set(tag_keys)): | ||
raise exceptions.UnauthorizedOperation( | ||
action='SAVE_KEY_VALUE_TAGS', | ||
message='Duplicate tag keys found. Please note that Tag keys are case insensitive', | ||
) | ||
|
||
tags = [] | ||
session.query(models.KeyValueTag).filter( | ||
models.KeyValueTag.targetUri == uri, | ||
models.KeyValueTag.targetType == data['targetType'], | ||
).delete() | ||
for tag in data.get('tags'): | ||
kv_tag: models.KeyValueTag = models.KeyValueTag( | ||
targetUri=uri, targetType=data['targetType'], key=tag['key'], value=tag['value'], cascade=tag['cascade'] | ||
) | ||
tags.append(kv_tag) | ||
session.add(kv_tag) | ||
|
||
return tags | ||
|
||
@staticmethod | ||
def list_key_value_tags(session, uri, target_type) -> dict: | ||
context = get_context() | ||
ResourcePolicyService.check_user_resource_permission( | ||
session=session, | ||
username=context.username, | ||
groups=context.groups, | ||
resource_uri=uri, | ||
permission_name=TargetType.get_resource_read_permission_name(target_type), | ||
) | ||
return KeyValueTag.find_key_value_tags(session, uri, target_type) | ||
|
||
@staticmethod | ||
def find_key_value_tags(session, target_uri, target_type) -> [models.KeyValueTag]: | ||
def find_key_value_tags(session, target_uri, target_type) -> List[KeyValueTag]: | ||
return ( | ||
session.query(models.KeyValueTag) | ||
session.query(KeyValueTag) | ||
.filter( | ||
models.KeyValueTag.targetUri == target_uri, | ||
models.KeyValueTag.targetType == target_type, | ||
KeyValueTag.targetUri == target_uri, | ||
KeyValueTag.targetType == target_type, | ||
) | ||
.all() | ||
) | ||
|
||
@staticmethod | ||
def find_environment_cascade_key_value_tags(session, target_uri) -> [models.KeyValueTag]: | ||
def find_environment_cascade_key_value_tags(session, target_uri) -> List[KeyValueTag]: | ||
return ( | ||
session.query(models.KeyValueTag) | ||
session.query(KeyValueTag) | ||
.filter( | ||
models.KeyValueTag.targetUri == target_uri, | ||
models.KeyValueTag.targetType == 'environment', | ||
models.KeyValueTag.cascade.is_(True), | ||
KeyValueTag.targetUri == target_uri, | ||
KeyValueTag.targetType == 'environment', | ||
KeyValueTag.cascade.is_(True), | ||
) | ||
.all() | ||
) | ||
|
||
@staticmethod | ||
def delete_key_value_tags(session, target_uri, target_type): | ||
return ( | ||
session.query(models.KeyValueTag) | ||
session.query(KeyValueTag) | ||
.filter( | ||
models.KeyValueTag.targetUri == target_uri, | ||
models.KeyValueTag.targetType == target_type, | ||
KeyValueTag.targetUri == target_uri, | ||
KeyValueTag.targetType == target_type, | ||
) | ||
.delete() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General comment about the refactoring...
resolvers.py
become redundant? They are now just proxy calls so can't we just point the resolver (where the GraphQL query is defined) directly to the service?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it's open question yet. At least it gives us asyncronous calls (like in get_logs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The principle was that in resolvers we check the api input and in service we just execute business logic. In the PRs for refactoring we are adding validation in the service layer but not in the resolvers. I did not add any comment in these PRs, because we already talked about implementing a better validation mechanism. I opened an issue so that we do not forget about it