|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import argparse |
| 18 | + |
| 19 | +from gcloud import logging |
| 20 | +from oauth2client.client import GoogleCredentials |
| 21 | + |
| 22 | +FILTER = 'logName="projects/{}/logs/syslog" AND severity>=ERROR' |
| 23 | +DESTINATION = 'storage.googleapis.com/{}' |
| 24 | + |
| 25 | + |
| 26 | +def create_sink_if_not_exists(client, args): |
| 27 | + # [START create] |
| 28 | + sink = client.sink( |
| 29 | + args.sink_name, |
| 30 | + FILTER.format(args.project_id), |
| 31 | + DESTINATION.format(args.destination_bucket)) |
| 32 | + |
| 33 | + if not sink.exists(): |
| 34 | + sink.create() |
| 35 | + print('Created sink {}'.format(sink.name)) |
| 36 | + # [END create] |
| 37 | + return sink |
| 38 | + |
| 39 | + |
| 40 | +def list_sinks(client, args): |
| 41 | + print('Listing sinks available') |
| 42 | + |
| 43 | + # [START list] |
| 44 | + sinks = [] |
| 45 | + while True: |
| 46 | + new_sinks, token = client.list_sinks() |
| 47 | + sinks += new_sinks |
| 48 | + if token is None: |
| 49 | + break |
| 50 | + |
| 51 | + for sink in sinks: |
| 52 | + print('{}: {}'.format(sink.name, sink.destination)) |
| 53 | + # [END list] |
| 54 | + |
| 55 | + return sinks |
| 56 | + |
| 57 | + |
| 58 | +def update_sink(client, args): |
| 59 | + """Changes the filter of a sink. |
| 60 | +
|
| 61 | + The filter is used to determine which log statements match this sink and |
| 62 | + will be exported to the destination. |
| 63 | + """ |
| 64 | + # Removes the robot in textPayload part of filter |
| 65 | + sink = client.sink( |
| 66 | + args.sink_name, |
| 67 | + FILTER.format(args.project_id), |
| 68 | + DESTINATION.format(args.destination_bucket)) |
| 69 | + # [START update] |
| 70 | + sink.filter = ('logName="projects/{}/logs/syslog" ' |
| 71 | + 'AND severity>= INFO'.format(sink.project)) |
| 72 | + print('Updated sink {}'.format(sink.name)) |
| 73 | + sink.update() |
| 74 | + # [END update] |
| 75 | + |
| 76 | + |
| 77 | +def delete_sink(client, args): |
| 78 | + """Deletes a sink""" |
| 79 | + sink = client.sink( |
| 80 | + args.sink_name, |
| 81 | + FILTER.format(args.project_id), |
| 82 | + DESTINATION.format(args.destination_bucket)) |
| 83 | + # [START delete] |
| 84 | + sink.delete() |
| 85 | + # [END delete] |
| 86 | + print('Deleted sink {}'.format(sink.name)) |
| 87 | + |
| 88 | + |
| 89 | +def get_client(project_id): |
| 90 | + """Builds an http client authenticated with the service account |
| 91 | + credentials.""" |
| 92 | + credentials = GoogleCredentials.get_application_default() |
| 93 | + return logging.Client(project=project_id, credentials=credentials) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + parser = argparse.ArgumentParser( |
| 98 | + description=__doc__, |
| 99 | + formatter_class=argparse.RawDescriptionHelpFormatter |
| 100 | + ) |
| 101 | + parser.add_argument( |
| 102 | + '--project_id', help='Project ID you want to access.', required=True,) |
| 103 | + |
| 104 | + parser.add_argument( |
| 105 | + '--sink_name', help='Output bucket to direct sink to', |
| 106 | + default="mysink") |
| 107 | + |
| 108 | + subparsers = parser.add_subparsers() |
| 109 | + |
| 110 | + create_parser = subparsers.add_parser('create_sink') |
| 111 | + create_parser.set_defaults(func=create_sink_if_not_exists) |
| 112 | + create_parser.add_argument( |
| 113 | + '--destination_bucket', help='Output bucket to direct sink to', |
| 114 | + required=True) |
| 115 | + |
| 116 | + list_parser = subparsers.add_parser('list_sinks') |
| 117 | + list_parser.set_defaults(func=list_sinks) |
| 118 | + |
| 119 | + update_parser = subparsers.add_parser('update_sink') |
| 120 | + update_parser.set_defaults(func=update_sink) |
| 121 | + update_parser.add_argument( |
| 122 | + '--destination_bucket', help='Output bucket to direct sink to', |
| 123 | + required=True) |
| 124 | + |
| 125 | + delete_parser = subparsers.add_parser('delete_sink') |
| 126 | + delete_parser.add_argument( |
| 127 | + '--destination_bucket', help='Output bucket to direct sink to', |
| 128 | + required=True) |
| 129 | + delete_parser.set_defaults(func=delete_sink) |
| 130 | + |
| 131 | + args = parser.parse_args() |
| 132 | + client = get_client(args.project_id) |
| 133 | + args.func(client, args) |
0 commit comments