Example #1
0
def IsServiceEnabled(project_id, service_name):
  """Return true if the service is enabled.

  Args:
    project_id: The ID of the project we want to query.
    service_name: The name of the service.

  Raises:
    exceptions.ListServicesPermissionDeniedException: if a 403 or 404
        error is returned by the List request.
    api_lib_exceptions.HttpException: Another miscellaneous error with the
        listing service.

  Returns:
    True if the service is enabled, false otherwise.
  """

  client = services_util.GetClientInstance()

  # Get the list of enabled services.
  request = services_util.GetEnabledListRequest(project_id)
  try:
    for service in list_pager.YieldFromList(
        client.services,
        request,
        batch_size_attribute='pageSize',
        field='services'):
      # If the service is present in the list of enabled services, return
      # True, otherwise return False
      if service.serviceName.lower() == service_name.lower():
        return True
  except apitools_exceptions.HttpError as e:
    _HandleStatusCode(e, exceptions.ListServicesPermissionDeniedException)
  return False
Example #2
0
  def Run(self, args):
    """Run 'services list'.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      The list of managed services for this project.
    """
    client = services_util.GetClientInstance()

    # Default mode is --enabled, so if no flags were specified,
    # turn on the args.enabled flag.
    if not (args.enabled or args.available):
      args.enabled = True

    validated_project = services_util.GetValidatedProject(args.project)

    if args.enabled:
      request = services_util.GetEnabledListRequest(validated_project)
    elif args.available:
      request = services_util.GetAvailableListRequest()

    return list_pager.YieldFromList(
        client.services,
        request,
        limit=args.limit,
        batch_size_attribute='pageSize',
        batch_size=args.page_size,
        field='services')
Example #3
0
def IsServiceEnabled(project_id, service_name):
    """Return true if the service is enabled.

  Args:
    project_id: The ID of the project we want to query.
    service_name: The name of the service.

  Raises:
    exceptions.ListServicesPermissionDeniedException: if a 403 or 404
        error is returned by the List request.
    apitools_exceptions.HttpError: Another miscellaneous error with the listing
        service.

  Returns:
    True if the service is enabled, false otherwise.
  """

    client = services_util.GetClientInstance()

    # Get the list of enabled services.
    request = services_util.GetEnabledListRequest(project_id)
    try:
        for service in list_pager.YieldFromList(
                client.services,
                request,
                batch_size_attribute='pageSize',
                field='services'):
            # If the service is present in the list of enabled services, return
            # True, otherwise return False
            if service.serviceName.lower() == service_name.lower():
                return True
    except (apitools_exceptions.HttpForbiddenError,
            apitools_exceptions.HttpNotFoundError) as e:
        # TODO(b/36865980): When backend supports it, differentiate errors.
        exceptions.ReraiseError(
            e, exceptions.ListServicesPermissionDeniedException)
    return False