Skip to content

Commit dc48bd1

Browse files
committed
Merge pull request #112 from GoogleCloudPlatform/namespace
Consolidate namespace example into this repo
2 parents a5a73b6 + f317bcf commit dc48bd1

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

appengine/multitenancy/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Multitenancy Using Namespaces Sample
2+
3+
This is a sample app for Google App Engine that exercises the [namespace manager Python API](https://cloud.google.com/appengine/docs/python/multitenancy/multitenancy).
4+
5+
See our other [Google Cloud Platform github
6+
repos](https://github.com/GoogleCloudPlatform) for sample applications and
7+
scaffolding for other python frameworks and use cases.
8+
9+
## Run Locally
10+
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).
11+
2. Setup the gcloud tool.
12+
13+
```
14+
gcloud components update app
15+
gcloud auth login
16+
gcloud config set project <your-app-id>
17+
```
18+
You don't need a valid app-id to run locally, but will need a valid id to deploy below.
19+
20+
1. Clone this repo.
21+
22+
```
23+
git clone https://github.com/GoogleCloudPlatform/appengine-multitenancy-python.git
24+
```
25+
1. Run this project locally from the command line.
26+
27+
```
28+
gcloud preview app run appengine-multitenancy-python/
29+
```
30+
31+
1. Visit the application at [http://localhost:8080](http://localhost:8080).
32+
33+
## Deploying
34+
35+
1. Use the [Cloud Developer Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical)
36+
2. Configure gcloud with your app id.
37+
38+
```
39+
gcloud config set project <your-app-id>
40+
```
41+
1. Use the [Admin Console](https://appengine.google.com) to view data, queues, and other App Engine specific administration tasks.
42+
1. Use gcloud to deploy your app.
43+
44+
```
45+
gcloud preview app deploy appengine-multitenancy-python/
46+
```
47+
48+
1. Congratulations! Your application is now live at your-app-id.appspot.com
49+
50+
## Contributing changes
51+
52+
* See [CONTRIBUTING.md](CONTRIBUTING.md)
53+
54+
## Licensing
55+
56+
* See [LICENSE](LICENSE)

appengine/multitenancy/app.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file specifies your Python application's runtime configuration
2+
# including URL routing, versions, static file uploads, etc. See
3+
# https://developers.google.com/appengine/docs/python/config/appconfig
4+
# for details.
5+
6+
version: 1
7+
runtime: python27
8+
api_version: 1
9+
threadsafe: yes
10+
11+
# Handlers define how to route requests to your application.
12+
handlers:
13+
14+
# This handler tells app engine how to route requests to a WSGI application.
15+
# The script value is in the format <path.to.module>.<wsgi_application>
16+
# where <wsgi_application> is a WSGI application object.
17+
- url: .* # This regex directs all routes to main.app
18+
script: main.app
19+
20+
libraries:
21+
- name: webapp2
22+
version: "2.5.2"

appengine/multitenancy/favicon.ico

8.15 KB
Binary file not shown.

appengine/multitenancy/main.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
15+
# [START all]
16+
from google.appengine.api import namespace_manager
17+
from google.appengine.ext import ndb
18+
import webapp2
19+
20+
21+
class Counter(ndb.Model):
22+
"""Model for containing a count."""
23+
count = ndb.IntegerProperty()
24+
25+
26+
def update_counter(name):
27+
"""Increment the named counter by 1."""
28+
29+
def _update_counter(inner_name):
30+
counter = Counter.get_by_id(inner_name)
31+
if counter is None:
32+
counter = Counter(id=inner_name)
33+
counter.count = 0
34+
counter.count += 1
35+
counter.put()
36+
37+
# Update counter in a transaction.
38+
ndb.transaction(lambda: _update_counter(name))
39+
counter = Counter.get_by_id(name)
40+
return counter.count
41+
42+
43+
class SomeRequest(webapp2.RequestHandler):
44+
"""Perform synchronous requests to update counter."""
45+
46+
def get(self):
47+
update_counter('SomeRequest')
48+
# try/finally pattern to temporarily set the namespace.
49+
# Save the current namespace.
50+
namespace = namespace_manager.get_namespace()
51+
try:
52+
namespace_manager.set_namespace('-global-')
53+
x = update_counter('SomeRequest')
54+
finally:
55+
# Restore the saved namespace.
56+
namespace_manager.set_namespace(namespace)
57+
self.response.write('<html><body><p>Updated counters')
58+
self.response.write(' to %s' % x)
59+
self.response.write('</p></body></html>')
60+
61+
62+
app = webapp2.WSGIApplication([('/', SomeRequest)], debug=True)
63+
# [END all]

0 commit comments

Comments
 (0)