Skip to content

Commit d33ea01

Browse files
author
Jon Wayne Parrott
committed
More consistency and standardization improvements.
* Adding argparse/docstring to cloud storage and monitoring samples. * Updating readmes for BigQuery, Monitoring, Cloud Storage, and the Blog. * Removing all non-appengine requirements.txt files and consolidating into a single top-level requirements.txt. This makes it easier for us to verify that the samples work with the latest version of all dependent libraries. * Moved test dependences out of tox.ini and into requirements-dev.txt, as per standard practice.
1 parent a1d2121 commit d33ea01

File tree

16 files changed

+193
-184
lines changed

16 files changed

+193
-184
lines changed

bigquery/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ This section contains samples for [Google BigQuery](https://cloud.google.com/big
44

55
## Running the samples
66

7-
In order to run it, your environment must be setup with [authentication
8-
information](https://developers.google.com/identity/protocols/application-default-credentials#howtheywork). If you're running it in your local development environment and you have the [Google Cloud SDK](https://cloud.google.com/sdk/) installed, you can do this easily by running:
7+
1. Your environment must be setup with [authentication
8+
information](https://developers.google.com/identity/protocols/application-default-credentials#howtheywork). If you're running in your local development environment and you have the [Google Cloud SDK](https://cloud.google.com/sdk/) installed, you can do this easily by running:
99

10-
$ gcloud auth login
10+
$ gcloud auth login
11+
12+
2. Install dependencies from the top-level [`requirements.txt`](../requirements.txt):
13+
14+
$ pip install -r requirements.txt
15+
16+
3. Depending on the sample, you may also need to create resources on the [Google Developers Console](https://console.developers.google.com). Refer to the sample description and associated documentation page.
1117

1218
## Additional resources
1319

blog/introduction_to_data_models_in_cloud_datastore/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# Introduction to data models in Cloud Datastore
22

3-
This sample code is used in [this blog post](). It demonstrates two data models
3+
This sample code is used in [this blog post](http://googlecloudplatform.blogspot.com/2015/08/Introduction-to-data-models-in-Cloud-Datastore.html). It demonstrates two data models
44
using [Google Cloud Datastore](https://cloud.google.com/datastore).
55

66
## Prerequisites
77

88
1. Create project with billing enabled on the [Google Developers Console](https://console.developers.google.com)
9+
910
2. [Enable the Datastore API](https://console.developers.google.com/project/_/apiui/apiview/datastore/overview).
11+
1012
3. Install the [Google Cloud SDK](https://cloud.google.com/sdk) and be sure to run ``gcloud auth``.
1113

1214

1315
## Running the samples
1416

15-
Install any dependencies:
17+
Install dependencies from the top-level [`requirements.txt`](../../requirements.txt):
1618

1719
pip install -r requirements.txt
1820

blog/introduction_to_data_models_in_cloud_datastore/requirements.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

cloud_logging/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ This section contains samples for [Google Cloud Logging](https://cloud.google.co
44

55
## Running the samples
66

7-
In order to run it, your environment must be setup with [authentication
8-
information](https://developers.google.com/identity/protocols/application-default-credentials#howtheywork). If you're running it in your local development environment and you have the [Google Cloud SDK](https://cloud.google.com/sdk/) installed, you can do this easily by running:
7+
1. Your environment must be setup with [authentication
8+
information](https://developers.google.com/identity/protocols/application-default-credentials#howtheywork). If you're running in your local development environment and you have the [Google Cloud SDK](https://cloud.google.com/sdk/) installed, you can do this easily by running:
99

10-
$ gcloud auth login
10+
$ gcloud auth login
11+
12+
2. Install dependencies from the top-level [`requirements.txt`](../requirements.txt):
13+
14+
$ pip install -r requirements.txt
15+
16+
3. Depending on the sample, you may also need to create resources on the [Google Developers Console](https://console.developers.google.com). Refer to the sample description and associated documentation page.
1117

1218
## Additional resources
1319

@@ -23,5 +29,3 @@ can visit:
2329
For information on the Python Client Library visit:
2430

2531
> https://developers.google.com/api-client-library/python
26-
27-
## Other Samples

monitoring/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Google Cloud Monitoring Samples
2+
3+
This section contains samples for [Google Cloud Monitoring](https://cloud.google.com/monitoring).
4+
5+
## Running the samples
6+
7+
1. Your environment must be setup with [authentication
8+
information](https://developers.google.com/identity/protocols/application-default-credentials#howtheywork). *Note* that Cloud Monitoring does not currently work
9+
with `gcloud auth`. You will need to use a *service account* when running
10+
locally and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
11+
12+
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
13+
14+
2. Install dependencies from the top-level [`requirements.txt`](../requirements.txt):
15+
16+
$ pip install -r requirements.txt
17+
18+
3. Depending on the sample, you may also need to create resources on the [Google Developers Console](https://console.developers.google.com). Refer to the sample description and associated documentation page.
19+
20+
## Additional resources
21+
22+
For more information on Cloud Monitoring you can visit:
23+
24+
> https://cloud.google.com/monitoring
25+
26+
For more information on the Cloud Monitoring API Python library surface you
27+
can visit:
28+
29+
> https://developers.google.com/resources/api-libraries/documentation/storage/v2beta2/python/latest/
30+
31+
For information on the Python Client Library visit:
32+
33+
> https://developers.google.com/api-client-library/python

monitoring/api/auth.py

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env pyhton
2+
13
# Copyright 2015 Google Inc. All rights reserved.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,81 +16,58 @@
1416

1517
"""Sample command-line program for retrieving Google Cloud Monitoring API data.
1618
17-
Simple command-line program to demonstrate connecting to the Google Cloud
18-
Monitoring API to retrieve API data, using application default credentials to
19-
authenticate.
20-
21-
This sample obtains authentication information from its environment via
22-
application default credentials [1].
23-
24-
If you're not running the sample on Google App Engine or Compute Engine (where
25-
the environment comes pre-authenticated as a service account), you'll have to
26-
initialize your environment with credentials the sample can use.
27-
28-
One way to do this is through the cloud console's credentials page [2]. Create
29-
a new client ID of type 'Service account', and download its JSON key. This will
30-
be the account the sample authenticates as.
31-
32-
Once you've downloaded the service account's JSON key, you provide it to the
33-
sample by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to
34-
point to the key file:
19+
This sample is used on this page:
3520
36-
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/json-key.json
21+
https://cloud.google.com/monitoring/api/authentication
3722
38-
[1] https://developers.google.com/identity/protocols/\
39-
application-default-credentials
40-
[2] https://console.developers.google.com/project/_/apiui/credential
41-
""" # NOQA
23+
For more information, see the README.md under /monitoring.
24+
"""
4225

4326
# [START all]
27+
import argparse
4428
import json
45-
import sys
4629

4730
from googleapiclient.discovery import build
48-
4931
from oauth2client.client import GoogleCredentials
5032

5133

5234
METRIC = 'compute.googleapis.com/instance/disk/read_ops_count'
5335
YOUNGEST = '2015-01-01T00:00:00Z'
5436

5537

56-
def ListTimeseries(project_name, service):
38+
def list_timeseries(monitoring, project_name):
5739
"""Query the Timeseries.list API method.
5840
5941
Args:
42+
monitoring: the CloudMonitoring service object.
6043
project_name: the name of the project you'd like to monitor.
61-
service: the CloudMonitoring service object.
6244
"""
45+
timeseries = monitoring.timeseries()
6346

64-
timeseries = service.timeseries()
47+
response = timeseries.list(
48+
project=project_name, metric=METRIC, youngest=YOUNGEST).execute()
6549

6650
print('Timeseries.list raw response:')
67-
try:
68-
response = timeseries.list(
69-
project=project_name, metric=METRIC, youngest=YOUNGEST).execute()
70-
71-
print(json.dumps(response,
72-
sort_keys=True,
73-
indent=4,
74-
separators=(',', ': ')))
75-
except:
76-
print('Error:')
77-
for error in sys.exc_info():
78-
print(error)
51+
print(json.dumps(response,
52+
sort_keys=True,
53+
indent=4,
54+
separators=(',', ': ')))
7955

8056

8157
def main(project_name):
82-
# Create and return the CloudMonitoring service object.
83-
service = build('cloudmonitoring', 'v2beta2',
84-
credentials=GoogleCredentials.get_application_default())
58+
credentials = GoogleCredentials.get_application_default()
59+
monitoring = build('cloudmonitoring', 'v2beta2', credentials=credentials)
8560

86-
ListTimeseries(project_name, service)
61+
list_timeseries(monitoring, project_name)
8762

8863

8964
if __name__ == '__main__':
90-
if len(sys.argv) != 2:
91-
print("Usage: {} <project-name>".format(sys.argv[0]))
92-
sys.exit(1)
93-
main(sys.argv[1])
65+
parser = argparse.ArgumentParser(
66+
description=__doc__,
67+
formatter_class=argparse.RawDescriptionHelpFormatter)
68+
parser.add_argument('project_id', help='Your Google Cloud project ID.')
69+
70+
args = parser.parse_args()
71+
72+
main(args.project_id)
9473
# [END all]

monitoring/api/auth_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class TestTimeseriesList(tests.CloudBaseTest):
2323
def test_main(self):
2424
with tests.capture_stdout() as stdout:
2525
auth.main(self.project_id)
26+
2627
output = stdout.getvalue().strip()
28+
2729
self.assertRegexpMatches(
2830
output, re.compile(r'Timeseries.list raw response:\s*'
2931
r'{\s*"kind": "[^"]+",'

requirements-dev.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
beautifulsoup4==4.4.0
2+
coverage==3.7.1
3+
Flask==0.10.1
4+
funcsigs==0.4
5+
itsdangerous==0.24
6+
Jinja2==2.8
7+
MarkupSafe==0.23
8+
mock==1.3.0
9+
nose==1.3.7
10+
nose-exclude==0.4.1
11+
nosegae==0.5.7
12+
pbr==1.8.0
13+
PyYAML==3.11
14+
waitress==0.8.10
15+
WebOb==1.4.1
16+
WebTest==2.0.18
17+
Werkzeug==0.10.4

requirements.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
argparse>=1.2.1
2-
google-api-python-client>=1.4.2
3-
httplib2>=0.9.1
4-
oauth2client>=1.5.1
5-
py==1.4.26
6-
pyasn1==0.1.7
7-
pyasn1-modules==0.0.5
8-
rsa==3.1.4
9-
simplejson==3.6.5
1+
gcloud==0.7.1
2+
google-api-python-client==1.4.2
3+
google-apitools==0.4.11
4+
httplib2==0.9.1
5+
oauth2client==1.5.1
6+
protobuf==3.0.0a1
7+
protorpc==0.11.1
8+
pyasn1==0.1.8
9+
pyasn1-modules==0.0.7
10+
pycrypto==2.6.1
11+
rsa==3.2
12+
simplejson==3.8.0
1013
six==1.9.0
11-
tox==1.9.0
1214
uritemplate==0.6
13-
virtualenv==12.0.7
14-
wsgiref==0.1.2

storage/README.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
## Python Samples for Google Cloud Storage
1+
# Google Cloud Storage Samples
22

3-
Two samples:
3+
This section contains samples for [Google Cloud Storage](https://cloud.google.com/storage).
44

5-
1. ``list_objects.py`` lists objects in a bucket.
6-
2. ``compose_objects.py`` composes objects together to create another.
5+
## Running the samples
76

8-
See the docstring for each sample for usage, or run the sample for the help text.
7+
1. Your environment must be setup with [authentication
8+
information](https://developers.google.com/identity/protocols/application-default-credentials#howtheywork). If you're running in your local development environment and you have the [Google Cloud SDK](https://cloud.google.com/sdk/) installed, you can do this easily by running:
99

10-
### Setup
10+
$ gcloud auth login
1111

12-
Before running the samples, you'll need the Google Cloud SDK in order to setup authentication.
12+
2. Install dependencies from the top-level [`requirements.txt`](../requirements.txt):
1313

14-
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app).
15-
2. Setup the gcloud tool.
14+
$ pip install -r requirements.txt
1615

17-
```
18-
gcloud components update app
19-
gcloud auth login
20-
gcloud config set project <your-app-id>
21-
```
16+
3. Depending on the sample, you may also need to create resources on the [Google Developers Console](https://console.developers.google.com). Refer to the sample description and associated documentation page.
2217

23-
You will also need to install the dependencies using [pip](https://pypi.python.org/pypi/pip):
18+
## Additional resources
2419

25-
```
26-
pip install -r requirements.txt
27-
```
20+
For more information on Cloud Storage you can visit:
21+
22+
> https://cloud.google.com/storage
23+
24+
For more information on the Cloud Storage API Python library surface you
25+
can visit:
26+
27+
> https://developers.google.com/resources/api-libraries/documentation/storage/v1/python/latest/
28+
29+
For information on the Python Client Library visit:
30+
31+
> https://developers.google.com/api-client-library/python

0 commit comments

Comments
 (0)