1

这是否可以使用二头肌与 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

我的意思是它创建了 db、collection,但没有 sproc 或 udfs,并且上面有错误。我有点担心二头肌还不可能。

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

更新

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

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

当我改为:

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

部署成功。

有没有办法影响dependsOn属性的生成?

4

1 回答 1

2

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

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 文件。

于 2021-05-20T15:00:57.590 回答