azure Bicep:创建多个主题,多个订阅

zi8p0yeb  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(141)

我是Bicep的新手,一直在努力实现Bicep脚本来部署具有许多主题和订阅的Azure服务总线。
我添加的每个主题都有不同数量的订阅(例如,通知主题可能有3个订阅,但分析主题可能有2个订阅)。
有没有一种方法可以循环并创建所有的主题资源,然后循环所有的订阅并将它们添加到正确的主题定义中?

zvokhttg

zvokhttg1#

这取决于我们在这里谈论多少主题。如果你只是创建一个小数字,那么我会这样做;

TopicsAndSubs.bicep
@description('Name of the Service Bus namespace')
param serviceBusNamespaceName string

@description('Name of the Topic')
param serviceBusTopicName string = 'theweather'

param topicSubscriptions int = 3

resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2022-10-01-preview' existing = {
  name: serviceBusNamespaceName
}

resource serviceBusTopic 'Microsoft.ServiceBus/namespaces/topics@2022-10-01-preview' = {
  parent: serviceBusNamespace
  name: serviceBusTopicName
  properties: {

  }

  resource sub 'subscriptions' = [for i in range(0,topicSubscriptions): {
    name: 'sub-${i}'
    properties: {}
  }]
}

但是,如果它更古怪,那么你需要使用模块(一个单独的主题文件和一个订阅文件)。类似这样的东西;

Topics.bicep
@description('Name of the Service Bus namespace')
param serviceBusNamespaceName string

@description('Name of the Topic')
param topicsAndSubscriptions array = [
  {
    name: 'notification'
    subscriptions: [
      'none'
      'ntwo'
      'nthree'
      'nfour'
    ]
  }
  {
    name: 'analysis'
    subscriptions: [
      'aone'
      'atwo'
      'athree'
      'afour'
    ]
  }
]

resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2022-10-01-preview' existing = {
  name: serviceBusNamespaceName
}

resource serviceBusTopic 'Microsoft.ServiceBus/namespaces/topics@2022-10-01-preview' = [ for topic in topicsAndSubscriptions: {
  parent: serviceBusNamespace
  name: topic.name
  properties: {

  }
}]

module subs 'sub.bicep' = [ for topic in topicsAndSubscriptions: {
  name: '${topic.name}-subs'
  params: {
    servicebusNamespaceName: serviceBusNamespaceName
    topicName: topic.name
    subscriptions: topic.subscriptions
  }
}]
订阅.bicep
param servicebusNamespaceName string
param topicName string
param subscriptions array = ['asubscription','anotherone']

resource sub 'Microsoft.ServiceBus/namespaces/topics/subscriptions@2022-10-01-preview' = [for i in subscriptions: {
  name: '${servicebusNamespaceName}/${topicName}/${i}'
  properties: {}
}]

相关问题