moonstream/moonstreamapi/moonstreamapi/admin/subscriptions.py

68 wiersze
1.9 KiB
Python
Czysty Zwykły widok Historia

2021-10-26 13:56:01 +00:00
"""
Utilities for managing subscription resources for a Moonstream application.
"""
import argparse
import json
from typing import Dict, List, Optional, Union
2021-11-04 14:14:37 +00:00
from bugout.data import BugoutResources
2021-10-26 13:56:01 +00:00
2021-11-04 14:14:37 +00:00
from .. import reporter
2021-12-16 13:26:04 +00:00
from ..settings import BUGOUT_REQUEST_TIMEOUT_SECONDS, MOONSTREAM_ADMIN_ACCESS_TOKEN
from ..settings import bugout_client as bc
2021-10-26 13:56:01 +00:00
def migrate_subscriptions(
match: Dict[str, Union[str, int]],
update: Dict[str, Union[str, int]],
2021-11-04 14:14:37 +00:00
descriptions: str,
file: str,
2021-11-02 14:32:40 +00:00
drop_keys: Optional[List[str]],
2021-10-26 13:56:01 +00:00
):
"""
Search all subscriptinons and replace them one by one
"""
response: BugoutResources = bc.list_resources(
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
params=match,
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS,
)
2021-11-01 16:37:45 +00:00
old_resources = [resource for resource in response.resources]
2021-10-26 13:56:01 +00:00
new_resources = []
2021-11-04 14:14:37 +00:00
reporter.custom_report(
title="Subscription migration",
content=descriptions,
tags=["subscriptions", "migration", f"migration_file:{file}"],
)
2021-11-08 12:24:11 +00:00
print(f"Affected resources: {len(old_resources)}")
2021-11-04 14:14:37 +00:00
2021-11-08 12:24:11 +00:00
for resource in old_resources:
try:
print(f"Updating resource: {resource.id}")
2021-10-26 13:56:01 +00:00
2021-11-04 14:14:37 +00:00
new_resource = bc.update_resource(
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
resource_id=resource.id,
resource_data={"update": update, "drop_keys": drop_keys},
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS,
)
new_resources.append(new_resource)
2021-11-08 12:24:11 +00:00
except Exception as err:
print(err)
reporter.error_report(
err,
tags=[
"subscriptions",
"migration",
"error",
f"resource_id:{resource.id}",
f"migration_file:{file}",
],
)
2021-10-26 13:56:01 +00:00
return new_resources