Skip to content

Commit

Permalink
Fix DeleteOptions on Public Ip Address (issue#38806 NetworkInterface …
Browse files Browse the repository at this point in the history
…fix only)
  • Loading branch information
v-hongli1 committed Mar 6, 2024
1 parent 6e0adf5 commit e7293ee
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 4 deletions.
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_271269e41a"
"Tag": "java/resourcemanager/azure-resourcemanager-network_258ece270f"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.azure.resourcemanager.network.NetworkManager;
import com.azure.resourcemanager.network.fluent.models.ApplicationSecurityGroupInner;
import com.azure.resourcemanager.network.models.ApplicationSecurityGroup;
import com.azure.resourcemanager.network.models.DeleteOptions;
import com.azure.resourcemanager.network.models.IpAllocationMethod;
import com.azure.resourcemanager.network.models.LoadBalancer;
import com.azure.resourcemanager.network.models.Network;
Expand Down Expand Up @@ -60,6 +61,8 @@ class NetworkInterfaceImpl
private NetworkSecurityGroup existingNetworkSecurityGroupToAssociate;
/** cached related resources. */
private NetworkSecurityGroup networkSecurityGroup;
/** the delete option of public ip address */
private DeleteOptions publicIpAddressDeleteOption;

NetworkInterfaceImpl(String name, NetworkInterfaceInner innerModel, final NetworkManager networkManager) {
super(name, innerModel, networkManager);
Expand Down Expand Up @@ -563,9 +566,15 @@ protected void beforeCreating() {
.withNetworkSecurityGroup(new NetworkSecurityGroupInner().withId(networkSecurityGroup.id()));
}

NicIpConfigurationImpl.ensureConfigurations(this.nicIPConfigurations.values());
NicIpConfigurationImpl.ensureConfigurations(this.nicIPConfigurations.values(), this.publicIpAddressDeleteOption);

// Reset and update IP configs
this.innerModel().withIpConfigurations(innersFromWrappers(this.nicIPConfigurations.values()));
}

@Override
public NetworkInterfaceImpl withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions) {
this.publicIpAddressDeleteOption = deleteOptions;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.azure.resourcemanager.network.models.ApplicationGateway;
import com.azure.resourcemanager.network.models.ApplicationGatewayBackendAddressPool;
import com.azure.resourcemanager.network.models.ApplicationSecurityGroup;
import com.azure.resourcemanager.network.models.DeleteOptions;
import com.azure.resourcemanager.network.models.IpAllocationMethod;
import com.azure.resourcemanager.network.models.IpVersion;
import com.azure.resourcemanager.network.models.LoadBalancer;
Expand Down Expand Up @@ -265,6 +266,14 @@ protected static void ensureConfigurations(Collection<NicIpConfiguration> nicIPC
}
}

protected static void ensureConfigurations(Collection<NicIpConfiguration> nicIPConfigurations, DeleteOptions deleteOptions) {
for (NicIpConfiguration nicIPConfiguration : nicIPConfigurations) {
NicIpConfigurationImpl config = (NicIpConfigurationImpl) nicIPConfiguration;
config.innerModel().withSubnet(config.subnetToAssociate());
config.innerModel().withPublicIpAddress(config.publicIPToAssociate(deleteOptions));
}
}

// Creates a creatable public IP address definition with the given name and DNS label.
private Creatable<PublicIpAddress> prepareCreatablePublicIP(String name, String leafDnsLabel) {
PublicIpAddress.DefinitionStages.WithGroup definitionWithGroup =
Expand Down Expand Up @@ -352,6 +361,37 @@ private PublicIpAddressInner publicIPToAssociate() {
}
}

/**
* Get the SubResource instance representing a public IP that needs to be associated with the IP configuration.
*
* <p>null will be returned if withoutPublicIP() is specified in the update fluent chain or user did't opt for
* public IP in create fluent chain. In case of update chain, if withoutPublicIP(..) is not specified then existing
* associated (if any) public IP will be returned.
*
* @return public IP SubResource
*/
private PublicIpAddressInner publicIPToAssociate(DeleteOptions deleteOptions) {
String pipId = null;
if (this.removePrimaryPublicIPAssociation) {
return null;
} else if (this.creatablePublicIPKey != null) {
pipId = ((PublicIpAddress) this.parent().createdDependencyResource(this.creatablePublicIPKey)).id();
} else if (this.existingPublicIPAddressIdToAssociate != null) {
pipId = this.existingPublicIPAddressIdToAssociate;
}

if (pipId != null) {
return new PublicIpAddressInner().withId(pipId).withDeleteOption(deleteOptions);
} else if (!this.isInCreateMode) {
if (Objects.nonNull(this.innerModel().publicIpAddress())) {
return this.innerModel().publicIpAddress().withDeleteOption(deleteOptions);
}
return this.innerModel().publicIpAddress();
} else {
return null;
}
}

