Office365 connection in Logic App

Using Powershell to Deploy a Logic App using an Office365 API Connection

Office365 connection in Logic App

Using Powershell to Deploy a Logic App using an Office365 API Connection

When using an Azure agent Powershell was available, so the deployment was straight forward:
  1. Validate then build the Office365 Connection
  2. "resources": [
        {
          "type": "Microsoft.Web/connections",
          "apiVersion": "2016-06-01",
          "name": "[parameters('name')]",
          "location": "[parameters('locationName')]",
          "tags": "[parameters('tags')]",
          "kind": "V2",
          "properties": {
            "api": {
              "id": "[concat('/subscriptions/',subscription().subscriptionId,'/providers/Microsoft.Web/locations/',parameters('locationName'),'/managedApis/office365')]"
            }
          }
        }
      ],
      "outputs": {
        "office365connectionid": {
          "type": "string",
          "value": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',resourceGroup().name,'/providers/',reference(resourceId('Microsoft.Web/connections', parameters('name')),'2018-07-01-preview', 'full').resourceId)]"
        },
        "office365connectionname": {
          "type": "string",
          "value": "[parameters('name')]"
        },
        "office365endpointurl": {
          "type": "string",
          "value": "[reference(resourceId('Microsoft.Web/connections', parameters('name')),'2018-07-01-preview', 'full').properties.connectionRuntimeUrl]"
        }
      }
    steps:
    - task: AzureResourceManagerTemplateDeployment@3
      displayName: 'Validate ARM template'
      inputs:
        azureResourceManagerConnection: 'company-ais-dev'
        subscriptionId: '1xf291x0-0x61-4xx1-9097-777x44x1xxxx'
        resourceGroupName: 'rsg-company-dev'
        location: 'UK South'
        csmFile: src/Company.Azure.AIS.Common/Company.Azure.AIS.Common.Office365/azuredeploy.json
        csmParametersFile: src/Company.Azure.AIS.Common/Company.Azure.AIS.Common.Office365/azuredeploy.parameters.json
        deploymentMode: Validation
    steps:
    - task: PublishBuildArtifacts@1
      displayName: 'Publish Artifact: Company.Azure.AIS.Common.Office365'
      inputs:
        PathtoPublish: src/Company.Azure.AIS.Common/Company.Azure.AIS.Common.Office365
        ArtifactName: Company.Azure.AIS.Common.Office365
  3. And the same for the Logic App container with the following app settings
  4. 
    {
    ...
                {
                  "name": "WORKFLOWS_SUBSCRIPTION_ID",
                  "value": "[parameters('subscriptionId')]"
                },
                {
                  "name": "WORKFLOWS_LOCATION_NAME",
                  "value": "[parameters('locationName')]"
                },
                {
                  "name": "WORKFLOWS_RG_NAME",
                  "value": "[parameters('resourceGroupName')]"
                },
                {
                  "name": "OFFICE365_CONNECTION_ID", 
                  "value": "[parameters('office365connectionid')]"
                },
                {
                  "name": "OFFICE365_CONNECTION_RUNTIMEURL", 
                  "value": "[parameters('office365endpointurl')]"
                },
                {
                  "name": "OFFICE365_CONNECTION_NAME", 
                  "value": "[parameters('office365connectionname')]"
                }
    ...
    }
  5. In the Release Pipeline I used the Office365 artifact and the Logic App container artifact
  6. Release pipeline office365connection
  7. In the Release Pipeline Stage deployment, I do the following steps: Release logicapp pipeline with office365 connection
        Ÿ Retrieve some Key Vault secrets that are required
        Ÿ Deploy the Office365 connection and output the API details
      steps:
      - task: AzureResourceManagerTemplateDeployment@3
        displayName: 'Deploy Office365 Connection'
        inputs:
          azureResourceManagerConnection: 'company-ais-dev'
          subscriptionId: '1xx291x0-0x61-4xx1-9097-777x44x1xxxx'
          resourceGroupName: 'rsg-company-dev'
          location: 'UK South'
          csmFile: '$(System.DefaultWorkingDirectory)/Company.Azure.AIS.Common.Office365/Company.Azure.AIS.Common.Office365/azuredeploy.json'
          csmParametersFile: '$(System.DefaultWorkingDirectory)/Company.Azure.AIS.Common.Office365/Company.Azure.AIS.Common.Office365/azuredeploy.parameters.json'
          overrideParameters: '-environment dev'
          deploymentOutputs: connectionOutputs
        Ÿ Run a Powershell script to save the output variables into pipeline variables to be used in the Logic App container deployment
      steps:
      - powershell: |
         ## connectionOutputs is already a pipeline variable returned via the ARM deployment
         ## task's deploymentOutputs attribute
         $armOutputObj = $env:connectionOutputs | convertfrom-json
         $armOutputObj.PSObject.Properties | ForEach-Object {
             $keyname = $_.Name
             $value = $_.Value.value
         
             ## Creates a standard pipeline variable
             Write-Output "##vso[task.setvariable variable=$keyName;]$value"
         
             ## Creates an output variable
             Write-Output "##vso[task.setvariable variable=$keyName;isOutput=true]$value"
         
             Write-Host "${keyName}:           ${value}"
         }
        displayName: 'Save Connection Outputs'
        Ÿ Finally, use the Office365 API connection outputs to create the connection in the Logic App container
      variables:
        office365connectionid: ''
        office365endpointurl: ''
        office365connectionname: ''
      
      steps:
      - task: AzureResourceManagerTemplateDeployment@3
        displayName: 'Deploy VLE Logic App'
        inputs:
          azureResourceManagerConnection: 'company-ais-dev'
          subscriptionId: '1xx291x0-0x61-4xx1-9097-777x44x1xxxx'
          resourceGroupName: 'rsg-company-dev'
          location: 'UK South'
          csmFile: '$(System.DefaultWorkingDirectory)/Company.Azure.AIS.VLE.LogicApp/Company.Azure.AIS.VLE.LogicApp/azuredeploy.json'
          csmParametersFile: '$(System.DefaultWorkingDirectory)/Company.Azure.AIS.VLE.LogicApp/Company.Azure.AIS.VLE.LogicApp/azuredeploy.parameters.json'
          overrideParameters: '-environment dev -office365connectionid "$(office365connectionid)" -office365endpointurl "$(office365endpointurl)"                  -office365connectionname "$(office365connectionname)"'

The connection in the Logic App will look like this:

{
    "managedApiConnections": {
        "office365": {
            "api": {
                "id": "/subscriptions/@appsetting('WORKFLOWS_SUBSCRIPTION_ID')/providers/Microsoft.Web/locations/@appsetting('WORKFLOWS_LOCATION_NAME')/managedApis/office365"
            },
            "authentication": {
                "type": "ManagedServiceIdentity"
            },
            "connection": {
                "id": "/subscriptions/@appsetting('WORKFLOWS_SUBSCRIPTION_ID')/resourceGroups/@appsetting('WORKFLOWS_RG_NAME')/providers/Microsoft.Web/connections/@appsetting('OFFICE365_CONNECTION_NAME')"
            },
            "connectionRuntimeUrl": "@appsetting('OFFICE365_CONNECTION_RUNTIMEURL')"
        }
    }
}

So the connection is built up from the app settings, that were populated from the deployment of the actual Office365 Connection.

Therefore, the workflow actions will only need to reference the connection:

...
   "connection": {
   "referenceName": "office365"
   }
...

Leave a Reply

Your email address will not be published. Required fields are marked *