diff --git a/api/Account.py b/api/Account.py index acdfbf1..92e312f 100644 --- a/api/Account.py +++ b/api/Account.py @@ -12,3 +12,21 @@ def handle_account(account_msg: dict, procurement_api: ProcurementApi): account = procurement_api.get_account(account_id) logger.debug("got account", account=account) + if account and settings.private_offers_only: + approval = None + for account_approval in account["approvals"]: + if account_approval["name"] == "signup": + approval = account_approval + break + logger.debug("found approval", approval=approval) + + if approval: + if approval["state"] == "PENDING": + logger.debug("approving account in procurementApi") + procurement_api.approve_account(account_id) + + elif approval["state"] == "APPROVED": + logger.info("account is already approved, no action performed") + else: + logger.debug("no approval found") + # The account has been deleted \ No newline at end of file diff --git a/api/README.md b/api/README.md index 5eb9772..da0e832 100644 --- a/api/README.md +++ b/api/README.md @@ -7,6 +7,13 @@ From the api directory you can build and publish the app using the following com gcloud builds submit --tag /doit-easily:1.0 . +# Running the application locally + + cd api + docker build . -t doit-easily:local + gcloud auth login --cred-file='path/to/SA_key.json' + gcloud auth application-default login --impersonate-service-account=doit-easily@.iam.gserviceaccount.com + docker run -e PORT=8080 -v ./default_settings.toml:/config/custom-settings.toml -e GOOGLE_APPLICATION_CREDENTIALS=/creds.json -v $GOOGLE_APPLICATION_CREDENTIALS:/creds.json -p 8080:8080 -e LOG_LEVEL=debug docker.io/library/doit-easily:local # Configuration diff --git a/api/api.py b/api/api.py index 3fc5d06..8525207 100644 --- a/api/api.py +++ b/api/api.py @@ -51,6 +51,7 @@ def entitlements(): return render_template("index.html", **page_context) except Exception as e: + logger.debug("an exception occurred loading index", exception=traceback.format_exc()) logger.error(e) return {"error": "Loading failed"}, 500 @@ -76,10 +77,19 @@ def show_account(account_id): logger.error(e) return {"error": "Loading failed"}, 500 + @app.route("/login", methods=["POST"]) @app.route("/activate", methods=["POST"]) +@app.route("/login", methods=["GET"]) +@app.route("/activate", methods=["GET"]) def login(): add_request_context_to_log(str(uuid.uuid4())) + if settings.private_offers_only: + return "This listing is only sold through custom pricing. Please contact the vendor.", 200 + + if request.method == "GET": + return "This integration is running.", 200 + encoded = request.form.get("x-gcp-marketplace-token") logger.debug('encoded token', token=encoded) if not encoded: diff --git a/api/config.py b/api/config.py index ef594fb..c1bdeee 100644 --- a/api/config.py +++ b/api/config.py @@ -16,11 +16,14 @@ Validator("marketplace_project", must_exist=True, is_type_of=str), # the domain that hosts your product, such as `example-pro.com` Validator("audience", must_exist=True, is_type_of=str), + Validator("private_offers_only", must_exist=True, is_type_of=bool), + # optional. If set, slack notifications will be sent. Validator("auto_approve_entitlements", must_exist=True, is_type_of=bool), # optional. If set, slack notifications will be sent. Validator("slack_webhook", eq=None) | Validator("slack_webhook", is_type_of=str), # optional. If set, google pubsub will be used. Validator("event_topic", eq=None) | Validator("event_topic", is_type_of=str), + ) settings.validators.validate_all() diff --git a/api/default_settings.toml b/api/default_settings.toml index db752a8..e744804 100644 --- a/api/default_settings.toml +++ b/api/default_settings.toml @@ -1,6 +1,7 @@ [default] marketplace_project = '@none None' # This should be set to the name of the project you want to use, otherwise an error will be thrown. auto_approve_entitlements = false # This should be set to true if you want to auto-approve entitlements. +private_offers_only = false # This should be set to true if you want to auto-approve accounts and are only offering private offers. event_topic = '@none None' # Optional. The name of the topic to publish events to. slack_webhook = '@none None' # Optional. The webhook URL to send Slack messages to. audience = 'api.dns_zone_name' diff --git a/api/requirements.txt b/api/requirements.txt index 2b45698..d71ddca 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -1,12 +1,12 @@ -Flask==2.0.2 +Flask==2.3.2 gunicorn==20.1.0 google-cloud-pubsub==2.18.0 pg8000==1.24.0 structlog==21.5.0 -requests==2.31.0 +requests==2.32.0 google-api-python-client==2.94.0 ratelimit==2.2.1 backoff==2.1.2 dynaconf==3.1.9 pyjwt[crypto]==2.3.0 -Werkzeug==2.0.3 \ No newline at end of file +Werkzeug==3.0.3 \ No newline at end of file diff --git a/api/test/requirements.txt b/api/test/requirements.txt index e503079..7ef7602 100644 --- a/api/test/requirements.txt +++ b/api/test/requirements.txt @@ -1 +1 @@ -werkzeug==2.0.3 \ No newline at end of file +werkzeug==3.0.3 \ No newline at end of file