Skip to content

Commit fc44cec

Browse files
author
Jonathan Wayne Parrott
committed
Merge pull request #118 from GoogleCloudPlatform/appengine-cloudsql-standards
Bringing cloud sql sample up to standards, and fixing a few other sta…
2 parents 656607f + 101e24e commit fc44cec

File tree

8 files changed

+119
-28
lines changed

8 files changed

+119
-28
lines changed

appengine/bigquery/README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
## Google App Engine accessing BigQuery using OAuth2
22

3-
This sample demonstrates [authenticating to BigQuery in App Engine using OAuth2](https://cloud.google.com/bigquery/authorization).
3+
This sample demonstrates [authenticating to BigQuery in App Engine using OAuth2](https://cloud.google.com/bigquery/authentication).
44

5-
### Setup
5+
### Running the sample
66

7-
* To install dependencies for this sample, run:
7+
1. To install dependencies for this sample, run:
88

9-
$ pip install -t lib -r requirements.txt
9+
$ pip install -t lib -r requirements.txt
1010

11-
* You must then update `main.py` and replace `<myproject_id>` with your project's
12-
id.
13-
* You'll need a client id from your project - instructions
14-
[here](https://cloud-dot-devsite.googleplex.com/bigquery/authorization#clientsecrets).
15-
Once you've downloaded the client's json secret, copy it to the root directory
16-
of this project, and rename it to `client_secrets.json`.
17-
* You can then run the sample on your development server:
11+
2. You must then update `main.py` and replace `<your-project-id>` with your project's
12+
ID.
1813

19-
$ dev_appserver.py .
14+
3. You'll need a client id from your project - instructions
15+
[here](https://cloud.google.com/bigquery/authentication#clientsecrets).
16+
Once you've downloaded the client's json secret, copy it to the root directory
17+
of this project, and rename it to `client_secrets.json`.
18+
19+
3. You can then run the sample on your development server:
20+
21+
$ dev_appserver.py .
22+
23+
Or deploy the application:
24+
25+
$ appcfg.py update .

appengine/bigquery/main.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,25 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# [START all]
15-
"""Sample appengine app demonstrating 3-legged oauth."""
15+
16+
"""
17+
Sample App Engine application that demonstrates authentication to BigQuery
18+
using User OAuth2 as opposed to OAuth2 Service Accounts.
19+
20+
For more information about BigQuery, see README.md under /bigquery.
21+
For more information about App Engine, see README.md under /appengine.
22+
"""
23+
1624
import json
1725
import os
1826

1927
from googleapiclient.discovery import build
20-
2128
from oauth2client.appengine import OAuth2DecoratorFromClientSecrets
22-
2329
import webapp2
2430

2531

2632
# The project id whose datasets you'd like to list
27-
PROJECTID = '<myproject_id>'
33+
PROJECTID = '<your-project-id>'
2834

2935
# Create the method decorator for oauth.
3036
decorator = OAuth2DecoratorFromClientSecrets(
@@ -37,21 +43,23 @@
3743

3844
class MainPage(webapp2.RequestHandler):
3945

46+
# oauth_required ensures that the user goes through the OAuth2
47+
# authorization flow before reaching this handler.
4048
@decorator.oauth_required
4149
def get(self):
42-
"""Lists the datasets in PROJECTID"""
50+
# This is an httplib2.Http instance that is signed with the user's
51+
# credentials. This allows you to access the BigQuery API on behalf
52+
# of the user.
4353
http = decorator.http()
44-
datasets = service.datasets()
4554

46-
response = datasets.list(projectId=PROJECTID).execute(http)
55+
response = service.datasets().list(projectId=PROJECTID).execute(http)
4756

4857
self.response.out.write('<h3>Datasets.list raw response:</h3>')
4958
self.response.out.write('<pre>%s</pre>' %
5059
json.dumps(response, sort_keys=True, indent=4,
5160
separators=(',', ': ')))
5261

5362

54-
# Create the webapp2 application
5563
app = webapp2.WSGIApplication([
5664
('/', MainPage),
5765
# Create the endpoint to receive oauth flow callbacks

appengine/cloudsql/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
# Using Cloud SQL from Google App Engine
2+
13
This is an example program showing how to use the native MySQL connections from Google App Engine to Google Cloud SQL.
24

3-
## Deploying
5+
## Running the sample
6+
7+
1. Edit the `CLOUDSQL_INSTANCE` and `CLOUDSQL_PROJECT` values in `main.py`.
8+
9+
2. If you have a local MySQL instance, run the app locally:
10+
11+
dev_appserver.py .
412

5-
1. Edit the `unix_socket` in `main.py` to point to a Cloud SQL instance.
13+
2. Upload the app:
614

7-
2. Upload the app: `appcfg.py update .`.
15+
appcfg.py update .

appengine/cloudsql/__init__.py

Whitespace-only changes.

appengine/cloudsql/main.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,48 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
"""
16+
Sample App Engine application demonstrating how to connect to Google Cloud SQL
17+
using App Engine's native unix socket.
18+
19+
For more information about App Engine, see README.md under /appengine.
20+
"""
21+
1422
import os
1523

1624
import MySQLdb
1725
import webapp2
1826

1927

20-
class IndexPage(webapp2.RequestHandler):
28+
CLOUDSQL_PROJECT = '<your-project-id>'
29+
CLOUDSQL_INSTANCE = '<your-cloud-sql-instance>'
30+
31+
32+
class MainPage(webapp2.RequestHandler):
2133
def get(self):
2234
self.response.headers['Content-Type'] = 'text/plain'
2335

36+
# When running on Google App Engine, use the special unix socket
37+
# to connect to Cloud SQL.
2438
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
2539
db = MySQLdb.connect(
26-
unix_socket='/cloudsql/my_project:my_instance',
40+
unix_socket='/cloudsql/{}:{}'.format(
41+
CLOUDSQL_PROJECT,
42+
CLOUDSQL_INSTANCE),
2743
user='root')
44+
# When running locally, you can either connect to a local running
45+
# MySQL instance, or connect to your Cloud SQL instance over TCP.
2846
else:
2947
db = MySQLdb.connect(host='localhost', user='root')
3048

3149
cursor = db.cursor()
3250
cursor.execute('SHOW VARIABLES')
51+
3352
for r in cursor.fetchall():
34-
self.response.write('%s\n' % str(r))
53+
self.response.write('{}\n'.format(r))
3554

3655

3756
app = webapp2.WSGIApplication([
38-
('/', IndexPage),
57+
('/', MainPage),
3958
], debug=True)

appengine/cloudsql/main_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import re
15+
16+
import tests
17+
import webtest
18+
19+
from . import main
20+
21+
22+
class TestMySQLSample(tests.AppEngineTestbedCase):
23+
24+
def setUp(self):
25+
super(TestMySQLSample, self).setUp()
26+
self.app = webtest.TestApp(main.app)
27+
28+
def test_get(self):
29+
response = self.app.get('/')
30+
self.assertEqual(response.status_int, 200)
31+
self.assertRegexpMatches(
32+
response.body,
33+
re.compile(r'.*version.*', re.DOTALL))

appengine/storage/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
## Google App Engine using Cloud Storage
22

3-
This sample demonstrates how to use cloud storage from Google App Engine
3+
This sample demonstrates how to use Google Cloud Storage from Google App Engine
4+
5+
### Running the sample
6+
7+
1. To install dependencies for this sample, run:
8+
9+
$ pip install -t lib -r requirements.txt
10+
11+
2. You must then update `main.py` and replace `<your-bucket-name>` with your Cloud Storage bucket.
12+
13+
3. You can then run the sample on your development server:
14+
15+
$ dev_appserver.py .
16+
17+
Or deploy the application:
18+
19+
$ appcfg.py update .

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ commonargs =
1818
[testenv:gae]
1919
deps =
2020
{[testenv]deps}
21+
mysql-python==1.2.5
2122
commands =
2223
nosetests --with-gae \
2324
--gae-app=tests/resources/app.yaml \

0 commit comments

Comments
 (0)