Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nodejs-functions/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ provider:
custom:
globalSchedule: cron(0 0 * * *)
plugins:
- serverless-azure-functions
- serverless-azure-functions
package:
exclude:
- local.settings.json
Expand Down
62 changes: 61 additions & 1 deletion python-functions/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
# Azure Functions

Refer to [Serverless docs](https://serverless.com/framework/docs/providers/azure/guide/intro/) for more information.

# CosmosDB bindings with Serverless Framework:

To use CosmosDB bindings modify the host.json on your main folder to:
``` yaml
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
```

## There is two types of CosmosDB bindings:
### "cosmosdbtrigger"

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.

To set-up this binding in the serverless framework, add this event to the serverless.yml
``` yaml
events:
- cosmosDB: true
x-azure-settings:
name: documents # name of input parameter in function signature
databaseName: <dbName>
direction: in
leaseCollectionName: leases
connectionStringSetting: <ConnectionStringToTheCosmosDB>
collectionName: <collectionName>
createLeaseCollectionIfNotExists: true
leaseCollectionPrefix: <prefixUsedIntheLeaseDB>
```

### "cosmosdb"

The Azure Cosmos DB output binding lets you write a new document to an Azure Cosmos DB database using the SQL API.
To set-up this binding in the serverless framework, add this event to the serverless.yml
``` yaml
- cosmosDB: true
x-azure-settings:
direction: out
name: events # name the output in function signature
databaseName: <dbName>
collectionName: <collectionName>
leaseCollectionName: leases
createLeaseCollectionIfNotExists: true
connectionStringSetting: <ConnectionStringToTheCosmosDB>
createIfNotExists: true # A boolean value to indicate whether the collection is created
```
Problems with the CosmosDB bindings and the serverless framework:
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:
``` py
def main(documents: func.DocumentList, events: func.Out[func.Document]):
```

Define the return of the function like this:
``` py
def main(documents: func.DocumentList) -> func.Document:
```
And return directly the document that you want to add to the database.
6 changes: 5 additions & 1 deletion python-functions/host.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"version": "2.0"
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
44 changes: 23 additions & 21 deletions python-functions/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
service: python-functions-test1
service: python-functions-test1-len
frameworkVersion: '2'

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

Expand Down Expand Up @@ -35,7 +35,7 @@ package:

functions:
handle_activity_events_trigger:
handler: src/handlers/handle_activity_events_trigger/__init__.py
handler: src/handlers/handle_activity_events_trigger/handle_events_trigger.main
events:
- cosmosDB: true
x-azure-settings:
Expand All @@ -46,19 +46,19 @@ functions:
databaseName: time-tracker-db
collectionName: activity
createLeaseCollectionIfNotExists: true
leaseCollectionPrefix: activity_
leaseCollectionPrefix: activity
- cosmosDB: true
x-azure-settings:
connectionStringSetting: COSMOS_DATABASE_URI
direction: out
name: events # name of input parameter in function signature
name: $return # name of input parameter in function signature
databaseName: time-tracker-db
collectionName: event
createIfNotExists: true # A boolean value to indicate whether the collection is created
createLeaseCollectionIfNotExists: true

handle_customer_trigger:
handler: src/handlers/handle_customer_trigger/__init__.py
handler: src/handlers/handle_customer_trigger/handle_events_trigger.main
events:
- cosmosDB: true
x-azure-settings:
Expand All @@ -68,11 +68,11 @@ functions:
databaseName: time-tracker-db
collectionName: customer
createLeaseCollectionIfNotExists: true
leaseCollectionPrefix: customer_
leaseCollectionPrefix: customer
- cosmosDB: true
x-azure-settings:
direction: out
name: events # name of input parameter in function signature
name: $return # name of input parameter in function signature
databaseName: time-tracker-db
collectionName: event
leaseCollectionName: leases
Expand All @@ -81,7 +81,7 @@ functions:
createIfNotExists: true # A boolean value to indicate whether the collection is created

handle_project_events_trigger:
handler: src/handlers/handle_project_events_trigger/__init__.py
handler: src/handlers/handle_project_events_trigger/handle_events_trigger.main
events:
- cosmosDB: true
x-azure-settings:
Expand All @@ -92,11 +92,11 @@ functions:
databaseName: time-tracker-db
collectionName: project
createLeaseCollectionIfNotExists: true
leaseCollectionPrefix: project_
leaseCollectionPrefix: project
- cosmosDB: true
x-azure-settings:
direction: out
name: events # name of input parameter in function signature
name: $return # name of input parameter in function signature
databaseName: time-tracker-db
collectionName: event
leaseCollectionName: leases
Expand All @@ -105,22 +105,23 @@ functions:
createIfNotExists: true # A boolean value to indicate whether the collection is created

handle_project_type_events_trigger:
handler: src/handlers/handle_project_type_events_trigger/__init__.py
handler: src/handlers/handle_project_type_events_trigger/handle_events_trigger.main
events:
- cosmosDB: true
x-azure-settings:
name: documents # name of input parameter in function signature
x-azure-settings:
name: document # name of input parameter in function signature
direction: in
leaseCollectionName: leases
connectionStringSetting: COSMOS_DATABASE_URI
databaseName: time-tracker-db
collectionName: project_type
createLeaseCollectionIfNotExists: true
leaseCollectionPrefix: project_type_
leaseCollectionPrefix: project_type

- cosmosDB: true
x-azure-settings:
direction: out
name: events # name of input parameter in function signature
name: $return # name of input parameter in function signature
databaseName: time-tracker-db
collectionName: event
leaseCollectionName: leases
Expand All @@ -129,25 +130,26 @@ functions:
createIfNotExists: true # A boolean value to indicate whether the collection is created

handle_time_entry_events_trigger:
handler: src/handlers/handle_time_entry_events_trigger/__init__.py
handler: src/handlers/handle_time_entry_events_trigger/handle_events_trigger.main
events:
- cosmosDB: true
x-azure-settings:
name: documents # name of input parameter in function signature
databaseName: time-tracker-db
direction: in
leaseCollectionName: leases
leaseCollectionPrefix: time_entry
connectionStringSetting: COSMOS_DATABASE_URI
databaseName: time-tracker-db
collectionName: time_entry
createLeaseCollectionIfNotExists: true
leaseCollectionPrefix: time_entry_

- cosmosDB: true
x-azure-settings:
direction: out
name: events # name of input parameter in function signature
name: $return # name of input parameter in function signature
databaseName: time-tracker-db
collectionName: event
leaseCollectionName: leases
createLeaseCollectionIfNotExists: true
connectionStringSetting: COSMOS_DATABASE_URI
createIfNotExists: true # A boolean value to indicate whether the collection is created
createIfNotExists: true # A boolean value to indicate whether the collection is created
24 changes: 0 additions & 24 deletions python-functions/src/handlers/goodbye.py

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import azure.functions as func


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

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

if len(new_events):
events.set(new_events)
return new_events

else:
logging.warning("No valid events were found!")

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import azure.functions as func


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

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

if len(new_events):
events.set(new_events)
return new_events

else:
logging.warning("No valid events were found!")

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import azure.functions as func


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

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

if len(new_events):
events.set(new_events)
return new_events

else:
logging.warning("No valid events were found!")

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import azure.functions as func


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

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

if len(new_events):
events.set(new_events)
return new_events

else:
logging.warning("No valid events were found!")

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import azure.functions as func


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

Expand All @@ -18,7 +18,7 @@ def main(documents: func.DocumentList, events: func.Out[func.Document]):
"id": str(uuid.uuid4()),
"date": datetime.utcnow().isoformat(),
"user_id": event_context.get("user_id"),
"action": event_context.get("action"),
"action": event_context.get("action") + 'delete_me',
"description": event_context.get("description"),
"item_id": doc.get("id"),
"container_id": event_context.get("container_id"),
Expand All @@ -29,6 +29,7 @@ def main(documents: func.DocumentList, events: func.Out[func.Document]):
logging.warning("- Not saved!")

if len(new_events):
events.set(new_events)
return new_events

else:
logging.warning("No valid events were found!")
Loading