Search code examples
pythonazureazure-functionsetlazure-logic-apps

How to create a Multi purpose Logic app in Azure which serves all email activity?


I am writing a ETL script in Python Azure function where I want to send emails about the job status and process trackability to different owners. I am trying to create a logic app which is more dynamic and serves multipurpose requirements as mentioned below:

Situation Subject Email_to Body Attachment
When Python code throws error Code Error abc@outlook.com, def@outlook.com Error code Info No
When code ran successfully Etl Success ghy@outlook.com, def@outlook.com Etl process successful No
When we write qc_file.csv to blob storage QC Sheet uwj@outlook.com, jsh@outlook.com please find the Qc_Sheet qc_file.csv

I think I need to create the logic app with switch case since I am very new to logic app, I am confused to do so.


Solution

  • To achieve your requirement, you can create 2 logic apps. One to achieve first 2 situations and the other logic app for the last situation.

    Logic App 1:

    In order to achieve the error situation, you can use run after for mail step. Below is the flow of my logic app.

    enter image description here

    Below are the settings in my run after.

    enter image description here

    Code view of Logic App1

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "HttpTrigger1": {
                    "inputs": {
                        "body": {
                            "name": ""
                        },
                        "function": {
                            "id": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Web/sites/fa75518485/functions/HttpTrigger1"
                        }
                    },
                    "runAfter": {},
                    "type": "Function"
                },
                "Send_an_email_(V2)": {
                    "inputs": {
                        "body": {
                            "Body": "<p>Error code Info</p>",
                            "Importance": "Normal",
                            "Subject": "Code Error",
                            "To": "abc@outlook.com; def@outlook.com"
                        },
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['office365']['connectionId']"
                            }
                        },
                        "method": "post",
                        "path": "/v2/Mail"
                    },
                    "runAfter": {
                        "HttpTrigger1": [
                            "FAILED"
                        ]
                    },
                    "type": "ApiConnection"
                },
                "Send_an_email_(V2)_2": {
                    "inputs": {
                        "body": {
                            "Body": "<p>Etl process successful</p>",
                            "Importance": "Normal",
                            "Subject": "Etl Success",
                            "To": "ghy@outlook.com; def@outlook.com"
                        },
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['office365']['connectionId']"
                            }
                        },
                        "method": "post",
                        "path": "/v2/Mail"
                    },
                    "runAfter": {
                        "HttpTrigger1": [
                            "Succeeded"
                        ]
                    },
                    "type": "ApiConnection"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "manual": {
                    "inputs": {
                        "schema": {}
                    },
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {
                    "office365": {
                        "connectionId": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Web/connections/office365",
                        "connectionName": "office365",
                        "id": "/subscriptions/xxx/providers/Microsoft.Web/locations/eastus/managedApis/office365"
                    }
                }
            }
        }
    }
    

    Logic App 2:

    As soon as the file gets uploaded to storage account, the mail will get triggered. Below is the flow of my logic app.

    enter image description here

    Code view of Logic App2

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Get_blob_content_(V2)": {
                    "inputs": {
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['azureblob']['connectionId']"
                            }
                        },
                        "method": "get",
                        "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files/@{encodeURIComponent(encodeURIComponent(triggerBody()?['Path']))}/content",
                        "queries": {
                            "inferContentType": true
                        }
                    },
                    "runAfter": {},
                    "type": "ApiConnection"
                },
                "Send_an_email_(V2)_2": {
                    "inputs": {
                        "body": {
                            "Attachments": [
                                {
                                    "ContentBytes": "@{base64(body('Get_blob_content_(V2)'))}",
                                    "Name": "@triggerBody()?['Name']"
                                }
                            ],
                            "Body": "<p>please find the Qc_Sheet</p>",
                            "Importance": "Normal",
                            "Subject": "QC Sheet",
                            "To": "uwj@outlook.com; jsh@outlook.com"
                        },
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['office365']['connectionId']"
                            }
                        },
                        "method": "post",
                        "path": "/v2/Mail"
                    },
                    "runAfter": {
                        "Get_blob_content_(V2)": [
                            "Succeeded"
                        ]
                    },
                    "type": "ApiConnection"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "When_a_blob_is_added_or_modified_(properties_only)_(V2)": {
                    "inputs": {
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['azureblob']['connectionId']"
                            }
                        },
                        "method": "get",
                        "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/triggers/batch/onupdatedfile",
                        "queries": {
                            "checkBothCreatedAndModifiedDateTime": false,
                            "folderId": "JTJmbXljb250YWluZXI="
                        }
                    },
                    "metadata": {
                        "JTJmbXljb250YWluZXI=": "/mycontainer"
                    },
                    "recurrence": {
                        "frequency": "Minute",
                        "interval": 3
                    },
                    "splitOn": "@triggerBody()",
                    "type": "ApiConnection"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {
                    "azureblob": {
                        "connectionId": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Web/connections/azureblob",
                        "connectionName": "azureblob",
                        "id": "/subscriptions/xxx/providers/Microsoft.Web/locations/eastus/managedApis/azureblob"
                    },
                    "office365": {
                        "connectionId": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Web/connections/office365",
                        "connectionName": "office365",
                        "id": "/subscriptions/xxx/providers/Microsoft.Web/locations/eastus/managedApis/office365"
                    }
                }
            }
        }
    }