azure vNet对等的Bicep中的循环问题

e37o9pze  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(168)

我希望为我的vNet对等方实施一种数据驱动的方法,这样我就可以通过添加到参数文件来添加其他对等方
这是我的二头肌

param peers object

resource sourcevNet 'Microsoft.Network/virtualNetworks@2021-03-01' existing = {
  name: peers.vNetPeeringSourceNetwork
}

resource vNetPeer 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings@2020-05-01' = [ for j in range(0, length(peers.peerings)): { 
  parent: sourcevNet
  name: '${peers.vNetPeeringSourceNetwork}-${peers.peerings[j]}'
  properties: [ for i in range(0, length(peers.peerings[j].vNetPeeringDestinationNetwork)): {
    allowVirtualNetworkAccess: peers.peerings[j].vNetPeeringDestinationNetwork[i].allowVirtualNetworkAccess
    allowForwardedTraffic: peers.peerings[j].vNetPeeringDestinationNetwork[i].allowForwardedTraffic
    allowGatewayTransit: peers.peerings[j].vNetPeeringDestinationNetwork[i].allowGatewayTransit
    useRemoteGateways: peers.peerings[j].vNetPeeringDestinationNetwork[i].useRemoteGateways
    remoteVirtualNetwork:  {
      id: peers.peerings[j].vNetPeeringDestinationNetwork[i].remote == false ? resourceId('${peers.peerings[j].vNetPeeringDestinationNetwork[i].peeredNetworkResourceGroup}','Microsoft.Network/virtualNetworks/', '${peers.peerings[j].vNetPeeringDestinationNetwork[i].peerNetworkSuffix}') : resourceId('${peers.peerings[j].vNetPeeringDestinationNetwork[i].remoteNetworkSubscription}','${peers.peerings[j].vNetPeeringDestinationNetwork[i].peeredNetworkResourceGroup}','Microsoft.Network/virtualNetworks/', '${peers.peerings[j].vNetPeeringDestinationNetwork[i].peerNetworkSuffix}')
    }
  }]
}]

这是参数

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": 
    {
    "peers": {
      "value":{
        "peerings":[
          {
        "vNetPeeringSourceNetwork": "Test-VM2",
        "vNetPeeringDestinationNetwork": [
          {
                "allowVirtualNetworkAccess": true,
                "allowForwardedTraffic": true,
                "allowGatewayTransit": false,
                "useRemoteGateways": false,
                "remote": false,
                "peerNetworkSuffix": "TestVM-vnet",
                "peeredNetworkResourceGroup": "TestVM",
                "remoteNetworkSubscription": "37beac69-e9c7-40fc-9d7b-6c1adf274185"
          },
          {
            "allowVirtualNetworkAccess": true,
            "allowForwardedTraffic": true,
            "allowGatewayTransit": false,
            "useRemoteGateways": false,
            "remote": false,
            "peerNetworkSuffix": "TestVM-vnet3",
            "peeredNetworkResourceGroup": "TestVM",
            "remoteNetworkSubscription": "37beac69-e9c7-40fc-9d7b-6c1adf274185"
      }   
          ]
        }
        ]
          }
      }
    }
  }

由于某种原因,它不喜欢属性的第二个循环。
属性“properties”需要类型为“VirtualNetworkPeeringPropertiesFormat”的值|null”,但提供的值类型为“object[]"。如果这是文档中的错误,请将其报告给Bicep Team.bicephttps://aka.ms/bicep-type-issues
任何帮助都将不胜感激!

lf5gs5x2

lf5gs5x21#

vNetPeer资源上的properties属性应该是一个对象,但您传递的是一个数组(由于循环)。
尚不支持嵌套循环(请参阅github issue)。
在您的情况下,可以将资源的创建 Package 在模块中。
同样在ARM参数文件中,可以直接使用数组,这样就不必执行peers.peerings

参数.json文件:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "peerings": {
      "value": [
        {
          "vNetPeeringSourceNetwork": "Test-VM2",
          "vNetPeeringDestinationNetwork": [
            {
              "allowVirtualNetworkAccess": true,
              "allowForwardedTraffic": true,
              "allowGatewayTransit": false,
              "useRemoteGateways": false,
              "remote": false,
              "peerNetworkSuffix": "TestVM-vnet",
              "peeredNetworkResourceGroup": "TestVM",
              "remoteNetworkSubscription": "37beac69-e9c7-40fc-9d7b-6c1adf274185"
            },
            {
              "allowVirtualNetworkAccess": true,
              "allowForwardedTraffic": true,
              "allowGatewayTransit": false,
              "useRemoteGateways": false,
              "remote": false,
              "peerNetworkSuffix": "TestVM-vnet3",
              "peeredNetworkResourceGroup": "TestVM",
              "remoteNetworkSubscription": "37beac69-e9c7-40fc-9d7b-6c1adf274185"
            }
          ]
        }
      ]
    }
  }
}

vnet-peering.二头肌模块:

param peering object

resource sourcevNet 'Microsoft.Network/virtualNetworks@2021-03-01' existing = {
  name: peering.vNetPeeringSourceNetwork
}

resource vNetPeer 'Microsoft.Network/virtualNetworks/virtualNetworkPeerings@2020-05-01' = [for destination in peering.vNetPeeringDestinationNetwork: {
  parent: sourcevNet
  name: '${peering.vNetPeeringSourceNetwork}-${destination.peerNetworkSuffix}'
  properties: {
    allowVirtualNetworkAccess: destination.allowVirtualNetworkAccess
    allowForwardedTraffic: destination.allowForwardedTraffic
    allowGatewayTransit: destination.allowGatewayTransit
    useRemoteGateways: destination.useRemoteGateways
    remoteVirtualNetwork: {
      id: destination.remote == false ? resourceId(destination.peeredNetworkResourceGroup, 'Microsoft.Network/virtualNetworks/', destination.peerNetworkSuffix) : resourceId(destination.remoteNetworkSubscription, destination.peeredNetworkResourceGroup, 'Microsoft.Network/virtualNetworks/', destination.peerNetworkSuffix)
    }
  }
}]

主.二头肌模板:

param peerings array

module vnetPeerings 'vnet-peering.bicep' = [for (peering, i) in peerings: {
  name: 'vnet-peering-${i}'
  params: {
    peering: peering
  }
}]

相关问题