Linter 規則 - 使用資源識別碼函式

確定針對代表資源識別碼的所有屬性,都是使用符號資源名稱或適當函式的識別碼,而不是手動建立的識別碼 (例如串連字串)。 盡可能使用資源符號名稱。

允許的函式包括:

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