Skip to content

Commit 0ac88d4

Browse files
testtest
authored andcommitted
fix: TT-252 Refactor of the python-functions
1 parent 0bdb17a commit 0ac88d4

File tree

17 files changed

+106
-132
lines changed

17 files changed

+106
-132
lines changed

nodejs-functions/serverless.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ provider:
1919
custom:
2020
globalSchedule: cron(0 0 * * *)
2121
plugins:
22-
- serverless-azure-functions
22+
- serverless-azure-functions
2323
package:
2424
exclude:
2525
- local.settings.json

python-functions/README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,63 @@
11
# Azure Functions
22

3-
Refer to [Serverless docs](https://serverless.com/framework/docs/providers/azure/guide/intro/) for more information.
3+
4+
# CosmosDB bindings with Serverless Framework:
5+
6+
To use CosmosDB bindings modify the host.json on your main folder to:
7+
``` yaml
8+
{
9+
"version": "2.0",
10+
"extensionBundle": {
11+
"id": "Microsoft.Azure.Functions.ExtensionBundle",
12+
"version": "[1.*, 2.0.0)"
13+
}
14+
}
15+
```
16+
17+
## There is two types of CosmosDB bindings:
18+
### "cosmosdbtrigger"
19+
20+
The Azure Cosmos DB Trigger uses the Azure Cosmos DB Change Feed to listen for inserts and updates across partitions. The change feed publishes inserts and updates, not deletions.
21+
22+
To set-up this binding in the serverless framework, add this event to the serverless.yml
23+
``` yaml
24+
events:
25+
- cosmosDB: true
26+
x-azure-settings:
27+
name: documents # name of input parameter in function signature
28+
databaseName: <dbName>
29+
direction: in
30+
leaseCollectionName: leases
31+
connectionStringSetting: <ConnectionStringToTheCosmosDB>
32+
collectionName: <collectionName>
33+
createLeaseCollectionIfNotExists: true
34+
leaseCollectionPrefix: <prefixUsedIntheLeaseDB>
35+
```
36+
37+
### "cosmosdb"
38+
39+
The Azure Cosmos DB output binding lets you write a new document to an Azure Cosmos DB database using the SQL API.
40+
To set-up this binding in the serverless framework, add this event to the serverless.yml
41+
``` yaml
42+
- cosmosDB: true
43+
x-azure-settings:
44+
direction: out
45+
name: events # name the output in function signature
46+
databaseName: <dbName>
47+
collectionName: <collectionName>
48+
leaseCollectionName: leases
49+
createLeaseCollectionIfNotExists: true
50+
connectionStringSetting: <ConnectionStringToTheCosmosDB>
51+
createIfNotExists: true # A boolean value to indicate whether the collection is created
52+
```
53+
Problems with the CosmosDB bindings and the serverless framework:
54+
If there is only one event with the direction set to out, the serverless framework will change the name of this binding to “$return” this will cause problems because this name needs to be the same as the defined in the function signature, to solve this, instead of defining the name in the function signature like this:
55+
``` py
56+
def main(documents: func.DocumentList, events: func.Out[func.Document]):
57+
```
58+
59+
Define the return of the function like this:
60+
``` py
61+
def main(documents: func.DocumentList) -> func.Document:
62+
```
63+
And return directly the document that you want to add to the database.

python-functions/host.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"version": "2.0"
2+
"version": "2.0",
3+
"extensionBundle": {
4+
"id": "Microsoft.Azure.Functions.ExtensionBundle",
5+
"version": "[1.*, 2.0.0)"
6+
}
37
}

python-functions/serverless.yml

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
service: python-functions-test1
1+
service: python-functions-test1-len
22
frameworkVersion: '2'
33

44
provider:
55
name: azure
66
region: West US 2
77
runtime: python3.8 # python3.7 or python3.8 also available
88
os: linux # linux is the only operating system available for python
9-
prefix: time-tracker
9+
prefix: time-tracker-test-lenin
1010
environment: # these will be created as application settings
1111
COSMOS_DATABASE_URI: ${file(keys.yml):COSMOS_DATABASE_URI}
1212

@@ -35,7 +35,7 @@ package:
3535

3636
functions:
3737
handle_activity_events_trigger:
38-
handler: src/handlers/handle_activity_events_trigger/__init__.py
38+
handler: src/handlers/handle_activity_events_trigger/handle_events_trigger.main
3939
events:
4040
- cosmosDB: true
4141
x-azure-settings:
@@ -46,19 +46,19 @@ functions:
4646
databaseName: time-tracker-db
4747
collectionName: activity
4848
createLeaseCollectionIfNotExists: true
49-
leaseCollectionPrefix: activity_
49+
leaseCollectionPrefix: activity
5050
- cosmosDB: true
5151
x-azure-settings:
5252
connectionStringSetting: COSMOS_DATABASE_URI
5353
direction: out
54-
name: events # name of input parameter in function signature
54+
name: $return # name of input parameter in function signature
5555
databaseName: time-tracker-db
5656
collectionName: event
5757
createIfNotExists: true # A boolean value to indicate whether the collection is created
5858
createLeaseCollectionIfNotExists: true
5959

