如果使用if表达式如何定义父级?

问题描述 投票:0回答:1

我有下面的 Kusto 集群的二头肌模板,我在其中使用 if 表达式 定义带或不带区域的资源(这是我理解可以做到这一点的唯一方法):

metadata description = 'Deploys a Kusto cluster with databases and principal assignments for enhanced data management and security.'

@description('The name of the Kusto cluster.')
param __clusterName string

@description('Array of databases to be created within the Kusto cluster. Each object should include databaseName.')
param __databases array

@description('Array of principal assignments for the Kusto cluster. Each object should include role, principalIdResourceId, tenantId, and principalType.')
param __clusterPrincipals array

@description('The capacity of the SKU tier for the Kusto cluster instance.')
param __clusterSkuCapacity int

@description('The SKU name for the Kusto cluster instance.')
param __clusterSkuName string

@description('The SKU tier for the Kusto cluster instance.')
param __clusterSkuTier string

@description('The location for the Kusto cluster (e.g., East US, West Europe).')
param __location string

@description('Enable or disable the purge feature in the Kusto cluster.')
param __enablePurge bool

@description('Enable or disable streaming ingestion in the Kusto cluster.')
param __enableStreamingIngest bool

@description('Enable or disable automatic stopping of the Kusto cluster.')
param __enableAutoStop bool

@description('Enable or disable optimized autoscale for the Kusto cluster.')
param __enableOptimizedAutoscale bool

@description('The maximum scale for optimized autoscale in the Kusto cluster.')
param __scaleMaximum int

@description('The minimum scale for optimized autoscale in the Kusto cluster.')
param __scaleMinimum int

@description('The version number for optimized autoscale in the Kusto cluster.')
param __scaleVersion int

@description('Availability zones for the Kusto cluster.')
param __zones array

@description('Flag to determine if user-assigned identities should be configured for the Kusto cluster.')
param __isToConfigureUserAssignedIdentities bool

@description('Flag to determine if AvailabilityZone should be disabled for this cluster.')
param __isToDisableAvailabilityZone bool

@description('User-assigned identities for the Kusto cluster.')
param __userAssignedIdentities object

@description('The type of identity to assign to the Kusto cluster (e.g., SystemAssigned, UserAssigned).')
param __identityType string

@description('Language extensions to be enabled in the Kusto cluster, if applicable.')
param __languageExtensions object

@description('Enable or disable language extensions in the Kusto cluster.')
param __languageExtensionsEnabled bool

@description('Tags to be applied to this resource.')
param __tags object = {}

var identityTypeSettingVar = {
  type: __identityType
}
var userAssignedIdentitiesSettingVar = {
  userAssignedIdentities: __userAssignedIdentities
}

var kustoClusterResource = __isToDisableAvailabilityZone ? kustoClusterWithoutZones : kustoClusterWithZones

@description('The Kusto cluster resource with specified configurations, including SKU, identity, and properties.')
resource kustoClusterWithZones 'Microsoft.Kusto/clusters@2023-08-15' = if (!__isToDisableAvailabilityZone) {
  name: __clusterName
  location: __location
  sku: {
    name: __clusterSkuName
    tier: __clusterSkuTier
    capacity: __clusterSkuCapacity
  }
  identity: ((__isToConfigureUserAssignedIdentities == true)
    ? union(identityTypeSettingVar, userAssignedIdentitiesSettingVar)
    : identityTypeSettingVar)
  zones: __zones
  properties: {
    trustedExternalTenants: [
      {
        value: '*'
      }
    ]
    enablePurge: __enablePurge
    enableStreamingIngest: __enableStreamingIngest
    enableAutoStop: __enableAutoStop
    enableDiskEncryption: true
    optimizedAutoscale: {
      isEnabled: __enableOptimizedAutoscale
      maximum: __scaleMaximum
      minimum: __scaleMinimum
      version: __scaleVersion
    }
    languageExtensions: __languageExtensionsEnabled ? __languageExtensions : null
  }
  tags: __tags
}

