Skip to content

Commit 016bb68

Browse files
Merge pull request #225563 from mumian/0130-linter-parent
New linter rule - use parent-property
2 parents 8c39c75 + 0bd1cc7 commit 016bb68

File tree

6 files changed

+95
-6
lines changed

6 files changed

+95
-6
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: 11/01/2022
5+
ms.date: 01/30/2023
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
"simplify-interpolation": {
9090
"level": "warning"
9191
},
92+
"use-parent-property": {
93+
"level": "warning"
94+
},
9295
"use-protectedsettings-for-commandtoexecute-secrets": {
9396
"level": "warning"
9497
},

articles/azure-resource-manager/bicep/child-resource-name-type.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ This article show different ways you can declare a child resource.
1717

1818
### Training resources
1919

20-
If you would rather learn about about child resources through step-by-step guidance, see [Deploy child and extension resources by using Bicep](/training/modules/child-extension-bicep-templates).
20+
If you would rather learn about child resources through step-by-step guidance, see [Deploy child and extension resources by using Bicep](/training/modules/child-extension-bicep-templates).
2121

2222
## Name and type pattern
2323

24-
In Bicep, you can specify the child resource either within the parent resource or outside of the parent resource. The values you provide for the resource name and resource type vary based on how you declare the child resource. However, the full name and type always resolve to the same pattern.
24+
In Bicep, you can specify the child resource either within the parent resource or outside of the parent resource. The values you provide for the resource name and resource type vary based on how you declare the child resource. However, the full name and type always resolve to the same pattern.
2525

2626
The **full name** of the child resource uses the pattern:
2727

@@ -47,7 +47,7 @@ If you have more than two levels in the hierarchy, keep repeating parent resourc
4747
{resource-provider-namespace}/{parent-resource-type}/{child-level1-resource-type}/{child-level2-resource-type}
4848
```
4949

50-
If you count the segments between `/` characters, the number of segments in the type is always one more than the number of segments in the name.
50+
If you count the segments between `/` characters, the number of segments in the type is always one more than the number of segments in the name.
5151

5252
## Within parent resource
5353

@@ -113,7 +113,7 @@ You can also use the full resource name and type when declaring the child resour
113113
:::code language="bicep" source="~/azure-docs-bicep-samples/syntax-samples/child-resource-name-type/fullnamedeclaration.bicep" highlight="10,11,17,18":::
114114

115115
> [!IMPORTANT]
116-
> Setting the full resource name and type isn't the recommended approach. It's not as type safe as using one of the other approaches.
116+
> Setting the full resource name and type isn't the recommended approach. It's not as type safe as using one of the other approaches. For more information, see [Linter rule: use parent property](./linter-rule-use-parent-property.md).
117117
118118
## Next steps
119119

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: Linter rule - use parent property
3+
description: Linter rule - use parent property
4+
ms.topic: conceptual
5+
ms.date: 01/30/2023
6+
---
7+
8+
# Linter rule - use parent property
9+
10+
When defined outside of the parent resource, you format name of the child resource with slashes to include the parent name. Setting the full resource name isn't the recommended approach. The syntax can be simplified by using the `parent` property. For more information, see [Full resource name outside parent](./child-resource-name-type.md#full-resource-name-outside-parent).
11+
12+
When defined outside of the parent resource, you use slashes to include the parent name in the name of the child resource. Setting the full resource name with parent resource name is not recommended. The `parent` property can be used to simplify the syntax. See [Full resource name outside parent](./child-resource-name-type.md#full-resource-name-outside-parent).
13+
14+
## Linter rule code
15+
16+
Use the following value in the [Bicep configuration file](bicep-config-linter.md) to customize rule settings:
17+
18+
`use-parent-property`
19+
20+
## Solution
21+
22+
The following example fails this test because of the name values for `service` and `share`:
23+
24+
```bicep
25+
param location string = resourceGroup().location
26+
27+
resource storage 'Microsoft.Storage/storageAccounts@2021-02-01' = {
28+
name: 'examplestorage'
29+
location: location
30+
kind: 'StorageV2'
31+
sku: {
32+
name: 'Standard_LRS'
33+
}
34+
}
35+
36+
resource service 'Microsoft.Storage/storageAccounts/fileServices@2021-02-01' = {
37+
name: 'examplestorage/default'
38+
dependsOn: [
39+
storage
40+
]
41+
}
42+
43+
resource share 'Microsoft.Storage/storageAccounts/fileServices/shares@2021-02-01' = {
44+
name: 'examplestorage/default/exampleshare'
45+
dependsOn: [
46+
service
47+
]
48+
}
49+
```
50+
51+
You can fix the problem by using the `parent` property:
52+
53+
```bicep
54+
param location string = resourceGroup().location
55+
56+
resource storage 'Microsoft.Storage/storageAccounts@2021-02-01' = {
57+
name: 'examplestorage'
58+
location: location
59+
kind: 'StorageV2'
60+
sku: {
61+
name: 'Standard_LRS'
62+
}
63+
}
64+
65+
resource service 'Microsoft.Storage/storageAccounts/fileServices@2021-02-01' = {
66+
parent: storage
67+
name: 'default'
68+
}
69+
70+
resource share 'Microsoft.Storage/storageAccounts/fileServices/shares@2021-02-01' = {
71+
parent: service
72+
name: 'exampleshare'
73+
}
74+
```
75+
76+
You can fix the issue automatically by selecting **Quick Fix** as shown on the following screenshot:
77+
78+
:::image type="content" source="./media/linter-rule-use-parent-property/bicep-linter-rule-use-parent-property-quick-fix.png" alt-text="Screenshot of use parent property quick fix.":::
79+
80+
## Next steps
81+
82+
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: 11/01/2022
5+
ms.date: 01/30/2023
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-params-in-nested-deploy](./linter-rule-secure-params-in-nested-deploy.md)
4040
- [secure-secrets-in-params](./linter-rule-secure-secrets-in-parameters.md)
4141
- [simplify-interpolation](./linter-rule-simplify-interpolation.md)
42+
- [use-parent-property](./linter-rule-use-parent-property.md)
4243
- [use-protectedsettings-for-commandtoexecute-secrets](./linter-rule-use-protectedsettings-for-commandtoexecute-secrets.md)
4344
- [use-recent-api-versions](./linter-rule-use-recent-api-versions.md)
4445
- [use-resource-id-functions](./linter-rule-use-resource-id-functions.md)
Loading

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,9 @@
458458
- name: Use explicit values for module location parameters
459459
displayName: linter
460460
href: linter-rule-explicit-values-for-loc-params.md
461+
- name: Use parent property
462+
displayName: linter
463+
href: linter-rule-use-parent-property.md
461464
- name: use recent API versions
462465
displayName: linter
463466
href: linter-rule-use-recent-api-versions.md

0 commit comments

Comments
 (0)