def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: The updated sink with its new destination. """ # One of the flags is required to update the sink. # log_filter can be an empty string, so check explicitly for None. if not args.destination and args.log_filter is None: raise exceptions.ToolException( '[destination] or --log-filter argument is required') # Calling Update on a non-existing sink creates it. # We need to make sure it exists, otherwise we would create it. try: if args.log: sink = self.GetLogSink() elif args.service: sink = self.GetLogServiceSink() else: sink = self.GetProjectSink() except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error)) # Only update fields that were passed to the command. if args.destination: destination = args.destination else: destination = sink.destination if args.log_filter is not None: log_filter = args.log_filter else: log_filter = sink.filter sink_ref = self.context['sink_reference'] updated_sink = self.context['logging_messages'].LogSink( name=sink_ref.sinksId, destination=destination, filter=log_filter) try: if args.log: result = self.UpdateLogSink(updated_sink) elif args.service: result = self.UpdateLogServiceSink(updated_sink) else: result = self.UpdateProjectSink(updated_sink) log.UpdatedResource(sink_ref) return result except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: The updated metric. """ # One of the flags is required to update the metric. if not (args.description or args.filter): raise exceptions.ToolException( '--description or --filter argument is required') client = self.context['logging_client'] messages = self.context['logging_messages'] project = properties.VALUES.core.project.Get(required=True) # Calling the API's Update method on a non-existing metric creates it. # Make sure the metric exists so we don't accidentally create it. try: metric = client.projects_metrics.Get( messages.LoggingProjectsMetricsGetRequest( metricsId=args.metric_name, projectsId=project)) except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error)) if args.description: metric_description = args.description else: metric_description = metric.description if args.filter: metric_filter = args.filter else: metric_filter = metric.filter updated_metric = messages.LogMetric(name=args.metric_name, description=metric_description, filter=metric_filter) try: result = client.projects_metrics.Update( messages.LoggingProjectsMetricsUpdateRequest( logMetric=updated_metric, metricsId=args.metric_name, projectsId=project)) log.UpdatedResource(args.metric_name) return result except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: The list of sinks. """ project = properties.VALUES.core.project.Get(required=True) try: if args.log: results = self.ListLogSinks(project, args.log) elif args.service: results = self.ListLogServiceSinks(project, args.service) elif args.only_project_sinks: results = self.ListProjectSinks(project) else: return self.YieldAllSinks(project, args.limit) if args.limit > 0: return results.sinks[:args.limit] else: return list(results.sinks) except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: The created sink with its destination. """ if not (args.log or args.service or args.log_filter): # Attempt to create a project sink with an empty filter. if not console_io.PromptContinue( 'Really create sink [%s] with an empty filter?' % args.sink_name): raise exceptions.ToolException('action canceled by user') sink_ref = self.context['sink_reference'] new_sink = self.context['logging_messages'].LogSink( name=sink_ref.sinksId, destination=args.destination) if args.log_filter: new_sink.filter = args.log_filter try: if args.log: result = self.CreateLogSink(new_sink) elif args.service: result = self.CreateLogServiceSink(new_sink) else: result = self.CreateProjectSink(new_sink) log.CreatedResource(sink_ref) return result except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. """ client = self.context['logging_client'] messages = self.context['logging_messages'] project = properties.VALUES.core.project.Get(required=True) severity_value = getattr( messages.LogEntryMetadata.SeverityValueValuesEnum, args.severity.upper()) labels = messages.LogEntryMetadata.LabelsValue() labels.additionalProperties = [ messages.LogEntryMetadata.LabelsValue.AdditionalProperty( key='compute.googleapis.com/resource_type', value='instance'), messages.LogEntryMetadata.LabelsValue.AdditionalProperty( key='compute.googleapis.com/resource_id', value='sent with gcloud'), ] # Cloud Logging uses RFC 3339 time format. rfc3339_format = '%Y-%m-%dT%H:%M:%SZ' meta = messages.LogEntryMetadata( timestamp=datetime.datetime.utcnow().strftime(rfc3339_format), severity=severity_value, serviceName='compute.googleapis.com', labels=labels) entry = messages.LogEntry(metadata=meta) if args.payload_type == 'struct': json_object = util.ConvertToJsonObject(args.message) struct = messages.LogEntry.StructPayloadValue() struct.additionalProperties = [ messages.LogEntry.StructPayloadValue.AdditionalProperty( key=json_property.key, value=json_property.value) for json_property in json_object.properties ] entry.structPayload = struct else: entry.textPayload = args.message request = messages.WriteLogEntriesRequest(entries=[entry]) try: unused_result = client.projects_logs_entries.Write( messages.LoggingProjectsLogsEntriesWriteRequest( projectsId=project, logsId=args.log_name, writeLogEntriesRequest=request)) log.status.write('Created log entry.\n') except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def Display(self, unused_args, result): """This method is called to print the result of the Run() method. Args: unused_args: The arguments that command was run with. result: The value returned from the Run() method. """ try: list_printer.PrintResourceList('logging.metrics', result) except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def Display(self, unused_args, result): """This method is called to print the result of the Run() method. Args: unused_args: The arguments that command was run with. result: The value returned from the Run() method. """ try: # Custom selector to return user friendly log names. selector = ('NAME', lambda log: util.ExtractLogName(log.name)) console_io.PrintExtendedList(result, (selector, )) except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: The specified sink with its destination. """ try: if args.log: return self.GetLogSink() elif args.service: return self.GetLogServiceSink() else: return self.GetProjectSink() except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: The specified metric with its description and configured filter. """ client = self.context['logging_client'] messages = self.context['logging_messages'] project = properties.VALUES.core.project.Get(required=True) try: return client.projects_metrics.Get( messages.LoggingProjectsMetricsGetRequest( metricsId=args.metric_name, projectsId=project)) except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. """ client = self.context['logging_client'] messages = self.context['logging_messages'] project = properties.VALUES.core.project.Get(required=True) if not console_io.PromptContinue( 'Really delete all log entries from [%s]?' % args.log_name): raise exceptions.ToolException('action canceled by user') try: client.projects_logs.Delete( messages.LoggingProjectsLogsDeleteRequest( projectsId=project, logsId=args.log_name)) log.DeletedResource(args.log_name) except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. """ sink_ref = self.context['sink_reference'] if args.log: sink_description = 'log sink [%s] from [%s]' % (sink_ref.sinksId, sink_ref.logsId) elif args.service: sink_description = 'log-service sink [%s] from [%s]' % ( sink_ref.sinksId, sink_ref.logServicesId) else: sink_description = 'project sink [%s]' % sink_ref.sinksId if not console_io.PromptContinue( 'Really delete %s?' % sink_description): raise exceptions.ToolException('action canceled by user') try: if args.log: self.DeleteLogSink() elif args.service: self.DeleteLogServiceSink() else: self.DeleteProjectSink() log.DeletedResource(sink_ref) except apitools_base.HttpError as error: project_sink = not args.log and not args.service # Suggest the user to add --log or --log-service flag. if project_sink and error.status_code == 404: log.Print( ('Project sink was not found. ' 'Did you forget to add --log or --log-service flag?')) raise exceptions.HttpException(util.GetError(error))
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: The created metric. """ client = self.context['logging_client'] messages = self.context['logging_messages'] project = properties.VALUES.core.project.Get(required=True) new_metric = messages.LogMetric(name=args.metric_name, description=args.description, filter=args.filter) try: result = client.projects_metrics.Create( messages.LoggingProjectsMetricsCreateRequest( projectsId=project, logMetric=new_metric)) log.CreatedResource(args.metric_name) return result except apitools_base.HttpError as error: raise exceptions.HttpException(util.GetError(error))