0

如何使用 azure bicep 在 azure eventHub 资源的网络部分中选择和添加 VNet 的多个子网。

// Create an event hub namespace

var eventHubNamespaceName = 'evhns-demo1436'

resource eventHubNamespace 'Microsoft.EventHub/namespaces@2021-01-01-preview' = {
  name: eventHubNamespaceName
  location: resourceGroup().location
  sku: {
    name: 'Standard'
    tier: 'Standard'
    capacity: 1
  }
  properties: {
    zoneRedundant: true
  }
}

// Create an event hub inside the namespace

var eventHubName = 'evh-demo1436'

resource eventHubNamespaceName_eventHubName 'Microsoft.EventHub/namespaces/eventhubs@2021-01-01-preview' = {
  parent: eventHubNamespace
  name: eventHubName
  properties: {
    messageRetentionInDays: 7
    partitionCount: 1
  }
}

// Grant Listen and Send on our event hub

resource eventHubNamespaceName_eventHubName_ListenSend 'Microsoft.EventHub/namespaces/eventhubs/authorizationRules@2021-01-01-preview' = {
  parent: eventHubNamespaceName_eventHubName
  name: 'ListenSend'
  properties: {
    rights: [
      'Listen'
      'Send'
    ]
  }
  dependsOn: [
    eventHubNamespace
  ]
}

resource testVnet 'Microsoft.Network/virtualNetworks@2021-03-01' existing = {
  name: 'testvnet'
}


resource testsubnet 'Microsoft.Network/virtualNetworks/subnets@2021-03-01' existing = {
  parent: testVnet
  name: 'testsubnet'
}

resource enHubVnetRule 'Microsoft.EventHub/namespaces/virtualnetworkrules@2018-01-01-preview' =  { 
  name: 'vnetName'
  parent: eventHubNamespace
  properties: {
    virtualNetworkSubnetId: testsubnet.id
  }
}

使用上面的代码,我只能使用 azure bicep 将 VNet 的一个特定子网添加到网络部分中 azure EventHub 资源的 VNet 条目中。

如何使用 azure bicep 获取 VNet 的所有子网并将它们全部添加/选择到网络部分中 azure EventHub 资源的 VNet 条目。

4

1 回答 1

1

您必须对块和规则块使用for循环,如下所示subnetvirtual-network

// Create an event hub namespace
param eventHubNamespaceName string = 'evhns-demo1436'
resource eventHubNamespace 'Microsoft.EventHub/namespaces@2021-01-01-preview' = {
  name: eventHubNamespaceName
  location: resourceGroup().location
  sku: {
    name: 'Standard'
    tier: 'Standard'
    capacity: 1
  }
  properties: {
    zoneRedundant: true
  }
}

// Create an event hub inside the namespace

param eventHubName string = 'evh-demo1436'

resource eventHubNamespaceName_eventHubName 'Microsoft.EventHub/namespaces/eventhubs@2021-01-01-preview' = {
  parent: eventHubNamespace
  name: eventHubName
  properties: {
    messageRetentionInDays: 7
    partitionCount: 1
  }
}

// Grant Listen and Send on our event hub

resource eventHubNamespaceName_eventHubName_ListenSend 'Microsoft.EventHub/namespaces/eventhubs/authorizationRules@2021-01-01-preview' = {
  parent: eventHubNamespaceName_eventHubName
  name: 'ListenSend'
  properties: {
    rights: [
      'Listen'
      'Send'
    ]
  }
  dependsOn: [
    eventHubNamespace
  ]
}
param subnets array =[
   'test'
   'mysubnet'
]


param vnetname string = 'test-ansuman'
resource testVnet 'Microsoft.Network/virtualNetworks@2021-03-01' existing = {
  name: vnetname
}


resource testsubnet 'Microsoft.Network/virtualNetworks/subnets@2021-03-01' existing =[for subnet in subnets : {
  parent: testVnet
  name: subnet
}]

resource enHubVnetRule 'Microsoft.EventHub/namespaces/virtualnetworkrules@2018-01-01-preview' = [for (subnet,i) in subnets :{ 
  name: '${vnetname}-${subnet}'
  parent: eventHubNamespace
  properties: {
    virtualNetworkSubnetId:testsubnet[i].id
}
}]

输出:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

于 2021-11-02T12:33:52.343 回答