From e7961c64cc5fcd23ac5b0aef8c2b18d1fc639c4b Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 29 Jan 2012 19:41:22 -0800 Subject: [PATCH] refactored create server command to have less args --- .../org/jclouds/glesys/domain/ServerSpec.java | 192 ++++++++++++++++++ .../glesys/features/ServerAsyncClient.java | 38 ++-- .../jclouds/glesys/features/ServerClient.java | 137 +++++++------ .../glesys/options/CreateServerOptions.java | 63 +++++- .../features/ServerClientExpectTest.java | 38 ++-- .../internal/BaseGleSYSClientLiveTest.java | 6 +- 6 files changed, 371 insertions(+), 103 deletions(-) create mode 100644 sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerSpec.java diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerSpec.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerSpec.java new file mode 100644 index 0000000000..cc4fff5b05 --- /dev/null +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/domain/ServerSpec.java @@ -0,0 +1,192 @@ +/** + * 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.glesys.domain; + +import static com.google.common.base.Objects.equal; +import static com.google.common.base.Objects.toStringHelper; +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.Objects; + +/** + * + * + * @author Adrian Cole + */ +public class ServerSpec { + + public static Builder builder() { + return new Builder(); + } + + public Builder toBuilder() { + return Builder.fromServerSpecification(this); + } + + public static class Builder { + protected String datacenter; + protected String platform; + protected String templateName; + protected int diskSizeGB; + protected int memorySizeMB; + protected int cpuCores; + protected int transfer; + + public Builder datacenter(String datacenter) { + this.datacenter = datacenter; + return this; + } + + public Builder platform(String platform) { + this.platform = platform; + return this; + } + + public Builder templateName(String templateName) { + this.templateName = templateName; + return this; + } + + public Builder diskSizeGB(int diskSizeGB) { + this.diskSizeGB = diskSizeGB; + return this; + } + + public Builder memorySizeMB(int memorySizeMB) { + this.memorySizeMB = memorySizeMB; + return this; + } + + public Builder cpuCores(int cpuCores) { + this.cpuCores = cpuCores; + return this; + } + + public Builder transfer(int transfer) { + this.transfer = transfer; + return this; + } + + public ServerSpec build() { + return new ServerSpec(platform, datacenter, memorySizeMB, diskSizeGB, templateName, cpuCores, transfer); + } + + public static Builder fromServerSpecification(ServerSpec in) { + return new Builder().platform(in.getPlatform()).datacenter(in.getDatacenter()) + .memorySizeMB(in.getMemorySizeMB()).diskSizeGB(in.getDiskSizeGB()).templateName(in.getTemplateName()) + .cpuCores(in.getCpuCores()).transfer(in.getTransfer()); + } + } + + protected final String platform; + protected final String datacenter; + protected final int memorySizeMB; + protected final int diskSizeGB; + protected final String templateName; + protected final int cpuCores; + protected final int transfer; + + protected ServerSpec(String platform, String datacenter, int memorySizeMB, int diskSizeGB, String templateName, + int cpuCores, int transfer) { + this.platform = checkNotNull(platform, "platform"); + this.datacenter = checkNotNull(datacenter, "datacenter"); + this.memorySizeMB = memorySizeMB; + this.diskSizeGB = diskSizeGB; + this.templateName = checkNotNull(templateName, "templateName"); + this.cpuCores = cpuCores; + this.transfer = transfer; + } + + /** + * @return the data center to create the new server in + */ + public String getDatacenter() { + return datacenter; + } + + /** + * @return the platform to use (i.e. "Xen" or "OpenVZ") + */ + public String getPlatform() { + return platform; + } + + /** + * @return the os template to use to create the new server + */ + public String getTemplateName() { + return templateName; + } + + /** + * @return the amount of disk space, in GB, to allocate + */ + public int getDiskSizeGB() { + return diskSizeGB; + } + + /** + * @return the memory, in MB, to allocate + */ + public int getMemorySizeMB() { + return memorySizeMB; + } + + /** + * @return the number of CPU cores to allocate + */ + public int getCpuCores() { + return cpuCores; + } + + /** + * @return number of transfer + */ + public int getTransfer() { + return transfer; + } + + @Override + public boolean equals(Object object) { + if (this == object) { + return true; + } + if (object instanceof ServerSpec) { + final ServerSpec that = ServerSpec.class.cast(object); + return equal(platform, that.platform) && equal(datacenter, that.datacenter) + && equal(memorySizeMB, that.memorySizeMB) && equal(diskSizeGB, that.diskSizeGB) + && equal(templateName, that.templateName) && equal(cpuCores, that.cpuCores) + && equal(transfer, that.transfer); + } else { + return false; + } + } + + @Override + public int hashCode() { + return Objects.hashCode(platform, datacenter, memorySizeMB, diskSizeGB, templateName, cpuCores, transfer); + } + + @Override + public String toString() { + return toStringHelper("").add("platform", platform).add("datacenter", datacenter) + .add("templateName", templateName).add("cpuCores", cpuCores).add("cpuCores", cpuCores) + .add("diskSizeGB", diskSizeGB).add("transfer", transfer).toString(); + } +} diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerAsyncClient.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerAsyncClient.java index 56b22c8383..41fdaf4b0a 100644 --- a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerAsyncClient.java +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerAsyncClient.java @@ -29,11 +29,12 @@ import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.MediaType; +import org.jclouds.glesys.domain.AllowedArgumentsForCreateServer; import org.jclouds.glesys.domain.Console; import org.jclouds.glesys.domain.Server; -import org.jclouds.glesys.domain.AllowedArgumentsForCreateServer; import org.jclouds.glesys.domain.ServerDetails; import org.jclouds.glesys.domain.ServerLimit; +import org.jclouds.glesys.domain.ServerSpec; import org.jclouds.glesys.domain.ServerStatus; import org.jclouds.glesys.domain.Template; import org.jclouds.glesys.functions.ParseTemplatesFromHttpResponse; @@ -45,6 +46,8 @@ import org.jclouds.glesys.options.ServerStatusOptions; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.FormParams; +import org.jclouds.rest.annotations.MapBinder; +import org.jclouds.rest.annotations.PayloadParam; import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.ResponseParser; import org.jclouds.rest.annotations.SelectJson; @@ -56,7 +59,7 @@ import com.google.common.util.concurrent.ListenableFuture; /** * Provides asynchronous access to Server via their REST API. *

- * + * * @author Adrian Cole * @author Adam Lowe * @see ServerClient @@ -105,7 +108,6 @@ public interface ServerAsyncClient { @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture> getServerLimits(@FormParam("serverid") String id); - /** * @see ServerClient#getConsole */ @@ -116,7 +118,6 @@ public interface ServerAsyncClient { @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture getConsole(@FormParam("serverid") String id); - /** * @see ServerClient#getAllowedArgumentsForCreateServerByPlatform */ @@ -125,7 +126,7 @@ public interface ServerAsyncClient { @SelectJson("argumentslist") @Consumes(MediaType.APPLICATION_JSON) ListenableFuture> getAllowedArgumentsForCreateServerByPlatform(); - + /** * @see ServerClient#getTemplates */ @@ -134,14 +135,15 @@ public interface ServerAsyncClient { @ResponseParser(ParseTemplatesFromHttpResponse.class) @Consumes(MediaType.APPLICATION_JSON) ListenableFuture> getTemplates(); - + /** * @see ServerClient#stopServer */ @POST @Path("/server/resetlimit/format/json") @Consumes(MediaType.APPLICATION_JSON) - ListenableFuture> resetServerLimit(@FormParam("serverid") String id, @FormParam("type") String type); + ListenableFuture> resetServerLimit(@FormParam("serverid") String id, + @FormParam("type") String type); /** * @see ServerClient#rebootServer @@ -181,22 +183,16 @@ public interface ServerAsyncClient { ListenableFuture hardStopServer(@FormParam("serverid") String id); /** - * @see ServerClient#createServer + * @see ServerClient#createServerWithHostnameAndRootPassword */ @POST @SelectJson("server") @Path("/server/create/format/json") @Consumes(MediaType.APPLICATION_JSON) - ListenableFuture createServer(@FormParam("datacenter") String datacenter, - @FormParam("platform") String platform, - @FormParam("hostname") String hostname, - @FormParam("templatename") String templateName, - @FormParam("disksize") int diskSize, - @FormParam("memorysize") int memorySize, - @FormParam("cpucores") int cpuCores, - @FormParam("rootpassword") String rootPassword, - @FormParam("transfer") int transfer, - CreateServerOptions... options); + @MapBinder(CreateServerOptions.class) + ListenableFuture createServerWithHostnameAndRootPassword(ServerSpec serverSpec, + @PayloadParam("hostname") String hostname, @PayloadParam("rootpassword") String rootPassword, + CreateServerOptions... options); /** * @see ServerClient#cloneServer @@ -206,8 +202,7 @@ public interface ServerAsyncClient { @SelectJson("server") @Consumes(MediaType.APPLICATION_JSON) ListenableFuture cloneServer(@FormParam("serverid") String serverid, - @FormParam("hostname") String hostname, - CloneServerOptions... options); + @FormParam("hostname") String hostname, CloneServerOptions... options); /** * @see ServerClient#editServer @@ -237,6 +232,7 @@ public interface ServerAsyncClient { */ @POST @Path("/server/resourceusage/format/json") - void resourceUsage(@FormParam("serverid") String id, @FormParam("resource") String resource, @FormParam("resolution") String resolution); + void resourceUsage(@FormParam("serverid") String id, @FormParam("resource") String resource, + @FormParam("resolution") String resolution); } diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerClient.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerClient.java index 4077ea925d..6ed82830d2 100644 --- a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerClient.java +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/features/ServerClient.java @@ -28,6 +28,7 @@ import org.jclouds.glesys.domain.Console; import org.jclouds.glesys.domain.Server; import org.jclouds.glesys.domain.ServerDetails; import org.jclouds.glesys.domain.ServerLimit; +import org.jclouds.glesys.domain.ServerSpec; import org.jclouds.glesys.domain.ServerStatus; import org.jclouds.glesys.domain.Template; import org.jclouds.glesys.options.CloneServerOptions; @@ -41,7 +42,7 @@ import com.google.common.annotations.Beta; /** * Provides synchronous access to Server. *

- * + * * @author Adrian Cole * @author Adam Lowe * @see ServerAsyncClient @@ -52,7 +53,7 @@ public interface ServerClient { /** * Get a list of all servers on this account. - * + * * @return an account's associated server objects. */ Set listServers(); @@ -61,18 +62,21 @@ public interface ServerClient { * Get detailed information about a server such as hostname, hardware * configuration (cpu, memory and disk), ip addresses, cost, transfer, os and * more. - * - * @param id id of the server + * + * @param id + * id of the server * @return server or null if not found */ ServerDetails getServerDetails(String id); /** - * Get detailed information about a server status including up-time and hardware usage - * (cpu, disk, memory and bandwidth) - * - * @param id id of the server - * @param options optional parameters + * Get detailed information about a server status including up-time and + * hardware usage (cpu, disk, memory and bandwidth) + * + * @param id + * id of the server + * @param options + * optional parameters * @return the status of the server or null if not found */ ServerStatus getServerStatus(String id, ServerStatusOptions... options); @@ -80,130 +84,141 @@ public interface ServerClient { /** * Get detailed information about a server's limits (for OpenVZ only). *

- * - * @param id id of the server + * + * @param id + * id of the server * @return the requested information about the server or null if not found */ Map getServerLimits(String id); - /** * Get information about how to connect to a server via VNC - * - * @param id id of the server + * + * @param id + * id of the server * @return the requested information about the server or null if not found */ Console getConsole(String id); /** * Get information about the OS templates available - * + * * @return the set of information about each template */ Set