使用二头肌部署逻辑应用 - 将 JSON 转换为有效的二头肌

时间:2021-07-31 22:37:24

标签: azure azure-logic-apps azure-bicep

我想生成一个用于构建逻辑应用程序的二头肌。这个的样板是

resource logicApp 'Microsoft.Logic/workflows@2019-05-01' = {
  name: 'lapp-${options.suffix}'
  location: options.location
  properties: {
    definition: {
      // here comes the definition
    }
  }
}

我的评论显示了应用程序本身定义的位置。如果我知道从现有的逻辑应用程序中获取 JSON(为简洁起见,我省略了一些内容):

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {},
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {

                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

你必须把它转换成这样:

{
    definition: {
        '$schema': "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#"
        actions: {}
        contentVersion: '1.0.0.0'
        outputs: {}
        parameters: {}
        triggers: {
            'manual': {
                inputs: {

                }
                kind: 'Http'
                type: 'Request'
            }
        }
    }
    parameters: {}
}

这意味着例如:

  • 删除尾随逗号
  • 删除属性上的引号
  • 单引号某些属性,例如 schema 或自定义操作名称
  • 当单引号出现在逻辑应用中常见的值中时,将它们转义

是否有任何转换器可以将 JSON 结构转换为有效的二头肌?我的意思不是 bicep decompile,因为这里假设您已经拥有一个有效的 ARM 模板。

1 个答案:

答案 0 :(得分:1)

一种方法是将您的定义保存在一个单独的文件中,并将 json 作为参数传递。

main.bicep:

// Parameters
param location string = resourceGroup().location
param logicAppName string
param logicAppDefinition object

// Basic logic app
resource logicApp 'Microsoft.Logic/workflows@2019-05-01' = {
  name: logicAppName
  location: location
  properties: {
    state: 'Enabled'
    definition: logicAppDefinition.definition
    parameters: logicAppDefinition.parameters
  }
}

然后你可以像这样部署你的模板(在这里使用 az cli 和 powershell):

$definitionPath="full/path/of/the/logic/app/definition.json"
az deployment group create `
  --resource-group "resource group name" `
  --template-file "full/path/of/the/main.bicep" `
  --parameters logicAppName="logic app name" `
  --parameters logicAppDefinition=@$definitionPath
  

使用这种方法,您不必每次更新逻辑应用时都修改“基础设施即代码”。

相关问题