Exemplo n.º 1
0
def DetermineBuildRegion(build_config, desired_region=None):
    """Determine what region of the GCB service this build should be sent to.

  Args:
    build_config: apitools.base.protorpclite.messages.Message, The Build message
      to analyze.
    desired_region: str, The region requested by the user, if any.

  Raises:
    RegionMismatchError: If the config conflicts with the desired region.

  Returns:
    str, The region that the build should be sent to, or None if it should be
    sent to the global region.
  """
    # If the build is configured to run in a regional worker pool, use the worker
    # pool's resource ID to determine which regional GCB service to send it to.
    wp_options = build_config.options
    if not wp_options:
        return desired_region
    wp_resource = wp_options.workerPool
    if not wp_resource:
        return desired_region
    if not cloudbuild_util.IsRegionalWorkerPool(wp_resource):
        return desired_region
    wp_region = cloudbuild_util.RegionalWorkerPoolRegion(wp_resource)
    # If the user told us to hit a different region, then they made a mistake.
    if desired_region and desired_region != wp_region:
        raise RegionMismatchError(desired_region, wp_region)
    return wp_region
 def testInvalid(self):
     with self.assertRaisesRegex(ValueError, '.*'):
         cloudbuild_util.RegionalWorkerPoolRegion('badresource')
 def testValid(self):
     self.assertEqual(
         cloudbuild_util.RegionalWorkerPoolRegion(
             'projects/abc/locations/def/workerPools/ghi'), 'def')