|
| 1 | +--- |
| 2 | +title: Linter rule - use resource symbol reference |
| 3 | +description: Linter rule - use resource symbol reference |
| 4 | +ms.topic: conceptual |
| 5 | +ms.date: 03/30/2023 |
| 6 | +--- |
| 7 | + |
| 8 | +# Linter rule - use resource symbol reference |
| 9 | + |
| 10 | +This rule detects suboptimal uses of the [`reference`](./bicep-functions-resource.md#reference), and [`list`](./bicep-functions-resource.md#list) functions. Instead of invoking these functions, using a resource reference simplifies the syntax and allows Bicep to better understand your deployment dependency graph. |
| 11 | + |
| 12 | +## Linter rule code |
| 13 | + |
| 14 | +Use the following value in the [Bicep configuration file](bicep-config-linter.md) to customize rule settings: |
| 15 | + |
| 16 | +`use-resource-symbol-reference` |
| 17 | + |
| 18 | +## Solution |
| 19 | + |
| 20 | +The following example fails this test because of the uses of `reference` and `listKey`: |
| 21 | + |
| 22 | +```bicep |
| 23 | +@description('The name of the HDInsight cluster to create.') |
| 24 | +param clusterName string |
| 25 | +
|
| 26 | +@description('These credentials can be used to submit jobs to the cluster and to log into cluster dashboards.') |
| 27 | +param clusterLoginUserName string |
| 28 | +
|
| 29 | +@description('The password must be at least 10 characters in length and must contain at least one digit, one upper case letter, one lower case letter, and one non-alphanumeric character except (single-quote, double-quote, backslash, right-bracket, full-stop). Also, the password must not contain 3 consecutive characters from the cluster username or SSH username.') |
| 30 | +@minLength(10) |
| 31 | +@secure() |
| 32 | +param clusterLoginPassword string |
| 33 | +
|
| 34 | +@description('Location for all resources.') |
| 35 | +param location string = resourceGroup().location |
| 36 | +
|
| 37 | +param storageAccountName string = uniqueString(resourceGroup().id) |
| 38 | +
|
| 39 | +resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' existing = { |
| 40 | + name: storageAccountName |
| 41 | +} |
| 42 | +
|
| 43 | +resource cluster 'Microsoft.HDInsight/clusters@2021-06-01' = { |
| 44 | + name: clusterName |
| 45 | + location: location |
| 46 | + properties: { |
| 47 | + clusterVersion: '4.0' |
| 48 | + osType: 'Linux' |
| 49 | + clusterDefinition: { |
| 50 | + kind: 'hbase' |
| 51 | + configurations: { |
| 52 | + gateway: { |
| 53 | + 'restAuthCredential.isEnabled': true |
| 54 | + 'restAuthCredential.username': clusterLoginUserName |
| 55 | + 'restAuthCredential.password': clusterLoginPassword |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + storageProfile: { |
| 60 | + storageaccounts: [ |
| 61 | + { |
| 62 | + name: replace(replace(reference(storageAccount.id, '2022-09-01').primaryEndpoints.blob, 'https://', ''), '/', '') |
| 63 | + isDefault: true |
| 64 | + container: clusterName |
| 65 | + key: listKeys(storageAccount.id, '2022-09-01').keys[0].value |
| 66 | + } |
| 67 | + ] |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +You can fix the problem by using resource reference: |
| 74 | + |
| 75 | +```bicep |
| 76 | +@description('The name of the HDInsight cluster to create.') |
| 77 | +param clusterName string |
| 78 | +
|
| 79 | +@description('These credentials can be used to submit jobs to the cluster and to log into cluster dashboards.') |
| 80 | +param clusterLoginUserName string |
| 81 | +
|
| 82 | +@description('The password must be at least 10 characters in length and must contain at least one digit, one upper case letter, one lower case letter, and one non-alphanumeric character except (single-quote, double-quote, backslash, right-bracket, full-stop). Also, the password must not contain 3 consecutive characters from the cluster username or SSH username.') |
| 83 | +@minLength(10) |
| 84 | +@secure() |
| 85 | +param clusterLoginPassword string |
| 86 | +
|
| 87 | +@description('Location for all resources.') |
| 88 | +param location string = resourceGroup().location |
| 89 | +
|
| 90 | +param storageAccountName string = uniqueString(resourceGroup().id) |
| 91 | +
|
| 92 | +resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' existing = { |
| 93 | + name: storageAccountName |
| 94 | +} |
| 95 | +
|
| 96 | +resource cluster 'Microsoft.HDInsight/clusters@2021-06-01' = { |
| 97 | + name: clusterName |
| 98 | + location: location |
| 99 | + properties: { |
| 100 | + clusterVersion: '4.0' |
| 101 | + osType: 'Linux' |
| 102 | + clusterDefinition: { |
| 103 | + kind: 'hbase' |
| 104 | + configurations: { |
| 105 | + gateway: { |
| 106 | + 'restAuthCredential.isEnabled': true |
| 107 | + 'restAuthCredential.username': clusterLoginUserName |
| 108 | + 'restAuthCredential.password': clusterLoginPassword |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + storageProfile: { |
| 113 | + storageaccounts: [ |
| 114 | + { |
| 115 | + name: replace(replace(storageAccount.properties.primaryEndpoints.blob, 'https://', ''), '/', '') |
| 116 | + isDefault: true |
| 117 | + container: clusterName |
| 118 | + key: storageAccount.listKeys().keys[0].value |
| 119 | + } |
| 120 | + ] |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +You can fix the issue automatically by selecting **Quick Fix** as shown on the following screenshot: |
| 127 | + |
| 128 | +:::image type="content" source="./media/linter-rule-use-resource-symbol-reference/bicep-linter-rule-use-resource-symbol-reference-quick-fix.png" alt-text="Screenshot of use resource symbol reference quick fix." lightbox="./media/linter-rule-use-resource-symbol-reference/bicep-linter-rule-use-resource-symbol-reference-quick-fix.png"::: |
| 129 | + |
| 130 | +## Next steps |
| 131 | + |
| 132 | +For more information about the linter, see [Use Bicep linter](./linter.md). |
0 commit comments