Skip to content

Commit a78b351

Browse files
authored
Remove NIC and IP address on deletion using Azure built-in functionality (#405)
1 parent b324c30 commit a78b351

12 files changed

Lines changed: 58 additions & 104 deletions

src/main/java/com/microsoft/azure/vmagent/AzureVMManagementServiceDelegate.java

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,9 @@ private void addPublicIPResourceNode(
802802
publicIPIdNode.put("id", "[resourceId('Microsoft.Network/publicIPAddresses', concat("
803803
+ ipName
804804
+ "))]");
805-
// propertiesNode.putObject("publicIPAddress").put
805+
ObjectNode ipAddressPropertiesNode = MAPPER.createObjectNode();
806+
ipAddressPropertiesNode.put("deleteOption", "Delete");
807+
publicIPIdNode.set("properties", ipAddressPropertiesNode);
806808
propertiesNode.set("publicIPAddress", publicIPIdNode);
807809
break;
808810
}
@@ -1834,14 +1836,7 @@ public void terminateVirtualMachine(
18341836
if (!Constants.ERROR_CODE_RESOURCE_NF.equalsIgnoreCase(e.getMessage())) {
18351837
throw AzureCloudException.create(e);
18361838
}
1837-
} finally {
1838-
LOGGER.log(Level.INFO, "Clean operation starting for {0} NIC and IP", vmName);
1839-
executionEngine.executeAsync((Callable<Void>) () -> {
1840-
removeIPName(resourceGroupName, vmName, usePrivateIP);
1841-
return null;
1842-
}, new NoRetryStrategy());
18431839
}
1844-
18451840
}
18461841

18471842
public void removeImage(AzureResourceManager azureClient, String vmName, String resourceGroupName) {
@@ -1880,33 +1875,6 @@ public void removeStorageBlob(URI blobURI, String resourceGroupName) throws Exce
18801875
}
18811876
}
18821877