@Override
public NicIpConfigurationImpl withPrivateIpVersion(IpVersion ipVersion) {
this.innerModel().withPrivateIpAddressVersion(ipVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@ interface WithAcceleratedNetworking {
WithCreate withAcceleratedNetworking();
}

/** The stage of the definition allowing to specify delete options for the public ip address. */
interface WithPublicIPAddressDeleteOptions {
/**
* Sets delete options for public ip address.
*
* @param deleteOptions the delete options for primary network interfaces
* @return the next stage of the definition
*/
WithCreate withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions);
}

/**
* The stage of the network interface definition which contains all the minimum required inputs for the resource
* to be created, but also allows for any other optional settings to be specified.
Expand All @@ -268,7 +279,8 @@ interface WithCreate
WithSecondaryIPConfiguration,
WithAcceleratedNetworking,
WithLoadBalancer,
WithApplicationSecurityGroup {
WithApplicationSecurityGroup,
WithPublicIPAddressDeleteOptions {
/**
* Enables IP forwarding in the network interface.
*
Expand Down Expand Up @@ -576,6 +588,17 @@ interface WithLoadBalancer {
*/
Update withoutLoadBalancerInboundNatRules();
}

/** Th stage of the network interface update allowing to specify delete options for the public ip address. */
interface WithPublicIPAddressDeleteOptions {
/**
* Sets delete options for public ip address.
*
* @param deleteOptions the delete options for primary network interfaces
* @return the next stage of the update
*/
Update withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions);
}
}

