Created
February 27, 2023 05:42
-
-
Save toabctl/9238faa63f510a8a24f9d94a5f2860fa 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/env python | |
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( | |
'instance-type-missing-list', | |
help='Get a list of products where the given instance type is missing (this may take ~1 min)') | |
@click.argument('instance-type') | |
def instance_type_missing_list(instance_type): | |
entity_summary_list = _entity_summary_list() | |
t = prettytable.PrettyTable() | |
t.field_names = ['product'] | |
t.align['product'] = 'l' | |
t.align['instance_types'] = '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']) | |
instance_type_names = [d['Name'] for d in details['Dimensions']] | |
if instance_type not in instance_type_names: | |
t.add_row([ent["Name"]]) | |
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']: | |
import pprint; | |
pprint.pprint(v) | |
return | |
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