Skip to content

Commit 6a9a560

Browse files
committed
update links & switch mod13b to Cloud NDB
1 parent d55e7f4 commit 6a9a560

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

Diff for: README.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ Module | Topic | Video | Codelab | START here | FINISH here
8888
8|Migrate to Cloud Tasks| [link](https://twitter.com/googledevs/status/1450960021018267656?utm_source=twitter&utm_medium=unpaidsoc&utm_campaign=CDR_wes_aap-serverless_mgrcloudtasks_sms_201112&utm_content=-) | [link](http://g.co/codelabs/pae-migrate-cloudtasks) | Module 7 [code](/mod7-gaetasks) (2.x) | Module 8 [code](/mod8-cloudtasks) (2.x)
8989
9|Migrate to Python 3, Cloud Datastore & Cloud Tasks v2| _TBD_ | _TBD_ | Module 8 [code](/mod8-cloudtasks) (2.x) | _TBD_
9090
10|Migrate Datastore/Firestore data to another project| _TBD_ | _N/A_ | _N/A_ | _TBD_
91-
11|Migrate to Cloud Functions| _TBD_ | _TBD_ | Module 2 [code](/mod2b-cloudndb) (3.x) | Module 11 [code](/mod11-functions) (3.x)
92-
12|Add App Engine `memcache`| _TBD_ | _TBD_ | Module 1 [code](/mod1-flask) (2.x) | Module 12 [code](/mod12-memcache) (2.x) & [code](/mod12b-memcache) (3.x)
91+
11|Migrate to Cloud Functions| _TBD_ | [link](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-11-functions?utm_source=codelabs&utm_medium=et&utm_campaign=CDR_wes_aap-serverless_mgrcloudfuncs_sms_202006&utm_content=-) | Module 2 [code](/mod2b-cloudndb) (3.x) | Module 11 [code](/mod11-functions) (3.x)
92+
12|Add App Engine `memcache`| _TBD_ | [link](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-12-memcache?utm_source=codelabs&utm_medium=et&utm_campaign=CDR_wes_aap-serverless_mgrcloudfuncs_sms_202006&utm_content=-) | Module 1 [code](/mod1-flask) (2.x) | Module 12 [code](/mod12-memcache) (2.x) & [code](/mod12b-memcache) (3.x)
9393
13|Migrate to Cloud Memorystore| _TBD_ | _TBD_ | Module 12 [code](/mod12-memcache) (2.x) & [code](/mod12b-memcache) (3.x) | Module 13 [code](/mod13a-memorystore) (2.x) & [code](/mod13b-memorystore) (3.x)
9494

9595

@@ -98,7 +98,7 @@ Module | Topic | Video | Codelab | START here | FINISH here
9898
If there is a logical codelab to do immediately after completing one, they will be designated as NEXT. Other recommended codelabs will be listed as RECOMMENDED, and the more optional ones will be labeled as OTHERS (and usually in some kind of priority order).
9999

100100

101-
- [Module 1 codelab](http://g.co/codelabs/pae-migrate-flask): **Migrate from `webapp2` to [Flask](https://flask.palletsprojects.com)**
101+
- [Module 1 codelab](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-1-flask?utm_source=codelabs&utm_medium=et&utm_campaign=CDR_wes_aap-serverless_mgrwa2flsk_201008&utm_content=-): **Migrate from `webapp2` to [Flask](https://flask.palletsprojects.com)**
102102
- **Required** migration (can also pick your own framework)
103103
- `webapp2` does not do routing thus unsupported by App Engine (even though a [3.x port exists](https://github.com/fili/webapp2-gae-python37))
104104
- Python 2 only
@@ -107,8 +107,7 @@ If there is a logical codelab to do immediately after completing one, they will
107107
- NEXT:
108108
- Module 2 - migrate to Cloud NDB
109109

110-
111-
- [Module 2 codelab](http://g.co/codelabs/pae-migrate-cloudndb): **Migrate from App Engine `ndb` to [Cloud NDB](https://googleapis.dev/python/python-ndb/latest)**
110+
- [Module 2 codelab](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-2-cloudndb?utm_source=codelabs&utm_medium=et&utm_campaign=CDR_wes_aap-serverless_mgrcloudndb_201021&utm_content=-): **Migrate from App Engine `ndb` to [Cloud NDB](https://googleapis.dev/python/python-ndb/latest)**
112111
- **Required** migration
113112
- Migration to Cloud NDB which is supported by Python 3 and the next-gen platform.
114113
- Python 2
@@ -124,14 +123,14 @@ If there is a logical codelab to do immediately after completing one, they will
124123
- Module 4 - migrate to Cloud Run container with Docker
125124
- Module 3 - migrate to Cloud Datastore
126125

127-
- [Module 7 codelab](http://g.co/codelabs/pae-migrate-gaetasks): **Add App Engine (push) Task Queues to App Engine `ndb` Flask app**
126+
- [Module 7 codelab](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-7-gaetasks?utm_source=codelabs&utm_medium=et&utm_campaign=CDR_wes_aap-serverless_mgrgaetasks_sms_201028&utm_content=-): **Add App Engine (push) Task Queues to App Engine `ndb` Flask app**
128127
- **Not a migration**: add GAE Task Queues to prepare for migration to Cloud Tasks
129128
- Python 2
130129
- START: [Module 1 code - Framework](/mod1-flask) (2.x)
131130
- FINISH: [Module 7 code - GAE Task Queues](/mod7-gaetasks) (2.x)
132131
- NEXT: Module 8 - migrate App Engine push tasks to Cloud Tasks
133132

134-
- [Module 8 codelab](http://g.co/codelabs/pae-migrate-cloudtasks): **Migrate from App Engine (push) Task Queues to [Cloud Tasks](http://cloud.google.com/tasks) v1**
133+
- [Module 8 codelab](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-8-cloudtasks?utm_source=codelabs&utm_medium=et&utm_campaign=CDR_wes_aap-serverless_mgrcloudtasks_sms_201112&utm_content=-): **Migrate from App Engine (push) Task Queues to [Cloud Tasks](http://cloud.google.com/tasks) v1**
135134
- **Required** migration
136135
- Migration to Cloud Tasks which is supported by Python 3 and the next-gen platform.
137136
- Note this is only push tasks... pull tasks will be handled in a different codelab.
@@ -153,7 +152,7 @@ If there is a logical codelab to do immediately after completing one, they will
153152
- Module 5 - migrate to Cloud Run container with Cloud Buildpacks
154153
- Module 4 - migrate to Cloud Run container with Docker
155154

156-
- [Module 4 codelab](http://g.co/codelabs/pae-migrate-rundocker): **Migrate from App Engine to Cloud Run with Docker**
155+
- [Module 4 codelab](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-4-rundocker?utm_source=codelabs&utm_medium=et&utm_campaign=CDR_wes_aap-serverless_mgrcrdckr_sms_201017&utm_content=-): **Migrate from App Engine to Cloud Run with Docker**
157156
- **Optional** migration
158157
- "Containerize" your app (migrate your app to a container) with Docker
159158
- Python 2
@@ -168,7 +167,7 @@ If there is a logical codelab to do immediately after completing one, they will
168167
- Module 7 - add App Engine (push) tasks
169168
- Module 11 - migrate to Cloud Functions
170169

171-
- [Module 5 codelab](http://g.co/codelabs/pae-migrate-runbldpks): **Migrate from App Engine to Cloud Run with Cloud Buildpacks**
170+
- [Module 5 codelab](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-5-runbldpks?utm_source=codelabs&utm_medium=et&utm_campaign=CDR_wes_aap-serverless_mgrcrbdpk_sms_201031&utm_content=-): **Migrate from App Engine to Cloud Run with Cloud Buildpacks**
172171
- **Optional** migration
173172
- "Containerize" your app (migrate your app to a container) with...
174173
- [Cloud Buildpacks]() which lets you containerize your app without `Dockerfile`s
@@ -181,7 +180,7 @@ If there is a logical codelab to do immediately after completing one, they will
181180
- Module 7 - add App Engine (push) tasks
182181
- Module 11 - migrate to Cloud Functions
183182

184-
- [Module 3 codelab](http://g.co/codelabs/pae-migrate-datastore): **Migrate from Cloud NDB to [Cloud Datastore](http://cloud.google.com/datastore)**
183+
- [Module 3 codelab](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-3-datastore?utm_source=codelabs&utm_medium=et&utm_campaign=CDR_wes_aap-serverless_mgrcloudds_201003&utm_content=-): **Migrate from Cloud NDB to [Cloud Datastore](http://cloud.google.com/datastore)**
185184
- **Optional** migration
186185
- Recommended only if using Cloud Datastore elsewhere (GAE *or* non-App Engine) apps
187186
- Helps w/code consistency & reusability, reduces maintenance costs
@@ -198,7 +197,7 @@ If there is a logical codelab to do immediately after completing one, they will
198197
- Module 5 - migrate to Cloud Run container with Cloud Buildpacks
199198
- Module 4 - migrate to Cloud Run container with Docker
200199

201-
- **Module 11 codelab** (TBD): **Migrate from App Engine to Cloud Functions**
200+
- [Module 11 codelab](https://codelabs.developers.google.com/codelabs/cloud-gae-python-migrate-11-functions?utm_source=codelabs&utm_medium=et&utm_campaign=CDR_wes_aap-serverless_mgrcloudfuncs_sms_202006&utm_content=-): **Migrate from App Engine to Cloud Functions**
202201
- **Optional** migration
203202
- Recommende for small apps or for breaking up large apps into multiple microservices
204203
- Python 3 only

Diff for: mod13b-memorystore/main.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,34 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from datetime import datetime
1615
import os
1716
import pickle
1817
from flask import Flask, render_template, request
19-
from google.cloud import datastore
18+
from google.cloud import ndb
2019
import redis
2120

2221
app = Flask(__name__)
23-
ds_client = datastore.Client()
22+
ds_client = ndb.Client()
2423
HOUR = 3600
2524
REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')
2625
REDIS_PORT = os.environ.get('REDIS_PORT', '6379')
2726
REDIS = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
2827

28+
class Visit(ndb.Model):
29+
'Visit entity registers visitor IP address & timestamp'
30+
visitor = ndb.StringProperty()
31+
timestamp = ndb.DateTimeProperty(auto_now_add=True)
32+
2933
def store_visit(remote_addr, user_agent):
3034
'create new Visit entity in Datastore'
31-
entity = datastore.Entity(key=ds_client.key('Visit'))
32-
entity.update({
33-
'timestamp': datetime.now(),
34-
'visitor': '{}: {}'.format(remote_addr, user_agent),
35-
})
36-
ds_client.put(entity)
35+
with ds_client.context():
36+
Visit(visitor='{}: {}'.format(remote_addr, user_agent)).put()
3737

3838
def fetch_visits(limit):
3939
'get most recent visits'
40-
query = ds_client.query(kind='Visit')
41-
query.order = ['-timestamp']
42-
return query.fetch(limit=limit)
40+
with ds_client.context():
41+
return (v.to_dict() for v in Visit.query().order(
42+
-Visit.timestamp).fetch(limit))
4343

4444
@app.route('/')
4545
def root():

Diff for: mod13b-memorystore/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
flask
22
redis
3-
google-cloud-datastore
3+
google-cloud-ndb

Diff for: mod9-py3fstasks/main.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
import time
1818
from flask import Flask, render_template, request
1919
import google.auth
20-
from google.cloud import firestore, tasks
20+
from google.cloud import datastore, tasks
2121

2222
app = Flask(__name__)
23-
fs_client = firestore.Client()
23+
ds_client = datastore.Client()
2424
ts_client = tasks.CloudTasksClient()
2525

2626
_, PROJECT_ID = google.auth.default()
@@ -30,12 +30,13 @@
3030
PATH_PREFIX = QUEUE_PATH.rsplit('/', 2)[0]
3131

3232
def store_visit(remote_addr, user_agent):
33-
'create new Visit document in Firestore'
34-
doc_ref = fs_client.collection('Visit')
35-
doc_ref.add({
33+
'create new Visit entity in Datastore'
34+
entity = datastore.Entity(key=ds_client.key('Visit'))
35+
entity.update({
3636
'timestamp': datetime.now(),
3737
'visitor': '{}: {}'.format(remote_addr, user_agent),
3838
})
39+
ds_client.put(entity)
3940

4041
def _create_queue_if():
4142
'app-internal function creating default queue if it does not exist'
@@ -49,9 +50,9 @@ def _create_queue_if():
4950

5051
def fetch_visits(limit):
5152
'get most recent visits & add task to delete older visits'
52-
visits_ref = fs_client.collection('Visit')
53-
visits = list(v.to_dict() for v in visits_ref.order_by('timestamp',
54-
direction=firestore.Query.DESCENDING).limit(limit).stream())
53+
query = ds_client.query(kind='Visit')
54+
query.order = ['-timestamp']
55+
visits = query.fetch(limit=limit)
5556
oldest = time.mktime(visits[-1]['timestamp'].timetuple())
5657
oldest_str = time.ctime(oldest)
5758
print('Delete entities older than %s' % oldest_str)
@@ -78,7 +79,7 @@ def _delete_docs(visits):
7879
def trim():
7980
'(push) task queue handler to delete oldest visits'
8081
oldest = float(request.get_json().get('oldest'))
81-
query = fs_client.collection('Visit')
82+
query = ds_client.collection('Visit')
8283
visits = query.where('timestamp', '<',
8384
datetime.fromtimestamp(oldest)).stream()
8485
dlist = ', '.join(str(v_id) for v_id in _delete_docs(visits))

0 commit comments

Comments
 (0)