diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalPodAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalPodAsyncClient.java index 46d7807aeb..76219e5eb3 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalPodAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalPodAsyncClient.java @@ -23,6 +23,7 @@ import org.jclouds.cloudstack.domain.Pod; import org.jclouds.cloudstack.filters.QuerySigner; import org.jclouds.cloudstack.options.CreatePodOptions; import org.jclouds.cloudstack.options.ListPodsOptions; +import org.jclouds.cloudstack.options.UpdatePodOptions; import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.OnlyElement; import org.jclouds.rest.annotations.QueryParams; @@ -119,4 +120,17 @@ public interface GlobalPodAsyncClient { @ExceptionParser(ReturnVoidOnNotFoundOr404.class) ListenableFuture deletePod(@QueryParam("id") long id); + /** + * Updates a Pod. + * @param id the ID of the Pod + * @param updatePodOptions optional arguments + * @return the updated pod + */ + @GET + @QueryParams(keys = "command", values = "updatePod") + @SelectJson("pod") + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture updatePod(@QueryParam("id") long id, UpdatePodOptions... updatePodOptions); + } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalPodClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalPodClient.java index e4541c250b..fa8ac1a64b 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalPodClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalPodClient.java @@ -21,6 +21,7 @@ package org.jclouds.cloudstack.features; import org.jclouds.cloudstack.domain.Pod; import org.jclouds.cloudstack.options.CreatePodOptions; import org.jclouds.cloudstack.options.ListPodsOptions; +import org.jclouds.cloudstack.options.UpdatePodOptions; import org.jclouds.concurrent.Timeout; import java.util.Set; @@ -88,4 +89,13 @@ public interface GlobalPodClient { * @param id the ID of the Pod */ void deletePod(long id); + + /** + * Updates a Pod. + * @param id the ID of the Pod + * @param updatePodOptions optional arguments + * @return the updated pod + */ + Pod updatePod(long id, UpdatePodOptions... updatePodOptions); + } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/UpdatePodOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/UpdatePodOptions.java new file mode 100644 index 0000000000..ea33124d76 --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/UpdatePodOptions.java @@ -0,0 +1,94 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.cloudstack.options; + +import com.google.common.collect.ImmutableSet; +import org.jclouds.cloudstack.domain.AllocationState; +import org.jclouds.http.options.BaseHttpRequestOptions; + +import javax.annotation.concurrent.Immutable; + +/** + * Options to the GlobalPodClient.updatePod API call. + * + * @author Richard Downer + */ +public class UpdatePodOptions extends BaseHttpRequestOptions { + + public static final UpdatePodOptions NONE = new UpdatePodOptions(); + + public static class Builder { + + public static UpdatePodOptions name(String name) { + return new UpdatePodOptions().name(name); + } + + public static UpdatePodOptions startIp(String startIp) { + return new UpdatePodOptions().startIp(startIp); + } + + public static UpdatePodOptions endIp(String endIp) { + return new UpdatePodOptions().endIp(endIp); + } + + public static UpdatePodOptions gateway(String gateway) { + return new UpdatePodOptions().gateway(gateway); + } + + public static UpdatePodOptions netmask(String netmask) { + return new UpdatePodOptions().netmask(netmask); + } + + public static UpdatePodOptions allocationState(AllocationState allocationState) { + return new UpdatePodOptions().allocationState(allocationState); + } + + } + + public UpdatePodOptions name(String name) { + this.queryParameters.replaceValues("name", ImmutableSet.of(name)); + return this; + } + + public UpdatePodOptions startIp(String startIp) { + this.queryParameters.replaceValues("startip", ImmutableSet.of(startIp)); + return this; + } + + public UpdatePodOptions endIp(String endIp) { + this.queryParameters.replaceValues("endip", ImmutableSet.of(endIp)); + return this; + } + + public UpdatePodOptions gateway(String gateway) { + this.queryParameters.replaceValues("gateway", ImmutableSet.of(gateway)); + return this; + } + + public UpdatePodOptions netmask(String netmask) { + this.queryParameters.replaceValues("netmask", ImmutableSet.of(netmask)); + return this; + } + + public UpdatePodOptions allocationState(AllocationState allocationState) { + this.queryParameters.replaceValues("allocationstate", ImmutableSet.of(allocationState.toString())); + return this; + } + +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalPodClientExpectTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalPodClientExpectTest.java index e4274fc3bd..93e8a8fa4e 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalPodClientExpectTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalPodClientExpectTest.java @@ -24,6 +24,7 @@ import org.jclouds.cloudstack.CloudStackContext; import org.jclouds.cloudstack.domain.AllocationState; import org.jclouds.cloudstack.domain.Pod; import org.jclouds.cloudstack.options.CreatePodOptions; +import org.jclouds.cloudstack.options.UpdatePodOptions; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; import org.testng.annotations.Test; @@ -136,6 +137,46 @@ public class GlobalPodClientExpectTest extends BaseCloudStackRestClientExpectTes assertEquals(actual, expected); } + public void testUpdatePodWhenResponseIs2xx() { + GlobalPodClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&command=updatePod&id=7&netmask=255.255.255.128&name=richard-updatepod&startip=172.21.0.129&endip=172.21.0.250&gateway=172.21.0.254&allocationstate=Disabled&apiKey=identity&signature=QpdbRyyF%2FxJ78ioJWhPKXEWhthY%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/updatepodresponse.json")) + .build()); + + Pod expected = Pod.builder() + .id(7) + .name("richard-updatedpod") + .zoneId(11) + .zoneName("richard-zone") + .gateway("172.21.0.254") + .netmask("255.255.255.128") + .startIp("172.21.0.129") + .endIp("172.21.0.250") + .allocationState(AllocationState.DISABLED) + .build(); + + Pod actual = client.updatePod(7, UpdatePodOptions.Builder + .netmask("255.255.255.128") + .name("richard-updatepod") + .startIp("172.21.0.129") + .endIp("172.21.0.250") + .gateway("172.21.0.254") + .allocationState(AllocationState.DISABLED) + ); + + assertEquals(actual, expected); + } + public void testDeletePodWhenResponseIs2xx() { GlobalPodClient client = requestSendsResponse( HttpRequest.builder() diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalPodClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalPodClientLiveTest.java index 64830c070f..20c3d6d2ce 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalPodClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalPodClientLiveTest.java @@ -27,6 +27,7 @@ import org.jclouds.cloudstack.domain.Pod; import org.jclouds.cloudstack.domain.Zone; import org.jclouds.cloudstack.options.CreatePodOptions; import org.jclouds.cloudstack.options.ListPodsOptions; +import org.jclouds.cloudstack.options.UpdatePodOptions; import org.testng.annotations.AfterClass; import org.testng.annotations.Test; @@ -87,6 +88,28 @@ public class GlobalPodClientLiveTest extends BaseCloudStackClientLiveTest { assertEquals(pod.getAllocationState(), AllocationState.ENABLED); } + @Test(dependsOnMethods = "testCreatePod") + public void testUpdatePod() { + Pod updated = globalAdminClient.getPodClient().updatePod(pod.getId(), UpdatePodOptions.Builder + .name(prefix + "-updatedpod") + .startIp("172.21.0.129") + .endIp("172.21.0.250") + .gateway("172.21.0.254") + .netmask("255.255.255.128") + .allocationState(AllocationState.DISABLED) + ); + + assertNotNull(updated); + assertEquals(updated.getName(), prefix + "-updatedpod"); + assertEquals(updated.getZoneId(), zone.getId()); + assertEquals(updated.getZoneName(), prefix + "-zone"); + assertEquals(updated.getStartIp(), "172.21.0.129"); + assertEquals(updated.getEndIp(), "172.21.0.250"); + assertEquals(updated.getGateway(), "172.21.0.254"); + assertEquals(updated.getNetmask(), "255.255.255.128"); + assertEquals(updated.getAllocationState(), AllocationState.DISABLED); + } + @AfterClass public void testFixtureTearDown() { if (pod != null) { diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/UpdatePodOptionsTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/UpdatePodOptionsTest.java new file mode 100644 index 0000000000..ffa1a01055 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/UpdatePodOptionsTest.java @@ -0,0 +1,96 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.cloudstack.options; + +import com.google.common.collect.ImmutableList; +import org.jclouds.cloudstack.domain.AllocationState; +import org.testng.annotations.Test; + +import static org.jclouds.cloudstack.options.UpdatePodOptions.Builder.*; +import static org.testng.Assert.assertEquals; + +/** + * Tests behavior of {@code UpdatePodOptions} + * + * @author Richard Downer + */ +@Test(groups = "unit") +public class UpdatePodOptionsTest { + + public void testName() { + UpdatePodOptions options = new UpdatePodOptions().name("fred"); + assertEquals(ImmutableList.of("fred"), options.buildQueryParameters().get("name")); + } + + public void testNameStatic() { + UpdatePodOptions options = name("fred"); + assertEquals(ImmutableList.of("fred"), options.buildQueryParameters().get("name")); + } + + public void testStartIp() { + UpdatePodOptions options = new UpdatePodOptions().startIp("192.168.42.42"); + assertEquals(ImmutableList.of("192.168.42.42"), options.buildQueryParameters().get("startip")); + } + + public void testStartIpStatic() { + UpdatePodOptions options = startIp("192.168.42.42"); + assertEquals(ImmutableList.of("192.168.42.42"), options.buildQueryParameters().get("startip")); + } + + public void testEndIp() { + UpdatePodOptions options = new UpdatePodOptions().endIp("192.168.42.52"); + assertEquals(ImmutableList.of("192.168.42.52"), options.buildQueryParameters().get("endip")); + } + + public void testEndIpStatic() { + UpdatePodOptions options = endIp("192.168.42.52"); + assertEquals(ImmutableList.of("192.168.42.52"), options.buildQueryParameters().get("endip")); + } + + public void testGateway() { + UpdatePodOptions options = new UpdatePodOptions().gateway("192.168.42.62"); + assertEquals(ImmutableList.of("192.168.42.62"), options.buildQueryParameters().get("gateway")); + } + + public void testGatewayStatic() { + UpdatePodOptions options = gateway("192.168.42.62"); + assertEquals(ImmutableList.of("192.168.42.62"), options.buildQueryParameters().get("gateway")); + } + + public void testNetmask() { + UpdatePodOptions options = new UpdatePodOptions().netmask("255.255.240.0"); + assertEquals(ImmutableList.of("255.255.240.0"), options.buildQueryParameters().get("netmask")); + } + + public void testNetmaskStatic() { + UpdatePodOptions options = netmask("255.255.240.0"); + assertEquals(ImmutableList.of("255.255.240.0"), options.buildQueryParameters().get("netmask")); + } + + public void testAllocationState() { + UpdatePodOptions options = new UpdatePodOptions().allocationState(AllocationState.ENABLED); + assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("allocationstate")); + } + + public void testAllocationStateStatic() { + UpdatePodOptions options = allocationState(AllocationState.ENABLED); + assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("allocationstate")); + } + +} diff --git a/apis/cloudstack/src/test/resources/updatepodresponse.json b/apis/cloudstack/src/test/resources/updatepodresponse.json new file mode 100644 index 0000000000..b9479a7f3e --- /dev/null +++ b/apis/cloudstack/src/test/resources/updatepodresponse.json @@ -0,0 +1 @@ +{ "updatepodresponse" : { "pod" : {"id":7,"name":"richard-updatedpod","zoneid":11,"zonename":"richard-zone","gateway":"172.21.0.254","netmask":"255.255.255.128","startip":"172.21.0.129","endip":"172.21.0.250","allocationstate":"Disabled"} } } \ No newline at end of file