Created
February 22, 2022 12:18
-
-
Save toabctl/e5e5aa74ed0ceffaba8d049f53180285 to your computer and use it in GitHub Desktop.
AWS marketplace interactions
This file contains hidden or 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
#!/usr/bin/python3 | |
# Author: Thomas Bechtold <[email protected]> | |
import click | |
import boto3 | |
import json | |
import prettytable | |
client = boto3.client('marketplace-catalog', region_name='us-east-1') | |
def _entity_summary_list(): | |
next_token=None | |
entity_summary_list = [] | |
while True: | |
kwargs = { | |
'Catalog': 'AWSMarketplace', | |
'EntityType': 'AmiProduct', | |
} | |
if next_token: | |
kwargs['NextToken'] = next_token | |
entity_summary = client.list_entities(**kwargs) | |
entity_summary_list.extend(entity_summary['EntitySummaryList']) | |
if 'NextToken' in entity_summary: | |
next_token = entity_summary['NextToken'] | |
else: | |
break | |
return entity_summary_list | |
@click.group() | |
def cli(): | |
pass | |
@cli.command( | |
'entity-list', | |
help='Get a list of entities') | |
def entity_list(): | |
entity_summary_list = _entity_summary_list() | |
t = prettytable.PrettyTable() | |
t.field_names = ['entity-id', 'name', 'last modification'] | |
for ent in entity_summary_list: | |
kwargs = { | |
'Catalog': 'AWSMarketplace', | |
'EntityId': ent['EntityId'], | |
} | |
t.add_row([ent["EntityId"], | |
ent['Name'], | |
ent['LastModifiedDate']]) | |
print(t.get_string(sortby='name')) | |
@cli.command( | |
'product-code-list', | |
help='Get a list of product codes (this may take ~1 min)') | |
def product_code_list(): | |
entity_summary_list = _entity_summary_list() | |
t = prettytable.PrettyTable() | |
t.field_names = ['product', 'product code'] | |
for ent in entity_summary_list: | |
kwargs = { | |
'Catalog': 'AWSMarketplace', | |
'EntityId': ent['EntityId'], | |
} | |
entity_details = client.describe_entity(**kwargs) | |
details = json.loads(entity_details['Details']) | |
t.add_row([ent["Name"], details["Description"]["ProductCode"]]) | |
print(t.get_string(sortby='product')) | |
@cli.command( | |
'region-list', | |
help='Get a list of regions (this may take ~1 min)') | |
def region_list(): | |
entity_summary_list = _entity_summary_list() | |
t = prettytable.PrettyTable() | |
t.field_names = ['product', 'future regions', 'regions'] | |
t.align['product'] = 'l' | |
t.align['regions'] = 'l' | |
for ent in entity_summary_list: | |
kwargs = { | |
'Catalog': 'AWSMarketplace', | |
'EntityId': ent['EntityId'], | |
} | |
entity_details = client.describe_entity(**kwargs) | |
details = json.loads(entity_details['Details']) | |
t.add_row([ent["Name"], | |
details['RegionAvailability']['FutureRegionSupport'], | |
','.join(sorted(details['RegionAvailability']['Regions']))]) | |
print(t.get_string(sortby='product')) | |
@cli.command( | |
'entity-version-list', | |
help='Get a list of versions for a given entity') | |
@click.option('--entity-id', required=True) | |
def entity_version_list(entity_id): | |
entity_summary_list = _entity_summary_list() | |
t = prettytable.PrettyTable() | |
t.field_names = ['version', 'amis', 'visibility'] | |
kwargs = { | |
'Catalog': 'AWSMarketplace', | |
'EntityId': entity_id, | |
} | |
entity_details = client.describe_entity(**kwargs) | |
details = json.loads(entity_details['Details']) | |
print(details['RegionAvailability']) | |
for v in details['Versions']: | |
t.add_row([v['VersionTitle'], | |
', '.join([i["Image"] for i in v['Sources']]), | |
', '.join([i["Visibility"] for i in v['DeliveryOptions']])]) | |
print(t.get_string(sortby='version')) | |
if __name__ == '__main__': | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment