Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve-error-handling #10

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 18 additions & 36 deletions aws_arn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,20 @@ def generate_markdown_table():

def generate_arn(
resource_id,
resource_type,
sub_resource_type,
service,
sub_service,
region,
account,
partition,
):
try:
arn = aws_arn_data[resource_type][sub_resource_type]["arn_format"].format(
arn = aws_arn_data[service][sub_service]["arn_format"].format(
partition=partition, region=region, account=account, resource_id=resource_id
)
except KeyError as e:
print(
"Invalid resource {} or sub resource type {}".format(
resource_type, sub_resource_type
)
raise KeyError(
"Invalid resource {} or sub resource type {}".format(service, sub_service)
)
return False
return arn


Expand All @@ -99,8 +96,7 @@ def generate_arn_from_terraform(
partition,
):
if not validate_terraform(terraform):
print("Invalid Terraform resource")
return False
raise ValueError("Invalid Terraform resource: {}".format(terraform))
service, sub_service = get_service_from_terraform(terraform)
arn = generate_arn(resource_id, service, sub_service, region, account, partition)
return arn
Expand All @@ -114,8 +110,7 @@ def generate_arn_from_cloudformation(
partition,
):
if not validate_cloudformation(cloudformation):
print("Invalid CloudFormation resource")
return False
raise ValueError("Invalid CloudFormation resource: {}".format(cloudformation))
service, sub_service = get_service_from_cloudformation(cloudformation)
arn = generate_arn(resource_id, service, sub_service, region, account, partition)
return arn
Expand All @@ -129,8 +124,7 @@ def generate_arn_from_asff(
partition,
):
if not validate_asff(asff_resource):
print("Invalid ASFF resource")
return False
raise ValueError("Invalid ASFF resource: {}".format(asff_resource))
service, sub_service = get_service_from_asff(asff_resource)
arn = generate_arn(resource_id, service, sub_service, region, account, partition)
return arn
Expand Down Expand Up @@ -159,7 +153,7 @@ def parse_arn(arn):
"terraform": terraform,
"cloudformation": cloudformation,
}
print("Invalid ARN: {}".format(arn))
raise ValueError("Invalid ARN: {}".format(arn))


def get_service_from_arn(arn):
Expand Down Expand Up @@ -192,12 +186,9 @@ def get_resource_id_from_arn(arn):
try:
arn_format = aws_arn_data[service][sub_service]["arn_format"]
except KeyError:
print(
"Invalid resource type {} or sub resource type {}".format(
service, sub_service
)
raise KeyError(
"Invalid resource {} or sub resource type {}".format(service, sub_service)
)
return None

arn_parts = arn.split(":")
format_parts = arn_format.split(":")
Expand All @@ -224,12 +215,9 @@ def get_asff_from_arn(arn):
try:
asff_name = aws_arn_data[service][sub_service]["asff_name"]
except KeyError:
print(
"Invalid resource type {} or sub resource type {}".format(
service, sub_service
)
raise KeyError(
"Invalid resource {} or sub resource type {}".format(service, sub_service)
)
return None
return asff_name


Expand All @@ -239,12 +227,9 @@ def get_terraform_from_arn(arn):
try:
terraform = aws_arn_data[service][sub_service]["terraform"]
except KeyError:
print(
"Invalid resource type {} or sub resource type {}".format(
service, sub_service
)
raise KeyError(
"Invalid resource {} or sub resource type {}".format(service, sub_service)
)
return None
return terraform


Expand All @@ -254,12 +239,9 @@ def get_cloudformation_from_arn(arn):
try:
cloudformation = aws_arn_data[service][sub_service]["cloudformation"]
except KeyError:
print(
"Invalid resource type {} or sub resource type {}".format(
service, sub_service
)
raise KeyError(
"Invalid resource {} or sub resource type {}".format(service, sub_service)
)
return None
return cloudformation


Expand All @@ -275,7 +257,7 @@ def get_service(item):
return get_service_from_cloudformation(item)
if validate_asff(item):
return get_service_from_asff(item)
print("Invalid input to get service: {}".format(item))
raise ValueError("Invalid input to get service: {}".format(item))


def get_service_from_asff(asff_resource):
Expand Down
10 changes: 8 additions & 2 deletions aws_arn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ def main():

# Parse ARN
if args.parse_arn:
print(parse_arn(args.parse_arn))
try:
print(parse_arn(args.parse_arn))
except ValueError as e:
print(e)

# Get service
if args.get_service:
print(get_service(args.get_service))
try:
print(get_service(args.get_service))
except ValueError as e:
print(e)

if args.validate_id:
print(check_resource_id_regexp(id, service, sub_service))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="aws-arn",
version="0.0.14",
version="0.0.15",
description="A library to work with AWS ARNs",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
Expand Down