使用 Bicep 将 UserDefinedFunction 与 Azure Cosmos DB 一起部署

Deployment of UserDefinedFunction together with Azure Cosmos DB using Bicep

是否可以使用 bicep 将存储过程或用户定义函数与 Azure CosmosDB SQL API 一起部署?

我知道可以直接使用 arm 模板: https://github.com/Azure/azure-quickstart-templates/blob/master/quickstarts/microsoft.documentdb/cosmosdb-sql-container-sprocs/azuredeploy.json

我还没有找到任何关于在二头肌上做这个的文档,但我试着自己做。 我尝试以与任何其他方式相同的方式构建二头肌资源,例如容器,但我总是有例外,例如:

Deployment failed. Correlation ID: 736b1c6e-fec7-479c-88e5-5a8034eac762. {
  "status": "Failed",
  "error": {
    "code": "ResourceDeploymentFailure",
    "message": "The resource operation completed with terminal provisioning state 'Failed'.",
    "details": [
      {
        "code": "DeploymentFailed",
        "message": "At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.",
        "details": [
          {
            "code": "Conflict",
            "message": "{\r\n  \"status\": \"Failed\",\r\n  \"error\": {\r\n    \"code\": \"ResourceDeploymentFailure\",\r\n    \"message\": \"The resource operation completed with terminal provisioning state 'Failed'.\",\r\n    \"details\": [\r\n      {\r\n        \"code\": \"NotFound\",\r\n        \"message\": \"Message: {\\"code\\":\\"NotFound\\",\\"message\\":\\"Message: {\\\\"Errors\\\\":[\\\\"Owner resource does not exist\\\\"]}\\r\\nActivityId: 4c994fea-249f-4fe2-bdc9-7f9759ef0d15, Request URI: /apps/46a2c1a8-e060-40ad-9893-6f6573d9463d/services/232ea27a-4028-42ee-b997-4cfea450f978/partitions/8c2e10e9-37d3-471a-9f99-be392fa75342/replicas/132659688051702465s, RequestStats: \\r\\nRequestStartTime: 2021-05-20T13:11:03.8469017Z, RequestEndTime: 2021-05-20T13:11:03.8469017Z,  Number of regions attempted:1\\r\\nResponseTime: 2021-05-20T13:11:03.8469017Z, StoreResult: StorePhysicalAddress: rntbd://10.0.0.19:11000/apps/46a2c1a8-e060-40ad-9893-6f6573d9463d/services/232ea27a-4028-42ee-b997-4cfea450f978/partitions/8c2e10e9-37d3-471a-9f99-be392fa75342/replicas/132659688051702465s, LSN: 7, GlobalCommittedLsn: 7, PartitionKeyRangeId: , IsValid: True, StatusCode: 404, SubStatusCode: 1003, RequestCharge: 1, ItemLSN: -1, SessionToken: -1#7, UsingLocalLSN: False, TransportException: null, ResourceType: Collection, OperationType: Read\\r\\nResponseTime: 2021-05-20T13:11:03.8469017Z, StoreResult: StorePhysicalAddress: rntbd://10.0.0.20:11300/apps/46a2c1a8-e060-40ad-9893-6f6573d9463d/services/232ea27a-4028-42ee-b997-4cfea450f978/partitions/8c2e10e9-37d3-471a-9f99-be392fa75342/replicas/132659688051702467s, LSN: 7, GlobalCommittedLsn: 7, PartitionKeyRangeId: , IsValid: True, StatusCode: 404, SubStatusCode: 1003, RequestCharge: 1, ItemLSN: -1, SessionToken: -1#7, UsingLocalLSN: False, TransportException: null, ResourceType: Collection, OperationType: Read\\r\\n, SDK: Microsoft.Azure.Documents.Common/2.11.0\\"}, Request URI: /dbs/dbone/colls/collone/udfs, RequestStats: , SDK: Microsoft.Azure.Documents.Common/2.11.0, Microsoft.Azure.Documents.Common/2.11.0, Microsoft.Azure.Documents.Common/2.11.0, Microsoft.Azure.Documents.Common/2.11.0, Microsoft.Azure.Documents.Common/2.11.0\"\r\n      }\r\n    ]\r\n  }\r\n}"
          }
        ]
      }
    ]
  }
}

这是我二头肌的一些片段:

resource databaseAccount 'Microsoft.DocumentDB/databaseAccounts@2021-01-15' = {
  name: accountName
  location: location
  kind: 'GlobalDocumentDB'
  properties: {
    consistencyPolicy: {
      defaultConsistencyLevel: 'Session'
    }
    locations: [
      {
        locationName: location
        failoverPriority: 0
        isZoneRedundant: false
      }
    ]
    databaseAccountOfferType: 'Standard'
    enableAutomaticFailover: false
    enableMultipleWriteLocations: false
    publicNetworkAccess: publicNetworkAccess
  }
}

resource cosmosdb 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2021-01-15' = {
  name: '${databaseAccount.name}/${databaseName}'
  properties: {
    resource: {
      id: databaseName
    }
  }
}

resource containers 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-01-15' = {
  name: '${databaseAccount.name}/${databaseName}/${containerName}'
  properties: {
    resource: {
      id: containerName
      partitionKey: {
        paths: [
          '/partitionKey'
        ]
        kind: 'Hash'
      }
    }
    options: {
      throughput: throughput
    }
  }
}

resource userDefinedFunctions 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/userDefinedFunctions@2021-01-15' = {
  name: '${databaseAccount.name}/${databaseName}/${containerName}/${userDefinedFunctionName}'
  properties: {
    resource: {
      id: userDefinedFunctionName
      body: 'function checkTime(ts){var ts_date=new Date(ts); var hour=ts_date.getHours(); return hour == 8;}'
    }
  }
}

当我添加最后一个资源时 userDefinedFunctions 它在执行时总是失败:

az deployment group create -f main.bicep -g myresgroup

我的意思是它创建了数据库、集合,但没有存储过程或 udfs,并且有上面的错误。 我有点担心二头肌还不可能。

我也尝试了许多其他 api 版本 @api

更新

我从 bicep 生成了 json 文件并检查 dependsOn 属性 是否生成错误:

"dependsOn": [
                "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('accountName'))]"
              ]

当我改为:

"dependsOn": [
                "[resourceId('Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers', parameters('accountName'), parameters('databaseName'), parameters('containerName'))]"
              ]

部署成功。

有没有办法影响 dependsOn 属性 代?

我在这里有一个例子,我将你展示的手臂模板样本反编译成二头肌。

https://github.com/markjbrown/azure-quickstart-templates/tree/sql-sp/quickstarts/microsoft.documentdb/cosmosdb-sql-container-sprocs

我还没有机会完成我对这个更新示例的 PR,但这将在您指向的 ARM 模板所在的位置发布。但是,如果您需要一个示例来解锁您,这就是您所需要的。

更新: 为空白的二头肌文件道歉。不确定那是怎么发生的。

要获取有关如何使用 bicep 部署 UDF 的示例,您可以使用位于此处的 ARM 模板,然后将其传递给 bicep 的反编译函数。示例如下。

bicep decompile "path/to/azuredeploy.json"

然后可以部署生成的 .bicep 文件。