Пример #1
0
def AutoscalersForLocations(zones, regions,
                            project, compute, http, batch_url,
                            fail_when_api_not_supported=True):
  """Finds all Autoscalers defined for a given project and locations.

  Args:
    zones: target zones
    regions: target regions
    project: project owning resources.
    compute: module representing compute api.
    http: communication channel.
    batch_url: batch url.
    fail_when_api_not_supported: If true, raise tool exception if API does not
        support autoscaling.
  Returns:
    A list of Autoscaler objects.
  """
  # Errors is passed through library calls and modified with
  # (ERROR_CODE, ERROR_MESSAGE) tuples.
  errors = []

  # Explicit list() is required to unwind the generator and make sure errors
  # are detected at this level.
  requests = []
  if zones:
    requests += lister.FormatListRequests(
        service=compute.autoscalers,
        project=project,
        scopes=zones,
        scope_name='zone',
        filter_expr=None)

  if regions:
    if hasattr(compute, 'regionAutoscalers'):
      requests += lister.FormatListRequests(
          service=compute.regionAutoscalers,
          project=project,
          scopes=regions,
          scope_name='region',
          filter_expr=None)
    else:
      if fail_when_api_not_supported:
        errors.append((None, 'API does not support regional autoscaling'))

  autoscalers = list(request_helper.MakeRequests(
      requests=requests,
      http=http,
      batch_url=batch_url,
      errors=errors,
      custom_get_requests=None))

  if errors:
    utils.RaiseToolException(
        errors,
        error_message='Could not check if the Managed Instance Group is '
        'Autoscaled.')

  return autoscalers
def AutoscalersForLocations(zones,
                            regions,
                            client,
                            fail_when_api_not_supported=True):
    """Finds all Autoscalers defined for a given project and locations.

  Args:
    zones: iterable of target zone references
    regions: iterable of target region references
    client: The compute client.
    fail_when_api_not_supported: If true, raise tool exception if API does not
        support autoscaling.
  Returns:
    A list of Autoscaler objects.
  """
    # Errors is passed through library calls and modified with
    # (ERROR_CODE, ERROR_MESSAGE) tuples.
    errors = []

    # Explicit list() is required to unwind the generator and make sure errors
    # are detected at this level.
    requests = []
    for project, zones in GroupByProject(zones).iteritems():
        requests += lister.FormatListRequests(
            service=client.apitools_client.autoscalers,
            project=project,
            scopes=sorted(set([zone_ref.zone for zone_ref in zones])),
            scope_name='zone',
            filter_expr=None)

    if regions:
        if hasattr(client.apitools_client, 'regionAutoscalers'):
            for project, regions in GroupByProject(regions).iteritems():
                requests += lister.FormatListRequests(
                    service=client.apitools_client.regionAutoscalers,
                    project=project,
                    scopes=sorted(
                        set([region_ref.region for region_ref in regions])),
                    scope_name='region',
                    filter_expr=None)
        else:
            if fail_when_api_not_supported:
                errors.append(
                    (None, 'API does not support regional autoscaling'))

    autoscalers = client.MakeRequests(requests=requests,
                                      errors_to_collect=errors)

    if errors:
        utils.RaiseToolException(
            errors,
            error_message='Could not check if the Managed Instance Group is '
            'Autoscaled.')

    return autoscalers
Пример #3
0
    def GetOwnerAccounts(self, owners):
        """Look up all users on the current project owned by the list of owners."""
        requests = []
        for owner in owners:
            requests += lister.FormatListRequests(self.service, self.project,
                                                  None, None,
                                                  'owner eq ' + owner)
        errors = []
        responses = request_helper.MakeRequests(requests=requests,
                                                http=self.http,
                                                batch_url=self.batch_url,
                                                errors=errors)

        if errors:
            utils.RaiseException(
                errors,
                users_client.UserException,
                error_message=('Could not get users for owners:'))
        return [response.name for response in responses]
    def GetOwnerAccounts(self, compute_client, client, owners):
        """Look up all users on the current project owned by the list of owners."""
        requests = []
        for owner in owners:
            requests += lister.FormatListRequests(
                client.users, properties.VALUES.core.project.GetOrFail(), None,
                None, 'owner eq ' + owner)
        errors = []
        responses = request_helper.MakeRequests(
            requests=requests,
            http=compute_client.apitools_client.http,
            batch_url=compute_client.batch_url,
            errors=errors)

        if errors:
            utils.RaiseException(
                errors,
                users_client.UserException,
                error_message=('Could not get users for owners:'))
        return [response.name for response in responses]