Skip to content

Commit

Permalink
mgmt, network, bug fix on bastion resource in subnet ipconf (#43082)
Browse files Browse the repository at this point in the history
* mgmt, network, bug fix on bastion resource in subnet ipconf

* ignore the NIC if not found

* add test case
  • Loading branch information
weidongxu-microsoft authored Nov 26, 2024
1 parent 8d891ca commit 4e80939
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +44,8 @@ class SubnetImpl extends ChildResourceImpl<SubnetInner, NetworkImpl, Network>
implements Subnet, Subnet.Definition<Network.DefinitionStages.WithCreateAndSubnet>,
Subnet.UpdateDefinition<Network.Update>, Subnet.Update {

private static final ClientLogger LOGGER = new ClientLogger(SubnetImpl.class);

SubnetImpl(SubnetInner inner, NetworkImpl parent) {
super(inner, parent);
}
Expand Down Expand Up @@ -253,7 +257,18 @@ public Collection<NicIpConfiguration> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<NicIpConfiguration> nicIpConfigurations = subnet.listNetworkInterfaceIPConfigurations();
// no NIC on either of the 2 subnets
Assertions.assertEquals(0, nicIpConfigurations.size());
}
}
}

0 comments on commit 4e80939

Please sign in to comment.