-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from SUSE-Enceladus/resolve_customer_service
Added resolve_customer lambda handler
- Loading branch information
Showing
13 changed files
with
517 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: CI-Code-Style | ||
|
||
on: | ||
push: | ||
branches: | ||
- "main" | ||
pull_request: | ||
|
||
jobs: | ||
unit_tests: | ||
name: Linter checks for SUSE SaaS tools | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.11"] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Python${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install Poetry | ||
run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install poetry | ||
- name: Run code checks | ||
run: | | ||
make -C aws/resolve_customer check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: CI-Unit-And-Types | ||
|
||
on: | ||
push: | ||
branches: | ||
- "main" | ||
pull_request: | ||
|
||
jobs: | ||
unit_tests: | ||
name: Unit and Static Type tests for SUSE SaaS tools | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.11"] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Python${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install Poetry | ||
run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install poetry | ||
- name: Run unit and type tests | ||
run: make -C aws/resolve_customer test | ||
env: | ||
PY_VER: ${{ matrix.python-version }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/python3 | ||
from resolve_customer.app import lambda_handler as resolve_customer_lambda | ||
|
||
|
||
def lambda_handler(event, context): | ||
return resolve_customer_lambda(event, context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Copyright (c) 2025 SUSE LLC. All rights reserved. | ||
# | ||
# This file is part of suse-saas-tools | ||
# | ||
# suse-saas-tools is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# mash is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with mash. If not, see <http://www.gnu.org/licenses/> | ||
# | ||
""" | ||
Lambda to retrieve customer information from | ||
AWS Metering/Entitlement Marketplace API's | ||
""" | ||
import json | ||
import base64 | ||
from typing import ( | ||
Dict, Union, List | ||
) | ||
|
||
from resolve_customer.customer import AWSCustomer | ||
from resolve_customer.entitlements import AWSCustomerEntitlement | ||
|
||
|
||
def lambda_handler(event, context): | ||
""" | ||
Expects event type matching the AWS API Gateway. | ||
Example event body: | ||
{ | ||
"x-amzn-marketplace-token": "some" | ||
} | ||
Example return: | ||
{ | ||
"statusCode": 200, | ||
"isBase64Encoded": False, | ||
"body": { | ||
"CustomerIdentifier": "id" | ||
"CustomerAWSAccountId": "account_id" | ||
"ProductCode": "product_code" | ||
"Entitlements": [ | ||
{ | ||
"CustomerIdentifier": "id", | ||
"Dimension": "some", | ||
"ExpirationDate": date, | ||
"ProductCode": "product_code", | ||
"Value": { | ||
"BooleanValue": true|false, | ||
"DoubleValue": number, | ||
"IntegerValue": number, | ||
"StringValue": "str" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
""" | ||
try: | ||
event_body = event['body'] | ||
if event.get('isBase64Encoded'): | ||
event_body = json.loads(base64.b64decode(event_body)) | ||
return json.dumps( | ||
process_event(event_body.get('x-amzn-marketplace-token')) | ||
) | ||
except Exception as error: | ||
return json.dumps( | ||
error_response(500, f'{type(error).__name__}: {error}') | ||
) | ||
|
||
|
||
def process_event( | ||
token: str | ||
) -> Dict[str, Union[str, int, Dict[str, Union[str, List]]]]: | ||
customer = AWSCustomer(token) | ||
if customer.error: | ||
return error_response(400, customer.error) | ||
entitlements = AWSCustomerEntitlement( | ||
customer.get_id(), customer.get_product_code() | ||
) | ||
if entitlements.error: | ||
return error_response(400, entitlements.error) | ||
return { | ||
'isBase64Encoded': False, | ||
'statusCode': 200, | ||
'body': { | ||
'CustomerIdentifier': customer.get_id(), | ||
'CustomerAWSAccountId': customer.get_account_id(), | ||
'ProductCode': customer.get_product_code(), | ||
'Entitlements': entitlements.get_entitlements() | ||
} | ||
} | ||
|
||
|
||
def error_response(status_code: int, message: str): | ||
return { | ||
'isBase64Encoded': False, | ||
'statusCode': status_code, | ||
'body': message | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright (c) 2025 SUSE LLC. All rights reserved. | ||
# | ||
# This file is part of suse-saas-tools | ||
# | ||
# suse-saas-tools is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# mash is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with mash. If not, see <http://www.gnu.org/licenses/> | ||
# | ||
import logging | ||
import boto3 | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
class AWSCustomer: | ||
""" | ||
Get AWS customer ID information from a marketplace token | ||
""" | ||
def __init__(self, token: str): | ||
self.customer = {} | ||
self.error = '' | ||
if token: | ||
try: | ||
marketplace = boto3.client('meteringmarketplace') | ||
self.customer = marketplace.resolve_customer( | ||
RegistrationToken=token | ||
) | ||
except Exception as error: | ||
self.error = f'meteringmarketplace client failed with: {error}' | ||
logger.error(self.error) | ||
else: | ||
self.error = 'no marketplace token provided' | ||
logger.error(self.error) | ||
|
||
def get_id(self) -> str: | ||
return self.__get('CustomerIdentifier') | ||
|
||
def get_account_id(self) -> str: | ||
return self.__get('CustomerAWSAccountId') | ||
|
||
def get_product_code(self) -> str: | ||
return self.__get('ProductCode') | ||
|
||
def __get(self, key) -> str: | ||
return self.customer[key] if self.customer else '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright (c) 2025 SUSE LLC. All rights reserved. | ||
# | ||
# This file is part of suse-saas-tools | ||
# | ||
# suse-saas-tools is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# mash is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with mash. If not, see <http://www.gnu.org/licenses/> | ||
# | ||
import logging | ||
import boto3 | ||
from typing import List | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
class AWSCustomerEntitlement: | ||
""" | ||
Get AWS customer entitlements for given customer ID and product code | ||
""" | ||
def __init__(self, customer_id: str, product_code: str): | ||
self.entitlements = {} | ||
self.error = '' | ||
if customer_id and product_code: | ||
try: | ||
marketplace = boto3.client('marketplace-entitlement') | ||
self.entitlements = marketplace.get_entitlements( | ||
{ | ||
'ProductCode': product_code, | ||
'Filter': {'CUSTOMER_IDENTIFIER': [customer_id]} | ||
} | ||
) | ||
except Exception as error: | ||
self.error = f'marketplace-entitlement client failed with: {error}' | ||
logger.error(self.error) | ||
else: | ||
self.error = 'no customer_id and/or product_code provided' | ||
logger.error(self.error) | ||
|
||
def get_entitlements(self) -> List[dict]: | ||
return self.entitlements.get('Entitlements') or [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[run] | ||
omit = | ||
*/version.py | ||
|
||
[report] | ||
omit = | ||
*/version.py |
Oops, something went wrong.