Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DeleteOptions on Public Ip Address (issue#38806) #38987

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-compute",
"Tag": "java/resourcemanager/azure-resourcemanager-compute_f43c5950dd"
"Tag": "java/resourcemanager/azure-resourcemanager-compute_71ff791c94"
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@
import com.azure.resourcemanager.network.NetworkManager;
import com.azure.resourcemanager.network.models.Network;
import com.azure.resourcemanager.network.models.NetworkInterface;
import com.azure.resourcemanager.network.models.NicIpConfigurationBase;
import com.azure.resourcemanager.network.models.PublicIPSkuType;
import com.azure.resourcemanager.network.models.PublicIpAddress;
import com.azure.resourcemanager.resources.fluentcore.arm.AvailabilityZoneId;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceId;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceUtils;
import com.azure.resourcemanager.resources.fluentcore.arm.models.implementation.GroupableResourceImpl;
import com.azure.resourcemanager.resources.fluentcore.model.Accepted;
import com.azure.resourcemanager.resources.fluentcore.model.Appliable;
import com.azure.resourcemanager.resources.fluentcore.model.Creatable;
import com.azure.resourcemanager.resources.fluentcore.model.Indexable;
import com.azure.resourcemanager.resources.fluentcore.model.implementation.AcceptedImpl;
Expand Down Expand Up @@ -2308,6 +2310,74 @@ public VirtualMachineImpl withDataDisksDeleteOptions(DeleteOptions deleteOptions
return this;
}

@Override
public VirtualMachineImpl withPrimaryNicPrimaryPipDeleteOption(DeleteOptions deleteOptions) {
if (this.innerModel().networkProfile() != null
&& this.innerModel().networkProfile().networkInterfaces() != null) {
this.innerModel().networkProfile().networkInterfaces()
.stream().filter(NetworkInterfaceReference::primary)
.forEach(nicReference -> {
NetworkInterface nic = networkManager.networkInterfaces().getById(nicReference.id());
nic.ipConfigurations().values()
.stream().filter(NicIpConfigurationBase::isPrimary)
.forEach(ipConfig -> {
PublicIpAddress publicIpAddress = networkManager.publicIpAddresses().getById(ipConfig.innerModel().publicIpAddress().id());
Appliable<NetworkInterface> appliable = nic.update().withSpecifiedNicSpecifiedPipDeleteOption(convertDeleteOptionType(deleteOptions), publicIpAddress, ipConfig.name());
this.addDependency(appliable);
});
});
}
return this;
}

@Override
public VirtualMachineImpl withSpecifiedNicPrimaryPIpDeleteOption(DeleteOptions deleteOptions, String... nicIds) {
if (this.innerModel().networkProfile() != null
&& this.innerModel().networkProfile().networkInterfaces() != null) {
Set<String> nicIdSet = Arrays.stream(nicIds).map(nicId -> nicId.toLowerCase(Locale.ROOT)).collect(Collectors.toSet());
this.innerModel().networkProfile().networkInterfaces()
.stream().filter(nicReference -> nicIdSet.isEmpty()
|| nicIdSet.contains(nicReference.id().toLowerCase(Locale.ROOT)))
.forEach(nicReference -> {
NetworkInterface nic = networkManager.networkInterfaces().getById(nicReference.id());
nic.ipConfigurations().values()
.stream().filter(NicIpConfigurationBase::isPrimary)
.forEach(ipConfig -> {
PublicIpAddress publicIpAddress = networkManager.publicIpAddresses().getById(ipConfig.innerModel().publicIpAddress().id());
Appliable<NetworkInterface> appliable =
nic.update().withSpecifiedNicSpecifiedPipDeleteOption(
convertDeleteOptionType(deleteOptions), publicIpAddress, ipConfig.name());
this.addDependency(appliable);
});
});
}
return this;
}

@Override
public VirtualMachineImpl withSpecifiedNicSpecifiedPIpDeleteOption(DeleteOptions deleteOptions, String nicId, String... ipConfigIds) {
if (this.innerModel().networkProfile() != null
&& this.innerModel().networkProfile().networkInterfaces() != null) {
this.innerModel().networkProfile().networkInterfaces()
.stream().filter(nicReference -> nicId.equalsIgnoreCase(nicReference.id()))
.forEach(nicReference -> {
NetworkInterface nic = networkManager.networkInterfaces().getById(nicReference.id());
Set<String> ipConfigIdSet = Arrays.stream(ipConfigIds).map(ipConfigId -> ipConfigId.toLowerCase(Locale.ROOT)).collect(Collectors.toSet());
nic.ipConfigurations().values()
.stream().filter(ipConfig -> ipConfigIdSet.isEmpty()
|| ipConfigIdSet.contains(ipConfig.innerModel().id().toLowerCase(Locale.ROOT)))
.forEach(ipConfig -> {
PublicIpAddress publicIpAddress = networkManager.publicIpAddresses().getById(ipConfig.innerModel().publicIpAddress().id());
Appliable<NetworkInterface> appliable =
nic.update().withSpecifiedNicSpecifiedPipDeleteOption(
convertDeleteOptionType(deleteOptions), publicIpAddress, ipConfig.name());
this.addDependency(appliable);
});
});
}
return this;
}

@Override
public VirtualMachineImpl withDataDisksDeleteOptions(DeleteOptions deleteOptions) {
this.innerModel().storageProfile().dataDisks().forEach(
Expand Down Expand Up @@ -2942,6 +3012,12 @@ public VirtualMachineImpl withoutEncryptionAtHost() {
return this;
}

@Override
public VirtualMachine.DefinitionStages.WithCreate withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions) {
this.nicDefinitionWithCreate.withPublicIPAddressDeleteOptions(convertDeleteOptionType(deleteOptions));
return this;
}

/** Class to manage Data disk collection. */
private class ManagedDataDiskCollection {
private final Map<String, DataDisk> newDisksToAttach = new HashMap<>();
Expand Down Expand Up @@ -3366,4 +3442,15 @@ private void enableDisable(boolean enable) {
}
}
}

private com.azure.resourcemanager.network.models.DeleteOptions convertDeleteOptionType(DeleteOptions deleteOptions) {
if (Objects.nonNull(deleteOptions)) {
if (DeleteOptions.DELETE.equals(deleteOptions)) {
return com.azure.resourcemanager.network.models.DeleteOptions.DELETE;
} else {
return com.azure.resourcemanager.network.models.DeleteOptions.DETACH;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,17 @@ interface WithNetworkInterfaceDeleteOptions {
WithCreate withPrimaryNetworkInterfaceDeleteOptions(DeleteOptions deleteOptions);
}

/** 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 VM definition allowing to specify additional capacities. */
interface WithAdditionalCapacities {
/**
Expand Down Expand Up @@ -1988,6 +1999,7 @@ interface WithCreate
DefinitionStages.WithLicenseType,
DefinitionStages.WithAdditionalCapacities,
DefinitionStages.WithNetworkInterfaceDeleteOptions,
DefinitionStages.WithPublicIPAddressDeleteOptions,
DefinitionStages.WithEphemeralOSDisk,
DefinitionStages.WithScaleSet,
DefinitionStages.WithSecurityTypes,
Expand Down Expand Up @@ -2610,6 +2622,33 @@ interface WithDeleteOptions {
*/
Update withDataDisksDeleteOptions(DeleteOptions deleteOptions, Integer... luns);

/**
* Update delete option for the primary public ip address of the primary network interface.
*
* @param deleteOptions what happens to the public IP address when the VM using it is deleted
* @return the next stage of the update
*/
Update withPrimaryNicPrimaryPipDeleteOption(DeleteOptions deleteOptions);

/**
* Update delete option for the primary public ip address of the specified network interface.
*
* @param deleteOptions what happens to the public IP address when the VM using it is deleted
* @param nicIds resource Ids of the specified network interface
* @return the next stage of the update
*/
Update withSpecifiedNicPrimaryPIpDeleteOption(DeleteOptions deleteOptions, String... nicIds);

/**
* Update delete option for the specified public ip address of the specified network interface.
*
* @param deleteOptions what happens to the public IP address when the VM using it is deleted
* @param nicId resource id of the specified network interfac
* @param ipConfigIds resource Ids of specified network interface ip configs
* @return the next stage of the update
*/
Update withSpecifiedNicSpecifiedPIpDeleteOption(DeleteOptions deleteOptions, String nicId, String... ipConfigIds);
Comment on lines +2625 to +2650
Copy link
Contributor

@XiaofeiCao XiaofeiCao Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether user really needs these APIs.

IMHO, two types are needed:

  1. withPublicIPAddressDeleteOptions(DeleteOptions deleteOptions) to update all attached PublicIpAddress'es deleteOption
  2. withPublicIPAddressDeleteOption(PublicIpAddress(/String) publicIpAddress(id)), like what user specifies in the creation flow.


/**
* Specifies delete options for all the existing data disk attached to the VM.
* <p>This operation only affects existing <strong>attached</strong> data disks. Any newly-attached data disks
Expand Down
Loading