| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package org.jclouds.glesys.options; |
| 18 | |
| 19 | import static com.google.common.base.Objects.equal; |
| 20 | import static com.google.common.base.Objects.toStringHelper; |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | import static com.google.common.base.Predicates.instanceOf; |
| 24 | import static com.google.common.collect.Iterables.find; |
| 25 | |
| 26 | import java.util.Map; |
| 27 | |
| 28 | import org.jclouds.glesys.domain.ServerSpec; |
| 29 | import org.jclouds.http.HttpRequest; |
| 30 | import org.jclouds.io.payloads.UrlEncodedFormPayload; |
| 31 | import org.jclouds.rest.MapBinder; |
| 32 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 33 | |
| 34 | import com.google.common.base.Objects; |
| 35 | import com.google.common.collect.ImmutableMultimap; |
| 36 | |
| 37 | /** |
| 38 | * @author Adam Lowe |
| 39 | */ |
| 40 | public class CreateServerOptions implements MapBinder { |
| 41 | |
| 42 | private String ip; |
| 43 | private String description; |
| 44 | |
| 45 | @Override |
| 46 | public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) { |
| 47 | checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, |
| 48 | "this binder is only valid for GeneratedHttpRequests!"); |
| 49 | GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; |
| 50 | ImmutableMultimap.Builder<String, String> formParams = ImmutableMultimap.builder(); |
| 51 | for (Map.Entry<String, Object> entry : postParams.entrySet()) |
| 52 | formParams.put(entry.getKey(), (String) entry.getValue()); |
| 53 | ServerSpec serverSpec = ServerSpec.class.cast(find(gRequest.getInvocation().getArgs(), |
| 54 | instanceOf(ServerSpec.class))); |
| 55 | formParams.put("datacenter", serverSpec.getDatacenter()); |
| 56 | formParams.put("platform", serverSpec.getPlatform()); |
| 57 | formParams.put("templatename", serverSpec.getTemplateName()); |
| 58 | formParams.put("disksize", serverSpec.getDiskSizeGB() + ""); |
| 59 | formParams.put("memorysize", serverSpec.getMemorySizeMB() + ""); |
| 60 | formParams.put("cpucores", serverSpec.getCpuCores() + ""); |
| 61 | formParams.put("transfer", serverSpec.getTransferGB() + ""); |
| 62 | if (ip != null) |
| 63 | formParams.put("ip", ip); |
| 64 | if (description != null) |
| 65 | formParams.put("description", description); |
| 66 | |
| 67 | request.setPayload(new UrlEncodedFormPayload(formParams.build())); |
| 68 | return request; |
| 69 | } |
| 70 | |
| 71 | public static class Builder { |
| 72 | /** |
| 73 | * @see CreateServerOptions#description |
| 74 | */ |
| 75 | public static CreateServerOptions description(String primaryNameServer) { |
| 76 | CreateServerOptions options = new CreateServerOptions(); |
| 77 | return options.description(primaryNameServer); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @see CreateServerOptions#ip |
| 82 | */ |
| 83 | public static CreateServerOptions ip(String ip) { |
| 84 | CreateServerOptions options = new CreateServerOptions(); |
| 85 | return options.ip(ip); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param description |
| 91 | * the description of the server |
| 92 | */ |
| 93 | public CreateServerOptions description(String description) { |
| 94 | this.description = description; |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param ip |
| 100 | * the ip address to assign to the server |
| 101 | */ |
| 102 | public CreateServerOptions ip(String ip) { |
| 103 | this.ip = ip; |
| 104 | return this; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 109 | throw new IllegalArgumentException(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int hashCode() { |
| 114 | return Objects.hashCode(ip, description); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean equals(Object obj) { |
| 119 | if (obj == null) |
| 120 | return false; |
| 121 | if (!(obj instanceof CreateServerOptions)) |
| 122 | return false; |
| 123 | CreateServerOptions that = CreateServerOptions.class.cast(obj); |
| 124 | return equal(this.ip, that.ip) && equal(this.description, that.description); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String toString() { |
| 129 | return toStringHelper("").add("ip", ip).add("description", description).toString(); |
| 130 | } |
| 131 | |
| 132 | } |