azure 肱二头肌中的条件性母体

wlsrxk51  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(124)

我有一个Bicep脚本作为Azure管道的一部分部署。通过一些谷歌搜索,我几乎实现了使用Powershell脚本为资源实现“如果不存在则创建”,如https://ochzhen.com/blog/check-if-resource-exists-azure-bicep所述
问题似乎是我不能传递一个ID或一个条件引用作为父。我想做的是:

param isFdEndpointExisting bool
...
resource endpoint 'Microsoft.Cdn/profiles/afdEndpoints@2020-09-01' =if(!isFdEndpointExisting) {
  name: fdEndpointName
  parent: fdProfile
  ... //  params to initialize here
}

resource endpointExisting 'Microsoft.Cdn/profiles/afdEndpoints@2020-09-01'existing =if(isFdEndpointExisting) {
  name: fdEndpointName
  parent: fdProfile
}

resource route 'Microsoft.Cdn/profiles/afdEndpoints/routes@2020-09-01' =if(!isFdRouteExisting) {
  name: routeName
  parent: isFdEndpointExisting ? endpointExisting : endpoint

我得到的错误是:The "parent" property only permits direct references to resources. Expressions are not supported.bicep(BCP240)
有没有别的方法来做这件事。感觉有点笨拙。

kqlmhetl

kqlmhetl1#

您可以在模块中创建端点:

// fd-route.bicep

param frontDoorName string
param endpointName string
param routeName string

... 
resource frontDoor 'Microsoft.Cdn/profiles@2022-11-01-preview'existing = {
  name: frontDoorName
}

resource endpoint 'Microsoft.Cdn/profiles/afdEndpoints@2022-11-01-preview'existing = {
  parent: frontDoor
  name: endpointName  
}

resource route 'Microsoft.Cdn/profiles/afdEndpoints/routes@2020-09-01' = {
  parent: endpoint
  name: routeName
  ...
}

然后你可以像这样从你的主

param isFdEndpointExisting bool
param fdProfileName string
param fdEndpointName string
param routeName string 

...

// Get a ference to front door
resource fdProfile 'Microsoft.Cdn/profiles@2020-09-01' existing = {
  name: fdProfileName
}

// Create the endpoit if not exists
resource endpoint 'Microsoft.Cdn/profiles/afdEndpoints@2020-09-01' =if(!isFdEndpointExisting) {
  parent: fdProfile
  name: fdEndpointName  
  ...  
}

// Get a reference to the existing endpoint if already exists
resource endpointExisting 'Microsoft.Cdn/profiles/afdEndpoints@2020-09-01'existing =if(isFdEndpointExisting) {
  name: fdEndpointName
  parent: fdProfile
}

// Create the rouyte
module route 'fd-route.bicep' = {
  name: routeName
  params: {
    frontDoorName: fdProfile.name
    endpointName: isFdEndpointExisting ? endpointExisting.name : endpoint.name
    routeName: routeName
    ...
  }
}

相关问题