Skip to content

Commit cf01fed

Browse files
committed
new linter rule - use resource id functions
1 parent d0872b9 commit cf01fed

File tree

4 files changed

+83
-59
lines changed

4 files changed

+83
-59
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
@@ -65,6 +65,9 @@ The following example shows the rules that are available for configuration.
6565
"secure-secrets-in-params": {
6666
"level": "warning"
6767
},
68+
"use-resource-id-functions": {
69+
"level": "warning"
70+
},
6871
"use-stable-resource-identifiers": {
6972
"level": "warning"
7073
},

articles/azure-resource-manager/bicep/linter-rule-use-resource-id-functions.md

Lines changed: 76 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
title: Linter rule - use resourceId functions
33
description: Linter rule - use resourceId functions
44
ms.topic: conceptual
5-
ms.date: 09/21/2022
5+
ms.date: 09/23/2022
66
---
77

88
# Linter rule - use resourceId function
99

10-
This rule finds uses of Azure location values that aren't parameterized.
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 is possible.
1111

12-
If property "id" represents a resource ID, it must use a symbolic resource reference, be a parameter or start with one of these functions: extensionResourceId, guid, if, reference, resourceId, subscription, subscriptionResourceId, tenantResourceId. Found nonconforming expression at id -> serviceBusConnectionId [https://aka.ms/bicep/linter/use-resource-id-functions]
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+
- [`if`](./conditional-resource-deployment.md)
19+
- [`reference`](./bicep-functions-resource.md#reference)
20+
- [`subscription`](./bicep-functions-scope.md#subscription)
21+
- [`guid`](./bicep-functions-string.md#guid)
1322

1423
## Linter rule code
1524

@@ -19,75 +28,84 @@ Use the following value in the [Bicep configuration file](bicep-config-linter.md
1928

2029
## Solution
2130

22-
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.
23-
24-
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`).
25-
26-
The following example fails this test because the resource's `location` property uses a string literal:
31+
The following example fails this test because the resource's `api/id` property uses a manually-created string:
2732

2833
```bicep
29-
resource stg 'Microsoft.Storage/storageAccounts@2021-02-01' = {
30-
location: 'westus'
31-
}
32-
```
33-
You can fix it by creating a new `location` string parameter (which may optionally have a default value - resourceGroup().location is frequently used as a default):
34+
@description('description')
35+
param connections_azuremonitorlogs_name string
3436
35-
```bicep
36-
param location string = resourceGroup().location
37-
resource stg 'Microsoft.Storage/storageAccounts@2021-02-01' = {
38-
location: location
39-
}
40-
```
37+
@description('description')
38+
param location string
4139
42-
The following example fails this test because the resource's `location` property uses a variable with a string literal.
40+
@description('description')
41+
param resourceTags object
42+
param tenantId string
4343
44-
```bicep
45-
var location = 'westus'
46-
resource stg 'Microsoft.Storage/storageAccounts@2021-02-01' = {
47-
location: location
44+
resource connections_azuremonitorlogs_name_resource 'Microsoft.Web/connections@2016-06-01' = {
45+
name: connections_azuremonitorlogs_name
46+
location: location
47+
tags: resourceTags
48+
properties: {
49+
displayName: 'azuremonitorlogs'
50+
statuses: [
51+
{
52+
status: 'Connected'
53+
}
54+
]
55+
nonSecretParameterValues: {
56+
'token:TenantId': tenantId
57+
'token:grantType': 'code'
58+
}
59+
api: {
60+
name: connections_azuremonitorlogs_name
61+
displayName: 'Azure Monitor Logs'
62+
description: 'Use this connector to query your Azure Monitor Logs across Log Analytics workspace and Application Insights component, to list or visualize results.'
63+
iconUri: 'https://connectoricons-prod.azureedge.net/releases/v1.0.1501/1.0.1501.2507/${connections_azuremonitorlogs_name}/icon.png'
64+
brandColor: '#0072C6'
65+
id: '/subscriptions/<subscription_id_here>/providers/Microsoft.Web/locations/<region_here>/managedApis/${connections_azuremonitorlogs_name}'
66+
type: 'Microsoft.Web/locations/managedApis'
67+
}
4868
}
69+
}
4970
```
5071

51-
You can fix it by turning the variable into a parameter:
72+
You can fix it by using the `subscriptionResourceId()` function:
5273

5374
```bicep
54-
param location string = 'westus'
55-
resource stg 'Microsoft.Storage/storageAccounts@2021-02-01' = {
56-
location: location
57-
}
58-
```
59-
60-
The following example fails this test because a string literal is being passed in to a module parameter that is in turn used for a resource's `location` property:
75+
@description('description')
76+
param connections_azuremonitorlogs_name string
6177
62-
```bicep
63-
module m1 'module1.bicep' = {
64-
name: 'module1'
65-
params: {
66-
location: 'westus'
67-
}
68-
}
69-
```
70-
where module1.bicep is:
71-
```bicep
78+
@description('description')
7279
param location string
7380
74-
resource storageaccount 'Microsoft.Storage/storageAccounts@2021-02-01' = {
75-
name: 'storageaccount'
76-
location: location
77-
kind: 'StorageV2'
78-
sku: {
79-
name: 'Premium_LRS'
80-
}
81-
}
82-
```
81+
@description('description')
82+
param resourceTags object
83+
param tenantId string
8384
84-
You can fix the failure by creating a new parameter for the value:
85-
```bicep
86-
param location string // optionally with a default value
87-
module m1 'module1.bicep' = {
88-
name: 'module1'
89-
params: {
90-
location: location
85+
resource connections_azuremonitorlogs_name_resource 'Microsoft.Web/connections@2016-06-01' = {
86+
name: connections_azuremonitorlogs_name
87+
location: location
88+
tags: resourceTags
89+
properties: {
90+
displayName: 'azuremonitorlogs'
91+
statuses: [
92+
{
93+
status: 'Connected'
94+
}
95+
]
96+
nonSecretParameterValues: {
97+
'token:TenantId': tenantId
98+
'token:grantType': 'code'
99+
}
100+
api: {
101+
name: connections_azuremonitorlogs_name
102+
displayName: 'Azure Monitor Logs'
103+
description: 'Use this connector to query your Azure Monitor Logs across Log Analytics workspace and Application Insights component, to list or visualize results.'
104+
iconUri: 'https://connectoricons-prod.azureedge.net/releases/v1.0.1501/1.0.1501.2507/${connections_azuremonitorlogs_name}/icon.png'
105+
brandColor: '#0072C6'
106+
id: subscriptionResourceId('Microsoft.Web/locations/managedApis', location, connections_azuremonitorlogs_name)
107+
type: 'Microsoft.Web/locations/managedApis'
108+
}
91109
}
92110
}
93111
```

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: 07/29/2022
5+
ms.date: 9/23/2022
66
---
77

88
# Use Bicep linter
@@ -35,6 +35,7 @@ The default set of linter rules is minimal and taken from [arm-ttk test cases](.
3535
- [secure-secrets-in-params](./linter-rule-secure-secrets-in-parameters.md)
3636
- [simplify-interpolation](./linter-rule-simplify-interpolation.md)
3737
- [use-protectedsettings-for-commandtoexecute-secrets](./linter-rule-use-protectedsettings-for-commandtoexecute-secrets.md)
38+
- [use-resource-id-functions](./linter-rule-use-resource-id-functions.md)
3839
- [use-stable-resource-identifiers](./linter-rule-use-stable-resource-identifier.md)
3940
- [use-stable-vm-image](./linter-rule-use-stable-vm-image.md)
4041

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@
430430
href: linter-rule-simplify-interpolation.md
431431
- name: Use explicit values for module location parameters
432432
href: linter-rule-explicit-values-for-loc-params.md
433+
- name: use resource ID functions
434+
href: linter-rule-use-resource-id-functions.md
433435
- name: Use stable resource identifier
434436
href: linter-rule-use-stable-resource-identifier.md
435437
- name: Use stable VM image

0 commit comments

Comments
 (0)