Skip to content

Commit 83e676c

Browse files
Merge pull request #212329 from mumian/0921-linter-resource-id
Linter rule - use resource id functions
2 parents 96d0ec7 + 0a735f7 commit 83e676c

File tree

5 files changed

+128
-8
lines changed

5 files changed

+128
-8
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Linter settings for Bicep config
33
description: Describes how to customize configuration values for the Bicep linter
44
ms.topic: conceptual
5-
ms.date: 09/21/2022
5+
ms.date: 09/23/2022
66
---
77

88
# Add linter settings in the Bicep config file
@@ -89,6 +89,9 @@ The following example shows the rules that are available for configuration.
8989
"use-protectedsettings-for-commandtoexecute-secrets": {
9090
"level": "warning"
9191
},
92+
"use-resource-id-functions": {
93+
"level": "warning"
94+
},
9295
"use-stable-resource-identifiers": {
9396
"level": "warning"
9497
},

articles/azure-resource-manager/bicep/linter-rule-no-hardcoded-location.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: Linter rule - no hard-coded locations
3-
description: Linter rule - no hard-coded locations
2+
title: Linter rule - no hardcoded locations
3+
description: Linter rule - no hardcoded locations
44
ms.topic: conceptual
55
ms.date: 1/6/2022
66
---
77

8-
# Linter rule - no hard-coded locations
8+
# Linter rule - no hardcoded locations
99

1010
This rule finds uses of Azure location values that aren't parameterized.
1111

