Skip to content

Commit

Permalink
improve-error-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielsoltz committed Mar 23, 2024
1 parent 783c517 commit 2c0e5fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
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

0 comments on commit 2c0e5fc

Please sign in to comment.