From 1bd3b8f9dfa148e3d3f9a3b13ce3335e620276e1 Mon Sep 17 00:00:00 2001 From: Dani Estevez Date: Fri, 29 Jun 2018 16:42:38 -0400 Subject: [PATCH] Implements VirtualMachineAPI deallocate operation https://docs.microsoft.com/en-us/rest/api/compute/virtualmachines/deallocate Adds missing mock test Fixes returning codes from API methods to 202 --- .../arm/features/VirtualMachineApi.java | 6 +++- .../features/VirtualMachineApiLiveTest.java | 11 +++++-- .../features/VirtualMachineApiMockTest.java | 29 +++++++++++++------ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/VirtualMachineApi.java b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/VirtualMachineApi.java index 39ed860d45..e8dc0658ea 100644 --- a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/VirtualMachineApi.java +++ b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/features/VirtualMachineApi.java @@ -19,7 +19,6 @@ package org.jclouds.azurecompute.arm.features; import java.net.URI; import java.util.List; import java.util.Map; - import javax.inject.Named; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -114,6 +113,11 @@ public interface VirtualMachineApi { @Path("/{name}/powerOff") void stop(@PathParam("name") String name); + @Named("DeallocateVirtualMachine") + @POST + @Path("/{name}/deallocate") + void deallocate(@PathParam("name") String name); + @Named("generalize") @POST @Path("/{name}/generalize") diff --git a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualMachineApiLiveTest.java b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualMachineApiLiveTest.java index 0154a2773a..27bdc75f55 100644 --- a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualMachineApiLiveTest.java +++ b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualMachineApiLiveTest.java @@ -156,13 +156,20 @@ public class VirtualMachineApiLiveTest extends BaseAzureComputeApiLiveTest { assertEquals(vm.properties().storageProfile().dataDisks().size(), oldDataDisks.size() + 1); } - @Test(dependsOnMethods = "testUpdate") + @Test(dependsOnMethods = "testRestart") public void testStop() { api().stop(vmName); assertTrue(stateReached(vmName, PowerState.STOPPED), "stop operation did not complete in the configured timeout"); } - @Test(dependsOnMethods = "testStop") + @Test(dependsOnMethods = "testUpdate") + public void testDeallocate() { + api().deallocate(vmName); + assertTrue(stateReached(vmName, PowerState.DEALLOCATED), + "deallocate operation did not complete in the configured timeout"); + } + + @Test(dependsOnMethods = "testDeallocate") public void testRestart() { api().start(vmName); assertTrue(stateReached(vmName, PowerState.RUNNING), "start operation did not complete in the configured timeout"); diff --git a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualMachineApiMockTest.java b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualMachineApiMockTest.java index 372b303300..737129427c 100644 --- a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualMachineApiMockTest.java +++ b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/VirtualMachineApiMockTest.java @@ -16,6 +16,12 @@ */ package org.jclouds.azurecompute.arm.features; +import static com.google.common.collect.Iterables.isEmpty; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + import java.net.URI; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -53,12 +59,6 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.squareup.okhttp.mockwebserver.MockResponse; -import static com.google.common.collect.Iterables.isEmpty; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; - @Test(groups = "unit", testName = "VirtualMachineApiMockTest", singleThreaded = true) public class VirtualMachineApiMockTest extends BaseAzureComputeApiMockTest { @@ -213,7 +213,7 @@ public class VirtualMachineApiMockTest extends BaseAzureComputeApiMockTest { } public void testStart() throws Exception { - server.enqueue(new MockResponse().setResponseCode(204)); + server.enqueue(response202WithHeader()); final VirtualMachineApi vmAPI = api.getVirtualMachineApi("groupname"); @@ -224,7 +224,7 @@ public class VirtualMachineApiMockTest extends BaseAzureComputeApiMockTest { } public void testRestart() throws Exception { - server.enqueue(new MockResponse().setResponseCode(204)); + server.enqueue(response202WithHeader()); final VirtualMachineApi vmAPI = api.getVirtualMachineApi("groupname"); @@ -235,7 +235,7 @@ public class VirtualMachineApiMockTest extends BaseAzureComputeApiMockTest { } public void testStop() throws Exception { - server.enqueue(new MockResponse().setResponseCode(204)); + server.enqueue(response202WithHeader()); final VirtualMachineApi vmAPI = api.getVirtualMachineApi("groupname"); @@ -245,6 +245,17 @@ public class VirtualMachineApiMockTest extends BaseAzureComputeApiMockTest { + "/virtualMachines/windowsmachine/powerOff?api-version=2016-04-30-preview"); } + public void testDeallocate() throws Exception { + server.enqueue(response202WithHeader()); + + final VirtualMachineApi vmAPI = api.getVirtualMachineApi("groupname"); + + vmAPI.deallocate("windowsmachine"); + + assertSent(server, "POST", "/subscriptions/SUBSCRIPTIONID/resourceGroups/groupname/providers/Microsoft.Compute" + + "/virtualMachines/windowsmachine/deallocate?api-version=2016-04-30-preview"); + } + public void testGeneralize() throws Exception { server.enqueue(new MockResponse().setResponseCode(200)); final VirtualMachineApi vmAPI = api.getVirtualMachineApi("groupname");