Skip to content

Commit 511eae4

Browse files
author
Jon Wayne Parrott
authored
Update standard cloudsql sample to use env vars, cloudsql v2 (GoogleCloudPlatform#556)
1 parent 07e92d7 commit 511eae4

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

appengine/standard/cloudsql/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@ Refer to the [App Engine Samples README](../README.md) for information on how to
88

99
1. You will need to create a [Cloud SQL instance](https://cloud.google.com/sql/docs/create-instance).
1010

11-
2. Edit the `CLOUDSQL_INSTANCE` and `CLOUDSQL_PROJECT` values in `main.py`.
12-
13-
3. To run locally, you will need to be running a local instance of MySQL. You may need to update the connection code in `main.py` with the appropriate local username and password.
11+
2. Edit the update the `env_variables` section in `app.yaml` with your Cloud SQL configuration.

appengine/standard/cloudsql/app.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ handlers:
99
libraries:
1010
- name: MySQLdb
1111
version: "latest"
12+
13+
# [START env_variables]
14+
env_variables:
15+
CLOUDSQL_CONNECTION_NAME: your-connection-name
16+
CLOUDSQL_USER: root
17+
CLOUDSQL_PASSWORD: your-cloudsql-user-password
18+
# [END env_variables]

appengine/standard/cloudsql/main.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,57 @@
1414

1515
"""
1616
Sample App Engine application demonstrating how to connect to Google Cloud SQL
17-
using App Engine's native unix socket.
17+
using App Engine's native unix socket or using TCP when running locally.
1818
1919
For more information, see the README.md.
2020
"""
2121

2222
# [START all]
23-
2423
import os
2524

2625
import MySQLdb
2726
import webapp2
2827

2928

30-
CLOUDSQL_PROJECT = ''
31-
CLOUDSQL_INSTANCE = ''
29+
# These environment variables are configured in app.yaml.
30+
CLOUDSQL_CONNECTION_NAME = os.environ.get('CLOUDSQL_CONNECTION_NAME')
31+
CLOUDSQL_USER = os.environ.get('CLOUDSQL_USER')
32+
CLOUDSQL_PASSWORD = os.environ.get('CLOUDSQL_PASSWORD')
33+
34+
35+
def connect_to_cloudsql():
36+
# When deployed to App Engine, the `SERVER_SOFTWARE` environment variable
37+
# will be set to 'Google App Engine/version'.
38+
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
39+
# Connect using the unix socket located at
40+
# /cloudsql/cloudsql-connection-name.
41+
cloudsql_unix_socket = os.path.join(
42+
'/cloudsql', CLOUDSQL_CONNECTION_NAME)
43+
44+
db = MySQLdb.connect(
45+
unix_socket=cloudsql_unix_socket,
46+
user=CLOUDSQL_USER,
47+
passwd=CLOUDSQL_PASSWORD)
48+
49+
# If the unix socket is unavailable, then try to connect using TCP. This
50+
# will work if you're running a local MySQL server or using the Cloud SQL
51+
# proxy, for example:
52+
#
53+
# $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
54+
#
55+
else:
56+
db = MySQLdb.connect(
57+
host='127.0.0.1', user=CLOUDSQL_USER, passwd=CLOUDSQL_PASSWORD)
58+
59+
return db
3260

3361

3462
class MainPage(webapp2.RequestHandler):
3563
def get(self):
64+
"""Simple request handler that shows all of the MySQL variables."""
3665
self.response.headers['Content-Type'] = 'text/plain'
3766

38-
# When running on Google App Engine, use the special unix socket
39-
# to connect to Cloud SQL.
40-
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
41-
db = MySQLdb.connect(
42-
unix_socket='/cloudsql/{}:{}'.format(
43-
CLOUDSQL_PROJECT,
44-
CLOUDSQL_INSTANCE),
45-
user='root')
46-
# When running locally, you can either connect to a local running
47-
# MySQL instance, or connect to your Cloud SQL instance over TCP.
48-
else:
49-
db = MySQLdb.connect(host='localhost', user='root')
50-
67+
db = connect_to_cloudsql()
5168
cursor = db.cursor()
5269
cursor.execute('SHOW VARIABLES')
5370

appengine/standard/cloudsql/main_test.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818
import pytest
1919
import webtest
2020

21-
import main
21+
22+
@pytest.fixture
23+
def main(monkeypatch):
24+
monkeypatch.setenv('CLOUDSQL_USER', 'root')
25+
monkeypatch.setenv('CLOUDSQL_PASSWORD', '')
26+
import main
27+
return main
2228

2329

2430
@pytest.mark.skipif(
2531
not os.path.exists('/var/run/mysqld/mysqld.sock'),
26-
reason='MySQL server not available.')
27-
def test_app():
32+
reason='Local MySQL server not available.')
33+
def test_app(main):
2834
app = webtest.TestApp(main.app)
2935
response = app.get('/')
3036

0 commit comments

Comments
 (0)