Repository: Netflix/consoleme
File: consoleme/lib/policies.py (Python)

Code before:
        resource_name = resource_name.split("/")[-1]
    return resource_name


async def get_resource_sub_type_for_arn(arn: str) -> str:
    resource_name = arn.split(":")[5]
    if "/" in resource_name:
        return resource_name.split("/")[0]
    return ""


async def get_url_for_resource(
    arn,
    resource_type=None,
    account_id=None,
    region=None,
    resource_name=None,
    resource_sub_type=None,
):
    if not resource_type:
        resource_type = await get_resource_type_for_arn(arn)
    if not account_id:
        account_id = await get_resource_account(arn)
    if not region:
        region = await get_region_for_arn(arn)
    if not resource_name:
        resource_name = await get_resource_name_for_arn(arn)
    if not resource_sub_type:
        resource_sub_type = await get_resource_sub_type_for_arn(arn)
    url = ""
    if resource_type == "iam" and resource_sub_type == "role":
        resource_name = arn.split("/")[-1]
        url = f"/policies/edit/{account_id}/iamrole/{resource_name}"
    elif resource_type == "s3":
        url = f"/policies/edit/{account_id}/s3/{resource_name}"
    elif resource_type in ["sqs", "sns"]:
        url = f"/policies/edit/{account_id}/{resource_type}/{region}/{resource_name}"
    elif (resource_type == "AWS::CloudFormation::Stack") or (
        resource_type == "cloudformation" and resource_sub_type == "stack"
    ):
        url = f"/role/{account_id}?redirect=https://console.aws.amazon.com/cloudformation/home?region={region}#/stacks/"
    elif resource_type == "AWS::CloudFront::Distribution" or (
        resource_type == "cloudfront" and resource_sub_type == "distribution"
    ):
        url = f"/role/{account_id}?redirect=https://console.aws.amazon.com/cloudfront/home?%23distribution-settings:{resource_name}"
    elif resource_type == "AWS::CloudTrail::Trail" or (
        resource_type == "cloudtrail" and resource_sub_type == "trail"
    ):
        url = f"/role/{account_id}?redirect=https://console.aws.amazon.com/cloudtrail/home?region={region}%23/configuration"
    elif resource_type == "AWS::CloudWatch::Alarm" or (
        resource_type == "cloudwatch" and arn.split(":")[5] == "alarm"

Review comment (suggestion):
Nice stuff!

I think we need to handle the situation where `account_id` is `None` here. This function was generally only used for known resources previously. But if we pass in an S3 bucket ARN for an unknown resource, I'd expect this to be None. 

We should try to adhere to the relatively new WebResponse model (This is in consoleme.models) and return a 404 with a description. Weep may need to be updated to show the error form the backend

Code after:
    if "/" in resource_name:
        resource_name = resource_name.split("/")[-1]
    return resource_name


async def get_resource_sub_type_for_arn(arn: str) -> str:
    resource_name = arn.split(":")[5]
    if "/" in resource_name:
        return resource_name.split("/")[0]
    return ""


async def get_url_for_resource(
    arn,
    resource_type=None,
    account_id=None,
    region=None,
    resource_name=None,
    resource_sub_type=None,
):
    if not resource_type:
        resource_type = await get_resource_type_for_arn(arn)
    if not account_id:
        account_id = await get_resource_account(arn)
    if not region:
        region = await get_region_for_arn(arn)
    if not resource_name:
        resource_name = await get_resource_name_for_arn(arn)
    if not resource_sub_type:
        resource_sub_type = await get_resource_sub_type_for_arn(arn)

    # If account id is not found
    if not account_id:
        raise ResourceNotFound("The account for the given ARN could not be determined")
    url = ""
    if resource_type == "iam" and resource_sub_type == "role":
        resource_name = arn.split("/")[-1]
        url = f"/policies/edit/{account_id}/iamrole/{resource_name}"
    elif resource_type == "s3":
        url = f"/policies/edit/{account_id}/s3/{resource_name}"
    elif resource_type in ["sqs", "sns"]:
        url = f"/policies/edit/{account_id}/{resource_type}/{region}/{resource_name}"
    elif (resource_type == "AWS::CloudFormation::Stack") or (
        resource_type == "cloudformation" and resource_sub_type == "stack"
    ):
        url = f"/role/{account_id}?redirect=https://console.aws.amazon.com/cloudformation/home?region={region}#/stacks/"
    elif resource_type == "AWS::CloudFront::Distribution" or (
        resource_type == "cloudfront" and resource_sub_type == "distribution"
    ):
        url = f"/role/{account_id}?redirect=https://console.aws.amazon.com/cloudfront/home?%23distribution-settings:{resource_name}"
    elif resource_type == "AWS::CloudTrail::Trail" or (