From 6f70c9247f45f577d1eae30673aa512b280fbf92 Mon Sep 17 00:00:00 2001 From: abhijeet007rocks8 Date: Wed, 19 Oct 2022 19:48:51 +0530 Subject: [PATCH 1/3] added python azure-funtions event trigger --- .../azure-functions/python/.funcignore | 4 + .../azure-functions/python/.gitignore | 135 ++++++++++++++++++ .../python/EchoHttpTrigger/__init__.py | 74 ++++++++++ .../python/EchoHttpTrigger/function.json | 20 +++ .../python/EchoHttpTrigger/sample.dat | 3 + .../azure-functions/python/host.json | 15 ++ .../azure-functions/python/requirements.txt | 5 + 7 files changed, 256 insertions(+) create mode 100644 community/boilerplates/event-triggers/azure-functions/python/.funcignore create mode 100644 community/boilerplates/event-triggers/azure-functions/python/.gitignore create mode 100644 community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/__init__.py create mode 100644 community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/function.json create mode 100644 community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/sample.dat create mode 100644 community/boilerplates/event-triggers/azure-functions/python/host.json create mode 100644 community/boilerplates/event-triggers/azure-functions/python/requirements.txt diff --git a/community/boilerplates/event-triggers/azure-functions/python/.funcignore b/community/boilerplates/event-triggers/azure-functions/python/.funcignore new file mode 100644 index 0000000000000..fa6cc3f80ee41 --- /dev/null +++ b/community/boilerplates/event-triggers/azure-functions/python/.funcignore @@ -0,0 +1,4 @@ +.git* +.vscode +local.settings.json +test \ No newline at end of file diff --git a/community/boilerplates/event-triggers/azure-functions/python/.gitignore b/community/boilerplates/event-triggers/azure-functions/python/.gitignore new file mode 100644 index 0000000000000..3e4bf64199b9e --- /dev/null +++ b/community/boilerplates/event-triggers/azure-functions/python/.gitignore @@ -0,0 +1,135 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don’t work, or not +# install all needed dependencies. +#Pipfile.lock + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# Azure Functions artifacts +bin +obj +appsettings.json +local.settings.json + +# Azurite artifacts +__blobstorage__ +__queuestorage__ +__azurite_db*__.json +.python_packages \ No newline at end of file diff --git a/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/__init__.py b/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/__init__.py new file mode 100644 index 0000000000000..b49b5e2a5dbe2 --- /dev/null +++ b/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/__init__.py @@ -0,0 +1,74 @@ +import logging + +import azure.functions as func + +from enum import Enum + + +class Note : + id = None + note = None + def __init__(self, id, note): + self.id = id + self.note=note + def getId(self) : + return self.id + def setId(self, id) : + self.id = id + def getNote(self) : + return self.note + def setNote(self, note) : + self.note = note + + + +class Notebook : + class Operation(Enum) : + INSERT = 0 + UPDATE = 1 + DELETE = 2 + def handleOperation(self, operation, newNote, oldNote) : + if (operation==Notebook.Operation.INSERT): + return "New note " + str(newNote.getId()) + " inserted, with data: " + newNote.getNote() + elif(operation==Notebook.Operation.UPDATE): + return "Note " + str(newNote.getId()) + " updated, with data: " + newNote.getNote() + elif(operation==Notebook.Operation.DELETE): + return "Note " + str(oldNote.getId()) + " deleted, with data: " + oldNote.getNote() + else: + raise Exception("operation: " + str(operation)) + + +def __inti__(self): + self.notebook = Notebook() + +def handleRequest(req_body): + table= req_body.get('table') + if (table != NULL and table == 'notes'): + operation = Notebook.Operation.valueOf(req_body.get("event.op")); + newNote = Note(req_body.get('event.data.new.id'), req_body.get('event.data.new.note')) + oldNote = Note(req_body.get('event.data.old.id'), req_body.get('event.data.old.note')) + return self.notebook.handleOperation(operation, newNote, oldNote) + else: + return "Table not supported " + table + + +def main(req: func.HttpRequest) -> func.HttpResponse: + logging.info('Python HTTP trigger function processed a request.') + + # name = req.params.get('name') + try: + req_body = req.get_json() + except ValueError: + pass + # name = req_body.get('name') + + response = handleRequest(req_body); + + return func.HttpResponse(response, status_code=200) + # if name: + # return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.") + # else: + # return func.HttpResponse( + # "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.", + # status_code=200 + # ) diff --git a/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/function.json b/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/function.json new file mode 100644 index 0000000000000..d9019652afe1f --- /dev/null +++ b/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/function.json @@ -0,0 +1,20 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "function", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "get", + "post" + ] + }, + { + "type": "http", + "direction": "out", + "name": "$return" + } + ] +} \ No newline at end of file diff --git a/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/sample.dat b/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/sample.dat new file mode 100644 index 0000000000000..2e6094396cb44 --- /dev/null +++ b/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/community/boilerplates/event-triggers/azure-functions/python/host.json b/community/boilerplates/event-triggers/azure-functions/python/host.json new file mode 100644 index 0000000000000..fd4bee790b930 --- /dev/null +++ b/community/boilerplates/event-triggers/azure-functions/python/host.json @@ -0,0 +1,15 @@ +{ + "version": "2.0", + "logging": { + "applicationInsights": { + "samplingSettings": { + "isEnabled": true, + "excludedTypes": "Request" + } + } + }, + "extensionBundle": { + "id": "Microsoft.Azure.Functions.ExtensionBundle", + "version": "[3.*, 4.0.0)" + } +} \ No newline at end of file diff --git a/community/boilerplates/event-triggers/azure-functions/python/requirements.txt b/community/boilerplates/event-triggers/azure-functions/python/requirements.txt new file mode 100644 index 0000000000000..bdb8fc593a87e --- /dev/null +++ b/community/boilerplates/event-triggers/azure-functions/python/requirements.txt @@ -0,0 +1,5 @@ +# DO NOT include azure-functions-worker in this file +# The Python Worker is managed by Azure Functions platform +# Manually managing azure-functions-worker may cause unexpected issues + +azure-functions From 3848e0374f1e11f1794b1ac95b40f69bad6b12f9 Mon Sep 17 00:00:00 2001 From: abhijeet007rocks8 Date: Wed, 19 Oct 2022 20:09:56 +0530 Subject: [PATCH 2/3] echo event trigger in python --- .../python/EchoHttpTrigger/__init__.py | 74 ------------------- .../python/{ => echo}/.funcignore | 0 .../python/{ => echo}/.gitignore | 0 .../python/echo/EchoHttpTrigger/__init__.py | 11 +++ .../{ => echo}/EchoHttpTrigger/function.json | 0 .../{ => echo}/EchoHttpTrigger/sample.dat | 0 .../python/{ => echo}/host.json | 0 .../python/{ => echo}/requirements.txt | 0 8 files changed, 11 insertions(+), 74 deletions(-) delete mode 100644 community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/__init__.py rename community/boilerplates/event-triggers/azure-functions/python/{ => echo}/.funcignore (100%) rename community/boilerplates/event-triggers/azure-functions/python/{ => echo}/.gitignore (100%) create mode 100644 community/boilerplates/event-triggers/azure-functions/python/echo/EchoHttpTrigger/__init__.py rename community/boilerplates/event-triggers/azure-functions/python/{ => echo}/EchoHttpTrigger/function.json (100%) rename community/boilerplates/event-triggers/azure-functions/python/{ => echo}/EchoHttpTrigger/sample.dat (100%) rename community/boilerplates/event-triggers/azure-functions/python/{ => echo}/host.json (100%) rename community/boilerplates/event-triggers/azure-functions/python/{ => echo}/requirements.txt (100%) diff --git a/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/__init__.py b/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/__init__.py deleted file mode 100644 index b49b5e2a5dbe2..0000000000000 --- a/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/__init__.py +++ /dev/null @@ -1,74 +0,0 @@ -import logging - -import azure.functions as func - -from enum import Enum - - -class Note : - id = None - note = None - def __init__(self, id, note): - self.id = id - self.note=note - def getId(self) : - return self.id - def setId(self, id) : - self.id = id - def getNote(self) : - return self.note - def setNote(self, note) : - self.note = note - - - -class Notebook : - class Operation(Enum) : - INSERT = 0 - UPDATE = 1 - DELETE = 2 - def handleOperation(self, operation, newNote, oldNote) : - if (operation==Notebook.Operation.INSERT): - return "New note " + str(newNote.getId()) + " inserted, with data: " + newNote.getNote() - elif(operation==Notebook.Operation.UPDATE): - return "Note " + str(newNote.getId()) + " updated, with data: " + newNote.getNote() - elif(operation==Notebook.Operation.DELETE): - return "Note " + str(oldNote.getId()) + " deleted, with data: " + oldNote.getNote() - else: - raise Exception("operation: " + str(operation)) - - -def __inti__(self): - self.notebook = Notebook() - -def handleRequest(req_body): - table= req_body.get('table') - if (table != NULL and table == 'notes'): - operation = Notebook.Operation.valueOf(req_body.get("event.op")); - newNote = Note(req_body.get('event.data.new.id'), req_body.get('event.data.new.note')) - oldNote = Note(req_body.get('event.data.old.id'), req_body.get('event.data.old.note')) - return self.notebook.handleOperation(operation, newNote, oldNote) - else: - return "Table not supported " + table - - -def main(req: func.HttpRequest) -> func.HttpResponse: - logging.info('Python HTTP trigger function processed a request.') - - # name = req.params.get('name') - try: - req_body = req.get_json() - except ValueError: - pass - # name = req_body.get('name') - - response = handleRequest(req_body); - - return func.HttpResponse(response, status_code=200) - # if name: - # return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.") - # else: - # return func.HttpResponse( - # "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.", - # status_code=200 - # ) diff --git a/community/boilerplates/event-triggers/azure-functions/python/.funcignore b/community/boilerplates/event-triggers/azure-functions/python/echo/.funcignore similarity index 100% rename from community/boilerplates/event-triggers/azure-functions/python/.funcignore rename to community/boilerplates/event-triggers/azure-functions/python/echo/.funcignore diff --git a/community/boilerplates/event-triggers/azure-functions/python/.gitignore b/community/boilerplates/event-triggers/azure-functions/python/echo/.gitignore similarity index 100% rename from community/boilerplates/event-triggers/azure-functions/python/.gitignore rename to community/boilerplates/event-triggers/azure-functions/python/echo/.gitignore diff --git a/community/boilerplates/event-triggers/azure-functions/python/echo/EchoHttpTrigger/__init__.py b/community/boilerplates/event-triggers/azure-functions/python/echo/EchoHttpTrigger/__init__.py new file mode 100644 index 0000000000000..a4428f95a3d24 --- /dev/null +++ b/community/boilerplates/event-triggers/azure-functions/python/echo/EchoHttpTrigger/__init__.py @@ -0,0 +1,11 @@ +import logging + +import azure.functions as func + +def main(req: func.HttpRequest) -> func.HttpResponse: + logging.info('Python HTTP trigger function processed a request.') + + if(req.body!=NULL): + return func.HttpResponse(req.body) + else: + return func.HttpResponse(status_code=400, body="No request body") \ No newline at end of file diff --git a/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/function.json b/community/boilerplates/event-triggers/azure-functions/python/echo/EchoHttpTrigger/function.json similarity index 100% rename from community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/function.json rename to community/boilerplates/event-triggers/azure-functions/python/echo/EchoHttpTrigger/function.json diff --git a/community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/sample.dat b/community/boilerplates/event-triggers/azure-functions/python/echo/EchoHttpTrigger/sample.dat similarity index 100% rename from community/boilerplates/event-triggers/azure-functions/python/EchoHttpTrigger/sample.dat rename to community/boilerplates/event-triggers/azure-functions/python/echo/EchoHttpTrigger/sample.dat diff --git a/community/boilerplates/event-triggers/azure-functions/python/host.json b/community/boilerplates/event-triggers/azure-functions/python/echo/host.json similarity index 100% rename from community/boilerplates/event-triggers/azure-functions/python/host.json rename to community/boilerplates/event-triggers/azure-functions/python/echo/host.json diff --git a/community/boilerplates/event-triggers/azure-functions/python/requirements.txt b/community/boilerplates/event-triggers/azure-functions/python/echo/requirements.txt similarity index 100% rename from community/boilerplates/event-triggers/azure-functions/python/requirements.txt rename to community/boilerplates/event-triggers/azure-functions/python/echo/requirements.txt From 06ea55d6c6b907a23a5ef14bb57393bfc81739ec Mon Sep 17 00:00:00 2001 From: Abhijeet Chatterjee Date: Wed, 19 Oct 2022 20:12:52 +0530 Subject: [PATCH 3/3] Added README.md for setup and deployment --- .../azure-functions/python/echo/README.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 community/boilerplates/event-triggers/azure-functions/python/echo/README.md diff --git a/community/boilerplates/event-triggers/azure-functions/python/echo/README.md b/community/boilerplates/event-triggers/azure-functions/python/echo/README.md new file mode 100644 index 0000000000000..3a67d76afc560 --- /dev/null +++ b/community/boilerplates/event-triggers/azure-functions/python/echo/README.md @@ -0,0 +1,35 @@ +# Setup tables +1. Create table: + +``` +notes: + id: int + note: text +``` + +# Setup Cloud Function +1. Run the following commands to deploy: +```bash +az group create --name 'my-functions-group' --location southindia + +az storage account create --name 'myfunctionsstorage' --location southindia --resource-group 'my-functions-group' --sku Standard_LRS + +az functionapp create --name 'myfunctionsapp' --storage-account 'myfunctionsstorage' --resource-group 'my-functions-group' --consumption-plan-location southindia + +func azure login +func azure subscriptions set 'Free Trial' +func azure functionapp publish 'myfunctionsapp' +``` +2. Set Environment variables `ADMIN_SECRET` and `HGE_ENDPOINT` +3. Add a X-Function-Key header if Authorization level is enabled + +# Running locally +`func host start` + +# Check Logs +`func azure functionapp logstream 'myfunctionsapp'` + +# Add the trigger in Hasura GraphQL +1. In events tab, add a trigger +2. Select all insert, update, delete operations for the trigger. +3. Paste your function URL as the webhook.