diff --git a/appengine/standard_python37/spanner/app.yaml b/appengine/standard_python37/spanner/app.yaml new file mode 100644 index 00000000000..7b6a30c8647 --- /dev/null +++ b/appengine/standard_python37/spanner/app.yaml @@ -0,0 +1,5 @@ +runtime: python37 + +env_variables: + SPANNER_INSTANCE: "YOUR-SPANNER-INSTANCE-ID" + SPANNER_DATABASE: "YOUR-SPANNER-DATABASE-ID" diff --git a/appengine/standard_python37/spanner/main.py b/appengine/standard_python37/spanner/main.py new file mode 100644 index 00000000000..7a834c2d51a --- /dev/null +++ b/appengine/standard_python37/spanner/main.py @@ -0,0 +1,35 @@ +# Copyright 2018 Google LLC +# +# 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. + +# [START gae_python37_cloudsql_mysql] +import os + +from flask import Flask +from google.cloud import spanner + +app = Flask(__name__) +spanner_client = spanner.Client() + +instance_id = os.environ.get('SPANNER_INSTANCE') +database_id = os.environ.get('SPANNER_DATABASE') + + +@app.route('/') +def main(): + database = spanner_client.instance(instance_id).database(database_id) + with database.snapshot() as snapshot: + cursor = snapshot.execute_sql('SELECT 1') + results = list(cursor) + + return 'Query Result: {}'.format(results[0][0]) diff --git a/appengine/standard_python37/spanner/main_test.py b/appengine/standard_python37/spanner/main_test.py new file mode 100644 index 00000000000..b3557941dad --- /dev/null +++ b/appengine/standard_python37/spanner/main_test.py @@ -0,0 +1,26 @@ +# Copyright 2018 Google LLC +# +# 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. + + +def test_main(): + import main + + main.database_id = 'example-db' + + main.app.testing = True + client = main.app.test_client() + + r = client.get('/') + assert r.status_code == 200 + assert 'Query Result: 1' in r.data.decode('utf-8') diff --git a/appengine/standard_python37/spanner/requirements.txt b/appengine/standard_python37/spanner/requirements.txt new file mode 100644 index 00000000000..c7f0ad25b7d --- /dev/null +++ b/appengine/standard_python37/spanner/requirements.txt @@ -0,0 +1,2 @@ +google-cloud-spanner==1.6.0 +Flask==1.0.2 \ No newline at end of file