Makina74
Makina74

Reputation: 45

Logic App with azure monitor and conditions

I create a workflow with logicAPP. The goal is to notify a team when patch is missing for VM. I use azure monitor in the logic app to set the query. I decided to put after the Azure Monitor , a condition to know if the query table is empty or have data. if the table is empty, the logix is true , so it does'nt send notification, and when its false , it sends notification.

When I run , I got a logic errors. Normally , the table has not data but after condition , the function empty([my_table]) returns false and sends me notification with the result ("The query yielded no data")

what is the problem ??

Thanks

enter image description here

Upvotes: 1

Views: 785

Answers (1)

VenkateshDodda
VenkateshDodda

Reputation: 5496

Based on the above shared requirement we have created the logic app & tested it our local environment , it is working fine.

Below is the complete logic code :

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Condition_2": {
                "actions": {
                    "Terminate_2": {
                        "inputs": {
                            "runStatus": "Cancelled"
                        },
                        "runAfter": {},
                        "type": "Terminate"
                    }
                },
                "else": {
                    "actions": {
                        "Send_an_email_(V2)_2": {
                            "inputs": {
                                "body": {
                                    "Body": "<p>@{base64ToString(body('Run_query_and_visualize_results')?['body'])}</p>",
                                    "Subject": "list of vm from update management ",
                                    "To": "<UserEmailId>"
                                },
                                "host": {
                                    "connection": {
                                        "name": "@parameters('$connections')['office365']['connectionId']"
                                    }
                                },
                                "method": "post",
                                "path": "/v2/Mail"
                            },
                            "runAfter": {},
                            "type": "ApiConnection"
                        }
                    }
                },
                "expression": {
                    "and": [
                        {
                            "equals": [
                                "@length(body('Run_query_and_visualize_results')?['body'])",
                                0
                            ]
                        }
                    ]
                },
                "runAfter": {
                    "Run_query_and_visualize_results": [
                        "Succeeded"
                    ]
                },
                "type": "If"
            },
            "Run_query_and_visualize_results": {
                "inputs": {
                    "body": "Update\n| where Classification == 'Security Updates' or Classification  == 'Critical Updates'\n| where UpdateState == 'Needed'\n| summarize by Computer,ResourceGroup,Classification,UpdateState\n|sort by Computer",
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['azuremonitorlogs']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/visualizeQuery",
                    "queries": {
                        "resourcegroups": "<Resource_group_Name",
                        "resourcename": "<log analytics workspacename",
                        "resourcetype": "Log Analytics Workspace",
                        "subscriptions": "<subcription_id>",
                        "timerange": "Last 12 hours",
                        "visType": "Html Table"
                    }
                },
                "runAfter": {},
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "Recurrence": {
                "evaluatedRecurrence": {
                    "frequency": "Hour",
                    "interval": 3
                },
                "recurrence": {
                    "frequency": "Hour",
                    "interval": 3
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "azuremonitorlogs": {
                    "connectionId": "/subscriptions/<subcription-id>/resourceGroups/<resource-group>/providers/Microsoft.Web/connections/azuremonitorlogs",
                    "connectionName": "azuremonitorlogs",
                    "id": "/subscriptions/<subcription-id>/providers/Microsoft.Web/locations/northcentralus/managedApis/azuremonitorlogs"
                },
                "office365": {
                    "connectionId": "/subscriptions/<subcription-id>/resourceGroups/<resource-group>/providers/Microsoft.Web/connections/office365",
                    "connectionName": "office365",
                    "id": "/subscriptions/<subcription-id>/providers/Microsoft.Web/locations/northcentralus/managedApis/office365"
                }
            }
        }
    }
}

please find the reference output of the above logic sample run :

enter image description here

Upvotes: 1

Related Questions