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_filter: # Attempt to create a sink with an empty filter. console_io.PromptContinue( 'Sink with empty filter matches all entries.', cancel_on_no=True) if args.include_children and not (args.organization or args.folder): log.warn('include-children only has an effect for sinks at the folder ' 'or organization level') sink_ref = util.GetSinkReference(args.sink_name, args) sink_data = { 'name': sink_ref.sinksId, 'destination': args.destination, 'filter': args.log_filter, 'includeChildren': args.include_children } result = self.CreateSink(util.GetParentFromArgs(args), sink_data) log.CreatedResource(sink_ref) self._epilog_result_destination = result.destination self._epilog_writer_identity = result.writerIdentity return result
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 calliope_exceptions.MinimumArgumentException( ['[destination]', '--log-filter'], 'Please specify at least one property to update') sink_ref = util.GetSinkReference(args.sink_name, args) # Calling Update on a non-existing sink creates it. # We need to make sure it exists, otherwise we would create it. sink = self.GetSink(util.GetParentFromArgs(args), sink_ref) # 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_data = { 'name': sink_ref.sinksId, 'destination': destination, 'filter': log_filter, 'includeChildren': sink.includeChildren, 'startTime': sink.startTime, 'endTime': sink.endTime } # Check for legacy configuration, and let users decide if they still want # to update the sink with new settings. if 'cloud-logs@' in sink.writerIdentity: console_io.PromptContinue( 'This update will create a new writerIdentity (service account) for ' 'the sink. In order for the sink to continue working, grant that ' 'service account correct permission on the destination. The service ' 'account will be displayed after a successful update operation.', cancel_on_no=True, default=False) result = self.UpdateSink(util.GetParentFromArgs(args), sink_data) log.UpdatedResource(sink_ref) self._epilog_result_destination = result.destination self._epilog_writer_identity = result.writerIdentity return result
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. """ util.CheckLegacySinksCommandArguments(args) sink_ref = util.GetSinkReference(args.sink_name, args.log, args.service) try: if args.log: return util.TypedLogSink(self.GetLogSink(sink_ref), log_name=args.log) elif args.service: return util.TypedLogSink(self.GetLogServiceSink(sink_ref), service_name=args.service) else: return util.TypedLogSink( self.GetSink(util.GetParentFromArgs(args), sink_ref)) except apitools_exceptions.HttpError as error: v2_sink = not args.log and not args.service # Suggest the user to add --log or --log-service flag. if v2_sink and exceptions.HttpException( error).payload.status_code == 404: log.status.Print( ('Sink was not found. ' 'Did you forget to add --log or --log-service flag?')) raise error
def _Run(self, args, is_alpha=False): if not args.log_filter: # Attempt to create a sink with an empty filter. console_io.PromptContinue( 'Sink with empty filter matches all entries.', cancel_on_no=True) if args.include_children and not (args.organization or args.folder): log.warning( 'include-children only has an effect for sinks at the folder ' 'or organization level') sink_ref = util.GetSinkReference(args.sink_name, args) sink_data = { 'name': sink_ref.sinksId, 'destination': args.destination, 'filter': args.log_filter, 'includeChildren': args.include_children } dlp_options = {} if is_alpha: if args.IsSpecified('dlp_inspect_template'): dlp_options['inspectTemplateName'] = args.dlp_inspect_template if args.IsSpecified('dlp_deidentify_template'): dlp_options[ 'deidentifyTemplateName'] = args.dlp_deidentify_template if dlp_options: sink_data['dlpOptions'] = dlp_options if args.IsSpecified('use_partitioned_tables'): bigquery_options = {} bigquery_options[ 'usePartitionedTables'] = args.use_partitioned_tables sink_data['bigqueryOptions'] = bigquery_options if args.IsSpecified('exclusion'): sink_data['exclusions'] = args.exclusion if args.IsSpecified('description'): sink_data['description'] = args.description if args.IsSpecified('disabled'): sink_data['disabled'] = args.disabled result = self.CreateSink(util.GetParentFromArgs(args), sink_data) log.CreatedResource(sink_ref) self._epilog_result_destination = result.destination self._epilog_writer_identity = result.writerIdentity self._epilog_is_dlp_sink = bool(dlp_options) return result
def _Run(self, args, support_dlp=False): sink_ref = util.GetSinkReference(args.sink_name, args) sink_data = {'name': sink_ref.sinksId} update_mask = [] if args.IsSpecified('destination'): sink_data['destination'] = args.destination update_mask.append('destination') if args.IsSpecified('log_filter'): sink_data['filter'] = args.log_filter update_mask.append('filter') parameter_names = ['[destination]', '--log-filter'] dlp_options = {} if support_dlp: parameter_names.extend( ['--dlp-inspect-template', '--dlp-deidentify-template']) if args.IsSpecified('dlp_inspect_template'): dlp_options['inspectTemplateName'] = args.dlp_inspect_template update_mask.append('dlp_options.inspect_template_name') if args.IsSpecified('dlp_deidentify_template'): dlp_options[ 'deidentifyTemplateName'] = args.dlp_deidentify_template update_mask.append('dlp_options.deidentify_template_name') if dlp_options: sink_data['dlpOptions'] = dlp_options if not update_mask: raise calliope_exceptions.MinimumArgumentException( parameter_names, 'Please specify at least one property to update') # Check for legacy configuration, and let users decide if they still want # to update the sink with new settings. sink = self.GetSink(util.GetParentFromArgs(args), sink_ref) if 'cloud-logs@' in sink.writerIdentity: console_io.PromptContinue( 'This update will create a new writerIdentity (service account) for ' 'the sink. In order for the sink to continue working, grant that ' 'service account correct permission on the destination. The service ' 'account will be displayed after a successful update operation.', cancel_on_no=True, default=False) result = self.PatchSink(util.GetParentFromArgs(args), sink_data, update_mask) log.UpdatedResource(sink_ref) self._epilog_result_destination = result.destination self._epilog_writer_identity = result.writerIdentity self._epilog_is_dlp_sink = bool(dlp_options) return result
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. """ sink_ref = util.GetSinkReference(args.sink_name, args) sink_resource = util.CreateResourceName(util.GetParentFromArgs(args), 'sinks', sink_ref.sinksId) return util.GetClient().projects_sinks.Get( util.GetMessages().LoggingProjectsSinksGetRequest( sinkName=sink_resource))
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 = util.GetSinkReference(args.sink_name, args) sink_resource = util.CreateResourceName(util.GetParentFromArgs(args), 'sinks', sink_ref.sinksId) console_io.PromptContinue('Really delete sink [%s]?' % sink_ref.sinksId, cancel_on_no=True) util.GetClient().projects_sinks.Delete( util.GetMessages().LoggingProjectsSinksDeleteRequest( sinkName=sink_resource)) log.DeletedResource(sink_ref)
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. """ util.CheckLegacySinksCommandArguments(args) sink_ref = util.GetSinkReference(args.sink_name, args.log, args.service) 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 = 'sink [%s]' % sink_ref.sinksId if not console_io.PromptContinue( 'Really delete %s?' % sink_description): raise calliope_exceptions.ToolException('action canceled by user') try: if args.log: self.DeleteLogSink(sink_ref) elif args.service: self.DeleteLogServiceSink(sink_ref) else: self.DeleteSink(util.GetParentFromArgs(args), sink_ref) log.DeletedResource(sink_ref) except apitools_exceptions.HttpError as error: v2_sink = not args.log and not args.service # Suggest the user to add --log or --log-service flag. if v2_sink and exceptions.HttpException( error).payload.status_code == 404: log.status.Print( ('Sink was not found. ' 'Did you forget to add --log or --log-service flag?')) raise error
def _Run(self, args, support_dlp=False): if not args.log_filter: # Attempt to create a sink with an empty filter. console_io.PromptContinue( 'Sink with empty filter matches all entries.', cancel_on_no=True) if args.include_children and not (args.organization or args.folder): log.warning( 'include-children only has an effect for sinks at the folder ' 'or organization level') sink_ref = util.GetSinkReference(args.sink_name, args) sink_data = { 'name': sink_ref.sinksId, 'destination': args.destination, 'filter': args.log_filter, 'includeChildren': args.include_children } if support_dlp: dlp_options = {} if args.IsSpecified('dlp_inspect_template'): dlp_options['inspectTemplateName'] = args.dlp_inspect_template if args.IsSpecified('dlp_deidentify_template'): dlp_options[ 'deidentifyTemplateName'] = args.dlp_deidentify_template if dlp_options: sink_data['dlpOptions'] = dlp_options result = self.CreateSink(util.GetParentFromArgs(args), sink_data) log.CreatedResource(sink_ref) self._epilog_result_destination = result.destination self._epilog_writer_identity = result.writerIdentity return result
def _Run(self, args): sink_ref = util.GetSinkReference(args.sink_name, args) sink = self.GetSink(util.GetParentFromArgs(args), sink_ref) sink_data = {'name': sink_ref.sinksId} update_mask = [] if args.IsSpecified('destination'): sink_data['destination'] = args.destination update_mask.append('destination') if args.IsSpecified('log_filter'): sink_data['filter'] = args.log_filter update_mask.append('filter') parameter_names = ['[destination]', '--log-filter'] parameter_names.extend( ['--use-partitioned-tables', '--clear-exclusions']) if args.IsSpecified('use_partitioned_tables'): bigquery_options = {} bigquery_options[ 'usePartitionedTables'] = args.use_partitioned_tables sink_data['bigqueryOptions'] = bigquery_options update_mask.append('bigquery_options.use_partitioned_tables') if args.IsSpecified('description'): sink_data['description'] = args.description update_mask.append('description') if args.IsSpecified('disabled'): sink_data['disabled'] = args.disabled update_mask.append('disabled') if (args.IsSpecified('clear_exclusions') or args.IsSpecified('remove_exclusions') or args.IsSpecified('add_exclusion') or args.IsSpecified('update_exclusion')): sink_data['exclusions'] = [] update_mask.append('exclusions') exclusions_to_remove = (args.remove_exclusions if args.IsSpecified('remove_exclusions') else []) exclusions_to_update = (args.update_exclusion if args.IsSpecified('update_exclusion') else []) for exclusion in sink.exclusions: if exclusion.name in exclusions_to_remove: exclusions_to_remove.remove(exclusion.name) else: for i in range(len(exclusions_to_update)): if exclusion.name == exclusions_to_update[i]['name']: for key, value in exclusions_to_update[i].items(): if key == 'description': exclusion.description = value if key == 'filter': exclusion.filter = value if key == 'disabled': exclusion.disabled = value exclusions_to_update.pop(i) break sink_data['exclusions'].append(exclusion) if exclusions_to_remove: raise calliope_exceptions.InvalidArgumentException( '--remove-exclusions', 'Exclusions {0} do not exist'.format( ','.join(exclusions_to_remove))) if exclusions_to_update: raise calliope_exceptions.InvalidArgumentException( '--update-exclusion', 'Exclusions {0} do not exist'.format(','.join([ exclusion['name'] for exclusion in exclusions_to_update ]))) if args.IsSpecified('clear_exclusions'): sink_data['exclusions'] = [] if args.IsSpecified('add_exclusion'): sink_data['exclusions'] += args.add_exclusion if not update_mask: raise calliope_exceptions.MinimumArgumentException( parameter_names, 'Please specify at least one property to update') # Check for legacy configuration, and let users decide if they still want # to update the sink with new settings. if sink.writerIdentity and 'cloud-logs@' in sink.writerIdentity: console_io.PromptContinue( 'This update will create a new writerIdentity (service account) for ' 'the sink. In order for the sink to continue working, grant that ' 'service account correct permission on the destination. The service ' 'account will be displayed after a successful update operation.', cancel_on_no=True, default=False) result = self.PatchSink(util.GetParentFromArgs(args), sink_data, update_mask) log.UpdatedResource(sink_ref) if args.IsSpecified('destination'): self._epilog_result_destination = result.destination self._epilog_writer_identity = result.writerIdentity return result
def _Run(self, args, is_alpha=False): sink_ref = util.GetSinkReference(args.sink_name, args) sink = self.GetSink(util.GetParentFromArgs(args), sink_ref) sink_data = {'name': sink_ref.sinksId} update_mask = [] if args.IsSpecified('destination'): sink_data['destination'] = args.destination update_mask.append('destination') if args.IsSpecified('log_filter'): sink_data['filter'] = args.log_filter update_mask.append('filter') parameter_names = ['[destination]', '--log-filter'] dlp_options = {} if is_alpha: parameter_names.extend([ '--dlp-inspect-template', '--dlp-deidentify-template', '--use-partitioned-tables', '--clear-exclusions' ]) if args.IsSpecified('dlp_inspect_template'): dlp_options['inspectTemplateName'] = args.dlp_inspect_template update_mask.append('dlp_options.inspect_template_name') if args.IsSpecified('dlp_deidentify_template'): dlp_options[ 'deidentifyTemplateName'] = args.dlp_deidentify_template update_mask.append('dlp_options.deidentify_template_name') if dlp_options: sink_data['dlpOptions'] = dlp_options if args.IsSpecified('use_partitioned_tables'): bigquery_options = {} bigquery_options[ 'usePartitionedTables'] = args.use_partitioned_tables sink_data['bigqueryOptions'] = bigquery_options update_mask.append('bigquery_options.use_partitioned_tables') if args.IsSpecified('description'): sink_data['description'] = args.description update_mask.append('description') if args.IsSpecified('disabled'): sink_data['disabled'] = args.disabled update_mask.append('disabled') sink_data['exclusions'] = [] if args.IsSpecified('clear_exclusions'): update_mask.append('exclusions') elif args.IsSpecified('remove_exclusions'): update_mask.append('exclusions') exclusions_to_remove = args.remove_exclusions for exclusion in sink.exclusions: if exclusion.name in exclusions_to_remove: exclusions_to_remove.remove(exclusion.name) else: sink_data['exclusions'].append(exclusion) if exclusions_to_remove: raise calliope_exceptions.InvalidArgumentException( '--remove-exclusions', 'Exclusions {0} do not exist'.format( ','.join(exclusions_to_remove))) elif args.IsSpecified('add_exclusions'): update_mask.append('exclusions') sink_data['exclusions'] = sink.exclusions if args.IsSpecified('add_exclusions'): sink_data['exclusions'] += args.add_exclusions if not update_mask: raise calliope_exceptions.MinimumArgumentException( parameter_names, 'Please specify at least one property to update') # Check for legacy configuration, and let users decide if they still want # to update the sink with new settings. if 'cloud-logs@' in sink.writerIdentity: console_io.PromptContinue( 'This update will create a new writerIdentity (service account) for ' 'the sink. In order for the sink to continue working, grant that ' 'service account correct permission on the destination. The service ' 'account will be displayed after a successful update operation.', cancel_on_no=True, default=False) result = self.PatchSink(util.GetParentFromArgs(args), sink_data, update_mask) log.UpdatedResource(sink_ref) self._epilog_result_destination = result.destination self._epilog_writer_identity = result.writerIdentity self._epilog_is_dlp_sink = bool(dlp_options) return result