1883-
/**
1884-
* Remove the IP name.
1885-
*
1886-
*/
1887-
public void removeIPName(String resourceGroupName,
1888-
String vmName,
1889-
boolean usePrivateIP) {
1890-
final String nic = vmName + "NIC";
1891-
try {
1892-
LOGGER.log(Level.INFO, "Remove NIC {0}", nic);
1893-
azureClient.networkInterfaces().deleteByResourceGroup(resourceGroupName, nic);
1894-
} catch (Exception e) {
1895-
LOGGER.log(Level.WARNING, "Exception while deleting NIC", e);
1896-
}
1897-
1898-
final String ip = vmName + "IPName";
1899-
try {
1900-
// Does not attempt to remove public IP if private IP is used because public IP does not exist
1901-
if (!usePrivateIP) {
1902-
LOGGER.log(Level.INFO, "Remove IP {0}", ip);
1903-
azureClient.publicIpAddresses().deleteByResourceGroup(resourceGroupName, ip);
1904-
}
1905-
} catch (Exception e) {
1906-
LOGGER.log(Level.WARNING, "Exception while deleting IPName", e);
1907-
}
1908-
}
1909-
19101878
/**
19111879
* Restarts Azure virtual machine.
19121880
*

src/main/resources/customImageTemplate.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"resources": [
2121
{
22-
"apiVersion": "2019-04-01",
22+
"apiVersion": "2022-07-01",
2323
"type": "Microsoft.Network/networkInterfaces",
2424
"name": "[concat(variables('vmName'), copyIndex(), 'NIC')]",
2525
"location": "[variables('location')]",

src/main/resources/customImageTemplateWithManagedDisk.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
}
4141
},
4242
{
43-
"apiVersion": "2019-04-01",
43+
"apiVersion": "2022-07-01",
4444
"type": "Microsoft.Network/networkInterfaces",
4545
"name": "[concat(variables('vmName'), copyIndex(), 'NIC')]",
4646
"location": "[variables('location')]",
@@ -68,7 +68,7 @@
6868
}
6969
},
7070
{
71-
"apiVersion": "2020-06-01",
71+
"apiVersion": "2022-11-01",
7272
"type": "Microsoft.Compute/virtualMachines",
7373
"name": "[concat(variables('vmName'), copyIndex())]",
7474
"location": "[variables('location')]",
@@ -105,7 +105,10 @@
105105
"networkProfile": {
106106
"networkInterfaces": [
107107
{
108-
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]"
108+
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]",
109+
"properties": {
110+
"deleteOption": "Delete"
111+
}
109112
}
110113
]
111114
}

src/main/resources/customImageTemplateWithScript.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"resources": [
2929
{
30-
"apiVersion": "2019-04-01",
30+
"apiVersion": "2022-07-01",
3131
"type": "Microsoft.Network/networkInterfaces",
3232
"name": "[concat(variables('vmName'), copyIndex(), 'NIC')]",
3333
"location": "[variables('location')]",
@@ -55,7 +55,7 @@
5555
}
5656
},
5757
{
58-
"apiVersion": "2020-06-01",
58+
"apiVersion": "2022-11-01",
5959
"type": "Microsoft.Compute/virtualMachines",
6060
"name": "[concat(variables('vmName'), copyIndex())]",
6161
"location": "[variables('location')]",
@@ -93,7 +93,10 @@
9393
"networkProfile": {
9494
"networkInterfaces": [
9595
{
96-
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]"
96+
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]",
97+
"properties": {
98+
"deleteOption": "Delete"
99+
}
97100
}
98101
]
99102
}

src/main/resources/customImageTemplateWithScriptAndManagedDisk.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
}
4949
},
5050
{
51-
"apiVersion": "2019-04-01",
51+
"apiVersion": "2022-07-01",
5252
"type": "Microsoft.Network/networkInterfaces",
5353
"name": "[concat(variables('vmName'), copyIndex(), 'NIC')]",
5454
"location": "[variables('location')]",
@@ -76,7 +76,7 @@
7676
}
7777
},
7878
{
79-
"apiVersion": "2020-06-01",
79+
"apiVersion": "2022-11-01",
8080
"type": "Microsoft.Compute/virtualMachines",
8181
"name": "[concat(variables('vmName'), copyIndex())]",
8282
"location": "[variables('location')]",
@@ -113,7 +113,10 @@
113113
"networkProfile": {
114114
"networkInterfaces": [
115115
{
116-
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]"
116+
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]",
117+
"properties": {
118+
"deleteOption": "Delete"
119+
}
117120
}
118121
]
119122
}

src/main/resources/referenceImageIDTemplateWithManagedDisk.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"resources": [
2222
{
23-
"apiVersion": "2019-04-01",
23+
"apiVersion": "2022-07-01",
2424
"type": "Microsoft.Network/networkInterfaces",
2525
"name": "[concat(variables('vmName'), copyIndex(), 'NIC')]",
2626
"location": "[variables('location')]",
@@ -48,7 +48,7 @@
4848
}
4949
},
5050
{
51-
"apiVersion": "2020-06-01",
51+
"apiVersion": "2022-11-01",
5252
"type": "Microsoft.Compute/virtualMachines",
5353
"name": "[concat(variables('vmName'), copyIndex())]",
5454
"location": "[variables('location')]",
@@ -84,7 +84,10 @@
8484
"networkProfile": {
8585
"networkInterfaces": [
8686
{
87-
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]"
87+
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]",
88+
"properties": {
89+
"deleteOption": "Delete"
90+
}
8891
}
8992
]
9093
}

src/main/resources/referenceImageIDTemplateWithScriptAndManagedDisk.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"resources": [
3030
{
31-
"apiVersion": "2019-04-01",
31+
"apiVersion": "2022-07-01",
3232
"type": "Microsoft.Network/networkInterfaces",
3333
"name": "[concat(variables('vmName'), copyIndex(), 'NIC')]",
3434
"location": "[variables('location')]",
@@ -56,7 +56,7 @@
5656
}
5757
},
5858
{
59-
"apiVersion": "2020-06-01",
59+
"apiVersion": "2022-11-01",
6060
"type": "Microsoft.Compute/virtualMachines",
6161
"name": "[concat(variables('vmName'), copyIndex())]",
6262
"location": "[variables('location')]",
@@ -92,7 +92,10 @@
9292
"networkProfile": {
9393
"networkInterfaces": [
9494
{
95-
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]"
95+
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]",
96+
"properties": {
97+
"deleteOption": "Delete"
98+
}
9699
}
97100
]
98101
}

src/main/resources/referenceImageTemplate.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"resources": [
2222
{
23-
"apiVersion": "2019-04-01",
23+
"apiVersion": "2022-07-01",
2424
"type": "Microsoft.Network/networkInterfaces",
2525
"name": "[concat(variables('vmName'), copyIndex(), 'NIC')]",
2626
"location": "[variables('location')]",
@@ -48,7 +48,7 @@
4848
}
4949
},
5050
{
51-
"apiVersion": "2020-06-01",
51+
"apiVersion": "2022-11-01",
5252
"type": "Microsoft.Compute/virtualMachines",
5353
"name": "[concat(variables('vmName'), copyIndex())]",
5454
"location": "[variables('location')]",
@@ -88,7 +88,10 @@
8888
"networkProfile": {
8989
"networkInterfaces": [
9090
{
91-
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]"
91+
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]",
92+
"properties": {
93+
"deleteOption": "Delete"
94+
}
9295
}
9396
]
9497
}

src/main/resources/referenceImageTemplateWithManagedDisk.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"resources": [
2222
{
23-
"apiVersion": "2019-04-01",
23+
"apiVersion": "2022-07-01",
2424
"type": "Microsoft.Network/networkInterfaces",
2525
"name": "[concat(variables('vmName'), copyIndex(), 'NIC')]",
2626
"location": "[variables('location')]",
@@ -48,7 +48,7 @@
4848
}
4949
},
5050
{
51-
"apiVersion": "2020-06-01",
51+
"apiVersion": "2022-11-01",
5252
"type": "Microsoft.Compute/virtualMachines",
5353
"name": "[concat(variables('vmName'), copyIndex())]",
5454
"location": "[variables('location')]",
@@ -87,7 +87,10 @@
8787
"networkProfile": {
8888
"networkInterfaces": [
8989
{
90-
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]"
90+
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]",
91+
"properties": {
92+
"deleteOption": "Delete"
93+
}
9194
}
9295
]
9396
}

src/main/resources/referenceImageTemplateWithScript.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"resources": [
3030
{
31-
"apiVersion": "2019-04-01",
31+
"apiVersion": "2022-07-01",
3232
"type": "Microsoft.Network/networkInterfaces",
3333
"name": "[concat(variables('vmName'), copyIndex(), 'NIC')]",
3434
"location": "[variables('location')]",
@@ -56,7 +56,7 @@
5656
}
5757
},
5858
{
59-
"apiVersion": "2020-06-01",
59+
"apiVersion": "2022-11-01",
6060
"type": "Microsoft.Compute/virtualMachines",
6161
"name": "[concat(variables('vmName'), copyIndex())]",
6262
"location": "[variables('location')]",
@@ -96,7 +96,10 @@
9696
"networkProfile": {
9797
"networkInterfaces": [
9898
{
99-
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]"
99+
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), copyIndex(), 'NIC'))]",
100+
"properties": {
101+
"deleteOption": "Delete"
102+
}
100103
}
101104
]
102105
}

0 commit comments

Comments
 (0)