共用方式為


Linter 規則 - 使用資源符號參考

此規則會偵測 和 list 函式的reference次佳用法。 使用資源參考可簡化語法並允許 Bicep 以更好的方式了解您的部署相依性關係圖,而不要叫用這些函式。

Linter 規則程式碼

使用 Bicep 設定檔中的下列值來自訂規則設定:

use-resource-symbol-reference

解決方案

由於使用 referencelistKey,下列範例會導致此測試失敗:

@description('The name of the HDInsight cluster to create.')
param clusterName string

@description('These credentials can be used to submit jobs to the cluster and to log into cluster dashboards.')
param clusterLoginUserName string

@description('The password must be at least 10 characters in length and must contain at least one digit, one uppercase letter, one lowercase letter, and one non-alphanumeric character (except single-quote, double-quote, backslash, right-bracket, or full-stop characters). Also, the password must not contain three consecutive characters from the cluster username or SSH username.')
@minLength(10)
@secure()
param clusterLoginPassword string

@description('Location for all resources.')
param location string = resourceGroup().location

param storageAccountName string = uniqueString(resourceGroup().id)

resource storageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' existing = {
  name: storageAccountName
}

resource cluster 'Microsoft.HDInsight/clusters@2024-08-01-preview' = {
  name: clusterName
  location: location
  properties: {
    clusterVersion: '4.0'
    osType: 'Linux'
    clusterDefinition: {
      kind: 'hbase'
      configurations: {
        gateway: {
          'restAuthCredential.isEnabled': true
          'restAuthCredential.username': clusterLoginUserName
          'restAuthCredential.password': clusterLoginPassword
        }
      }
    }
    storageProfile: {
      storageaccounts: [
        {
          name: replace(replace(reference(storageAccount.id, '2023-05-01').primaryEndpoints.blob, 'https://', ''), '/', '')
          isDefault: true
          container: clusterName
          key: listKeys(storageAccount.id, '2023-05-01').keys[0].value
        }
      ]
    }
  }
}

您可以使用資源參考來修正問題:

@description('The name of the HDInsight cluster to create.')
param clusterName string

@description('These credentials can be used to submit jobs to the cluster and to log into cluster dashboards.')
param clusterLoginUserName string

@description('The password must be at least 10 characters in length and must contain at least one digit, one uppercase letter, one lowercase letter, and one non-alphanumeric character (except single-quote, double-quote, backslash, right-bracket, or full-stop characters). Also, the password must not contain three consecutive characters from the cluster username or SSH username.')
@minLength(10)
@secure()
param clusterLoginPassword string

@description('Location for all resources.')
param location string = resourceGroup().location

param storageAccountName string = uniqueString(resourceGroup().id)

resource storageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' existing = {
  name: storageAccountName
}

resource cluster 'Microsoft.HDInsight/clusters@2024-08-01-preview' = {
  name: clusterName
  location: location
  properties: {
    clusterVersion: '4.0'
    osType: 'Linux'
    clusterDefinition: {
      kind: 'hbase'
      configurations: {
        gateway: {
          'restAuthCredential.isEnabled': true
          'restAuthCredential.username': clusterLoginUserName
          'restAuthCredential.password': clusterLoginPassword
        }
      }
    }
    storageProfile: {
      storageaccounts: [
        {
          name: replace(replace(storageAccount.properties.primaryEndpoints.blob, 'https://', ''), '/', '')
          isDefault: true
          container: clusterName
          key: storageAccount.listKeys().keys[0].value
        }
      ]
    }
  }
}

使用 快速修正 來簡化語法:

針對 use-resource-symbol-reference linter 規則使用快速修正的螢幕快照。

下一步

如需 Linter 的詳細資訊,請參閱使用 Bicep Linter