Handle API Gateway Tenant Registration in your Microservice
If you intend to publish your APIs on AWS Marketplace, you will need to do a few things.
Snowpal APIs are published on multiple API Hubs— AWS Marketplace being one of them. It does take a bit of work to support onboarding a new tenant if they are coming from the Marketplace.
Here’s a Ruby code snippet that shows one way to support this (works even when you have multiple API products).
Step 1
First, we’ll look at the onboard_customer method that’s called when user clicks through the subscription links on AWS Marketplace.
def onboard_customer(payload)
user = GatewayUser.user_by_email encrypt_text(payload[:email])
unless user
raise Errors::UserDoesNotExistError, "gateway user does not exist for email: #{payload[:email]}"
end
user.onboard_aws_subscriber payload
send_gateway_user_subscription_email
to_email: decrypt_text(user.email),
first_name: user.first_name || 'there',
additional_attrs: {
api_key: payload[:api_key],
api_product_code: payload[:aws_subscription_details][:productCode]
}
end
Step 2
Next, we’ll take a look at how an AWS Subscriber is onboarded. We extract certain attributes from the payload and persist it so we know which tenant the API Requests originate from when we handle those requests.
def onboard_aws_subscriber(payload)
token = payload[:"#{PlanitApp::GatewayService::AMZN_MKTPLACE_TOKEN}"]
aws_subscription = gateway_aws_subscriptions.detect { |x| x[:aws_marketplace_token] == token }
unless aws_subscription
raise Errors::UserDoesNotExistError, "gateway user does not exist for mktplace token: #{token}"
end
self.first_name = payload[:first_name]
self.last_name = encrypt_text payload[:last_name]
self.phone_number = encrypt_text payload[:phone_number]
self.org_name = payload[:org_name]
api_keys << payload[:api_key]
self.latest_api_key = payload[:api_key]
aws_subscription.associate_subscription_details payload
persist self
self
end
Let's break down what the above method does step by step:
It takes a
payload
parameter, which is presumably a hash containing various data needed for onboarding an AWS subscriber.It extracts a token from the payload using a key
PlanitApp::GatewayService::AMZN_MKTPLACE_TOKEN
. The exact implementation ofpayload[:"#{PlanitApp::GatewayService::AMZN_MKTPLACE_TOKEN}"]
is not visible in this code snippet, but it suggests extracting a token from the payload based on a predefined constant.It searches for an AWS subscription in `gateway_aws_subscriptions` that matches the extracted token.
If no AWS subscription is found (
aws_subscription is nil
), it raises an error of type `Errors::UserDoesNotExistError`, indicating that the gateway user does not exist for the given marketplace token.If an AWS subscription is found, it proceeds to update various attributes of the object on which this method is being called (likely an instance of a class) using data from the payload. Specifically:
It sets the
first_name
attribute to the value found in the payload.It encrypts and sets the
last_name
andphone_number
attributes using a methodencrypt_text
.It sets the
org_name
attribute to the value found in the payload.It appends an API key from the payload to an
api_keys
array associated with the object.It updates the
latest_api_key
attribute with the value found in the payload.
It calls a method
associate_subscription_details
on theaws_subscription
, passing the payload to it. This method presumably associates additional subscription details with the AWS subscription.It persists the changes made to the object by calling a method
persist
, passingself
(which refers to the current object) to it.Finally, it returns
self
which allows method chaining or simply returning the object after the onboarding process is completed.
Overall, this method handles the onboarding process for AWS subscribers, updating various attributes of an object and associating subscription details with it.
Have more questions? Reach out to us and we would be happy to help.
Snowpal Products
Backends as Services on AWS Marketplace
Mobile Apps on App Store and Play Store
Web App
Education Platform for Learners and Course Creators