def urls_for_zone()

in tools/dcgm-on-gke/grafana/proxy/get_proxy_url.py [0:0]


def urls_for_zone(zone, location_to_urls_map):
    """Returns list of potential proxy URLs for a given zone.

  Returns:
    List of possible URLs, in order of proximity.
  Args:
    zone: GCP zone
    location_to_urls_map: Maps region/country/continent to list of URLs, e.g.:
      {
        "us-west1" : [ us-west1-url ],
        "us-east1" : [ us-east1-url ],
        "us" : [ us-west1-url ],
        ...
      }
  """
    zone_match = re.match("((([a-z]+)-[a-z]+)\d+)-[a-z]", zone)
    if not zone_match:
        raise ValueError("Incorrect zone specified: {}".format(zone))

    # e.g. zone = us-west1-b
    region = zone_match.group(1)  # us-west1
    approx_region = zone_match.group(2)  # us-west
    country = zone_match.group(3)  # us

    urls = []
    if region in location_to_urls_map:
        urls.extend([
            url for url in location_to_urls_map[region] if url not in urls
        ])

    region_regex = re.compile("([a-z]+-[a-z]+)\d+")
    for location in location_to_urls_map:
        region_match = region_regex.match(location)
        if region_match and region_match.group(1) == approx_region:
            urls.extend([
                url for url in location_to_urls_map[location] if url not in urls
            ])

    if country in location_to_urls_map:
        urls.extend([
            url for url in location_to_urls_map[country] if url not in urls
        ])

    return urls