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 8, 2024
1 parent 06af125 commit 045ce63
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 72 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_258ece270f"
"Tag": "java/resourcemanager/azure-resourcemanager-network_3b15fe745d"
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@
import com.azure.resourcemanager.resources.fluentcore.arm.models.Resource;
import com.azure.resourcemanager.resources.fluentcore.model.Creatable;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.Set;
import java.util.stream.Collectors;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand All @@ -63,6 +68,9 @@ class NetworkInterfaceImpl
private NetworkSecurityGroup networkSecurityGroup;
/** the delete option of public ip address */
private DeleteOptions publicIpAddressDeleteOption;
/** the name of specified ip config name */
private Map<DeleteOptions, Set<String>> specifiedIpConfigNames;
private boolean isInUpdateIpConfigMode = false;

NetworkInterfaceImpl(String name, NetworkInterfaceInner innerModel, final NetworkManager networkManager) {
super(name, innerModel, networkManager);
Expand Down Expand Up @@ -144,18 +152,21 @@ public NetworkInterfaceImpl withExistingPrimaryNetwork(Network network) {

@Override
public NetworkInterfaceImpl withNewPrimaryPublicIPAddress(Creatable<PublicIpAddress> creatable) {
this.isInUpdateIpConfigMode = true;
this.primaryIPConfiguration().withNewPublicIpAddress(creatable);
return this;
}

@Override
public NetworkInterfaceImpl withNewPrimaryPublicIPAddress() {
this.isInUpdateIpConfigMode = true;
this.primaryIPConfiguration().withNewPublicIpAddress();
return this;
}

@Override
public NetworkInterfaceImpl withNewPrimaryPublicIPAddress(String leafDnsLabel) {
this.isInUpdateIpConfigMode = true;
this.primaryIPConfiguration().withNewPublicIpAddress(leafDnsLabel);
return this;
}
Expand Down Expand Up @@ -199,6 +210,7 @@ public NetworkInterfaceImpl withoutPrimaryPublicIPAddress() {
public NetworkInterfaceImpl withExistingPrimaryPublicIPAddress(PublicIpAddress publicIPAddress) {
this.primaryIPConfiguration().withExistingPublicIpAddress(publicIPAddress);
this.primaryIPConfiguration().withPrivateIpVersion(publicIPAddress.version());
this.isInUpdateIpConfigMode = true;
return this;
}

Expand Down Expand Up @@ -255,6 +267,7 @@ public NetworkInterfaceImpl withoutApplicationSecurityGroup(String applicationSe

@Override
public NicIpConfigurationImpl defineSecondaryIPConfiguration(String name) {
this.isInUpdateIpConfigMode = true;
NicIpConfigurationImpl nicIpConfiguration = prepareNewNicIPConfiguration(name);

// copy application security group from primary to secondary,
Expand All @@ -271,6 +284,7 @@ public NicIpConfigurationImpl defineSecondaryIPConfiguration(String name) {

@Override
public NicIpConfigurationImpl updateIPConfiguration(String name) {
this.isInUpdateIpConfigMode = true;
return (NicIpConfigurationImpl) this.nicIPConfigurations.get(name);
}

Expand Down Expand Up @@ -566,15 +580,45 @@ protected void beforeCreating() {
.withNetworkSecurityGroup(new NetworkSecurityGroupInner().withId(networkSecurityGroup.id()));
}

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

if (Objects.nonNull(this.specifiedIpConfigNames)) {
NicIpConfigurationImpl.deleteOptionConfigurations(this.nicIPConfigurations.values(), this.specifiedIpConfigNames);
this.isInUpdateIpConfigMode = false;
}

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

@Override
public NetworkInterfaceImpl withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions) {
this.publicIpAddressDeleteOption = deleteOptions;
public NetworkInterfaceImpl withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions, String... ipConfigNames) {
Set<String> ipConfigNameSet = Arrays.stream(ipConfigNames).map(ipConfigName -> ipConfigName.toLowerCase(Locale.ROOT)).collect(Collectors.toSet());
if (!isInCreateMode() && !isInUpdateIpConfigMode) {
this.nicIPConfigurations.values()
.stream().filter(nicIpConfiguration ->
(ipConfigNameSet.isEmpty() || ipConfigNameSet.contains(nicIpConfiguration.name().toLowerCase(Locale.ROOT)))
&& Objects.nonNull(nicIpConfiguration.innerModel().publicIpAddress()))
.forEach(nicIpConfiguration -> {
nicIpConfiguration.innerModel().publicIpAddress().withDeleteOption(deleteOptions);
});
} else {
if (Objects.isNull(this.specifiedIpConfigNames)) {
this.specifiedIpConfigNames = new LinkedHashMap<>();
}
if (this.specifiedIpConfigNames.containsKey(deleteOptions)) {
this.specifiedIpConfigNames.get(deleteOptions).addAll(ipConfigNameSet);
} else {
this.specifiedIpConfigNames.put(deleteOptions, ipConfigNameSet);
}
}
return this;
}

@Override
public NetworkInterfaceImpl update() {
this.specifiedIpConfigNames = new LinkedHashMap<>();
this.isInUpdateIpConfigMode = false;
return super.update();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/** Implementation for NicIPConfiguration and its create and update interfaces. */
class NicIpConfigurationImpl extends NicIpConfigurationBaseImpl<NetworkInterfaceImpl, NetworkInterface>
Expand Down Expand Up @@ -266,11 +269,20 @@ 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));
protected static void deleteOptionConfigurations(Collection<NicIpConfiguration> nicIPConfigurations, Map<DeleteOptions, Set<String>> specifiedIpConfigNames) {
if (!specifiedIpConfigNames.isEmpty()) {
specifiedIpConfigNames.keySet().forEach(deleteOptions -> {
Set<String> ipConfigNameSet = specifiedIpConfigNames.get(deleteOptions);
if (!ipConfigNameSet.isEmpty()) {
nicIPConfigurations
.stream().filter(nicIpConfig -> ipConfigNameSet.contains(nicIpConfig.name().toLowerCase(Locale.ROOT))
&& Objects.nonNull(nicIpConfig.innerModel().publicIpAddress()))
.forEach(nicIpConfig -> nicIpConfig.innerModel().publicIpAddress().withDeleteOption(deleteOptions));
} else {
nicIPConfigurations.stream().filter(nicIpConfig -> Objects.nonNull(nicIpConfig.innerModel().publicIpAddress()))
.forEach(nicIpConfig -> nicIpConfig.innerModel().publicIpAddress().withDeleteOption(deleteOptions));
}
});
}
}

Expand Down Expand Up @@ -361,37 +373,6 @@ 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 @@ -262,9 +262,10 @@ 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
* @param ipConfigNames the names of specified ip configurations
* @return the next stage of the update
*/
WithCreate withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions);
WithCreate withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions, String... ipConfigNames);
}

/**
Expand Down Expand Up @@ -595,9 +596,10 @@ interface WithPublicIPAddressDeleteOptions {
* Sets delete options for public ip address.
*
* @param deleteOptions the delete options for primary network interfaces
* @param ipConfigNames the names of specified ip configurations
* @return the next stage of the update
*/
Update withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions);
Update withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions, String... ipConfigNames);
}
}

Expand Down
Loading

0 comments on commit 045ce63

Please sign in to comment.