kopia lustrzana https://github.com/bugout-dev/moonstream
Add reporting and validations.
rodzic
df6c8db67c
commit
c76e5ca129
|
@ -34,33 +34,36 @@ def cli_migrate_subscriptions(args: argparse.Namespace) -> None:
|
|||
with open(args.file) as migration_json_file:
|
||||
migration_json = json.load(migration_json_file)
|
||||
|
||||
if "match" not in migration_json or "update" not in migration_json:
|
||||
if (
|
||||
"match" not in migration_json
|
||||
or "update" not in migration_json[args.command]
|
||||
or "description" not in migration_json
|
||||
):
|
||||
print(
|
||||
"Migration file plan have incorrect format require specified {match:{}, update:{}, drop_keys: Optional}"
|
||||
'Migration file plan have incorrect format require specified {"match": {},"description": "","upgrade": { "update": {}, "drop_keys": [] }, "downgrade": { "update": {}, "drop_keys": [] }}'
|
||||
)
|
||||
return
|
||||
|
||||
match = migration_json["match"]
|
||||
update = migration_json["update"]
|
||||
description = migration_json["description"]
|
||||
update = migration_json[args.command]["update"]
|
||||
file = args.file
|
||||
|
||||
if "drop_keys" in migration_json:
|
||||
drop_keys = migration_json["drop_keys"]
|
||||
if "drop_keys" in migration_json[args.command]:
|
||||
drop_keys = migration_json[args.command]["drop_keys"]
|
||||
|
||||
elif args.match is not None and args.update is not None:
|
||||
|
||||
match = json.loads(args.update)
|
||||
|
||||
update = json.loads(args.match)
|
||||
|
||||
if args.drop_keys is not None:
|
||||
drop_keys = json.loads(args.drop_keys)
|
||||
subscriptions.migrate_subscriptions(
|
||||
match=match,
|
||||
descriptions=description,
|
||||
update=update,
|
||||
drop_keys=drop_keys,
|
||||
file=file,
|
||||
)
|
||||
|
||||
else:
|
||||
print("Specified file or --match and --update is required.")
|
||||
print("Specified file is required.")
|
||||
return
|
||||
|
||||
subscriptions.migrate_subscriptions(match=match, update=update, drop_keys=drop_keys)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
cli_description = f"""Moonstream Admin CLI
|
||||
|
@ -266,27 +269,16 @@ This CLI is configured to work with the following API URLs:
|
|||
"-f", "--file", required=False, type=str, help="path to file"
|
||||
)
|
||||
parser_subscription_migrate.add_argument(
|
||||
"-m",
|
||||
"--match",
|
||||
required=False,
|
||||
"-c",
|
||||
"--command",
|
||||
default="upgrade",
|
||||
choices=["upgrade", "downgrade"],
|
||||
type=str,
|
||||
help="Matching json",
|
||||
help="Command for migration",
|
||||
)
|
||||
parser_subscription_migrate.add_argument(
|
||||
"-u",
|
||||
"--update",
|
||||
required=False,
|
||||
type=str,
|
||||
help="Updated/Added json fields",
|
||||
parser_subscription_migrate.set_defaults(
|
||||
func=lambda _: parser_subscription.print_help()
|
||||
)
|
||||
parser_subscription_migrate.add_argument(
|
||||
"-d",
|
||||
"--drop_keys",
|
||||
required=False,
|
||||
type=str,
|
||||
help="Droped json fields",
|
||||
)
|
||||
parser_subscription_migrate.set_defaults(func=cli_migrate_subscriptions)
|
||||
|
||||
args = parser.parse_args()
|
||||
args.func(args)
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
{
|
||||
"match": { "type": "subscription" },
|
||||
"update": {
|
||||
"abi": null,
|
||||
"bucket": null,
|
||||
"s3_path": null
|
||||
},
|
||||
"drop_keys": []
|
||||
}
|
|
@ -5,20 +5,21 @@ import argparse
|
|||
import json
|
||||
from typing import Dict, List, Optional, Union
|
||||
|
||||
from bugout.data import BugoutResources, BugoutResource
|
||||
from sqlalchemy.sql.expression import update
|
||||
from bugout.data import BugoutResources
|
||||
|
||||
from ..data import SubscriptionTypeResourceData
|
||||
from ..settings import (
|
||||
MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
||||
bugout_client as bc,
|
||||
BUGOUT_REQUEST_TIMEOUT_SECONDS,
|
||||
)
|
||||
from .. import reporter
|
||||
|
||||
|
||||
def migrate_subscriptions(
|
||||
match: Dict[str, Union[str, int]],
|
||||
update: Dict[str, Union[str, int]],
|
||||
descriptions: str,
|
||||
file: str,
|
||||
drop_keys: Optional[List[str]],
|
||||
):
|
||||
"""
|
||||
|
@ -35,14 +36,32 @@ def migrate_subscriptions(
|
|||
|
||||
new_resources = []
|
||||
|
||||
for resource in old_resources:
|
||||
reporter.custom_report(
|
||||
title="Subscription migration",
|
||||
content=descriptions,
|
||||
tags=["subscriptions", "migration", f"migration_file:{file}"],
|
||||
)
|
||||
|
||||
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,
|
||||
try:
|
||||
for resource in old_resources:
|
||||
|
||||
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)
|
||||
except Exception as err:
|
||||
reporter.error_report(
|
||||
err,
|
||||
tags=[
|
||||
"subscriptions",
|
||||
"migration",
|
||||
"error",
|
||||
f"resource_id:{resource.id}",
|
||||
f"migration_file:{file}",
|
||||
],
|
||||
)
|
||||
new_resources.append(new_resource)
|
||||
|
||||
return new_resources
|
||||
|
|
|
@ -3,12 +3,14 @@ The Moonstream subscriptions HTTP API
|
|||
"""
|
||||
import logging
|
||||
import json
|
||||
from typing import List, Optional, Dict, Any, Union
|
||||
from typing import List, Optional, Dict, Any
|
||||
|
||||
|
||||
import boto3 # type: ignore
|
||||
from bugout.data import BugoutResource, BugoutResources
|
||||
from bugout.exceptions import BugoutResponseException
|
||||
from fastapi import APIRouter, Request, Form
|
||||
from web3._utils.validation import validate_abi
|
||||
|
||||
from ..admin import subscription_types
|
||||
from .. import data
|
||||
|
@ -86,14 +88,17 @@ async def add_subscription_handler(
|
|||
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
||||
|
||||
if abi:
|
||||
|
||||
# ABI validation
|
||||
|
||||
|
||||
try:
|
||||
json.loads(abi)
|
||||
validate_abi(json.loads(abi))
|
||||
except json.JSONDecodeError:
|
||||
raise MoonstreamHTTPException(status_code=400, detail="Malformed abi body.")
|
||||
except ValueError as e:
|
||||
raise MoonstreamHTTPException(status_code=400, detail=e)
|
||||
except:
|
||||
raise MoonstreamHTTPException(status_code=400, detail="ABI is incorret")
|
||||
|
||||
raise MoonstreamHTTPException(
|
||||
status_code=400, detail="Error on abi valiadation."
|
||||
)
|
||||
s3_client = boto3.client("s3")
|
||||
|
||||
bucket = MOONSTREAM_SMARTCONTRACTS_ABI_BUCKET
|
||||
|
@ -228,7 +233,6 @@ async def update_subscriptions_handler(
|
|||
subscription_id: str,
|
||||
color: Optional[str] = Form(None),
|
||||
label: Optional[str] = Form(None),
|
||||
abi: Optional[str] = Form(None),
|
||||
) -> data.SubscriptionResourceData:
|
||||
"""
|
||||
Get user's subscriptions.
|
||||
|
@ -243,56 +247,6 @@ async def update_subscriptions_handler(
|
|||
if label:
|
||||
update["label"] = label
|
||||
|
||||
if abi:
|
||||
|
||||
# ABI validation
|
||||
|
||||
try:
|
||||
json.loads(abi)
|
||||
except:
|
||||
raise MoonstreamHTTPException(status_code=400, detail="ABI is incorret")
|
||||
|
||||
try:
|
||||
subscription: BugoutResource = bc.get_resource(
|
||||
token=token,
|
||||
resource_id=subscription_id,
|
||||
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS,
|
||||
)
|
||||
except BugoutResponseException as e:
|
||||
raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail)
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting user subscriptions: {str(e)}")
|
||||
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
||||
|
||||
if subscription.resource_data["abi"] is not None:
|
||||
raise MoonstreamHTTPException(
|
||||
status_code=403, detail="ABI can't be updated"
|
||||
)
|
||||
|
||||
s3_client = boto3.client("s3")
|
||||
|
||||
bucket = MOONSTREAM_SMARTCONTRACTS_ABI_BUCKET
|
||||
|
||||
result_bytes = abi.encode("utf-8")
|
||||
result_key = (
|
||||
f"abi/v1/{subscription.resource_data['address']}/{subscription_id}/abi.json"
|
||||
)
|
||||
|
||||
s3_client.put_object(
|
||||
Body=result_bytes,
|
||||
Bucket=bucket,
|
||||
Key=result_key,
|
||||
ContentType="application/json",
|
||||
Metadata={"Moonstream": "Abi data"},
|
||||
)
|
||||
|
||||
update["abi"] = True
|
||||
|
||||
update["bucket"] = MOONSTREAM_SMARTCONTRACTS_ABI_BUCKET
|
||||
update[
|
||||
"s3_path"
|
||||
] = f"abi/v1/{subscription.resource_data['address']}/{subscription_id}/abi.json"
|
||||
|
||||
try:
|
||||
resource: BugoutResource = bc.update_resource(
|
||||
token=token,
|
||||
|
|
Ładowanie…
Reference in New Issue