예제 #1
0
    def Run(self, args):
        """Run 'service-management list'.

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

    Returns:
      The list of managed services for this project.
    """
        if args.simple_list:
            args.format = 'value(serviceName)'

        # Default mode is --enabled, so if no flags were specified,
        # turn on the args.enabled flag.
        if not (args.enabled or args.available or args.produced):
            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(validated_project)
        elif args.produced:
            request = services_util.GetProducedListRequest(validated_project)

        return list_pager.YieldFromList(self.services_client.services,
                                        request,
                                        limit=args.limit,
                                        batch_size_attribute='pageSize',
                                        batch_size=args.page_size,
                                        field='services')
예제 #2
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
예제 #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.

  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)
    services = 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
    for service in services:
        if service.serviceName.lower() == service_name.lower():
            return True
    return False
예제 #4
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:
    services_util.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 exceptions.HttpError as e:
        if e.status_code in [403, 404]:
            msg = json.loads(e.content).get('error', {}).get('message', '')
            raise services_util.ListServicesPermissionDeniedException(msg)
        raise api_lib_exceptions.HttpException(e)
    return False
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.
        _ReraiseError(e, exceptions.ListServicesPermissionDeniedException)
    return False
예제 #6
0

def EnableServiceIfDisabled(project_id, service_name, async=False):
    """Check to see if the service is enabled, and if it is not, do so.

  Args:
    project_id: The ID of the project for which to enable the service.
    service_name: The name of the service to enable on the project.
    async: bool, if True, return the operation ID immediately, without waiting
           for the op to complete.
  """

    client = services_util.GetClientInstance()

    # Get the list of enabled services.
    request = services_util.GetEnabledListRequest(project_id)
    services = list_pager.YieldFromList(client.services,
                                        request,
                                        batch_size_attribute='pageSize',
                                        field='services')

    # If the service is already present in the list of enabled services, return
    # early, otherwise, enable the service.
    for service in services:
        if service.serviceName.lower() == service_name.lower():
            log.debug(
                'Service [{0}] is already enabled for project [{1}]'.format(
                    service_name, project_id))
            return

    # If the service is not yet enabled, enable it