def medialive_channel_mediapackage_channel_ddb_items()

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


def medialive_channel_mediapackage_channel_ddb_items():
    """
    Identify and format MediaLive to MediaPackage channel connections for cache storage.
    """
    items = []
    ml_service_name = "medialive-channel-mediapackage-channel"
    try:
        # get medialive channels
        medialive_ch_cached = cache.cached_by_service("medialive-channel")
        # get mediapackage channels
        mediapackage_ch_cached = cache.cached_by_service(
            "mediapackage-channel")
        # compare each medialive output url to a mediapackage ingest url
        for ml_channel in medialive_ch_cached:
            ml_channel_data = json.loads(ml_channel["data"])
            for destination in ml_channel_data["Destinations"]:
                # if setting is empty, we have to connect medialive with mediapackage via channel ID
                if destination["MediaPackageSettings"]:
                    for mp_setting in destination["MediaPackageSettings"]:
                        for mp_channel in mediapackage_ch_cached:
                            mp_channel_data = json.loads(mp_channel["data"])
                            if mp_channel_data['Id'] == mp_setting[
                                    'ChannelId']:
                                pipelines_count = fetch_running_pipelines_count(
                                    ml_channel_data)
                                for pipeline in range(pipelines_count):
                                    # create a 'connection' out of matches
                                    config = {
                                        "from": ml_channel_data["Arn"],
                                        "to": mp_channel_data["Arn"],
                                        "pipeline": pipeline
                                    }
                                    print(config)
                                    items.append(
                                        connection_to_ddb_item_pl(
                                            ml_channel_data["Arn"],
                                            mp_channel_data["Arn"],
                                            ml_service_name, config))
                # otherwise we check via URL endpoints
                else:
                    for setting in destination["Settings"]:
                        ml_url = setting["Url"]
                        ml_url_v2 = None
                        # convert a mediapackage v1 ingest url to a v2 url before
                        # checking
                        parsed = urlparse(ml_url)
                        if parsed.path.startswith("/in/v1/"):
                            pieces = parsed.path.split("/")
                            if len(pieces) == 5:
                                ml_url_v2 = "{scheme}://{netloc}/in/v2/{uid}/{uid}/channel".format(
                                    scheme=parsed.scheme,
                                    netloc=parsed.netloc,
                                    uid=pieces[3])
                        for mp_channel in mediapackage_ch_cached:
                            mp_channel_data = json.loads(mp_channel["data"])
                            for ingest_endpoint in mp_channel_data[
                                    "HlsIngest"]["IngestEndpoints"]:
                                if ml_url == ingest_endpoint[
                                        "Url"] or ml_url_v2 == ingest_endpoint[
                                            "Url"]:
                                    # create a 'connection' out of matches
                                    config = {
                                        "from":
                                        ml_channel_data["Arn"],
                                        "to":
                                        mp_channel_data["Arn"],
                                        "pipeline":
                                        destination["Settings"].index(setting)
                                    }
                                    print(config)
                                    items.append(
                                        connection_to_ddb_item_pl(
                                            ml_channel_data["Arn"],
                                            mp_channel_data["Arn"],
                                            ml_service_name, config))
    except ClientError as error:
        print(error)
    return items