Added eventgrid trigger template for python

This commit is contained in:
pthummar 2023-05-09 16:14:12 -05:00
Родитель 437f6a471e
Коммит b99863f15c
6 изменённых файлов: 313 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,10 @@
# Register this blueprint by adding the following line of code
# to your entry point file.
# app.register_functions($(BLUEPRINT_FILENAME))
#
#Please refer to https://aka.ms/azure-functions-python-blueprints
from azure.functions import Blueprint
$(BLUEPRINT_FILENAME) = Blueprint()

Просмотреть файл

@ -0,0 +1,16 @@
@$(BLUEPRINT_FILENAME).event_grid_trigger(arg_name="azeventgrid")
def $(FUNCTION_NAME_INPUT)(azeventgrid: EventGridEvent):
result = json.dumps({
'id': azeventgrid.id,
'data': azeventgrid.get_json(),
'topic': azeventgrid.topic,
'subject': azeventgrid.subject,
'event_type': azeventgrid.event_type,
})
logging.info('Python EventGrid trigger processed an event: %s', result)

Просмотреть файл

@ -0,0 +1,43 @@
# Azure Functions: Event Grid Trigger in Python
## Event Grid Trigger
The Event Grid function trigger can be used to respond to an event sent by an Event Grid source. You must have an event subscription to the source to receive events. When the function is triggered, it converts the event data into a JSON string which is then logged using the Python logging module.
## Using the Template
Following is an example code snippet for Event Grid Trigger using the Python programming model V2 (currently in Preview).
```python
import logging
import azure.functions as func
app = func.FunctionApp()
@app.function_name(name="eventgridtrigger")
@app.event_grid_trigger(arg_name="event")
def test_function(event: func.EventGridEvent):
result = json.dumps({
'id': event.id,
'data': event.get_json(),
'topic': event.topic,
'subject': event.subject,
'event_type': event.event_type,
})
logging.info('Python EventGrid trigger processed an event: %s', result)
```
To run the code snippet generated through the command palette, note the following:
- The function application is defined and named `app`.
- Confirm that the parameters within the trigger reflect values that correspond with your storage account.
- The name of the file must be `function_app.py`.
## Programming Model V2 (Preview)
The new programming model in Azure Functions Python delivers an experience that aligns with Python development principles, and subsequently with commonly used Python frameworks.
The improved programming model requires fewer files than the default model, and specifically eliminates the need for a configuration file (`function.json`). Instead, triggers and bindings are represented in the `function_app.py` file as decorators. Moreover, functions can be logically organized with support for multiple functions to be stored in the same file. Functions within the same function application can also be stored in different files, and be referenced as blueprints.
To learn more about using the new Python programming model for Azure Functions, see the [Azure Functions Python developer guide](https://aka.ms/pythondeveloperguide). Note that in addition to the documentation, [hints](https://aka.ms/functions-python-hints) are available in code editors that support type checking with PYI files.
To learn more about the new programming model for Azure Functions in Python, see [Programming Models in Azure Functions](https://aka.ms/functions-programming-models).

Просмотреть файл

@ -0,0 +1,17 @@
from azure.functions import FunctionApp, EventGridEvent
import logging
app = FunctionApp()
@app.event_grid_trigger(arg_name="azeventgrid")
def $(FUNCTION_NAME_INPUT)(azeventgrid: EventGridEvent):
result = json.dumps({
'id': azeventgrid.id,
'data': azeventgrid.get_json(),
'topic': azeventgrid.topic,
'subject': azeventgrid.subject,
'event_type': azeventgrid.event_type,
})
logging.info('Python EventGrid trigger processed an event: %s', result)

Просмотреть файл

@ -0,0 +1,13 @@
@app.event_grid_trigger(arg_name="azeventgrid")
def $(FUNCTION_NAME_INPUT)(azeventgrid: EventGridEvent):
result = json.dumps({
'id': azeventgrid.id,
'data': azeventgrid.get_json(),
'topic': azeventgrid.topic,
'subject': azeventgrid.subject,
'event_type': azeventgrid.event_type,
})
logging.info('Python EventGrid trigger processed an event: %s', result)

Просмотреть файл

@ -0,0 +1,214 @@
{
"author": "Prashant Thummar",
"name": "EventGrid trigger",
"description": "$EventGridTrigger_description",
"programmingModel": "v2",
"language": "python",
"jobs": [
{
"name": "Create New Project with EventGridTrigger function",
"type": "CreateNewApp",
"inputs" : [
{
"assignTo": "$(APP_FILENAME)",
"paramId": "app-fileName",
"defaultValue": "function_app.py",
"required": true
},
{
"assignTo": "$(FUNCTION_NAME_INPUT)",
"paramId": "trigger-functionName",
"required": true,
"defaultValue": "EventGridTrigger"
},
{
"assignTo": "$(EVENTGRID_NAME_INPUT)",
"paramId": "eventgridTrigger-eventgridName",
"required": true,
"defaultValue": "myeventgrid"
},
{
"assignTo": "$(CONNECTION_STRING_INPUT)",
"paramId": "eventgridTrigger-connection",
"required": true,
"defaultValue": ""
}
],
"actions": [
"readFileContent_FunctionApp",
"writeFile_FunctionApp",
"showMarkdownPreview"
]
},
{
"name": "Add EventGridTrigger function to the main file",
"type": "AppendToFile",
"inputs" : [
{
"assignTo": "$(APP_FILENAME)",
"paramId": "app-selectedFileName",
"defaultValue": "function_app.py",
"required": true
},
{
"assignTo": "$(FUNCTION_NAME_INPUT)",
"paramId": "trigger-functionName",
"required": true,
"defaultValue": "EventGridTrigger"
},
{
"assignTo": "$(EVENTGRID_NAME_INPUT)",
"paramId": "eventgridTrigger-eventgridName",
"required": true,
"defaultValue": "myeventgrid"
},
{
"assignTo": "$(CONNECTION_STRING_INPUT)",
"paramId": "eventgridTrigger-connection",
"required": true,
"defaultValue": ""
}
],
"actions": [
"readFileContent_FunctionBody",
"appendFileContent_FunctionApp"
]
},
{
"name": "Create New Blueprint file",
"type": "CreateNewBlueprint",
"inputs" : [
{
"assignTo": "$(BLUEPRINT_FILENAME)",
"paramId": "blueprint-fileName",
"defaultValue": "blueprint.py",
"required": true
},
{
"assignTo": "$(FUNCTION_NAME_INPUT)",
"paramId": "trigger-functionName",
"required": true,
"defaultValue": "EventGridTrigger"
},
{
"assignTo": "$(EVENTGRID_NAME_INPUT)",
"paramId": "eventgridTrigger-eventgridName",
"required": true,
"defaultValue": "myeventgrid"
},
{
"assignTo": "$(CONNECTION_STRING_INPUT)",
"paramId": "eventgridTrigger-connection",
"required": true,
"defaultValue": ""
}
],
"actions": [
"readFileContent_BlueprintFile",
"writeFile_BlueprintFile",
"readFileContent_BlueprintBody",
"appendFileContent_BlueprintBody"
]
},
{
"name": "Add EventGridTrigger function to the Blueprint",
"type": "AppendToBlueprint",
"inputs" : [
{
"assignTo": "$(BLUEPRINT_FILENAME)",
"paramId": "blueprint-existingFileName",
"required": true
},
{
"assignTo": "$(FUNCTION_NAME_INPUT)",
"paramId": "trigger-functionName",
"required": true,
"defaultValue": "EventGridTrigger"
},
{
"assignTo": "$(EVENTGRID_NAME_INPUT)",
"paramId": "eventgridTrigger-eventgridName",
"required": true,
"defaultValue": "myeventgrid"
},
{
"assignTo": "$(CONNECTION_STRING_INPUT)",
"paramId": "eventgridTrigger-connection",
"required": true,
"defaultValue": ""
}
],
"actions": [
"readFileContent_BlueprintBody",
"appendFileContent_BlueprintBody"
]
}
],
"actions" : [
{
"name": "readFileContent_FunctionApp",
"type": "GetTemplateFileContent",
"assignTo": "$(FUNCTION_APP_CONTENT)",
"filePath": "function_app.py"
},
{
"name": "writeFile_FunctionApp",
"type": "WriteToFile",
"source": "$(FUNCTION_APP_CONTENT)",
"filePath": "$(APP_FILENAME).py",
"continueOnError": false,
"errorText": "Unable to create the function app",
"replaceTokens": true
},
{
"name": "readFileContent_FunctionBody",
"type": "GetTemplateFileContent",
"assignTo": "$(FUNCTION_BODY)",
"filePath" : "function_body.py"
},
{
"name": "appendFileContent_FunctionApp",
"type": "AppendToFile",
"createIfNotExists" : false,
"source": "$(FUNCTION_BODY)",
"filePath" : "$(APP_FILENAME).py",
"continueOnError" : false,
"errorText" : "Unable to create eventgrid trigger function"
},
{
"name": "ShowMarkdownPreview",
"type": "ShowMarkdownPreview",
"filePath" : "eventgrid_trigger_template.md"
},
{
"name": "readFileContent_BlueprintFile",
"type": "GetTemplateFileContent",
"assignTo": "$(BLUEPRINT_CONTENT)",
"filePath" : "blueprint.py"
},
{
"name": "writeFile_BlueprintFile",
"type": "WriteToFile",
"source": "$(BLUEPRINT_CONTENT)",
"filePath" : "$(BLUEPRINT_FILENAME).py",
"continueOnError" : false,
"errorText" : "Unable to create blueprint",
"replaceTokens": true
},
{
"name": "readFileContent_BlueprintBody",
"type": "GetTemplateFileContent",
"assignTo": "$(BLUEPRINT_BODY_CONTENT)",
"filePath" : "blueprint_body.py"
},
{
"name": "appendFileContent_BlueprintBody",
"type": "AppendToFile",
"source": "$(BLUEPRINT_BODY_CONTENT)",
"filePath" : "$(BLUEPRINT_FILENAME).py",
"continueOnError" : false,
"errorText" : "Unable to create the Blueprint",
"replaceTokens": true
}
]
}