def mediapackage_endpoint_cloudfront_distribution_by_tag_ddb_items()

in source/msam/chalicelib/connections.py [0:0]


def mediapackage_endpoint_cloudfront_distribution_by_tag_ddb_items():
    """
    Identify and format MediaPackage origin endpoints to CloudFront Distributions by tags for cache storage.
    """
    items = []
    try:
        # get CloudFront distros
        cloudfront_distros_cached = cache.cached_by_service(
            "cloudfront-distribution")
        # get MediaPackage channels
        mediapackage_ch_cached = cache.cached_by_service(
            "mediapackage-channel")
        # get MediaPackage origin endpoints
        mediapackage_ep_cached = cache.cached_by_service(
            "mediapackage-origin-endpoint")
        # iterate over all distributions
        for distro in cloudfront_distros_cached:
            distro_data = json.loads(distro["data"])
            for key, value in distro_data["Tags"].items():
                if (key in [
                        "MP-Endpoint-ARN", "mediapackage:cloudfront_assoc"
                ]) and ":channels/" in value:
                    channel_arn = value
                    channel_id = None
                    # find the channel
                    for channel in mediapackage_ch_cached:
                        if channel_arn == channel["arn"]:
                            channel_data = json.loads(channel["data"])
                            channel_id = channel_data["Id"]
                            break
                    if channel_id:
                        # add a connection to each endpoint
                        for endpoint in mediapackage_ep_cached:
                            endpoint_data = json.loads(endpoint["data"])
                            if endpoint_data["ChannelId"] == channel_id:
                                scheme = "https"
                                # URL is in diff loc for CMAF
                                if "CmafPackage" in endpoint_data:
                                    scheme = urlparse(endpoint_data["CmafPackage"]["HlsManifests"][0]["Url"]).scheme
                                else:
                                    scheme = urlparse(endpoint_data["Url"]).scheme
                                config = {
                                    "from": endpoint["arn"],
                                    "to": distro["arn"],
                                    "scheme": scheme,
                                    "connected_by": "tag",
                                    "tag": key
                                }
                                print(config)
                                items.append(
                                    connection_to_ddb_item(
                                        endpoint["arn"], distro["arn"],
                                        "mediapackage-origin-endpoint-cloudfront-distribution",
                                        config))
    except ClientError as error:
        print(error)
    return items