Skip to content

Commit a793105

Browse files
mgmt, convenient code for batch operation to VMSS instances (Azure#26916)
1 parent 1e794e3 commit a793105

12 files changed

+4208
-18
lines changed

sdk/resourcemanager/azure-resourcemanager-compute/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Supported option of `filter` and `expand` for list instances of virtual machines by `VirtualMachineScaleSetVMs.list`.
88
- Changed to include the instance view of the virtual machine, when getting the virtual machine by `VirtualMachineScaleSetVMs.getInstance`.
9+
- Supported batch deallocate, powerOff, start, restart, redeploy for `VirtualMachineScaleSetVMs`.
910
- Supported deep deletion on virtual machine via `withPrimaryNetworkInterfaceDeleteOptions`, `withOSDiskDeleteOptions`, `withDataDiskDefaultDeleteOptions` in `VirtualMachine` during create.
1011

1112
### Other Changes

sdk/resourcemanager/azure-resourcemanager-compute/src/main/java/com/azure/resourcemanager/compute/implementation/VirtualMachineImpl.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,20 @@ public Mono<Void> powerOffAsync() {
308308
.powerOffAsync(this.resourceGroupName(), this.name(), null);
309309
}
310310

311+
@Override
312+
public void powerOff(boolean skipShutdown) {
313+
this.powerOffAsync(skipShutdown).block();
314+
}
315+
316+
@Override
317+
public Mono<Void> powerOffAsync(boolean skipShutdown) {
318+
return this
319+
.manager()
320+
.serviceClient()
321+
.getVirtualMachines()
322+
.powerOffAsync(this.resourceGroupName(), this.name(), skipShutdown);
323+
}
324+
311325
@Override
312326
public void restart() {
313327
this.restartAsync().block();
@@ -2737,9 +2751,6 @@ private StorageAccountTypes getDefaultStorageAccountType() {
27372751
}
27382752

27392753
private DiskDeleteOptionTypes getDeleteOptions() {
2740-
if (defaultDeleteOptions == null) {
2741-
return DiskDeleteOptionTypes.DETACH;
2742-
}
27432754
return defaultDeleteOptions;
27442755
}
27452756
}

sdk/resourcemanager/azure-resourcemanager-compute/src/main/java/com/azure/resourcemanager/compute/implementation/VirtualMachineScaleSetVMImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,16 @@ public PowerState powerState() {
445445
return PowerState.fromInstanceView(this.instanceView());
446446
}
447447

448+
@Override
449+
public void redeploy() {
450+
this.redeployAsync().block();
451+
}
452+
453+
@Override
454+
public Mono<Void> redeployAsync() {
455+
return client.redeployAsync(this.parent().resourceGroupName(), this.parent().name(), this.instanceId());
456+
}
457+
448458
@Override
449459
public void reimage() {
450460
this.reimageAsync().block();
@@ -479,6 +489,17 @@ public Mono<Void> powerOffAsync() {
479489
.powerOffAsync(this.parent().resourceGroupName(), this.parent().name(), this.instanceId(), null);
480490
}
481491

492+
@Override
493+
public void powerOff(boolean skipShutdown) {
494+
this.powerOffAsync(skipShutdown).block();
495+
}
496+
497+
@Override
498+
public Mono<Void> powerOffAsync(boolean skipShutdown) {
499+
return this.client
500+
.powerOffAsync(this.parent().resourceGroupName(), this.parent().name(), this.instanceId(), skipShutdown);
501+
}
502+
482503
@Override
483504
public void start() {
484505
this.startAsync().block();

sdk/resourcemanager/azure-resourcemanager-compute/src/main/java/com/azure/resourcemanager/compute/implementation/VirtualMachineScaleSetVMsImpl.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3+
34
package com.azure.resourcemanager.compute.implementation;
45

56
import com.azure.core.http.rest.PagedFlux;
@@ -11,6 +12,7 @@
1112
import com.azure.resourcemanager.compute.models.InstanceViewTypes;
1213
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetVM;
1314
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetVMExpandType;
15+
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetVMInstanceIDs;
1416
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetVMInstanceRequiredIDs;
1517
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetVMs;
1618
import com.azure.resourcemanager.resources.fluentcore.arm.collection.implementation.ReadableWrappersImpl;
@@ -86,6 +88,78 @@ public void deleteInstances(Collection<String> instanceIds, boolean forceDeletio
8688
this.deleteInstancesAsync(instanceIds, forceDeletion).block();
8789
}
8890

91+
@Override
92+
public void deallocateInstances(Collection<String> instanceIds) {
93+
this.deallocateInstancesAsync(instanceIds).block();
94+
}
95+
96+
@Override
97+
public Mono<Void> deallocateInstancesAsync(Collection<String> instanceIds) {
98+
return this.scaleSet.manager().serviceClient().getVirtualMachineScaleSets().deallocateAsync(
99+
this.scaleSet.resourceGroupName(), this.scaleSet.name(),
100+
new VirtualMachineScaleSetVMInstanceIDs().withInstanceIds(new ArrayList<>(instanceIds)));
101+
}
102+
103+
@Override
104+
public void powerOffInstances(Collection<String> instanceIds, boolean skipShutdown) {
105+
this.powerOffInstancesAsync(instanceIds, skipShutdown).block();
106+
}
107+
108+
@Override
109+
public Mono<Void> powerOffInstancesAsync(Collection<String> instanceIds, boolean skipShutdown) {
110+
return this.scaleSet.manager().serviceClient().getVirtualMachineScaleSets().powerOffAsync(
111+
this.scaleSet.resourceGroupName(), this.scaleSet.name(), skipShutdown,
112+
new VirtualMachineScaleSetVMInstanceIDs().withInstanceIds(new ArrayList<>(instanceIds)));
113+
}
114+
115+
@Override
116+
public void startInstances(Collection<String> instanceIds) {
117+
this.startInstancesAsync(instanceIds).block();
118+
}
119+
120+
@Override
121+
public Mono<Void> startInstancesAsync(Collection<String> instanceIds) {
122+
return this.scaleSet.manager().serviceClient().getVirtualMachineScaleSets().startAsync(
123+
this.scaleSet.resourceGroupName(), this.scaleSet.name(),
124+
new VirtualMachineScaleSetVMInstanceIDs().withInstanceIds(new ArrayList<>(instanceIds)));
125+
}
126+
127+
@Override
128+
public void restartInstances(Collection<String> instanceIds) {
129+
this.restartInstancesAsync(instanceIds).block();
130+
}
131+
132+
@Override
133+
public Mono<Void> restartInstancesAsync(Collection<String> instanceIds) {
134+
return this.scaleSet.manager().serviceClient().getVirtualMachineScaleSets().restartAsync(
135+
this.scaleSet.resourceGroupName(), this.scaleSet.name(),
136+
new VirtualMachineScaleSetVMInstanceIDs().withInstanceIds(new ArrayList<>(instanceIds)));
137+
}
138+
139+
@Override
140+
public void redeployInstances(Collection<String> instanceIds) {
141+
this.redeployInstancesAsync(instanceIds).block();
142+
}
143+
144+
@Override
145+
public Mono<Void> redeployInstancesAsync(Collection<String> instanceIds) {
146+
return this.scaleSet.manager().serviceClient().getVirtualMachineScaleSets().redeployAsync(
147+
this.scaleSet.resourceGroupName(), this.scaleSet.name(),
148+
new VirtualMachineScaleSetVMInstanceIDs().withInstanceIds(new ArrayList<>(instanceIds)));
149+
}
150+
151+
// @Override
152+
// public void reimageInstances(Collection<String> instanceIds) {
153+
// this.reimageInstancesAsync(instanceIds).block();
154+
// }
155+
//
156+
// @Override
157+
// public Mono<Void> reimageInstancesAsync(Collection<String> instanceIds) {
158+
// return this.scaleSet.manager().serviceClient().getVirtualMachineScaleSets().reimageAsync(
159+
// this.scaleSet.resourceGroupName(), this.scaleSet.name(),
160+
// new VirtualMachineScaleSetReimageParameters().withInstanceIds(new ArrayList<>(instanceIds)));
161+
// }
162+
89163
@Override
90164
public VirtualMachineScaleSetVM getInstance(String instanceId) {
91165
return this.getInstanceAsync(instanceId).block();

sdk/resourcemanager/azure-resourcemanager-compute/src/main/java/com/azure/resourcemanager/compute/models/VirtualMachine.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ public interface VirtualMachine
8080
*/
8181
Mono<Void> powerOffAsync();
8282

83+
/**
84+
* Stops the virtual machine.
85+
*
86+
* @param skipShutdown power off without graceful shutdown
87+
*/
88+
void powerOff(boolean skipShutdown);
89+
90+
/**
91+
* Stops the virtual machine.
92+
*
93+
* @param skipShutdown power off without graceful shutdown
94+
* @return a representation of the deferred computation of this call.
95+
*/
96+
Mono<Void> powerOffAsync(boolean skipShutdown);
97+
8398
/** Restarts the virtual machine. */
8499
void restart();
85100

sdk/resourcemanager/azure-resourcemanager-compute/src/main/java/com/azure/resourcemanager/compute/models/VirtualMachineScaleSetVM.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ public interface VirtualMachineScaleSetVM
146146
/** @return true if managed disk is used for the virtual machine's disks (os, data) */
147147
boolean isManagedDiskEnabled();
148148

149+
/** Shuts down the virtual machine instance, move them to new node, and powers them back on. */
150+
void redeploy();
151+
152+
/**
153+
* Shuts down the virtual machine instance, move them to new node, and powers them back on.
154+
*
155+
* @return a representation of the deferred computation of this call
156+
*/
157+
Mono<Void> redeployAsync();
158+
149159
/** Updates the version of the installed operating system in the virtual machine instance. */
150160
void reimage();
151161

@@ -176,6 +186,21 @@ public interface VirtualMachineScaleSetVM
176186
*/
177187
Mono<Void> powerOffAsync();
178188

189+
/**
190+
* Stops the virtual machine instance.
191+
*
192+
* @param skipShutdown power off without graceful shutdown
193+
*/
194+
void powerOff(boolean skipShutdown);
195+
196+
/**
197+
* Stops the virtual machine instances.
198+
*
199+
* @param skipShutdown power off without graceful shutdown
200+
* @return a representation of the deferred computation of this call.
201+
*/
202+
Mono<Void> powerOffAsync(boolean skipShutdown);
203+
179204
/** Starts the virtual machine instance. */
180205
void start();
181206

sdk/resourcemanager/azure-resourcemanager-compute/src/main/java/com/azure/resourcemanager/compute/models/VirtualMachineScaleSetVMs.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public interface VirtualMachineScaleSetVMs extends SupportsListing<VirtualMachin
5656
/**
5757
* Get the specified virtual machine instance from the scale set.
5858
*
59-
* @param instanceId instance ID of the virtual machine scale set instance to be fetched
59+
* @param instanceId instance ID of the virtual machine scale set instance to be fetched.
6060
* @return the virtual machine scale set instance.
6161
*/
6262
VirtualMachineScaleSetVM getInstance(String instanceId);
@@ -92,6 +92,98 @@ public interface VirtualMachineScaleSetVMs extends SupportsListing<VirtualMachin
9292
*/
9393
void updateInstances(String... instanceIds);
9494

95+
/**
96+
* Shuts down the virtual machine instances and releases the associated compute resources.
97+
*
98+
* @param instanceIds instance IDs of the virtual machine scale set instances
99+
*/
100+
void deallocateInstances(Collection<String> instanceIds);
101+
102+
/**
103+
* Shuts down the virtual machine instances and releases the associated compute resources.
104+
*
105+
* @param instanceIds instance IDs of the virtual machine scale set instances
106+
* @return a representation of the deferred computation of this call.
107+
*/
108+
Mono<Void> deallocateInstancesAsync(Collection<String> instanceIds);
109+
110+
/**
111+
* Stops the virtual machine instances.
112+
*
113+
* @param instanceIds instance IDs of the virtual machine scale set instances
114+
* @param skipShutdown power off without graceful shutdown
115+
*/
116+
void powerOffInstances(Collection<String> instanceIds, boolean skipShutdown);
117+
118+
/**
119+
* Stops the virtual machine instances.
120+
*
121+
* @param instanceIds instance IDs of the virtual machine scale set instances
122+
* @param skipShutdown power off without graceful shutdown
123+
* @return a representation of the deferred computation of this call.
124+
*/
125+
Mono<Void> powerOffInstancesAsync(Collection<String> instanceIds, boolean skipShutdown);
126+
127+
/**
128+
* Starts the virtual machine instances.
129+
*
130+
* @param instanceIds instance IDs of the virtual machine scale set instances
131+
*/
132+
void startInstances(Collection<String> instanceIds);
133+
134+
/**
135+
* Starts the virtual machine instances.
136+
*
137+
* @param instanceIds instance IDs of the virtual machine scale set instances
138+
* @return a representation of the deferred computation of this call.
139+
*/
140+
Mono<Void> startInstancesAsync(Collection<String> instanceIds);
141+
142+
/**
143+
* Restarts the virtual machine instances.
144+
*
145+
* @param instanceIds instance IDs of the virtual machine scale set instances
146+
*/
147+
void restartInstances(Collection<String> instanceIds);
148+
149+
/**
150+
* Restarts the virtual machine instances.
151+
*
152+
* @param instanceIds instance IDs of the virtual machine scale set instances
153+
* @return a representation of the deferred computation of this call.
154+
*/
155+
Mono<Void> restartInstancesAsync(Collection<String> instanceIds);
156+
157+
/**
158+
* Shuts down the virtual machine instances, move them to new node, and powers them back on.
159+
*
160+
* @param instanceIds instance IDs of the virtual machine scale set instances
161+
*/
162+
void redeployInstances(Collection<String> instanceIds);
163+
164+
/**
165+
* Shuts down the virtual machine instances, move them to new node, and powers them back on.
166+
*
167+
* @param instanceIds instance IDs of the virtual machine scale set instances
168+
* @return a representation of the deferred computation of this call.
169+
*/
170+
Mono<Void> redeployInstancesAsync(Collection<String> instanceIds);
171+
172+
// /**
173+
// * Updates the version of the installed operating system in the virtual machine instances.
174+
// *
175+
// * @param instanceIds instance IDs of the virtual machine scale set instances
176+
// */
177+
// void reimageInstances(Collection<String> instanceIds);
178+
//
179+
// /**
180+
// * Updates the version of the installed operating system in the virtual machine instances.
181+
// *
182+
// * @param instanceIds instance IDs of the virtual machine scale set instances
183+
// * @return a representation of the deferred computation of this call.
184+
// */
185+
// Mono<Void> reimageInstancesAsync(Collection<String> instanceIds);
186+
95187
/**
96188
* Simulates the eviction of the specified spot virtual machine in the scale set asynchronously. The eviction will
97189
* occur with 30 minutes after calling this API.

sdk/resourcemanager/azure-resourcemanager-compute/src/test/java/com/azure/resourcemanager/compute/VirtualMachineOperationsTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,42 @@ public void canHibernateVirtualMachine() {
12151215
Assertions.assertFalse(vm.isHibernationEnabled());
12161216
}
12171217

1218+
@Test
1219+
public void canOperateVirtualMachine() {
1220+
VirtualMachine vm = computeManager.virtualMachines()
1221+
.define(vmName)
1222+
.withRegion(Region.US_WEST3)
1223+
.withNewResourceGroup(rgName)
1224+
.withNewPrimaryNetwork("10.0.0.0/28")
1225+
.withPrimaryPrivateIPAddressDynamic()
1226+
.withoutPrimaryPublicIPAddress()
1227+
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_18_04_LTS)
1228+
.withRootUsername("Foo12")
1229+
.withSsh(sshPublicKey())
1230+
.withSize(VirtualMachineSizeTypes.STANDARD_A1_V2)
1231+
.create();
1232+
1233+
Assertions.assertEquals(PowerState.RUNNING, vm.powerState());
1234+
1235+
vm.redeploy();
1236+
1237+
vm.powerOff(true);
1238+
vm.refreshInstanceView();
1239+
Assertions.assertEquals(PowerState.STOPPED, vm.powerState());
1240+
1241+
vm.start();
1242+
vm.refreshInstanceView();
1243+
Assertions.assertEquals(PowerState.RUNNING, vm.powerState());
1244+
1245+
vm.restart();
1246+
vm.refreshInstanceView();
1247+
Assertions.assertEquals(PowerState.RUNNING, vm.powerState());
1248+
1249+
vm.deallocate();
1250+
vm.refreshInstanceView();
1251+
Assertions.assertEquals(PowerState.DEALLOCATED, vm.powerState());
1252+
}
1253+
12181254
private CreatablesInfo prepareCreatableVirtualMachines(
12191255
Region region, String vmNamePrefix, String networkNamePrefix, String publicIpNamePrefix, int vmCount) {
12201256

0 commit comments

Comments
 (0)