Skip to content

Commit 4b8da40

Browse files
authored
Merge pull request #232479 from mumian/0328-linter-symbol
New linter rule: use reference symbol-reference
2 parents b26afd3 + 4da56b5 commit 4b8da40

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

articles/azure-resource-manager/bicep/bicep-config-linter.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ The following example shows the rules that are available for configuration.
104104
"use-resource-id-functions": {
105105
"level": "warning"
106106
},
107+
"use-resource-symbol-reference": {
108+
"level": "warning"
109+
},
107110
"use-stable-resource-identifiers": {
108111
"level": "warning"
109112
},
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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).

articles/azure-resource-manager/bicep/linter.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ The default set of linter rules is minimal and taken from [arm-ttk test cases](.
4444
- [use-protectedsettings-for-commandtoexecute-secrets](./linter-rule-use-protectedsettings-for-commandtoexecute-secrets.md)
4545
- [use-recent-api-versions](./linter-rule-use-recent-api-versions.md)
4646
- [use-resource-id-functions](./linter-rule-use-resource-id-functions.md)
47+
- [use-resource-symbol-reference](./linter-rule-use-resource-symbol-reference.md)
4748
- [use-stable-resource-identifiers](./linter-rule-use-stable-resource-identifier.md)
4849
- [use-stable-vm-image](./linter-rule-use-stable-vm-image.md)
4950

Loading

articles/azure-resource-manager/bicep/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@
479479
- name: use resource ID functions
480480
displayName: linter
481481
href: linter-rule-use-resource-id-functions.md
482+
- name: use resource symbol reference
483+
displayName: linter
484+
href: linter-rule-use-resource-symbol-reference.md
482485
- name: Use stable resource identifier
483486
displayName: linter
484487
href: linter-rule-use-stable-resource-identifier.md

0 commit comments

Comments
 (0)