resource kustoClusterWithoutZones 'Microsoft.Kusto/clusters@2023-08-15' = if (__isToDisableAvailabilityZone) {
  name: __clusterName
  location: __location
  sku: {
    name: __clusterSkuName
    tier: __clusterSkuTier
    capacity: __clusterSkuCapacity
  }
  identity: ((__isToConfigureUserAssignedIdentities == true)
    ? union(identityTypeSettingVar, userAssignedIdentitiesSettingVar)
    : identityTypeSettingVar)
  properties: {
    trustedExternalTenants: [
      {
        value: '*'
      }
    ]
    enablePurge: __enablePurge
    enableStreamingIngest: __enableStreamingIngest
    enableAutoStop: __enableAutoStop
    enableDiskEncryption: true
    optimizedAutoscale: {
      isEnabled: __enableOptimizedAutoscale
      maximum: __scaleMaximum
      minimum: __scaleMinimum
      version: __scaleVersion
    }
    languageExtensions: __languageExtensionsEnabled ? __languageExtensions : null
  }
  tags: __tags
}

@description('Database resources created within the Kusto cluster, with a default read/write kind.')
resource kustoClusterName_database 'Microsoft.Kusto/clusters/databases@2023-08-15' = [
  for item in __databases: {
    parent: kustoClusterWithZones
    name: '${item}'
    location: __location
    kind: 'ReadWrite'
    properties: {
      softDeletePeriod: 'P30D'
      hotCachePeriod: 'P7D'
    }
  }
]

@description('Principal assignments for the Kusto cluster, including role and tenant information.')
resource kustoCluster_clusterPrincipalAssignments 'Microsoft.Kusto/clusters/principalAssignments@2023-08-15' = [
  for item in __clusterPrincipals: {
    parent: kustoClusterWithZones
    name: '${last(split(item.principalIdResourceId,'/'))}-${item.role}'
    properties: {
      principalId: reference(item.principalIdResourceId, '2023-08-15').principalId
      role: item.role
      tenantId: item.tenantId
      principalType: item.principalType
    }
  }
]

@description('The ID of the deployed Kusto cluster resource.')
output kustoClusterId string = ((__isToDisableAvailabilityZone == true) ? kustoClusterWithZones.id : kustoClusterWithZones.id)


@description('Array of database IDs created within the Kusto cluster.')
output databaseIds array = [for (name, i) in __databases: {
  databaseName: name
  databaseId: kustoClusterName_database[i].id
}]

@description('Array of principal assignment IDs created within the Kusto cluster.')
output clusterPrincipalAssignmentIds array = [for (name, i) in __clusterPrincipals: {
  principalName: name
  principalId: kustoCluster_clusterPrincipalAssignments[i].id
}]

如何根据这个条件定义

kustoClusterName_database 
kustoCluster_clusterPrincipalAssignments 
使用哪个parent?由于
parent
属性只允许直接引用资源。不支持表达式。我也尝试使用 if 来复制这些内容,但由于循环而无法做到这一点。

azure azure-resource-manager azure-bicep
1个回答
0
投票

您可以使用父级之外的完整资源名称语法,尽管不建议这样做:

resource kustoClusterName_database 'Microsoft.Kusto/clusters/databases@2023-08-15' = [
  for item in __databases: {
    name: '${__clusterName}/${item}'
    location: __location
    kind: 'ReadWrite'
    properties: {
      softDeletePeriod: 'P30D'
      hotCachePeriod: 'P7D'
    }
    dependsOn: __isToDisableAvailabilityZone ? [kustoClusterWithoutZones] : [kustoClusterWithZones]
  }
]

您看到的主要问题是因为您定义了两倍的集群资源。我想你可以做类似的事情,而不必定义集群资源两次:

resource kustoCluster 'Microsoft.Kusto/clusters@2023-08-15' = {
  name: __clusterName
  location: __location
  ...
  zones: __isToDisableAvailabilityZone ? null : __zones
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.