From 4e809399337f36f977851439ae0098aa05b5e945 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 26 Nov 2024 13:26:44 +0800 Subject: [PATCH] mgmt, network, bug fix on bastion resource in subnet ipconf (#43082) * mgmt, network, bug fix on bastion resource in subnet ipconf * ignore the NIC if not found * add test case --- .../CHANGELOG.md | 2 + .../azure-resourcemanager-network/assets.json | 2 +- .../network/implementation/SubnetImpl.java | 17 ++++- .../resourcemanager/network/NetworkTests.java | 69 +++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 sdk/resourcemanager/azure-resourcemanager-network/src/test/java/com/azure/resourcemanager/network/NetworkTests.java diff --git a/sdk/resourcemanager/azure-resourcemanager-network/CHANGELOG.md b/sdk/resourcemanager/azure-resourcemanager-network/CHANGELOG.md index d8c04b54dc833..861d2d251787d 100644 --- a/sdk/resourcemanager/azure-resourcemanager-network/CHANGELOG.md +++ b/sdk/resourcemanager/azure-resourcemanager-network/CHANGELOG.md @@ -8,6 +8,8 @@ ### Bugs Fixed +- Fixed bug that "Microsoft.Network/bastionHosts" resource causes exception on `listNetworkInterfaceIPConfigurations` method in `Subnet` class. + ### Other Changes ## 2.44.0 (2024-10-25) diff --git a/sdk/resourcemanager/azure-resourcemanager-network/assets.json b/sdk/resourcemanager/azure-resourcemanager-network/assets.json index 678c39147e811..6c14900a39551 100644 --- a/sdk/resourcemanager/azure-resourcemanager-network/assets.json +++ b/sdk/resourcemanager/azure-resourcemanager-network/assets.json @@ -2,5 +2,5 @@ "AssetsRepo": "Azure/azure-sdk-assets", "AssetsRepoPrefixPath": "java", "TagPrefix": "java/resourcemanager/azure-resourcemanager-network", - "Tag": "java/resourcemanager/azure-resourcemanager-network_195291e896" + "Tag": "java/resourcemanager/azure-resourcemanager-network_34df1bcb86" } diff --git a/sdk/resourcemanager/azure-resourcemanager-network/src/main/java/com/azure/resourcemanager/network/implementation/SubnetImpl.java b/sdk/resourcemanager/azure-resourcemanager-network/src/main/java/com/azure/resourcemanager/network/implementation/SubnetImpl.java index 8d0ceee8fa2b6..86c268659958a 100644 --- a/sdk/resourcemanager/azure-resourcemanager-network/src/main/java/com/azure/resourcemanager/network/implementation/SubnetImpl.java +++ b/sdk/resourcemanager/azure-resourcemanager-network/src/main/java/com/azure/resourcemanager/network/implementation/SubnetImpl.java @@ -4,7 +4,9 @@ import com.azure.core.management.Region; import com.azure.core.management.SubResource; +import com.azure.core.management.exception.ManagementException; import com.azure.core.util.CoreUtils; +import com.azure.core.util.logging.ClientLogger; import com.azure.resourcemanager.network.fluent.models.IpAddressAvailabilityResultInner; import com.azure.resourcemanager.network.fluent.models.IpConfigurationInner; import com.azure.resourcemanager.network.fluent.models.NetworkSecurityGroupInner; @@ -42,6 +44,8 @@ class SubnetImpl extends ChildResourceImpl implements Subnet, Subnet.Definition, Subnet.UpdateDefinition, Subnet.Update { + private static final ClientLogger LOGGER = new ClientLogger(SubnetImpl.class); + SubnetImpl(SubnetInner inner, NetworkImpl parent) { super(inner, parent); } @@ -253,7 +257,18 @@ public Collection listNetworkInterfaceIPConfigurations() { NetworkInterface nic = nics.get(nicID); if (nic == null) { // NIC not previously found, so ask Azure for it - nic = this.parent().manager().networkInterfaces().getById(nicID); + String resourceType = ResourceUtils.resourceTypeFromResourceId(nicID); + if ("networkInterfaces".equalsIgnoreCase(resourceType)) { + // skip other resource types like "bastionHosts" + try { + nic = this.parent().manager().networkInterfaces().getById(nicID); + } catch (ManagementException e) { + if (e.getResponse().getStatusCode() == 404) { + // NIC not found, ignore this ipConfigRef + LOGGER.warning("Network interface not found '{}'", nicID); + } + } + } } if (nic == null) { diff --git a/sdk/resourcemanager/azure-resourcemanager-network/src/test/java/com/azure/resourcemanager/network/NetworkTests.java b/sdk/resourcemanager/azure-resourcemanager-network/src/test/java/com/azure/resourcemanager/network/NetworkTests.java new file mode 100644 index 0000000000000..9cc18aeb335c2 --- /dev/null +++ b/sdk/resourcemanager/azure-resourcemanager-network/src/test/java/com/azure/resourcemanager/network/NetworkTests.java @@ -0,0 +1,69 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.resourcemanager.network; + +import com.azure.core.management.Region; +import com.azure.core.management.SubResource; +import com.azure.resourcemanager.network.fluent.models.BastionHostInner; +import com.azure.resourcemanager.network.models.BastionHostIpConfiguration; +import com.azure.resourcemanager.network.models.IpAllocationMethod; +import com.azure.resourcemanager.network.models.Network; +import com.azure.resourcemanager.network.models.NicIpConfiguration; +import com.azure.resourcemanager.network.models.PublicIPSkuType; +import com.azure.resourcemanager.network.models.PublicIpAddress; +import com.azure.resourcemanager.network.models.Subnet; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Collection; +import java.util.Collections; + +public class NetworkTests extends NetworkManagementTest { + + private static final Region REGIN = Region.US_WEST3; + + @Test + public void testSubnetNetworkInterfaceIPConfigurationWithBastion() { + String vnetName = generateRandomResourceName("vnet", 15); + String pipName = generateRandomResourceName("pip", 15); + String bastionName = generateRandomResourceName("bastion", 15); + + Network network = networkManager.networks() + .define(vnetName) + .withRegion(REGIN) + .withNewResourceGroup(rgName) + .withAddressSpace("10.0.0.0/24") + .withSubnet("default", "10.0.0.0/25") + .withSubnet("AzureBastionSubnet", "10.0.0.128/25") + .create(); + + PublicIpAddress publicIpBastion = networkManager.publicIpAddresses() + .define(pipName) + .withRegion(REGIN) + .withExistingResourceGroup(rgName) + .withSku(PublicIPSkuType.STANDARD) + .withStaticIP() + .create(); + + networkManager.networks() + .manager() + .serviceClient() + .getBastionHosts() + .createOrUpdate(rgName, bastionName, new BastionHostInner().withLocation(REGIN.toString()) + .withIpConfigurations(Collections.singletonList(new BastionHostIpConfiguration().withName("ipconfig1") + .withSubnet(network.subnets().get("AzureBastionSubnet").innerModel()) + .withPrivateIpAllocationMethod(IpAllocationMethod.DYNAMIC) + .withPublicIpAddress(new SubResource().withId(publicIpBastion.id()))))); + + // get a new instance of the network after creating bastion + network = networkManager.networks().getById(network.id()); + + Assertions.assertEquals(2, network.subnets().size()); + for (Subnet subnet : network.subnets().values()) { + Collection nicIpConfigurations = subnet.listNetworkInterfaceIPConfigurations(); + // no NIC on either of the 2 subnets + Assertions.assertEquals(0, nicIpConfigurations.size()); + } + } +}