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
def Run(self, args): """Run 'services operations list'. Args: args: argparse.Namespace, The arguments that this command was invoked with. Returns: The list of operations for this project. """ messages = services_util.GetMessagesModule() client = services_util.GetClientInstance() service = arg_parsers.GetServiceNameFromArg(args.service) msg_filter = 'serviceName="{0}"'.format(service) if args.filter: msg_filter += ' AND ({0})'.format(args.filter) args.filter = None # We don't want Display() to handle the filter. msg = messages.ServicemanagementOperationsListRequest(filter=msg_filter) return list_pager.YieldFromList( client.operations, msg, limit=args.limit, batch_size_attribute='pageSize', batch_size=args.page_size, field='operations')
def Run(self, args): """Run 'services operations describe'. Args: args: argparse.Namespace, The arguments that this command was invoked with. Returns: The response from the operations.Get API call. """ messages = services_util.GetMessagesModule() client = services_util.GetClientInstance() operation_id = arg_parsers.GetOperationIdFromArg(args.operation) request = messages.ServicemanagementOperationsGetRequest( operationsId=operation_id, ) operation = client.operations.Get(request) if (sys.getsizeof(str(operation.response)) > MAX_RESPONSE_BYTES and not args.full): log.warning('Response portion of operation resource redacted. ' 'Use --full to see the whole Operation.\n') operation.response = None # Set async to True because we don't need to wait for the operation # to complete to check the status of it. return services_util.GetProcessedOperationResult(operation, async=True)
def EnableServiceApiCall(project_id, service_name): """Make API call to enable a specific API. 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. Raises: exceptions.EnableServicePermissionDeniedException: when enabling the API fails. api_lib_exceptions.HttpException: Another miscellaneous error with the enabling service. Returns: The result of the Enable operation """ client = services_util.GetClientInstance() messages = services_util.GetMessagesModule() request = messages.ServicemanagementServicesEnableRequest( serviceName=service_name, enableServiceRequest=messages.EnableServiceRequest( consumerId='project:' + project_id ) ) try: return client.services.Enable(request) except apitools_exceptions.HttpError as e: _HandleStatusCode(e, exceptions.EnableServicePermissionDeniedException)
def EnableServiceApiCall(project_id, service_name): """Make API call to enable a specific API. 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. Raises: exceptions.EnableServicePermissionDeniedException: when enabling the API fails. apitools_exceptions.HttpError: Another miscellaneous error with the enabling service. Returns: The result of the Enable operation """ client = services_util.GetClientInstance() messages = services_util.GetMessagesModule() request = messages.ServicemanagementServicesEnableRequest( serviceName=service_name, enableServiceRequest=messages.EnableServiceRequest( consumerId='project:' + project_id)) try: return client.services.Enable(request) except (apitools_exceptions.HttpForbiddenError, apitools_exceptions.HttpNotFoundError) as e: # TODO(b/36865980): When backend supports it, differentiate errors. exceptions.ReraiseError( e, exceptions.EnableServicePermissionDeniedException)
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')
def Run(self, args): """Run 'service-management operations wait'. Args: args: argparse.Namespace, The arguments that this command was invoked with. Returns: If successful, the response from the operations.Get API call. """ messages = services_util.GetMessagesModule() client = services_util.GetClientInstance() operation_id = arg_parsers.GetOperationIdFromArg(args.operation) request = messages.ServicemanagementOperationsGetRequest( operationsId=operation_id, ) operation = client.operations.Get(request) return services_util.ProcessOperationResult(operation, async=False)
def Run(self, args): """Run 'service-management disable'. Args: args: argparse.Namespace, The arguments that this command was invoked with. Returns: Nothing. """ messages = services_util.GetMessagesModule() client = services_util.GetClientInstance() project = properties.VALUES.core.project.Get(required=True) for service_name in args.service: service_name = arg_parsers.GetServiceNameFromArg(service_name) request = messages.ServicemanagementServicesDisableRequest( serviceName=service_name, disableServiceRequest=messages.DisableServiceRequest( consumerId='project:' + project)) operation = client.services.Disable(request) services_util.ProcessOperationResult(operation, args.async)
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