我正在创建三个资源:cosmos 帐户、cosmos 数据库和 cosmos 容器。除了我将其模块化的容器外,我将它们全部放在一个文件夹中。
主二头肌
// Cosmos DB Account
resource cosmos 'Microsoft.DocumentDB/databaseAccounts@2020-06-01-preview' = if (deployDB) {
name: cosmosAccountName
location: location
tags: appTags
kind: 'GlobalDocumentDB'
identity: {
type: 'None'
}
properties: {
...
}
dependsOn: [
virtualNetwork
]
}
// Cosmos SQL Database
resource cosmosdb 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2020-04-01' = if (deployDB) {
name: '${cosmos.name}/${databaseName}'
properties: {
resource: {
id: databaseName
}
options: {
throughput: 400
}
}
}
// Cosmos DB Containers
module cosmosContainersUser '../../../cicd/bicep/modules/datastore/cosmos-db.bicep' = [for container in cosmosContainers: if (deployDB) {
name: container.containerName
params: {
cosmosContainerName: container.name
id: container.id
partitionKey: container.partitionKey
}
}]
cosmos-db.二头肌
param cosmosContainerName string
param id string
param partitionKey string
// Cosmos Containers
resource cosmosContainerUser 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-06-15' = {
name: cosmosContainerName
properties: {
resource: {
id: id
indexingPolicy: {
indexingMode: 'consistent'
automatic: true
includedPaths:[
{
path: '/*'
}
]
excludedPaths: [
{
path: '/"_etag"/?'
}
]
}
partitionKey: {
kind: 'Hash'
paths: [
partitionKey
]
}
conflictResolutionPolicy: {
mode: 'LastWriterWins'
conflictResolutionPath: '/_ts'
}
}
}
}
这没有问题,但是 main.bicep 仍然很大,我想继续对其进行模块化,并且无法将其他资源移动到 cosmos-db.bicep 中。如果我要将 cosmos 数据库资源添加到文件中:
cosmos-db.二头肌
param databaseName string
param cosmosDbName string
param cosmosContainerName string
param id string
param partitionKey string
// Cosmos Database
resource cosmosdb 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2020-04-01' = {
name: cosmosDbName
properties: {
resource: {
id: databaseName
}
options: {
throughput: 400
}
}
}
// Cosmos Containers
resource cosmosContainerUser 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-06-15' = {
name: cosmosContainerName
properties: {
resource: {
id: id
indexingPolicy: {
indexingMode: 'consistent'
automatic: true
includedPaths:[
{
path: '/*'
}
]
excludedPaths: [
{
path: '/"_etag"/?'
}
]
}
partitionKey: {
kind: 'Hash'
paths: [
partitionKey
]
}
conflictResolutionPolicy: {
mode: 'LastWriterWins'
conflictResolutionPath: '/_ts'
}
}
}
}
主二头肌
module cosmosdb '../../../cicd/bicep/modules/datastore/cosmos-db.bicep' = {
name: 'cosmosDbName'
params: {
cosmosDbName: 'cosmosDbName'
databaseName: databaseName
}
}
我params
在 main.bicep 中的 cosmosdb 和 cosmosContainersUser 模块中的关键字下都有一条红线。对于 cosmosdb 模块,它说:The specified "object" declaration is missing the following required properties: "cosmosContainerName", "id", "partitionKey".bicep(BCP035)
并且params
cosmosContainersUser 模块中的关键字说The specified "object" declaration is missing the following required properties: "cosmosDbName", "databaseName".bicep(BCP035)
。
我猜这是一个依赖问题,因为这些资源相互依赖。我不能在 main.bicep 中使用 parent 关键字,因为模块不支持它。我尝试使用dependsOn
并尝试将父级添加到 cosmos-db.bicep 中的 cosmosContainerUser 并且仍然收到相同的错误消息。