code: def default_services_region(service_catalog, request=None,
                              ks_endpoint=None):
      
      if service_catalog:
          available_regions = [get_endpoint_region(endpoint) for service
                               in service_catalog for endpoint
                               in service.get('endpoints', [])
                               if (service.get('type') is not None and
                                   service.get('type') != 'identity')]
          if not available_regions:
              # this is very likely an incomplete keystone setup
              LOG.warning('No regions could be found excluding identity.')
              available_regions = [get_endpoint_region(endpoint) for service
                                   in service_catalog for endpoint
                                   in service.get('endpoints', [])]
  
              if not available_regions:
                  # if there are no region setup for any service endpoint,
                  # this is a critical problem and it's not clear how this occurs
                  LOG.error('No regions can be found in the service catalog.')
                  return None
  
          region_options = []
          if request:
              region_options.append(request.COOKIES.get('services_region'))
          if ks_endpoint:
              default_service_regions = getattr(
                  settings, 'DEFAULT_SERVICE_REGIONS', {})
              region_options.append(default_service_regions.get(ks_endpoint))
          region_options.append(
              getattr(settings, 'DEFAULT_SERVICE_REGIONS', {}).get('*'))
  
          for region in region_options:
              if region in available_regions:
                  return region
          return available_regions[0]
      return None
docs: Return the default service region.
  
      Order of precedence:
      1. 'services_region' cookie value
      2. Matching endpoint in DEFAULT_SERVICE_REGIONS
      3. '*' key in DEFAULT_SERVICE_REGIONS
      4. First valid region from catalog
  
      In each case the value must also be present in available_regions or
      we move to the next level of precedence.