Linter 规则 - 使用资源 ID 函数

对表示资源 ID 的所有属性,确保使用符号资源名称或合适函数的 ID,而不是手动创建的 ID,例如串联字符串。 尽可能使用资源符号名称。

允许的函数包括:

Linter 规则代码

请在 Bicep 配置文件中使用以下值自定义规则设置:

use-resource-id-functions

解决方案

下面的示例未通过此测试,因为资源的 api/id 属性使用手动创建的字符串:

@description('description')
param connections_azuremonitorlogs_name string

@description('description')
param location string

@description('description')
param resourceTags object
param tenantId string

resource connections_azuremonitorlogs_name_resource 'Microsoft.Web/connections@2016-06-01' = {
  name: connections_azuremonitorlogs_name
  location: location
  tags: resourceTags
  properties: {
    displayName: 'azuremonitorlogs'
    statuses: [
      {
        status: 'Connected'
      }
    ]
    nonSecretParameterValues: {
      'token:TenantId': tenantId
      'token:grantType': 'code'
    }
    api: {
      name: connections_azuremonitorlogs_name
      displayName: 'Azure Monitor Logs'
      description: 'Use this connector to query your Azure Monitor Logs across Log Analytics workspace and Application Insights component, to list or visualize results.'
      iconUri: 'https://connectoricons-prod.azureedge.net/releases/v1.0.1501/1.0.1501.2507/${connections_azuremonitorlogs_name}/icon.png'
      brandColor: '#0072C6'
      id: '/subscriptions/<subscription_id_here>/providers/Microsoft.Web/locations/<region_here>/managedApis/${connections_azuremonitorlogs_name}'
      type: 'Microsoft.Web/locations/managedApis'
    }
  }
}

可以使用 subscriptionResourceId() 函数修复它:

@description('description')
param connections_azuremonitorlogs_name string

@description('description')
param location string

@description('description')
param resourceTags object
param tenantId string

resource connections_azuremonitorlogs_name_resource 'Microsoft.Web/connections@2016-06-01' = {
  name: connections_azuremonitorlogs_name
  location: location
  tags: resourceTags
  properties: {
    displayName: 'azuremonitorlogs'
    statuses: [
      {
        status: 'Connected'
      }
    ]
    nonSecretParameterValues: {
      'token:TenantId': tenantId
      'token:grantType': 'code'
    }
    api: {
      name: connections_azuremonitorlogs_name
      displayName: 'Azure Monitor Logs'
      description: 'Use this connector to query your Azure Monitor Logs across Log Analytics workspace and Application Insights component, to list or visualize results.'
      iconUri: 'https://connectoricons-prod.azureedge.net/releases/v1.0.1501/1.0.1501.2507/${connections_azuremonitorlogs_name}/icon.png'
      brandColor: '#0072C6'
      id: subscriptionResourceId('Microsoft.Web/locations/managedApis', location, connections_azuremonitorlogs_name)
      type: 'Microsoft.Web/locations/managedApis'
    }
  }
}

后续步骤

有关 Linter 的详细信息,请参阅使用 Bicep Linter