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



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

我知道直接使用手臂模板是可能的: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": "{rn  "status": "Failed",rn  "error": {rn    "code": "ResourceDeploymentFailure",rn    "message": "The resource operation completed with terminal provisioning state 'Failed'.",rn    "details": [rn      {rn        "code": "NotFound",rn        "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"rn      }rn    ]rn  }rn}"
}
]
}
]
}
}

以下是我二头肌的一些片段:

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

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

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

更新

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

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

当我改为:

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

部署成功。

有没有办法影响dependentsOn属性生成?

我在这里有一个例子,我构建了一个反编译的手臂模板样本,你展示到二头肌中。

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"

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

最新更新