template.json

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "prefix": {
            "defaultValue": "nimbus",
            "type": "String"
        },
        "location": {
            "type": "string",
            "defaultValue": "eastus2"
        },
        "recipients": {
            "type": "string",
            "defaultValue": ""
        },
        "nimbus-credentials-user": {
            "type": "string",
            "defaultValue": ""
        },
        "nimbus-credentials-password": {
            "type": "securestring",
            "defaultValue": ""
        },
        "nimbus-environment": {
            "type": "string",
            "defaultValue": "'placeholder'"
        }
    },
    "resources": [
        {
            "type": "Microsoft.Web/connections",
            "apiVersion": "2016-06-01",
            "name": "[concat(parameters('prefix'), '-automation')]",
            "location": "[parameters('location')]",
            "tags": {
                "app": "Nimbus",
                "purpose": "Approval Automation"
            },
            "properties": {
                "displayName": "runbook-trigger",
                "api": {
                    "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('location'), '/managedAPis/azureautomation')]"
                }
            }
        },
        {
            "type": "Microsoft.Web/connections",
            "apiVersion": "2016-06-01",
            "name": "[concat(parameters('prefix'), '-office365')]",
            "location": "[parameters('location')]",
            "tags": {
                "app": "Nimbus",
                "purpose": "Approval Automation"
            },
            "properties": {
                "displayName": "email-service-account",
                "api": {
                    "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('location'), '/managedAPis/office365')]"
                }
            }
        },
        {
            "type": "Microsoft.Logic/workflows",
            "apiVersion": "2017-07-01",
            "name": "[concat(parameters('prefix'), '-logic-app')]",
            "location": "[parameters('location')]",
            "tags": {
                "app": "Nimbus",
                "purpose": "Approval Automation"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Web/connections', concat(parameters('prefix'), '-automation'))]",
                "[resourceId('Microsoft.Web/connections', concat(parameters('prefix'), '-office365'))]"
            ],
            "properties": {
                "state": "Enabled",
                "definition": {
                    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {
                        "$connections": {
                            "defaultValue": {},
                            "type": "Object"
                        }
                    },
                    "triggers": {
                        "manual": {
                            "type": "Request",
                            "kind": "Http",
                            "inputs": {
                                "schema": {
                                    "properties": {
                                        "applicationId": {
                                            "type": "string"
                                        },
                                        "botId": {
                                            "type": "string"
                                        },
                                        "currentTeamServiceDetails": {
                                            "properties": {
                                                "name": {
                                                    "type": "string"
                                                },
                                                "pstnEnabled": {
                                                    "type": "boolean"
                                                },
                                                "pstnNumber": {
                                                    "type": "string"
                                                },
                                                "upn": {
                                                    "type": "string"
                                                }
                                            },
                                            "type": "object"
                                        },
                                        "failed": {
                                            "type": "boolean"
                                        },
                                        "isSuspended": {
                                            "type": "boolean"
                                        },
                                        "teamId": {
                                            "type": "string"
                                        },
                                        "teamName": {
                                            "type": "string"
                                        },
                                        "teamServiceDetails": {
                                            "properties": {
                                                "name": {
                                                    "type": "string"
                                                },
                                                "pstnEnabled": {
                                                    "type": "boolean"
                                                },
                                                "pstnNumber": {
                                                    "type": "string"
                                                },
                                                "upn": {
                                                    "type": "string"
                                                }
                                            },
                                            "type": "object"
                                        }
                                    },
                                    "type": "object"
                                }
                            }
                        }
                    },
                    "actions": {
                        "Condition": {
                            "actions": {
                                "Send_an_email_(V2)": {
                                    "inputs": {
                                        "body": {
                                            "Body": "<p>Dear Approver,<br>\n<br>\nThe changes you have tried to approve are outdated and cannot be applied for the team: <strong>\"</strong><strong>@{triggerBody()?['teamName']}</strong><strong>\"</strong>.<br>\nPlease check your inbox for most recent approval requests.<br>\n<br>\nThank you,<br>\n<strong>Nimbus team</strong><br>\n</p>",
                                            "Subject": "Approved changes are outdated: @{triggerBody()?['teamName']}",
                                            "To": "[parameters('recipients')]"
                                        },
                                        "host": {
                                            "connection": {
                                                "name": "@parameters('$connections')['office365']['connectionId']"
                                            }
                                        },
                                        "method": "post",
                                        "path": "/v2/Mail"
                                    },
                                    "runAfter": {},
                                    "type": "ApiConnection"
                                }
                            },
                            "else": {
                                "actions": {
                                    "Condition_2": {
                                        "actions": {
                                            "Create_job": {
                                                "inputs": {
                                                    "body": {
                                                        "properties": {
                                                            "parameters": {
                                                                "webhookData": "@triggerBody()"
                                                            }
                                                        }
                                                    },
                                                    "host": {
                                                        "connection": {
                                                            "name": "@parameters('$connections')['azureautomation']['connectionId']"
                                                        }
                                                    },
                                                    "method": "put",
                                                    "path": "[concat(resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation')), '/jobs')]",
                                                    "queries": {
                                                        "runbookName": "nimbus-provisioning",
                                                        "wait": false,
                                                        "x-ms-api-version": "2015-10-31"
                                                    }
                                                },
                                                "runAfter": {},
                                                "type": "ApiConnection"
                                            }
                                        },
                                        "expression": {
                                            "and": [
                                                {
                                                    "equals": [
                                                        "@body('Send_approval_email')?['SelectedOption']",
                                                        "Approve"
                                                    ]
                                                }
                                            ]
                                        },
                                        "runAfter": {
                                            "Send_approval_email": [
                                                "Succeeded"
                                            ]
                                        },
                                        "type": "If"
                                    },
                                    "Send_approval_email": {
                                        "inputs": {
                                            "body": {
                                                "Message": {
                                                    "Body": "Dear Approver,</br></br>\n\nThe team <b>\" @{triggerBody()?['teamName']}\"</b> has been updated recently. Could you please approve the following pending changes?</br></br>\n\n<table style=\"width: 532px; text-align: left;\">\n<tbody>\n<tr>\n<td style=\"width: 133px;\"></td>\n<td style=\"width: 399px;\"><b>Current Value</b></td>\n<td style=\"width: 399px;\"><b>Pending Value</b></td>\n</tr>\n<tr>\n<td style=\"width: 133px;\"><b>Service Name</b></td>\n<td style=\"width: 399px;\">@{triggerBody()?['currentTeamServiceDetails']?['name']}</td>\n<td style=\"width: 399px;\">@{if(empty(triggerBody()?['teamServiceDetails']?['name']), 'no changes', triggerBody()?['teamServiceDetails']?['name'])}</td>\n</tr>\n<tr>\n<td style=\"width: 133px;\"><b>Service UPN</b></td>\n<td style=\"width: 399px;\">@{triggerBody()?['currentTeamServiceDetails']?['upn']}</td>\n<td style=\"width: 399px;\">@{if(empty(triggerBody()?['teamServiceDetails']?['upn']), 'no changes', triggerBody()?['teamServiceDetails']?['upn'])}</td>\n</tr>\n<tr>\n<td style=\"width: 133px;\"><b>PSTN Enabled</b></td><td style=\"width: 399px;\">@{triggerBody()?['currentTeamServiceDetails']?['pstnEnabled']}</td>\n<td style=\"width: 399px;\">@{if(empty(string(triggerBody()?['teamServiceDetails']?['pstnEnabled'])), 'no changes', triggerBody()?['teamServiceDetails']?['pstnEnabled'])}</td>\n</tr>\n<tr>\n<td style=\"width: 133px;\"><b>E.164 Number</b></td>\n<td style=\"width: 399px;\">@{triggerBody()?['currentTeamServiceDetails']?['pstnNumber']}</td>\n<td style=\"width: 399px;\">@{if(empty(triggerBody()?['teamServiceDetails']?['pstnNumber']), 'no changes', triggerBody()?['teamServiceDetails']?['pstnNumber'])}</td>\n</tr>\n<tr>\n<td style=\"width: 133px;\"><b>Delete Team</b></td>\n<td style=\"width: 399px;\">False</td>\n<td style=\"width: 399px;\">@{if(triggerBody()?['isSuspended'], 'True', 'no changes')}</td>\n</tr>\n</tbody>\n</table>\n<br><br>\nThank you,</br>\nNimbus team</br>\n",
                                                    "HideHTMLMessage": false,
                                                    "UseOnlyHTMLMessage": true,
                                                    "Importance": "Normal",
                                                    "Options": "Approve, Reject",
                                                    "Subject": "Pending changes approval request: @{triggerBody()?['teamName']}",
                                                    "To": "[parameters('recipients')]"
                                                },
                                                "NotificationUrl": "@{listCallbackUrl()}"
                                            },
                                            "host": {
                                                "connection": {
                                                    "name": "@parameters('$connections')['office365']['connectionId']"
                                                }
                                            },
                                            "path": "/approvalmail/$subscriptions"
                                        },
                                        "runAfter": {},
                                        "type": "ApiConnectionWebhook"
                                    }
                                }
                            },
                            "expression": {
                                "and": [
                                    {
                                        "equals": [
                                            "@equals(triggerBody()?['failed'], true)",
                                            "@true"
                                        ]
                                    }
                                ]
                            },
                            "runAfter": {},
                            "type": "If"
                        }
                    },
                    "outputs": {}
                },
                "parameters": {
                    "$connections": {
                        "value": {
                            "azureautomation": {
                                "connectionId": "[resourceId('Microsoft.Web/connections', concat(parameters('prefix'), '-automation'))]",
                                "id": "[concat(subscription().Id, '/providers/Microsoft.Web/locations/', parameters('location'),'/managedApis/azureautomation')]"
                            },
                            "office365": {
                                "connectionId": "[resourceId('Microsoft.Web/connections', concat(parameters('prefix'), '-office365'))]",
                                "id": "[concat(subscription().Id, '/providers/Microsoft.Web/locations/', parameters('location'),'/managedApis/office365')]"
                            }
                        }
                    }
                }
            }
        },
        {
            "name": "[concat(parameters('prefix'), '-automation')]",
            "type": "Microsoft.Automation/automationAccounts",
            "apiVersion": "2015-10-31",
            "location": "[parameters('location')]",
            "tags": {
                "app": "Nimbus",
                "purpose": "Approval Automation"
            },
            "properties": {
                "sku": {
                    "name": "Basic"
                }
            },
            "resources": [
                {
                    "name": "nimbusEnvironment",
                    "type": "variables",
                    "apiVersion": "2015-10-31",
                    "dependsOn": [
                        "[resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation'))]"
                    ],
                    "properties": {
                        "description": "Nimbus variable used to select the environment to update in the runbook",
                        "value": "[parameters('nimbus-environment')]"
                    }
                },
                {
                    "name": "nimbusCredentials",
                    "type": "credentials",
                    "apiVersion": "2015-10-31",
                    "dependsOn": [
                        "[resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation'))]"
                    ],
                    "properties": {
                        "userName": "[parameters('nimbus-credentials-user')]",
                        "password": "[parameters('nimbus-credentials-password')]",
                        "description": "Credentials used to update nimbus configuration in tenant"
                    }
                },
                {
                    "name": "callBackUrl",
                    "type": "variables",
                    "apiVersion": "2015-10-31",
                    "dependsOn": [
                        "[resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation'))]",
                        "[resourceId('Microsoft.Logic/workflows', concat(parameters('prefix'), '-logic-app'))]"
                    ],
                    "properties": {
                        "value": "[concat('''', base64(listCallbackURL(resourceId('Microsoft.Logic/workflows/triggers', concat(parameters('prefix'), '-logic-app'), 'manual'), '2016-06-01').value), '''')]",
                        "description": "Logic App call-back url to signal failure"
                    }
                },
                {
                    "apiVersion": "2015-10-31",
                    "type": "runbooks",
                    "name": "nimbus-provisioning",
                    "location": "[parameters('location')]",
                    "tags": {
                        "app": "Nimbus",
                        "purpose": "Approval Automation"
                    },
                    "dependsOn": [
                        "[resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation'))]"
                    ],
                    "properties": {
                        "runbookType": "PowerShell",
                        "logProgress": false,
                        "logVerbose": true,
                        "description": "Powershell runbook to update Nimbus configuration",
                        "publishContentLink": {
                            "uri": "https://nimbusterraformcitest.blob.core.windows.net/powershell-modules/runbook-module.ps1",
                            "version": "1.0.0.0"
                        }
                    }
                },
                {
                    "apiVersion": "2015-10-31",
                    "type": "modules",
                    "name": "nimbus",
                    "location": "[parameters('location')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation'))]",
                        "[resourceId('Microsoft.Automation/automationAccounts/modules', concat(parameters('prefix'), '-automation'), 'SkypeOnlineConnector')]",
                        "[resourceId('Microsoft.Automation/automationAccounts/modules', concat(parameters('prefix'), '-automation'), 'Az.Accounts')]",
                        "[resourceId('Microsoft.Automation/automationAccounts/modules', concat(parameters('prefix'), '-automation'), 'Az.Resources')]",
                        "[resourceId('Microsoft.Automation/automationAccounts/modules', concat(parameters('prefix'), '-automation'), 'MSOnline')]",
                        "[resourceId('Microsoft.Automation/automationAccounts/modules', concat(parameters('prefix'), '-automation'), 'AzureAD')]"
                    ],
                    "properties": {
                        "contentLink": {
                            "uri": "https://nimbusterraformcitest.blob.core.windows.net/powershell-modules/nimbus.zip"
                        }
                    }
                },
                {
                    "apiVersion": "2015-10-31",
                    "type": "modules",
                    "name": "SkypeOnlineConnector",
                    "location": "[parameters('location')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation'))]"
                    ],
                    "properties": {
                        "contentLink": {
                            "uri": "https://nimbusterraformcitest.blob.core.windows.net/powershell-modules/SkypeOnlineConnector.zip"
                        }
                    }
                },
                {
                    "apiVersion": "2015-10-31",
                    "type": "modules",
                    "name": "Az.Accounts",
                    "location": "[parameters('location')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation'))]"
                    ],
                    "properties": {
                        "contentLink": {
                            "uri": "https://psg-prod-eastus.azureedge.net/packages/az.accounts.1.9.4.nupkg"
                        }
                    }
                },
                {
                    "apiVersion": "2015-10-31",
                    "type": "modules",
                    "name": "Az.Resources",
                    "location": "[parameters('location')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation'))]",
                        "[resourceId('Microsoft.Automation/automationAccounts/modules', concat(parameters('prefix'), '-automation'), 'Az.Accounts')]"
                    ],
                    "properties": {
                        "contentLink": {
                            "uri": "https://psg-prod-eastus.azureedge.net/packages/az.resources.2.5.1.nupkg"
                        }
                    }
                },
                {
                    "apiVersion": "2015-10-31",
                    "type": "modules",
                    "name": "MSOnline",
                    "location": "[parameters('location')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation'))]"
                    ],
                    "properties": {
                        "contentLink": {
                            "uri": "https://psg-prod-eastus.azureedge.net/packages/msonline.1.1.183.57.nupkg"
                        }
                    }
                },
                {
                    "apiVersion": "2015-10-31",
                    "type": "modules",
                    "name": "AzureAD",
                    "location": "[parameters('location')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Automation/automationAccounts', concat(parameters('prefix'), '-automation'))]"
                    ],
                    "properties": {
                        "contentLink": {
                            "uri": "https://psg-prod-eastus.azureedge.net/packages/azuread.2.0.2.116.nupkg"
                        }
                    }
                }
            ]
        }
    ],
    "outputs": {
        "callbackUrl": {
            "type": "string",
            "value": "[base64(listCallbackURL(resourceId('Microsoft.Logic/workflows/triggers', concat(parameters('prefix'), '-logic-app'), 'manual'), '2016-06-01').value)]"
        }
    }
}