From 438d1c8428282b56b05522aecad02aad245a6fdc Mon Sep 17 00:00:00 2001 From: Sam Harbison Date: Tue, 15 Sep 2015 16:37:51 -0400 Subject: [PATCH 1/2] First cut at Cloud Logging sample. Fails with _no attribute 'getLogger'_. --- logging/__init__.py | 0 logging/samples/__init__.py | 0 logging/samples/listlogs.py | 80 ++++++++++++++++++++++++++ logging/samples/tests/__init__.py | 0 logging/samples/tests/test_listlogs.py | 38 ++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 logging/__init__.py create mode 100644 logging/samples/__init__.py create mode 100644 logging/samples/listlogs.py create mode 100644 logging/samples/tests/__init__.py create mode 100644 logging/samples/tests/test_listlogs.py diff --git a/logging/__init__.py b/logging/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/logging/samples/__init__.py b/logging/samples/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/logging/samples/listlogs.py b/logging/samples/listlogs.py new file mode 100644 index 00000000000..c4bfdf16d8e --- /dev/null +++ b/logging/samples/listlogs.py @@ -0,0 +1,80 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Command-line program to list the logs in a Google Cloud Platform project. + +Simple command-line program to demonstrate connecting to the Google Cloud +Logging API to retrieve logs data, using application default credentials[1] to +authenticate. + +This application will run--without modification--on 1) Google App Engine, +2) Google Compute Engine, 3) a local Linux workstation with the Google Cloud +SDK, or 4) a Linux computer without the SDK. + +In cases 1) and 2), authorization comes from a built-in service account on the +instance running this application. In case 3) you must run "gcloud auth login" +your workstation to authorize the application. In case 4) you must create a +service account[2] in the Google Cloud project whose log data you will fetch, +copy the account's JSON private key file to the computer running this +application, and on that computer set the environment variable +GOOGLE_APPLICATION_CREDENTIALS to point to the JSON private key file. For +example: + + $ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/json-key.json + +[1] https://developers.google.com/identity/protocols/application-default-credentials +[2] https://console.developers.google.com/project/_/apiui/credential +""" # NOQA + +# [START all] +import json +import sys + +from googleapiclient.discovery import build + +from oauth2client.client import GoogleCredentials + +# [START auth] +def GetLoggingService(): + credentials = GoogleCredentials.get_application_default() + service = build('logging', 'v1beta3', credentials=credentials) + return service +# [END auth] + + +# [START listlogs] +def ListLogs(project_id, service): + next_page_token = None + while True: + response = service.projects().logs().list( + projectsID=project_id, pageToken=next_page_token).execute() + for log in response["logs"]: + print log["name"] + next_page_token = response.get("nextPageToken") + if not next_page_token: + break +# [END listlogs] + + +def main(project_id): + service = GetLoggingService() + ListLogs(project_id, service) + + +if __name__ == '__main__': + if len(sys.argv) != 2: + print "Usage: %s " % sys.argv[0] + sys.exit(1) + main(sys.argv[1]) +# [END all] diff --git a/logging/samples/tests/__init__.py b/logging/samples/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/logging/samples/tests/test_listlogs.py b/logging/samples/tests/test_listlogs.py new file mode 100644 index 00000000000..34bdb1211c0 --- /dev/null +++ b/logging/samples/tests/test_listlogs.py @@ -0,0 +1,38 @@ +# Copyright 2015, Google, Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import re +import unittest + +from logging.samples import listlogs + +import tests + + +class TestListLogs(tests.CloudBaseTest): + + @classmethod + def setUpClass(cls): + cls.test_project_id = os.environ.get(tests.PROJECT_ID_ENV) + + def test_main(self): + with tests.capture_stdout() as stdout: + listlogs.main(self.test_project_id) + output = stdout.getvalue().strip() + self.assertRegexpMatches( + output, re.compile(r'.*', re.S)) + + +if __name__ == '__main__': + unittest.main() From e7778580dbef72e6994352e19304b1166947da72 Mon Sep 17 00:00:00 2001 From: Sam Harbison Date: Tue, 15 Sep 2015 18:28:48 -0400 Subject: [PATCH 2/2] Logging sample loglist.py is now working. Issue was PYTHONPATH. --- logging/samples/listlogs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging/samples/listlogs.py b/logging/samples/listlogs.py index c4bfdf16d8e..e93d8699ea6 100644 --- a/logging/samples/listlogs.py +++ b/logging/samples/listlogs.py @@ -58,7 +58,7 @@ def ListLogs(project_id, service): next_page_token = None while True: response = service.projects().logs().list( - projectsID=project_id, pageToken=next_page_token).execute() + projectsId=project_id, pageToken=next_page_token).execute() for log in response["logs"]: print log["name"] next_page_token = response.get("nextPageToken")