Bicep 접근자 연산자

접근자 연산자는 자식 리소스, 개체에 대한 속성 및 배열의 요소에 액세스하는 데 사용됩니다. 속성 접근자를 사용하여 일부 함수를 사용할 수도 있습니다.

연산자 이름
[] 인덱스 접근자
. 함수 접근자
:: 중첩된 리소스 접근자
. 속성 접근자

인덱스 접근자

array[integerIndex]

object['stringIndex']

인덱스 접근자를 사용하여 배열에서 요소를 가져오거나 개체에서 속성을 가져옵니다.

배열의 경우 인덱스를 정수로 제공합니다. 정수는 검색할 요소의 0부터 시작하는 위치와 일치합니다.

개체의 경우 인덱스를 문자열로 제공합니다. 문자열은 검색할 개체의 이름과 일치합니다.

다음 예제에서는 배열의 요소를 가져옵니다.

var arrayVar = [
  'Coho'
  'Contoso'
  'Fabrikan'
]

output accessorResult string = arrayVar[1]

예제의 출력:

이름 타입
accessorResult string 'Contoso'

다음 예제에서는 개체의 속성을 가져옵니다.

var environmentSettings = {
  dev: {
    name: 'Development'
  }
  prod: {
    name: 'Production'
  }
}

output accessorResult string = environmentSettings['dev'].name

예제의 출력:

이름 타입
accessorResult string 'Development'

함수 접근자

resourceName.functionName()

두 함수 getSecretlist*는 함수를 호출하기 위한 접근자 연산자를 지원합니다. 이 두 함수만이 접근자 연산자를 지원하는 함수입니다.

예시

다음 예제에서는 기존 키 자격 증명 모음을 참조한 다음, getSecret를 사용하여 모듈에 비밀을 전달합니다.

resource kv 'Microsoft.KeyVault/vaults@2023-02-01' existing = {
  name: kvName
  scope: resourceGroup(subscriptionId, kvResourceGroup )
}

module sql './sql.bicep' = {
  name: 'deploySQL'
  params: {
    sqlServerName: sqlServerName
    adminLogin: adminLogin
    adminPassword: kv.getSecret('vmAdminPassword')
  }
}

중첩된 리소스 접근자

parentResource::nestedResource

중첩된 리소스는 다른 리소스 내에서 선언된 리소스입니다. 부모 리소스 외부에서 중첩된 리소스에 액세스하려면 중첩된 리소스 접근자 ::을 사용합니다.

부모 리소스 내에서 기호 이름만으로 중첩된 리소스를 참조합니다. 부모 리소스 외부에서 중첩된 리소스를 참조하는 경우에만 중첩된 리소스 접근자를 사용해야 합니다.

예시

다음 예제에서는 부모 리소스 내부와 부모 리소스의 외부에서 중첩된 리소스를 참조하는 방법을 보여 줍니다.

resource demoParent 'demo.Rp/parentType@2023-01-01' = {
  name: 'demoParent'
  location: 'West US'

  // Declare a nested resource within 'demoParent'
  resource demoNested 'childType' = {
    name: 'demoNested'
    properties: {
      displayName: 'The nested instance.'
    }
  }

  // Declare another nested resource
  resource demoSibling 'childType' = {
    name: 'demoSibling'
    properties: {
      // Use symbolic name to reference because this line is within demoParent
      displayName: 'Sibling of ${demoNested.properties.displayName}'
    }
  }
}

// Use nested accessor to reference because this line is outside of demoParent
output displayName string = demoParent::demoNested.properties.displayName

속성 접근자

objectName.propertyName

개체의 속성에 액세스하려면 속성 접근자를 사용합니다. 속성 접근자는 개체인 매개 변수 및 변수를 포함하여 모든 개체와 함께 사용할 수 있습니다. 개체가 아닌 식에 속성 액세스를 사용하는 경우 오류가 발생합니다.

예시

다음 예제에서는 개체 변수 및 속성에 액세스하는 방법을 보여 줍니다.

var x = {
  y: {
    z: 'Hello'
    a: true
  }
  q: 42
}

output outputZ string = x.y.z
output outputQ int = x.q

예제의 출력:

이름 타입
outputZ string 'Hello'
outputQ 정수 42

일반적으로 Bicep 파일에 배포된 리소스에 속성 접근자를 사용합니다. 다음 예제에서는 공용 IP 주소를 만들고 속성 접근자를 사용하여 배포된 리소스에서 값을 반환합니다.

resource publicIp 'Microsoft.Network/publicIPAddresses@2022-11-01' = {
  name: publicIpResourceName
  location: location
  properties: {
    publicIPAllocationMethod: dynamicAllocation ? 'Dynamic' : 'Static'
    dnsSettings: {
      domainNameLabel: publicIpDnsLabel
    }
  }
}

// Use property accessor to get value
output ipFqdn string = publicIp.properties.dnsSettings.fqdn

다음 단계