| 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.compute.options; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkArgument; |
| 20 | import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | |
| 22 | import java.util.Map; |
| 23 | |
| 24 | import org.jclouds.compute.options.TemplateOptions; |
| 25 | import org.jclouds.glesys.domain.ServerSpec; |
| 26 | |
| 27 | import com.google.common.base.Objects.ToStringHelper; |
| 28 | import com.google.common.net.InetAddresses; |
| 29 | |
| 30 | /** |
| 31 | * Contains options supported by the |
| 32 | * {@link ComputeService#createNodesInGroup(String, int, TemplateOptions)} and |
| 33 | * {@link ComputeService#createNodesInGroup(String, int, TemplateOptions)} |
| 34 | * operations on the <em>glesys</em> provider. |
| 35 | * |
| 36 | * <h2>Usage</h2> The recommended way to instantiate a |
| 37 | * {@link GleSYSTemplateOptions} object is to statically import |
| 38 | * {@code GleSYSTemplateOptions.*} and invoke a static creation method followed |
| 39 | * by an instance mutator (if needed): |
| 40 | * <p> |
| 41 | * |
| 42 | * <pre> |
| 43 | * import static org.jclouds.compute.options.GleSYSTemplateOptions.Builder.*; |
| 44 | * ComputeService api = // get connection |
| 45 | * templateBuilder.options(rootPassword("caQu5rou")); |
| 46 | * Set<? extends NodeMetadata> set = api.createNodesInGroup(tag, 2, templateBuilder.build()); |
| 47 | * </pre> |
| 48 | * |
| 49 | * @author Adrian Cole |
| 50 | */ |
| 51 | public class GleSYSTemplateOptions extends TemplateOptions implements Cloneable { |
| 52 | |
| 53 | /** |
| 54 | * The IP address to assign to the new node instance. If set to " |
| 55 | * <code>any</code>" the node will be automatically assigned a free IP |
| 56 | * address. |
| 57 | */ |
| 58 | protected String ip = "any"; |
| 59 | /** |
| 60 | * The password to set for the root user on the created server instance. If |
| 61 | * left unspecified, a random password will be assigned. |
| 62 | */ |
| 63 | protected String rootPassword = null; |
| 64 | |
| 65 | /** The monthly data transfer limit (in GB) for the server. */ |
| 66 | protected int transferGB = 50; |
| 67 | |
| 68 | @Override |
| 69 | public GleSYSTemplateOptions clone() { |
| 70 | GleSYSTemplateOptions options = new GleSYSTemplateOptions(); |
| 71 | copyTo(options); |
| 72 | return options; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void copyTo(TemplateOptions to) { |
| 77 | super.copyTo(to); |
| 78 | if (to instanceof GleSYSTemplateOptions) { |
| 79 | GleSYSTemplateOptions copy = GleSYSTemplateOptions.class.cast(to); |
| 80 | copy.ip(ip); |
| 81 | copy.rootPassword(rootPassword); |
| 82 | copy.transferGB(transferGB); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Sets the IP address to assign to the new server instance. If set to " |
| 88 | * <code>any</code>" the server will be automatically assigned a free IP |
| 89 | * address. |
| 90 | * |
| 91 | * @see ServerApi#createWithHostnameAndRootPassword |
| 92 | * @see InetAddresses#isInetAddress |
| 93 | */ |
| 94 | public GleSYSTemplateOptions ip(String ip) { |
| 95 | checkNotNull(ip); |
| 96 | checkArgument("any".equals(ip) || InetAddresses.isInetAddress(ip), "ip %s is not valid", ip); |
| 97 | this.ip = ip; |
| 98 | return this; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @return the IP address to assign to the new server instance. |
| 103 | */ |
| 104 | public String getIp() { |
| 105 | return ip; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Sets the password for the root user on the created server instance. If |
| 110 | * left unspecified, a random password will be assigned. |
| 111 | * |
| 112 | * @see ServerApi#createWithHostnameAndRootPassword |
| 113 | */ |
| 114 | public GleSYSTemplateOptions rootPassword(String rootPassword) { |
| 115 | checkNotNull(rootPassword, "root password cannot be null"); |
| 116 | this.rootPassword = rootPassword; |
| 117 | return this; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @return the password set for the root user or <code>null</code> if none is |
| 122 | * set (and a random password will be assigned). |
| 123 | */ |
| 124 | public String getRootPassword() { |
| 125 | return rootPassword; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @return <code>true</code> if a root password has been specified. |
| 130 | */ |
| 131 | public boolean hasRootPassword() { |
| 132 | return rootPassword != null; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Sets the monthly data transfer limit (in GB) for the server. |
| 137 | * |
| 138 | * @see ServerSpec#getTransferGB() |
| 139 | */ |
| 140 | public GleSYSTemplateOptions transferGB(int transferGB) { |
| 141 | checkArgument(transferGB >= 0, "transferGB value must be >= 0", transferGB); |
| 142 | this.transferGB = transferGB; |
| 143 | return this; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @return the monthly data transfer limit (in GB) for the server. |
| 148 | */ |
| 149 | public int getTransferGB() { |
| 150 | return transferGB; |
| 151 | } |
| 152 | |
| 153 | public static class Builder { |
| 154 | |
| 155 | /** |
| 156 | * @see GleSYSTemplateOptions#ip |
| 157 | */ |
| 158 | public static GleSYSTemplateOptions ip(String ip) { |
| 159 | GleSYSTemplateOptions options = new GleSYSTemplateOptions(); |
| 160 | return GleSYSTemplateOptions.class.cast(options.ip(ip)); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @see GleSYSTemplateOptions#rootPassword |
| 165 | */ |
| 166 | public static GleSYSTemplateOptions rootPassword(String rootPassword) { |
| 167 | GleSYSTemplateOptions options = new GleSYSTemplateOptions(); |
| 168 | return GleSYSTemplateOptions.class.cast(options.rootPassword(rootPassword)); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @see GleSYSTemplateOptions#transferGB |
| 173 | */ |
| 174 | public static GleSYSTemplateOptions transferGB(int transferGB) { |
| 175 | GleSYSTemplateOptions options = new GleSYSTemplateOptions(); |
| 176 | return GleSYSTemplateOptions.class.cast(options.transferGB(transferGB)); |
| 177 | } |
| 178 | |
| 179 | // methods that only facilitate returning the correct object type |
| 180 | |
| 181 | /** |
| 182 | * @see TemplateOptions#inboundPorts(int...) |
| 183 | */ |
| 184 | public static GleSYSTemplateOptions inboundPorts(int... ports) { |
| 185 | GleSYSTemplateOptions options = new GleSYSTemplateOptions(); |
| 186 | return GleSYSTemplateOptions.class.cast(options.inboundPorts(ports)); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @see TemplateOptions#blockOnPort(int, int) |
| 191 | */ |
| 192 | public static GleSYSTemplateOptions blockOnPort(int port, int seconds) { |
| 193 | GleSYSTemplateOptions options = new GleSYSTemplateOptions(); |
| 194 | return GleSYSTemplateOptions.class.cast(options.blockOnPort(port, seconds)); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @see TemplateOptions#userMetadata(Map) |
| 199 | */ |
| 200 | public static GleSYSTemplateOptions userMetadata(Map<String, String> userMetadata) { |
| 201 | GleSYSTemplateOptions options = new GleSYSTemplateOptions(); |
| 202 | return GleSYSTemplateOptions.class.cast(options.userMetadata(userMetadata)); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @see TemplateOptions#userMetadata(String, String) |
| 207 | */ |
| 208 | public static GleSYSTemplateOptions userMetadata(String key, String value) { |
| 209 | GleSYSTemplateOptions options = new GleSYSTemplateOptions(); |
| 210 | return GleSYSTemplateOptions.class.cast(options.userMetadata(key, value)); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // methods that only facilitate returning the correct object type |
| 215 | |
| 216 | /** |
| 217 | * @see TemplateOptions#blockOnPort(int, int) |
| 218 | */ |
| 219 | @Override |
| 220 | public GleSYSTemplateOptions blockOnPort(int port, int seconds) { |
| 221 | return GleSYSTemplateOptions.class.cast(super.blockOnPort(port, seconds)); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @see TemplateOptions#inboundPorts(int...) |
| 226 | */ |
| 227 | @Override |
| 228 | public GleSYSTemplateOptions inboundPorts(int... ports) { |
| 229 | return GleSYSTemplateOptions.class.cast(super.inboundPorts(ports)); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * @see TemplateOptions#authorizePublicKey(String) |
| 234 | */ |
| 235 | @Override |
| 236 | public GleSYSTemplateOptions authorizePublicKey(String publicKey) { |
| 237 | return GleSYSTemplateOptions.class.cast(super.authorizePublicKey(publicKey)); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @see TemplateOptions#installPrivateKey(String) |
| 242 | */ |
| 243 | @Override |
| 244 | public GleSYSTemplateOptions installPrivateKey(String privateKey) { |
| 245 | return GleSYSTemplateOptions.class.cast(super.installPrivateKey(privateKey)); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * {@inheritDoc} |
| 250 | */ |
| 251 | @Override |
| 252 | public GleSYSTemplateOptions userMetadata(Map<String, String> userMetadata) { |
| 253 | return GleSYSTemplateOptions.class.cast(super.userMetadata(userMetadata)); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * {@inheritDoc} |
| 258 | */ |
| 259 | @Override |
| 260 | public GleSYSTemplateOptions userMetadata(String key, String value) { |
| 261 | return GleSYSTemplateOptions.class.cast(super.userMetadata(key, value)); |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public ToStringHelper string() { |
| 266 | ToStringHelper stringHelper = super.string(); |
| 267 | |
| 268 | stringHelper.add("transferGB", this.transferGB); |
| 269 | stringHelper.add("ip", this.ip); |
| 270 | if (this.hasRootPassword()) { |
| 271 | stringHelper.add("rootPasswordPresent", true); |
| 272 | } |
| 273 | |
| 274 | return stringHelper; |
| 275 | } |
| 276 | |
| 277 | } |