Skip to content

Commit

Permalink
Merge pull request #1131 from moonstream-to/add-instance-tag
Browse files Browse the repository at this point in the history
Update tags.
  • Loading branch information
Andrei-Dolgolev authored Oct 2, 2024
2 parents d614643 + 1fcd69d commit 1fe6726
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
8 changes: 4 additions & 4 deletions moonstreamapi/moonstreamapi/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,10 +1181,10 @@ def create_resource_for_user(
return resource


def chekc_user_resource_access(
def check_user_resource_access(
customer_id: uuid.UUID,
user_token: uuid.UUID,
) -> bool:
) -> Optional[BugoutResource]:
"""
Check if user has access to customer_id
"""
Expand All @@ -1198,10 +1198,10 @@ def chekc_user_resource_access(

except BugoutResponseException as e:
if e.status_code == 404:
return False
return None
raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail)
except Exception as e:
logger.error(f"Error get customer: {str(e)}")
raise MoonstreamHTTPException(status_code=500, internal_error=e)

return str(response.id) == customer_id
return response
63 changes: 43 additions & 20 deletions moonstreamapi/moonstreamapi/routes/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
EntityJournalNotFoundException,
apply_moonworm_tasks,
check_if_smart_contract,
chekc_user_resource_access,
check_user_resource_access,
get_entity_subscription_journal_id,
get_list_of_support_interfaces,
get_moonworm_tasks,
Expand Down Expand Up @@ -104,17 +104,19 @@ async def add_subscription_handler(

if customer_id is not None:

results = chekc_user_resource_access(
results = check_user_resource_access(
customer_id=customer_id,
user_token=token,
)

if not results:
if results is None:
raise MoonstreamHTTPException(
status_code=403,
detail="User has no access to this customer",
status_code=404,
detail="Not found customer",
)

customer_instance_name = results.resource_data["name"]

active_subscription_types_response = subscription_types.list_subscription_types(
active_only=True
)
Expand Down Expand Up @@ -156,12 +158,12 @@ async def add_subscription_handler(
if description:
content["description"] = description

excluded_keys = MOONSTREAM_ENTITIES_RESERVED_TAGS

allowed_required_fields: List[Any] = []
if tags:
allowed_required_fields = [
item
for item in tags
if not any(key in item for key in MOONSTREAM_ENTITIES_RESERVED_TAGS)
item for item in tags if not any(key in item for key in excluded_keys)
]

required_fields: List[Dict[str, Union[str, bool, int, List[Any]]]] = [
Expand All @@ -172,6 +174,18 @@ async def add_subscription_handler(
{"user_id": f"{user.id}"},
]

if customer_id is not None and customer_instance_name is not None:
required_fields.extend(
[
{
"customer_id": f"{customer_id}",
},
{
"instance_name": f"{customer_instance_name}",
},
]
)

if allowed_required_fields:
required_fields.extend(allowed_required_fields)

Expand Down Expand Up @@ -214,11 +228,15 @@ async def add_subscription_handler(
entity_secondary_fields = (
entity.secondary_fields if entity.secondary_fields is not None else {}
)

# We remove the instance_name for return that tag to the frontend
excluded_keys = excluded_keys - {"instance_name"}

normalized_entity_tags = [
f"{key}:{value}"
for tag in entity_required_fields
for key, value in tag.items()
if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS
if key not in excluded_keys
]

if entity_secondary_fields.get("abi") and customer_id is not None:
Expand Down Expand Up @@ -310,7 +328,7 @@ async def delete_subscription_handler(
f"{key}:{value}"
for tag in tags_raw
for key, value in tag.items()
if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS
if key not in (MOONSTREAM_ENTITIES_RESERVED_TAGS - {"instance_name"})
]

if deleted_entity.secondary_fields is not None:
Expand Down Expand Up @@ -383,6 +401,9 @@ async def get_subscriptions_handler(
List[BugoutSearchResultAsEntity], subscriptions_list.results
)

# We remove the instance_name for return that tag to the frontend
excluded_keys = MOONSTREAM_ENTITIES_RESERVED_TAGS - {"instance_name"}

for subscription in user_subscriptions_results:
tags = subscription.required_fields

Expand All @@ -402,7 +423,7 @@ async def get_subscriptions_handler(
f"{key}:{value}"
for tag in tags
for key, value in tag.items()
if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS
if key not in excluded_keys
]

subscriptions.append(
Expand Down Expand Up @@ -460,17 +481,19 @@ async def update_subscriptions_handler(

if customer_id is not None:

results = chekc_user_resource_access(
results = check_user_resource_access(
customer_id=customer_id,
user_token=token,
)

if not results:
if results is None:
raise MoonstreamHTTPException(
status_code=403,
detail="User has no access to this customer",
status_code=404,
detail="Not found customer",
)

excluded_keys = MOONSTREAM_ENTITIES_RESERVED_TAGS

try:
journal_id = get_entity_subscription_journal_id(
resource_type=BUGOUT_RESOURCE_TYPE_ENTITY_SUBSCRIPTION,
Expand All @@ -490,7 +513,7 @@ async def update_subscriptions_handler(
update_required_fields = [
field
for field in subscription_entity.required_fields
if any(key in field for key in MOONSTREAM_ENTITIES_RESERVED_TAGS)
if any(key in field for key in excluded_keys)
]

update_secondary_fields = (
Expand Down Expand Up @@ -556,9 +579,7 @@ async def update_subscriptions_handler(

if tags:
allowed_required_fields = [
item
for item in tags
if not any(key in item for key in MOONSTREAM_ENTITIES_RESERVED_TAGS)
item for item in tags if not any(key in item for key in excluded_keys)
]

if allowed_required_fields:
Expand Down Expand Up @@ -619,12 +640,14 @@ async def update_subscriptions_handler(
if subscription.secondary_fields is not None
else {}
)
# We remove the instance_name for return that tag to the frontend
excluded_keys = excluded_keys - {"instance_name"}

normalized_entity_tags = [
f"{key}:{value}"
for tag in subscription_required_fields
for key, value in tag.items()
if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS
if key not in excluded_keys
]

return data.SubscriptionResourceData(
Expand Down
22 changes: 13 additions & 9 deletions moonstreamapi/moonstreamapi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,19 @@
)

# Entities reserved tags
MOONSTREAM_ENTITIES_RESERVED_TAGS = [
"type",
"subscription_type_id",
"color",
"label",
"user_id",
"address",
"blockchain",
]
MOONSTREAM_ENTITIES_RESERVED_TAGS = set(
[
"type",
"subscription_type_id",
"color",
"label",
"user_id",
"address",
"blockchain",
"customer_id",
"instance_name",
]
)

## Moonstream resources types

Expand Down

0 comments on commit 1fe6726

Please sign in to comment.