From 9bb9312d4a23f99c057d50ce51fbc1d12c675095 Mon Sep 17 00:00:00 2001 From: Richard Downer Date: Thu, 12 Jan 2012 13:55:32 +0200 Subject: [PATCH] Add GlobalHost[Async]Client.updateHost() --- .../features/GlobalHostAsyncClient.java | 14 +++ .../cloudstack/features/GlobalHostClient.java | 10 +++ .../cloudstack/options/UpdateHostOptions.java | 87 +++++++++++++++++++ .../features/GlobalHostClientExpectTest.java | 21 +++++ .../options/UpdateHostOptionsTest.java | 67 ++++++++++++++ .../test/resources/updatehostresponse.json | 2 + 6 files changed, 201 insertions(+) create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/UpdateHostOptions.java create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/UpdateHostOptionsTest.java create mode 100644 apis/cloudstack/src/test/resources/updatehostresponse.json diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalHostAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalHostAsyncClient.java index 871818ae21..46328fc9a3 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalHostAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalHostAsyncClient.java @@ -25,6 +25,7 @@ import org.jclouds.cloudstack.filters.QuerySigner; import org.jclouds.cloudstack.options.AddHostOptions; import org.jclouds.cloudstack.options.ListClustersOptions; import org.jclouds.cloudstack.options.ListHostsOptions; +import org.jclouds.cloudstack.options.UpdateHostOptions; import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.QueryParams; import org.jclouds.rest.annotations.RequestFilters; @@ -76,6 +77,19 @@ public interface GlobalHostAsyncClient { @Consumes(MediaType.APPLICATION_JSON) ListenableFuture addHost(@QueryParam("zoneid") long zoneId, @QueryParam("url") String url, @QueryParam("hypervisor") String hypervisor, @QueryParam("username") String username, @QueryParam("password") String password, AddHostOptions... options); + /** + * Updates a host. + * + * @param hostId the ID of the host to update + * @param options optional arguments + * @return the modified host. + */ + @GET + @QueryParams(keys = "command", values = "updateHost") + @SelectJson("host") + @Consumes(MediaType.APPLICATION_JSON) + ListenableFuture updateHost(@QueryParam("id") long hostId, UpdateHostOptions... options); + /** * @see GlobalHostClient#listClusters */ diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalHostClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalHostClient.java index 6006c91458..0dceadd261 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalHostClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalHostClient.java @@ -23,6 +23,7 @@ import org.jclouds.cloudstack.domain.Host; import org.jclouds.cloudstack.options.AddHostOptions; import org.jclouds.cloudstack.options.ListClustersOptions; import org.jclouds.cloudstack.options.ListHostsOptions; +import org.jclouds.cloudstack.options.UpdateHostOptions; import org.jclouds.concurrent.Timeout; import java.util.Set; @@ -62,6 +63,15 @@ public interface GlobalHostClient { */ Host addHost(long zoneId, String url, String hypervisor, String username, String password, AddHostOptions... options); + /** + * Updates a host. + * + * @param hostId the ID of the host to update + * @param options optional arguments + * @return the modified host. + */ + Host updateHost(long hostId, UpdateHostOptions... options); + /** * Lists clusters * diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/UpdateHostOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/UpdateHostOptions.java new file mode 100644 index 0000000000..e89013ae92 --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/UpdateHostOptions.java @@ -0,0 +1,87 @@ +/** + * 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.base.Joiner; +import com.google.common.collect.ImmutableSet; +import org.jclouds.cloudstack.domain.Host; +import org.jclouds.functions.JoinOnComma; +import org.jclouds.http.options.BaseHttpRequestOptions; + +import java.util.Set; + +/** + * Options to the GlobalHostClient.addHost() API call + * + * @author Richard Downer + */ +public class UpdateHostOptions extends BaseHttpRequestOptions { + + + public static final UpdateHostOptions NONE = new UpdateHostOptions(); + + /** + * @param allocationState Allocation state of this Host for allocation of new resources + */ + public UpdateHostOptions allocationState(Host.AllocationState allocationState) { + this.queryParameters.replaceValues("allocationstate", ImmutableSet.of(allocationState.toString())); + return this; + } + + /** + * @param hostTags list of tags to be added to the host + */ + public UpdateHostOptions hostTags(Set hostTags) { + this.queryParameters.replaceValues("hosttags", ImmutableSet.of(Joiner.on(',').join(hostTags))); + return this; + } + + /** + * @param osCategoryId the id of Os category to update the host with + */ + public UpdateHostOptions osCategoryId(long osCategoryId) { + this.queryParameters.replaceValues("oscategoryid", ImmutableSet.of(osCategoryId + "")); + return this; + } + + public static class Builder { + + /** + * @param allocationState Allocation state of this Host for allocation of new resources + */ + public static UpdateHostOptions allocationState(Host.AllocationState allocationState) { + return new UpdateHostOptions().allocationState(allocationState); + } + + /** + * @param hostTags list of tags to be added to the host + */ + public static UpdateHostOptions hostTags(Set hostTags) { + return new UpdateHostOptions().hostTags(hostTags); + } + + /** + * @param podId the Pod ID for the host + */ + public static UpdateHostOptions osCategoryId(long osCategoryId) { + return new UpdateHostOptions().osCategoryId(osCategoryId); + } + + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalHostClientExpectTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalHostClientExpectTest.java index a2c5c82490..808b225382 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalHostClientExpectTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalHostClientExpectTest.java @@ -26,6 +26,7 @@ import org.jclouds.cloudstack.domain.Cluster; import org.jclouds.cloudstack.domain.ConfigurationEntry; import org.jclouds.cloudstack.domain.Host; import org.jclouds.cloudstack.options.AddHostOptions; +import org.jclouds.cloudstack.options.UpdateHostOptions; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; import org.testng.annotations.Test; @@ -117,6 +118,26 @@ public class GlobalHostClientExpectTest extends BaseCloudStackRestClientExpectTe assertEquals(actual, expected); } + @Test + public void testUpdateHostWhenResponseIs2xx() { + HttpRequest request = HttpRequest.builder() + .method("GET") + .endpoint(URI.create("http://localhost:8080/client/api?response=json&command=updateHost&id=1&allocationstate=Enabled&hosttags=&oscategoryid=5&apiKey=identity&signature=qTxNq9yQG8S108giqS%2FROFzgev8%3D")) + .headers(ImmutableMultimap.builder().put("Accept", "application/json").build()) + .build(); + HttpResponse response = HttpResponse.builder() + .payload(payloadFromResource("/updatehostresponse.json")) + .statusCode(200).build(); + + Date lastPinged = makeDate(1970, Calendar.JANUARY, 16, 0, 54, 43, "UTC"); + Date created = makeDate(2011, Calendar.NOVEMBER, 26, 23, 28, 36, "UTC"); + Host expected = Host.builder().id(1).name("cs2-xevsrv.alucloud.local").state(Host.State.UP).type(Host.Type.ROUTING).ipAddress("10.26.26.107").zoneId(1).zoneName("Dev Zone 1").podId(1).podName("Dev Pod 1").version("2.2.12.20110928142833").hypervisor("XenServer").cpuNumber(24).cpuSpeed(2266).cpuAllocated("2.76%").cpuUsed("0.1%").cpuWithOverProvisioning(54384.0F).networkKbsRead(4443).networkKbsWrite(15048).memoryTotal(100549733760L).memoryAllocated(3623878656L).memoryUsed(3623878656L).capabilities("xen-3.0-x86_64 , xen-3.0-x86_32p , hvm-3.0-x86_32 , hvm-3.0-x86_32p , hvm-3.0-x86_64").lastPinged(lastPinged).managementServerId(223098941760041L).clusterId(1).clusterName("Xen Clust 1").clusterType(Host.ClusterType.CLOUD_MANAGED).localStorageActive(false).created(created).events("PrepareUnmanaged; HypervisorVersionChanged; ManagementServerDown; PingTimeout; AgentDisconnected; MaintenanceRequested; HostDown; AgentConnected; StartAgentRebalance; ShutdownRequested; Ping").hostTags("").hasEnoughCapacity(false).allocationState(Host.AllocationState.ENABLED).build(); + + Host actual = requestSendsResponse(request, response).updateHost(1, UpdateHostOptions.Builder.allocationState(Host.AllocationState.ENABLED).hostTags(Collections.emptySet()).osCategoryId(5)); + + assertEquals(actual, expected); + } + @Test public void testListClustersWhenResponseIs2xx() { HttpRequest request = HttpRequest.builder() diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/UpdateHostOptionsTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/UpdateHostOptionsTest.java new file mode 100644 index 0000000000..69ffc18592 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/UpdateHostOptionsTest.java @@ -0,0 +1,67 @@ +/** + * 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 com.google.common.collect.ImmutableSet; +import org.jclouds.cloudstack.domain.Host; +import org.testng.annotations.Test; + +import static org.jclouds.cloudstack.options.UpdateHostOptions.Builder.*; +import static org.testng.Assert.assertEquals; + +/** + * Tests behavior of {@code UpdateHostOptions} + * + * @author Richard Downer + */ +@Test(groups = "unit") +public class UpdateHostOptionsTest { + + public void testAllocationState() { + UpdateHostOptions options = new UpdateHostOptions().allocationState(Host.AllocationState.ENABLED); + assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("allocationstate")); + } + + public void testAllocationStateStatic() { + UpdateHostOptions options = allocationState(Host.AllocationState.ENABLED); + assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("allocationstate")); + } + + public void testHostTags() { + UpdateHostOptions options = new UpdateHostOptions().hostTags(ImmutableSet.of("foo", "bar", "baz")); + assertEquals(ImmutableList.of("foo,bar,baz"), options.buildQueryParameters().get("hosttags")); + } + + public void testHostTagsStatic() { + UpdateHostOptions options = hostTags(ImmutableSet.of("foo", "bar", "baz")); + assertEquals(ImmutableList.of("foo,bar,baz"), options.buildQueryParameters().get("hosttags")); + } + + public void testOsCategoryId() { + UpdateHostOptions options = new UpdateHostOptions().osCategoryId(42L); + assertEquals(ImmutableList.of("42"), options.buildQueryParameters().get("oscategoryid")); + } + + public void testOsCategoryIdStatic() { + UpdateHostOptions options = osCategoryId(42L); + assertEquals(ImmutableList.of("42"), options.buildQueryParameters().get("oscategoryid")); + } + +} diff --git a/apis/cloudstack/src/test/resources/updatehostresponse.json b/apis/cloudstack/src/test/resources/updatehostresponse.json new file mode 100644 index 0000000000..cb70ddd6bf --- /dev/null +++ b/apis/cloudstack/src/test/resources/updatehostresponse.json @@ -0,0 +1,2 @@ +{ "updatehostresponse" : { "host" : + {"warning":"This test data is fabricated","id":1,"name":"cs2-xevsrv.alucloud.local","state":"Up","type":"Routing","ipaddress":"10.26.26.107","zoneid":1,"zonename":"Dev Zone 1","podid":1,"podname":"Dev Pod 1","version":"2.2.12.20110928142833","hypervisor":"XenServer","cpunumber":24,"cpuspeed":2266,"cpuallocated":"2.76%","cpuused":"0.1%","cpuwithoverprovisioning":"54384.0","networkkbsread":4443,"networkkbswrite":15048,"memorytotal":100549733760,"memoryallocated":3623878656,"memoryused":3623878656,"capabilities":"xen-3.0-x86_64 , xen-3.0-x86_32p , hvm-3.0-x86_32 , hvm-3.0-x86_32p , hvm-3.0-x86_64","lastpinged":"1970-01-16T00:54:43+0200","managementserverid":223098941760041,"clusterid":1,"clustername":"Xen Clust 1","clustertype":"CloudManaged","islocalstorageactive":false,"created":"2011-11-26T23:28:36+0200","events":"PrepareUnmanaged; HypervisorVersionChanged; ManagementServerDown; PingTimeout; AgentDisconnected; MaintenanceRequested; HostDown; AgentConnected; StartAgentRebalance; ShutdownRequested; Ping","hosttags":"","hasEnoughCapacity":false,"allocationstate":"Enabled"} } } \ No newline at end of file