From 6da8e778ccfe181397670f4f0066a5820eff17de Mon Sep 17 00:00:00 2001 From: Chris Holden Date: Tue, 19 Nov 2024 12:54:38 -0500 Subject: [PATCH] Add CLI to subscription manager script --- lambdas/link_fetcher/Pipfile | 4 +- lambdas/link_fetcher/Pipfile.lock | 21 +++---- ...subscription.py => manage_subscription.py} | 59 +++++++++++++++---- 3 files changed, 60 insertions(+), 24 deletions(-) rename lambdas/link_fetcher/{create_subscription.py => manage_subscription.py} (86%) diff --git a/lambdas/link_fetcher/Pipfile b/lambdas/link_fetcher/Pipfile index b1938ac..dfa60a1 100644 --- a/lambdas/link_fetcher/Pipfile +++ b/lambdas/link_fetcher/Pipfile @@ -31,12 +31,10 @@ types-humanfriendly = "*" ruff = "==0.7.1" uvicorn = "*" httpx = "*" +click = "*" [requires] python_version = "3.11" [pipenv] allow_prereleases = false - -[tool.ruff.lint.isort] -known-first-party = ["app", "db"] diff --git a/lambdas/link_fetcher/Pipfile.lock b/lambdas/link_fetcher/Pipfile.lock index a118942..2dae7ce 100644 --- a/lambdas/link_fetcher/Pipfile.lock +++ b/lambdas/link_fetcher/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "6843e84fd599e39b9aa527bfaae7462d055958b88e99b421959aafc50138242b" + "sha256": "10d6699a7f6d8b389921f581be00b53c2f5304f0f6045615dd9ca2500b94d759" }, "pipfile-spec": 6, "requires": { @@ -43,11 +43,11 @@ }, "botocore": { "hashes": [ - "sha256:0ca1200694a4c0a3fa846795d8e8a08404c214e21195eb9e010c4b8a4ca78a4a", - "sha256:2b8196bab0a997d206c3d490b52e779ef47dffb68c57c685443f77293aca1589" + "sha256:2f95c83f31c9e38a66995c88810fc638c829790e125032ba00ab081a2cf48cb9", + "sha256:bbd96bf7f442b1d5e35b36f501076e4a588c83d8d84a1952e9ee1d767e5efb3e" ], "markers": "python_version >= '3.8'", - "version": "==1.35.63" + "version": "==1.35.64" }, "certifi": { "hashes": [ @@ -486,12 +486,12 @@ }, "starlette": { "hashes": [ - "sha256:9834fd799d1a87fd346deb76158668cfa0b0d56f85caefe8268e2d97c3468b62", - "sha256:fbc189474b4731cf30fcef52f18a8d070e3f3b46c6a04c97579e85e6ffca942d" + "sha256:0e4ab3d16522a255be6b28260b938eae2482f98ce5cc934cb08dce8dc3ba5835", + "sha256:44cedb2b7c77a9de33a8b74b2b90e9f50d11fcf25d8270ea525ad71a25374ff7" ], "index": "pypi", "markers": "python_version >= '3.8'", - "version": "==0.41.2" + "version": "==0.41.3" }, "typing-extensions": { "hashes": [ @@ -566,11 +566,11 @@ }, "botocore": { "hashes": [ - "sha256:0ca1200694a4c0a3fa846795d8e8a08404c214e21195eb9e010c4b8a4ca78a4a", - "sha256:2b8196bab0a997d206c3d490b52e779ef47dffb68c57c685443f77293aca1589" + "sha256:2f95c83f31c9e38a66995c88810fc638c829790e125032ba00ab081a2cf48cb9", + "sha256:bbd96bf7f442b1d5e35b36f501076e4a588c83d8d84a1952e9ee1d767e5efb3e" ], "markers": "python_version >= '3.8'", - "version": "==1.35.63" + "version": "==1.35.64" }, "certifi": { "hashes": [ @@ -769,6 +769,7 @@ "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28", "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de" ], + "index": "pypi", "markers": "python_version >= '3.7'", "version": "==8.1.7" }, diff --git a/lambdas/link_fetcher/create_subscription.py b/lambdas/link_fetcher/manage_subscription.py similarity index 86% rename from lambdas/link_fetcher/create_subscription.py rename to lambdas/link_fetcher/manage_subscription.py index 79790d7..62bbdb0 100644 --- a/lambdas/link_fetcher/create_subscription.py +++ b/lambdas/link_fetcher/manage_subscription.py @@ -26,7 +26,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -Modifications are largely replacing Pydantic with "dataclasses" from stdlib. +Modifications are largely replacing Pydantic with "dataclasses" from stdlib and +adding a CLI to run list/create/terminate subscriptions. """ import datetime as dt @@ -34,8 +35,10 @@ import os import urllib.parse from dataclasses import dataclass +from typing import Literal import boto3 +import click import requests from app.subscription_endpoint import EndpointConfig @@ -183,7 +186,6 @@ def create_subscription(self) -> str: ) subscription_data = { "StageOrder": True, - # example filter - You can adjust value to Your desired product type, collection etc. "FilterParam": "Attributes/OData.CSC.StringAttribute/any(att:att/Name eq 'productType' and att/OData.CSC.StringAttribute/Value eq 'S2MSI1C')", "Priority": 1, "NotificationEndpoint": endpoint_url, @@ -242,22 +244,57 @@ def terminate_subscription(self, subscription_id: str): print("Subscription terminated. Its ID was " + subscription_id + ".") -if __name__ == "__main__": +@click.command() +@click.argument("command", type=click.Choice(["create", "list", "terminate"])) +@click.option( + "--email", + default=lambda: os.getenv("ESA_CDSE_EMAIL", ""), + help="CDSE user email for subscription", +) +@click.option( + "--password", + default=lambda: os.getenv("ESA_CDSE_PASSWORD", ""), + prompt=True, + help="CDSE user password for subscription", +) +def main( + command: Literal["create", "list", "terminate"], + email: str, + password: str, +): + """Manage ESA 'push' subscriptions""" endpoint_cfg = EndpointConfig.load_from_secrets_manager(os.environ["STAGE"]) - subscription_cfg = SubscriptionAPIConfig() + subscription_cfg = SubscriptionAPIConfig( + user_email=email, + user_password=password, + ) token_api = TokenAPI(subscription_cfg) subscription_api = SubscriptionAPI(token_api, endpoint_cfg) subscriptions = subscription_api.list_subscriptions() - if not subscriptions: - print("Creating new subscription") - sub = subscription_api.create_subscription() - print(sub) - else: + if command == "create": + if subscriptions: + click.echo( + "Cannot create a second subscriptions (only 1 active is allowed)" + ) + raise click.Abort() + subscription = subscription_api.create_subscription() + click.echo(f"Created subscription id={subscription}") + + elif command == "list": + click.echo("Listing subscriptions:") + for subscription in subscriptions: + click.echo(subscription) + + elif command == "terminate": subscription_id = subscriptions[0]["Id"] - print(f"Terminating subscription id={subscription_id}") + click.echo("Terminating first listed subscription id={subscription_id}") subscription_api.terminate_subscription(subscription_id) - print("DONE") + click.echo("Complete") + + +if __name__ == "__main__": + main()