Skip to content

Commit 57a62a6

Browse files
committed
add nested accessor
1 parent 06236c3 commit 57a62a6

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Describes the functions to use in a Bicep file to retrieve values a
44
author: mumian
55
ms.author: jgao
66
ms.topic: conceptual
7-
ms.date: 12/28/2021
7+
ms.date: 03/02/2022
88
---
99

1010
# Resource functions for Bicep
@@ -145,7 +145,7 @@ param subscriptionId string
145145
param kvResourceGroup string
146146
param kvName string
147147
148-
resource kv 'Microsoft.KeyVault/vaults@2019-09-01' existing = {
148+
resource keyVault 'Microsoft.KeyVault/vaults@2019-09-01' existing = {
149149
name: kvName
150150
scope: resourceGroup(subscriptionId, kvResourceGroup )
151151
}
@@ -155,7 +155,7 @@ module sql './sql.bicep' = {
155155
params: {
156156
sqlServerName: sqlServerName
157157
adminLogin: adminLogin
158-
adminPassword: kv.getSecret('vmAdminPassword')
158+
adminPassword: keyVault.getSecret('vmAdminPassword')
159159
}
160160
}
161161
```
@@ -171,7 +171,7 @@ You can call a list function for any resource type with an operation that starts
171171

172172
The syntax for this function varies by the name of the list operation. The returned values also vary by operation. Bicep doesn't currently support completions and validation for `list*` functions.
173173

174-
With **Bicep version 0.4.412 or later**, you call the list function by using the [accessor operator](operators-access.md#function-accessor). For example, `stg.listKeys()`.
174+
With **Bicep version 0.4.412 or later**, you call the list function by using the [accessor operator](operators-access.md#function-accessor). For example, `storageAccount.listKeys()`.
175175

176176
A [namespace qualifier](bicep-functions.md#namespaces-for-functions) isn't needed because the function is used with a resource type.
177177

@@ -218,7 +218,7 @@ Other `list` functions have different return formats. To see the format of a fun
218218
The following example deploys a storage account and then calls `listKeys` on that storage account. The key is used when setting a value for [deployment scripts](../templates/deployment-script-template.md).
219219

220220
```bicep
221-
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
221+
resource storageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' = {
222222
name: 'dscript${uniqueString(resourceGroup().id)}'
223223
location: location
224224
kind: 'StorageV2'
@@ -234,8 +234,8 @@ resource dScript 'Microsoft.Resources/deploymentScripts@2019-10-01-preview' = {
234234
properties: {
235235
azCliVersion: '2.0.80'
236236
storageAccountSettings: {
237-
storageAccountName: stg.name
238-
storageAccountKey: stg.listKeys().keys[0].value
237+
storageAccountName: storageAccount.name
238+
storageAccountKey: storageAccount.listKeys().keys[0].value
239239
}
240240
...
241241
}
@@ -254,7 +254,7 @@ param accountSasProperties object {
254254
}
255255
}
256256
...
257-
sasToken: stg.listAccountSas('2021-04-01', accountSasProperties).accountSasToken
257+
sasToken: storageAccount.listAccountSas('2021-04-01', accountSasProperties).accountSasToken
258258
```
259259

260260
### Implementations
@@ -493,12 +493,12 @@ Namespace: [az](bicep-functions.md#namespaces-for-functions).
493493

494494
The reference function is available in Bicep files, but typically you don't need it. Instead, use the symbolic name for the resource.
495495

496-
The following example deploys a storage account. It uses the symbolic name `stg` for the storage account to return a property.
496+
The following example deploys a storage account. It uses the symbolic name `storageAccount` for the storage account to return a property.
497497

498498
```bicep
499499
param storageAccountName string
500500
501-
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
501+
resource storageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' = {
502502
name: storageAccountName
503503
location: 'eastus'
504504
kind: 'Storage'
@@ -507,25 +507,29 @@ resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
507507
}
508508
}
509509
510-
output storageEndpoint object = stg.properties.primaryEndpoints
510+
output storageEndpoint object = storageAccount.properties.primaryEndpoints
511511
```
512512

513513
To get a property from an existing resource that isn't deployed in the template, use the `existing` keyword:
514514

515515
```bicep
516516
param storageAccountName string
517517
518-
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
518+
resource storageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
519519
name: storageAccountName
520520
}
521521
522522
// use later in template as often as needed
523-
output blobAddress string = stg.properties.primaryEndpoints.blob
523+
output blobAddress string = storageAccount.properties.primaryEndpoints.blob
524524
```
525525

526-
If you attempt to reference a resource that doesn't exist, you get the `NotFound` error and your deployment fails.
526+
To reference a resource that is nested inside a parent resource, use the [nested accessor](operators-access.md#nested-resource-accessor) (`::`). You only use this syntax when you're accessing the nested resource from outside of the parent resource.
527+
528+
```bicep
529+
vNet1::subnet1.properties.addressPrefix
530+
```
527531

528-
For more information, see [Reference resources](./compare-template-syntax.md#reference-resources) and the [JSON template reference function](../templates/template-functions-resource.md#reference).
532+
If you attempt to reference a resource that doesn't exist, you get the `NotFound` error and your deployment fails.
529533

530534
## resourceId
531535

@@ -544,7 +548,7 @@ For example:
544548
```bicep
545549
param storageAccountName string
546550
547-
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
551+
resource storageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' = {
548552
name: storageAccountName
549553
location: 'eastus'
550554
kind: 'Storage'
@@ -553,19 +557,19 @@ resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
553557
}
554558
}
555559
556-
output storageID string = stg.id
560+
output storageID string = storageAccount.id
557561
```
558562

559563
To get the resource ID for a resource that isn't deployed in the Bicep file, use the existing keyword.
560564

561565
```bicep
562566
param storageAccountName string
563567
564-
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
568+
resource storageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
565569
name: storageAccountName
566570
}
567571
568-
output storageID string = stg.id
572+
output storageID string = storageAccount.id
569573
```
570574

571575
For more information, see the [JSON template resourceId function](../templates/template-functions-resource.md#resourceid)
@@ -618,7 +622,7 @@ var roleDefinitionId = {
618622
}
619623
}
620624
621-
resource myRoleAssignment 'Microsoft.Authorization/roleAssignments@2018-09-01-preview' = {
625+
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2018-09-01-preview' = {
622626
name: guid(resourceGroup().id, principalId, roleDefinitionId[builtInRoleType].id)
623627
properties: {
624628
roleDefinitionId: roleDefinitionId[builtInRoleType].id
@@ -652,7 +656,7 @@ param policyDefinitionID string = '0a914e76-4921-4c19-b460-a2d36003525a'
652656
@description('Specifies the name of the policy assignment, can be used defined or an idempotent name as the defaultValue provides.')
653657
param policyAssignmentName string = guid(policyDefinitionID, resourceGroup().name)
654658
655-
resource myPolicyAssignment 'Microsoft.Authorization/policyAssignments@2019-09-01' = {
659+
resource policyAssignment 'Microsoft.Authorization/policyAssignments@2019-09-01' = {
656660
name: policyAssignmentName
657661
properties: {
658662
scope: subscriptionResourceId('Microsoft.Resources/resourceGroups', resourceGroup().name)

0 commit comments

Comments
 (0)