6060
handle_customer_trigger:
61-
handler: src/handlers/handle_customer_trigger/__init__.py
61+
handler: src/handlers/handle_customer_trigger/handle_events_trigger.main
6262
events:
6363
- cosmosDB: true
6464
x-azure-settings:
@@ -68,11 +68,11 @@ functions:
6868
databaseName: time-tracker-db
6969
collectionName: customer
7070
createLeaseCollectionIfNotExists: true
71-
leaseCollectionPrefix: customer_
71+
leaseCollectionPrefix: customer
7272
- cosmosDB: true
7373
x-azure-settings:
7474
direction: out
75-
name: events # name of input parameter in function signature
75+
name: $return # name of input parameter in function signature
7676
databaseName: time-tracker-db
7777
collectionName: event
7878
leaseCollectionName: leases
@@ -81,7 +81,7 @@ functions:
8181
createIfNotExists: true # A boolean value to indicate whether the collection is created
8282

8383
handle_project_events_trigger:
84-
handler: src/handlers/handle_project_events_trigger/__init__.py
84+
handler: src/handlers/handle_project_events_trigger/handle_events_trigger.main
8585
events:
8686
- cosmosDB: true
8787
x-azure-settings:
@@ -92,11 +92,11 @@ functions:
9292
databaseName: time-tracker-db
9393
collectionName: project
9494
createLeaseCollectionIfNotExists: true
95-
leaseCollectionPrefix: project_
95+
leaseCollectionPrefix: project
9696
- cosmosDB: true
9797
x-azure-settings:
9898
direction: out
99-
name: events # name of input parameter in function signature
99+
name: $return # name of input parameter in function signature
100100
databaseName: time-tracker-db
101101
collectionName: event
102102
leaseCollectionName: leases
@@ -105,22 +105,23 @@ functions:
105105
createIfNotExists: true # A boolean value to indicate whether the collection is created
106106

107107
handle_project_type_events_trigger:
108-
handler: src/handlers/handle_project_type_events_trigger/__init__.py
108+
handler: src/handlers/handle_project_type_events_trigger/handle_events_trigger.main
109109
events:
110110
- cosmosDB: true
111-
x-azure-settings:
112-
name: documents # name of input parameter in function signature
111+
x-azure-settings:
112+
name: document # name of input parameter in function signature
113113
direction: in
114114
leaseCollectionName: leases
115115
connectionStringSetting: COSMOS_DATABASE_URI
116116
databaseName: time-tracker-db
117117
collectionName: project_type
118118
createLeaseCollectionIfNotExists: true
119-
leaseCollectionPrefix: project_type_
119+
leaseCollectionPrefix: project_type
120+
120121
- cosmosDB: true
121122
x-azure-settings:
122123
direction: out
123-
name: events # name of input parameter in function signature
124+
name: $return # name of input parameter in function signature
124125
databaseName: time-tracker-db
125126
collectionName: event
126127
leaseCollectionName: leases
@@ -129,25 +130,26 @@ functions:
129130
createIfNotExists: true # A boolean value to indicate whether the collection is created
130131

131132
handle_time_entry_events_trigger:
132-
handler: src/handlers/handle_time_entry_events_trigger/__init__.py
133+
handler: src/handlers/handle_time_entry_events_trigger/handle_events_trigger.main
133134
events:
134135
- cosmosDB: true
135136
x-azure-settings:
136137
name: documents # name of input parameter in function signature
138+
databaseName: time-tracker-db
137139
direction: in
138140
leaseCollectionName: leases
141+
leaseCollectionPrefix: time_entry
139142
connectionStringSetting: COSMOS_DATABASE_URI
140-
databaseName: time-tracker-db
141143
collectionName: time_entry
142144
createLeaseCollectionIfNotExists: true
143-
leaseCollectionPrefix: time_entry_
145+
144146
- cosmosDB: true
145147
x-azure-settings:
146148
direction: out
147-
name: events # name of input parameter in function signature
149+
name: $return # name of input parameter in function signature
148150
databaseName: time-tracker-db
149151
collectionName: event
150152
leaseCollectionName: leases
151153
createLeaseCollectionIfNotExists: true
152154
connectionStringSetting: COSMOS_DATABASE_URI
153-
createIfNotExists: true # A boolean value to indicate whether the collection is created
155+
createIfNotExists: true # A boolean value to indicate whether the collection is created

python-functions/src/handlers/goodbye.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

python-functions/src/handlers/handle_activity_events_trigger/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

python-functions/src/handlers/handle_activity_events_trigger/handle_events_trigger.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import azure.functions as func
66

77

8-
def main(documents: func.DocumentList, events: func.Out[func.Document]):
8+
def main(documents: func.DocumentList) -> func.Document:
99
if documents:
1010
new_events = func.DocumentList()
1111

@@ -29,6 +29,7 @@ def main(documents: func.DocumentList, events: func.Out[func.Document]):
2929
logging.warning("- Not saved!")
3030

3131
if len(new_events):
32-
events.set(new_events)
32+
return new_events
33+
3334
else:
3435
logging.warning("No valid events were found!")

python-functions/src/handlers/handle_customer_trigger/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

python-functions/src/handlers/handle_customer_trigger/handle_events_trigger.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import azure.functions as func
66

77

8-
def main(documents: func.DocumentList, events: func.Out[func.Document]):
8+
def main(documents: func.DocumentList) -> func.Document:
99
if documents:
1010
new_events = func.DocumentList()
1111

@@ -29,6 +29,7 @@ def main(documents: func.DocumentList, events: func.Out[func.Document]):
2929
logging.warning("- Not saved!")
3030

3131
if len(new_events):
32-
events.set(new_events)
32+
return new_events
33+
3334
else:
3435
logging.warning("No valid events were found!")

python-functions/src/handlers/handle_project_events_trigger/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)