@@ -17,9 +17,9 @@ Use the following value in the [Bicep configuration file](bicep-config-linter.md
1717

1818
## Solution
1919

20-
Template users may have limited access to regions where they can create resources. A hard-coded resource location might block users from creating a resource, thus preventing them from using the template. By providing a location parameter that defaults to the resource group location, users can use the default value when convenient but also specify a different location.
20+
Template users may have limited access to regions where they can create resources. A hardcoded resource location might block users from creating a resource, thus preventing them from using the template. By providing a location parameter that defaults to the resource group location, users can use the default value when convenient but also specify a different location.
2121

22-
Rather than using a hard-coded string or variable value, use a parameter, the string 'global', or an expression (but not `resourceGroup().location` or `deployment().location`, see [no-loc-expr-outside-params](./linter-rule-no-loc-expr-outside-params.md)). Best practice suggests that to set your resources' locations, your template should have a string parameter named `location`. This parameter may default to the resource group or deployment location (`resourceGroup().location` or `deployment().location`).
22+
Rather than using a hardcoded string or variable value, use a parameter, the string 'global', or an expression (but not `resourceGroup().location` or `deployment().location`, see [no-loc-expr-outside-params](./linter-rule-no-loc-expr-outside-params.md)). Best practice suggests that to set your resources' locations, your template should have a string parameter named `location`. This parameter may default to the resource group or deployment location (`resourceGroup().location` or `deployment().location`).
2323

2424
The following example fails this test because the resource's `location` property uses a string literal:
2525

@@ -61,7 +61,7 @@ The following example fails this test because a string literal is being passed i
6161
module m1 'module1.bicep' = {
6262
name: 'module1'
6363
params: {
64-
location: 'westus'
64+
location: 'westus'
6565
}
6666
}
6767
```
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
title: Linter rule - use resource ID functions
3+
description: Linter rule - use resource ID functions
4+
ms.topic: conceptual
5+
ms.date: 09/23/2022
6+
---
7+
8+
# Linter rule - use resource ID functions
9+
10+
Ensures that the ID of a symbolic resource name or a suitable function is used rather than a manually created ID, such as a concatenating string, for all properties representing a resource ID. Use resource symbolic names whenever it's possible.
11+
12+
The allowed functions include:
13+
14+
- [`extensionResourceId`](./bicep-functions-resource.md#extensionresourceid)
15+
- [`resourceId`](./bicep-functions-resource.md#resourceid)
16+
- [`subscriptionResourceId`](./bicep-functions-resource.md#subscriptionresourceid)
17+
- [`tenantResourceId`](./bicep-functions-resource.md#tenantresourceid)
18+
- [`reference`](./bicep-functions-resource.md#reference)
19+
- [`subscription`](./bicep-functions-scope.md#subscription)
20+
- [`guid`](./bicep-functions-string.md#guid)
21+
22+
## Linter rule code
23+
24+
Use the following value in the [Bicep configuration file](bicep-config-linter.md) to customize rule settings:
25+
26+
`use-resource-id-functions`
27+
28+
## Solution
29+
30+
The following example fails this test because the resource's `api/id` property uses a manually created string:
31+
32+
```bicep
33+
@description('description')
34+
param connections_azuremonitorlogs_name string
35+
36+
@description('description')
37+
param location string
38+
39+
@description('description')
40+
param resourceTags object
41+
param tenantId string
42+
43+
resource connections_azuremonitorlogs_name_resource 'Microsoft.Web/connections@2016-06-01' = {
44+
name: connections_azuremonitorlogs_name
45+
location: location
46+
tags: resourceTags
47+
properties: {
48+
displayName: 'azuremonitorlogs'
49+
statuses: [
50+
{
51+
status: 'Connected'
52+
}
53+
]
54+
nonSecretParameterValues: {
55+
'token:TenantId': tenantId
56+
'token:grantType': 'code'
57+
}
58+
api: {
59+
name: connections_azuremonitorlogs_name
60+
displayName: 'Azure Monitor Logs'
61+
description: 'Use this connector to query your Azure Monitor Logs across Log Analytics workspace and Application Insights component, to list or visualize results.'
62+
iconUri: 'https://connectoricons-prod.azureedge.net/releases/v1.0.1501/1.0.1501.2507/${connections_azuremonitorlogs_name}/icon.png'
63+
brandColor: '#0072C6'
64+
id: '/subscriptions/<subscription_id_here>/providers/Microsoft.Web/locations/<region_here>/managedApis/${connections_azuremonitorlogs_name}'
65+
type: 'Microsoft.Web/locations/managedApis'
66+
}
67+
}
68+
}
69+
```
70+
71+
You can fix it by using the `subscriptionResourceId()` function:
72+
73+
```bicep
74+
@description('description')
75+
param connections_azuremonitorlogs_name string
76+
77+
@description('description')
78+
param location string
79+
80+
@description('description')
81+
param resourceTags object
82+
param tenantId string
83+
84+
resource connections_azuremonitorlogs_name_resource 'Microsoft.Web/connections@2016-06-01' = {
85+
name: connections_azuremonitorlogs_name
86+
location: location
87+
tags: resourceTags
88+
properties: {
89+
displayName: 'azuremonitorlogs'
90+
statuses: [
91+
{
92+
status: 'Connected'
93+
}
94+
]
95+
nonSecretParameterValues: {
96+
'token:TenantId': tenantId
97+
'token:grantType': 'code'
98+
}
99+
api: {
100+
name: connections_azuremonitorlogs_name
101+
displayName: 'Azure Monitor Logs'
102+
description: 'Use this connector to query your Azure Monitor Logs across Log Analytics workspace and Application Insights component, to list or visualize results.'
103+
iconUri: 'https://connectoricons-prod.azureedge.net/releases/v1.0.1501/1.0.1501.2507/${connections_azuremonitorlogs_name}/icon.png'
104+
brandColor: '#0072C6'
105+
id: subscriptionResourceId('Microsoft.Web/locations/managedApis', location, connections_azuremonitorlogs_name)
106+
type: 'Microsoft.Web/locations/managedApis'
107+
}
108+
}
109+
}
110+
```
111+
112+
## Next steps
113+
114+
For more information about the linter, see [Use Bicep linter](./linter.md).

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Use Bicep linter
33
description: Learn how to use Bicep linter.
44
ms.topic: conceptual
5-
ms.date: 09/21/2022
5+
ms.date: 9/23/2022
66
---
77

88
# Use Bicep linter
@@ -39,6 +39,7 @@ The default set of linter rules is minimal and taken from [arm-ttk test cases](.
3939
- [secure-secrets-in-params](./linter-rule-secure-secrets-in-parameters.md)
4040
- [simplify-interpolation](./linter-rule-simplify-interpolation.md)
4141
- [use-protectedsettings-for-commandtoexecute-secrets](./linter-rule-use-protectedsettings-for-commandtoexecute-secrets.md)
42+
- [use-resource-id-functions](./linter-rule-use-resource-id-functions.md)
4243
- [use-stable-resource-identifiers](./linter-rule-use-stable-resource-identifier.md)
4344
- [use-stable-vm-image](./linter-rule-use-stable-vm-image.md)
4445

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,8 @@
432432
href: linter-rule-simplify-interpolation.md
433433
- name: Use explicit values for module location parameters
434434
href: linter-rule-explicit-values-for-loc-params.md
435+
- name: use resource ID functions
436+
href: linter-rule-use-resource-id-functions.md
435437
- name: Use stable resource identifier
436438
href: linter-rule-use-stable-resource-identifier.md
437439
- name: Use stable VM image

0 commit comments

Comments
 (0)