/** The template for an update operation, containing all the settings that can be modified. */
Expand All @@ -591,6 +614,7 @@ interface Update
UpdateStages.WithIPConfiguration,
UpdateStages.WithLoadBalancer,
UpdateStages.WithAcceleratedNetworking,
UpdateStages.WithApplicationSecurityGroup {
UpdateStages.WithApplicationSecurityGroup,
UpdateStages.WithPublicIPAddressDeleteOptions {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.core.management.Region;
import com.azure.resourcemanager.network.fluent.models.NatGatewayInner;
import com.azure.resourcemanager.network.models.ApplicationSecurityGroup;
import com.azure.resourcemanager.network.models.DeleteOptions;
import com.azure.resourcemanager.network.models.NatGatewaySku;
import com.azure.resourcemanager.network.models.NatGatewaySkuName;
import com.azure.resourcemanager.network.models.Network;
Expand Down Expand Up @@ -509,6 +510,148 @@ public void canAssociateNatGateway() {
Assertions.assertEquals(gateway2.id(), subnet2.natGatewayId());
}

@Test
public void canCreateMultipleIpConfigurationsWithDeleteOption() {
String subnetName = generateRandomResourceName("subnet-", 15);
Network vnet = networkManager.networks()
.define(generateRandomResourceName("vnet-", 15))
.withRegion(Region.US_EAST)
.withNewResourceGroup(rgName)
.withAddressSpace("10.0.0.0/28")
.withSubnet(subnetName, "10.0.0.0/28")
.create();

NetworkInterface nic = networkManager.networkInterfaces()
.define(generateRandomResourceName("nic-", 15))
.withRegion(Region.US_EAST)
.withNewResourceGroup(rgName)
.withExistingPrimaryNetwork(vnet)
.withSubnet(subnetName)
.withPrimaryPrivateIPAddressDynamic()
.withNewPrimaryPublicIPAddress()
.defineSecondaryIPConfiguration("secondary")
.withExistingNetwork(vnet)
.withSubnet(subnetName)
.withPrivateIpAddressDynamic()
.withNewPublicIpAddress()
.attach()
.withPublicIPAddressDeleteOptions(DeleteOptions.DELETE)
.create();

nic.refresh();
List<DeleteOptions> deleteOptionsList = new ArrayList<>();
nic.innerModel().ipConfigurations().forEach(ipConfig -> deleteOptionsList.add(ipConfig.publicIpAddress().deleteOption()));

Assertions.assertEquals(2, deleteOptionsList.size());
deleteOptionsList.forEach(deleteOptions -> Assertions.assertEquals(DeleteOptions.DELETE, deleteOptions));
}

@Test
public void canUpdateDeleteOptionWithDefineSecondaryIpConfiguration() {
String subnetName = generateRandomResourceName("subnet-", 15);
Network vnet = networkManager.networks()
.define(generateRandomResourceName("vnet-", 15))
.withRegion(Region.US_EAST)
.withNewResourceGroup(rgName)
.withAddressSpace("10.0.0.0/28")
.withSubnet(subnetName, "10.0.0.0/28")
.create();

NetworkInterface nic = networkManager.networkInterfaces()
.define(generateRandomResourceName("nic-", 15))
.withRegion(Region.US_EAST)
.withNewResourceGroup(rgName)
.withExistingPrimaryNetwork(vnet)
.withSubnet(subnetName)
.withPrimaryPrivateIPAddressDynamic()
.withNewPrimaryPublicIPAddress()
.create();

nic.update()
.defineSecondaryIPConfiguration("secondary")
.withExistingNetwork(vnet)
.withSubnet(subnetName)
.withPrivateIpAddressDynamic()
.withNewPublicIpAddress()
.attach()
.withPublicIPAddressDeleteOptions(DeleteOptions.DELETE)
.apply();

nic.refresh();
List<DeleteOptions> deleteOptionsList = new ArrayList<>();
nic.innerModel().ipConfigurations().stream().filter(ipConfig -> "secondary".equalsIgnoreCase(ipConfig.name()))
.forEach(ipConfig -> deleteOptionsList.add(ipConfig.publicIpAddress().deleteOption()));

Assertions.assertEquals(1, deleteOptionsList.size());
deleteOptionsList.forEach(deleteOptions -> Assertions.assertEquals(DeleteOptions.DELETE, deleteOptions));
}

@Test
public void canUpdateDeleteOptionWithPrimaryIpConfig() {
String subnetName = generateRandomResourceName("subnet-", 15);
Network vnet = networkManager.networks()
.define(generateRandomResourceName("vnet-", 15))
.withRegion(Region.US_EAST)
.withNewResourceGroup(rgName)
.withAddressSpace("10.0.0.0/28")
.withSubnet(subnetName, "10.0.0.0/28")
.create();

NetworkInterface nic = networkManager.networkInterfaces()
.define(generateRandomResourceName("nic-", 15))
.withRegion(Region.US_EAST)
.withNewResourceGroup(rgName)
.withExistingPrimaryNetwork(vnet)
.withSubnet(subnetName)
.withPrimaryPrivateIPAddressDynamic()
.withNewPrimaryPublicIPAddress()
.create();

nic.update().withNewPrimaryPublicIPAddress().withPublicIPAddressDeleteOptions(DeleteOptions.DELETE).apply();
nic.refresh();
Assertions.assertEquals(DeleteOptions.DELETE, nic.primaryIPConfiguration().innerModel().publicIpAddress().deleteOption());
}

@Test
public void canUpdateDeleteOptionWithUpdateIPConfiguration() {
String subnetName = generateRandomResourceName("subnet-", 15);
Network vnet = networkManager.networks()
.define(generateRandomResourceName("vnet-", 15))
.withRegion(Region.US_EAST)
.withNewResourceGroup(rgName)
.withAddressSpace("10.0.0.0/28")
.withSubnet(subnetName, "10.0.0.0/28")
.create();

NetworkInterface nic = networkManager.networkInterfaces()
.define(generateRandomResourceName("nic-", 15))
.withRegion(Region.US_EAST)
.withNewResourceGroup(rgName)
.withExistingPrimaryNetwork(vnet)
.withSubnet(subnetName)
.withPrimaryPrivateIPAddressDynamic()
.withNewPrimaryPublicIPAddress()
.defineSecondaryIPConfiguration("secondary")
.withExistingNetwork(vnet)
.withSubnet(subnetName)
.withPrivateIpAddressDynamic()
.withNewPublicIpAddress()
.attach()
.create();

nic.update().updateIPConfiguration("secondary")
.withNewPublicIpAddress().parent()
.withPublicIPAddressDeleteOptions(DeleteOptions.DELETE)
.apply();
nic.refresh();

List<DeleteOptions> deleteOptionsList = new ArrayList<>();
nic.innerModel().ipConfigurations().stream().filter(ipConfig -> "secondary".equalsIgnoreCase(ipConfig.name()))
.forEach(ipConfig -> deleteOptionsList.add(ipConfig.publicIpAddress().deleteOption()));
Assertions.assertEquals(1, deleteOptionsList.size());
deleteOptionsList.forEach(deleteOptions -> Assertions.assertEquals(DeleteOptions.DELETE, deleteOptions));
}

private NatGatewayInner createNatGateway() {
String natGatewayName = generateRandomResourceName("natgw", 10);
return networkManager.serviceClient()
Expand Down

0 comments on commit e7293ee

Please sign in to comment.