예제 #1
0
def export_csv(csv_file_path):
    global dynamodb
    global _table_name

    if _table_exists():
        table = dynamodb.Table(_table_name)
        response = table.scan()
        data = response["Items"]

        while "LastEvaluatedKey" in response:
            response = table.scan(
                ExclusiveStartKey=response["LastEvaluatedKey"])
            data.extend(response["Items"])
        keys = data[0].keys()
        with open(csv_file_path, "w", newline="") as output_file:
            dict_writer = csv.DictWriter(
                output_file,
                [
                    "resource_id", "type", "tag_name", "tag_value",
                    "delete(y/n)"
                ],
            )
            dict_writer.writeheader()
            dict_writer.writerows(data)

        print("Export completed.")
        continue_prompt()
예제 #2
0
def delete():
    global dynamodb
    global _table_name

    if _table_exists():
        table = dynamodb.Table(_table_name)
        table.delete()
        print(f"Deleted {_table_name} Table")
        continue_prompt()
    else:
        print(f"Table {_table_name} not exists")
        continue_prompt()
예제 #3
0
def create():
    global _table_name
    global dynamodb
    global _continue_printing

    if _table_exists():
        print("Table already exists. Delete the table and try again")
        continue_prompt()
        return
    print(f"Creating {_table_name} table")
    # Create the DynamoDB table.
    table = dynamodb.create_table(
        TableName=_table_name,
        KeySchema=[
            {
                "AttributeName": "resource_id",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "tag_name",
                "KeyType": "RANGE"
            },
        ],
        AttributeDefinitions=[
            {
                "AttributeName": "resource_id",
                "AttributeType": "S"
            },
            {
                "AttributeName": "tag_name",
                "AttributeType": "S"
            },
        ],
        ProvisionedThroughput={
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        },
    )

    start_print_progress()

    # Wait until the table exists.
    table.meta.client.get_waiter("table_exists").wait(TableName=_table_name)
    stop_print_progress()

    # Print out some data about the table.
    print("\nFinished creating table.")
    continue_prompt()
예제 #4
0
def import_data_from_csv(csv_file_path, replace=True):
    global dynamodb
    global _table_name

    if _table_exists():
        table = dynamodb.Table(_table_name)
        if replace == True:
            delete_all_records(prompt=False)
        print("Importing data")
        start_print_progress()
        with open(csv_file_path) as csv_file:
            csv_reader = csv.DictReader(csv_file, delimiter=",")
            for row in csv_reader:
                table.put_item(Item=row)
        stop_print_progress()
        print("Import completed.")
        continue_prompt()
예제 #5
0
def load_resources():
    global _resource_types
    global ec2

    # ec2_resource = boto3.resource("ec2", region_name=aws_region.get())
    ec2_client = boto3.client("ec2", region_name=aws_region.get())
    resource_types = json.load(open("./resource_types.json"))["resource_types"]

    resources = []
    for resource_type in resource_types:
        tag = resource_mappings[resource_type](ec2_client)
        resources.extend(tag)

    for resource in resources:
        print(resource)
    dynamo_table.insert_records(resources)
    continue_prompt()
예제 #6
0
def delete_all_records(prompt=True):
    global dynamodb
    global _table_name

    if _table_exists():
        table = dynamodb.Table(_table_name)
        scan = table.scan(
            ProjectionExpression="#k,resource_id",
            ExpressionAttributeNames={"#k": "tag_name"},
        )
        with table.batch_writer() as batch:
            for each in scan["Items"]:
                batch.delete_item(
                    Key={
                        "resource_id": each["resource_id"],
                        "tag_name": each["tag_name"],
                    })
        print(f"Deleted all records from {_table_name} Table")
        if prompt == True:
            continue_prompt()
    else:
        print(f"Table {_table_name} not exists")
        if continue_prompt == True:
            continue_prompt()
예제 #7
0
def apply():
    ec2Client = boto3.client("ec2", region_name=aws_region.get())
    data = dynamo_table.get_all_items()
    for item in data:
        delete = item["delete(y/n)"]
        if delete == "y":
            print(
                f'Deleting tag for resource -> {item["resource_id"]}, type -> {item["type"]}, tag -> {item["tag_name"]}, value -> {item["tag_value"]}'
            )
            ec2Response = ec2Client.delete_tags(
                DryRun=False,
                Resources=[item["resource_id"]],
                Tags=[{"Key": item["tag_name"], "Value": item["tag_value"]}],
            )
        else:
            print(
                f'Applying tag for resource -> {item["resource_id"]}, type -> {item["type"]}, tag -> {item["tag_name"]}, value -> {item["tag_value"]}'
            )
            ec2Response = ec2Client.create_tags(
                DryRun=False,
                Resources=[item["resource_id"]],
                Tags=[{"Key": item["tag_name"], "Value": item["tag_value"]}],
            )
    continue_prompt()
예제 #8
0
def print_profile():
    print(f"current profile is {aws_profile.get()}")
    continue_prompt()
예제 #9
0
def set_profile():
    print(f"Current Profile is {aws_profile.get()}")
    new_profile = input("Enter New profile : ")
    aws_profile.set(new_profile)
    print(f"Profile set to {aws_profile.get()}")
    continue_prompt()
예제 #10
0
def print_region():
    print(f"Current Region is {aws_region.get()}")
    continue_prompt()
예제 #11
0
def set_region():
    print(f"Current Region is {aws_region.get()}")
    new_region = input("Enter New Region : ")
    aws_region.set(new_region)
    print(f"Region set to {aws_region.get()}")
    continue_prompt()