Which Logic App Connector uses which On Premise Data Gateway
  • Updated on 13 May 2020
  • 3 minutes to read
  • Contributors
  • Print
  • Comments
  • Share
  • Dark
    Light

Which Logic App Connector uses which On Premise Data Gateway

  • Print
  • Comments
  • Share
  • Dark
    Light

One of the challenges as your integration platform evolves with Microsoft Azure Integration is that you will potentially be using Logic Apps, API Connectors and On-Premise Data Gateways. In this scenario it is difficult to get a holistic view of which connector is using which gateway. In our case we had dev/test/prod environments and over time due to misconfigurations a couple of connectors were using the wrong gateway and we also wanted to replace our gateways with new ones and we needed to workout who used which gateway.

On an individual logic app basis you can see which gateway is used when you open the logic app action or trigger and look at the connection it is pointing to but the API connectors dont show you this and you cant get a view across all of your connectors.

I wrote a script which is below that will allow you to specify a list of resource groups and it will look for all gateways and connectors in the resource groups and workout who is using which connector. It will also:

  • let you list the gateways you want to use and if any connector is using a different gateway it will log an error
  • If you have a connector pointing to a gateway in a different resource group it will log an error (note this may not be relevant to your usecase

Hopefully this will help you to keep things tidy

#Name: Check which Connector uses which Data Gateway
#Description:
#This script will look which data gateways are in your resource groups and then look at all of the API connections to workout which connector uses which gateway
#=============================================

#Use the below commands to connect to Azure
#az login
#az account set --subscription $subscriptioname
cls

#This is a temp file to compare the json to see who uses what and let us troubleshoot each connector
$tempFilePath = 'C:\Temp\ConnectorFile.txt'

#These are the resource groups we will look in to find connectors and gateways
$allowedResourceGroups = New-Object "System.Collections.Generic.List[System.String]" 
$allowedResourceGroups.Add('MY RG1')
$allowedResourceGroups.Add('MY RG2')


#These are the gateways we are allowed to use, if any others are used they will flag as an error
$allowedGateways = New-Object "System.Collections.Generic.List[System.String]" 
$allowedGateways.Add('OPDG-DEV')
$allowedGateways.Add('OPDG-TEST')
$allowedGateways.Add('OPDG-PROD')


$infoMessages = New-Object "System.Collections.Generic.List[System.String]"
$errorMessages = New-Object "System.Collections.Generic.List[System.String]"
$gateWayDictionary = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.Object]" 

$resourceGroups = Get-AzResourceGroup

Write-Host 'Checking for Data Gateways'
Write-Host '=========================='

foreach($resourceGroup in $resourceGroups)
{
    if($allowedResourceGroups.Contains($resourceGroup.ResourceGroupName))
    {
        $resourceGroupPath = $resourceGroup.ResourceId
    
        $dataGatewayResources = Get-AzResource -ResourceGroupName $resourceGroup.ResourceGroupName -ResourceType 'Microsoft.Web/connectionGateways'
        foreach($dataGatewayResource in $dataGatewayResources)
        {
            Write-Host 'Gateway Id =' $dataGatewayResource.Id

            $gateWayDictionary.Add($dataGatewayResource.Id.ToLower(), $dataGatewayResource) 
        }
    }    
}
Write-Host ''


Write-Host 'Checking which connector uses which gateway'
Write-Host '==========================================='

foreach($resourceGroup in $resourceGroups)
{
    if($allowedResourceGroups.Contains($resourceGroup.ResourceGroupName))
    {
        $connectionResources = Get-AzResource -ResourceGroupName $resourceGroup.ResourceGroupName -ResourceType 'Microsoft.Web/connections'
        foreach($connectionResource in $connectionResources)
        {     
            $connectorUrl = $connectionResource.Id + '?api-version=2016-06-01'
    
            #Get Logic App Content
            $connectorJson = az rest --method get --uri $connectorUrl                          
            $connectorJsonText = $connectorJson | ConvertTo-Json -Compress | Out-File $tempFilePath         
        
            $content = Get-Content $tempFilePath

            foreach($dictionaryKey in $gateWayDictionary.Keys)
            {                
                $dataGatewayId = $dictionaryKey
                $dataGatewayResource = $gateWayDictionary.Item($dictionaryKey)

                #If the content of the connector json contains the gateway then we want to inspect it
                if($content.ToLower().Contains($dataGatewayId))
                {

                    $message =  $dataGatewayResource.Name + ':' + $dataGatewayResource.ResourceGroupName + ' - is used by - ' + $connectionResource.ResourceGroupName + ':' + $connectionResource.Name
                    $infoMessages.Add($message)

                    #If the connector is in a different resource group to the gateway then we want to record an error
                    if($dataGatewayResource.ResourceGroupName.ToLower() -ne $connectionResource.ResourceGroupName.ToLower())
                    {
                        $wrongRGError = 'WRONG RESOURCE GROUP: ' + $dataGatewayResource.ResourceGroupName + ':' + $dataGatewayResource.Name + ' - is used by - ' + $connectionResource.ResourceGroupName + ':' + $connectionResource.Name
                        $errorMessages.Add($wrongRGError)
                    }

                    $gatewayAllowed = $allowedGateways.Contains($dataGatewayResource.Name.ToLower())
                    #If the connector is in a different resource group to the gateway then we want to record an error
                    if($gatewayAllowed -eq $false)
                    {
                        $disallowedGateWayError = 'DISALLOWED GATEWAY: ' + $dataGatewayResource.ResourceGroupName + ':' + $dataGatewayResource.Name + ' - is  to be used by - ' + $connectionResource.ResourceGroupName + ':' + $connectionResource.Name
                        $errorMessages.Add($disallowedGateWayError)
                    }


                    Write-Host $connectionResource.Name ' in resource group ' $connectionResource.ResourceGroupName ' uses data gateway ' $dataGatewayResource.Name ' from resource group ' $dataGatewayResource.ResourceGroupName
                }
            }    
        }
    }
}

Write-Host 'Errors'
Write-Host '======'
foreach($message in $errorMessages)
{
    Write-Host $message
}
Write-Host ''


Write-Host 'Data Gateway Usage'
Write-Host '=================='
foreach($message in $infoMessages)
{
    Write-Host $message
}
Write-Host ''

Was this article helpful?