| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.vcloud.terremark.compute.options; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Preconditions.checkState; |
| 23 | |
| 24 | import java.util.Arrays; |
| 25 | |
| 26 | import org.jclouds.compute.options.TemplateOptions; |
| 27 | import org.jclouds.io.Payload; |
| 28 | import org.jclouds.util.Preconditions2; |
| 29 | |
| 30 | /** |
| 31 | * Contains options supported in the {@code ComputeService#runNode} operation on the |
| 32 | * "trmk-vcloudexpress" provider. <h2> |
| 33 | * Usage</h2> The recommended way to instantiate a TerremarkVCloudTemplateOptions object is to |
| 34 | * statically import TerremarkVCloudTemplateOptions.* and invoke a static creation method followed |
| 35 | * by an instance mutator (if needed): |
| 36 | * <p/> |
| 37 | * <code> |
| 38 | * import static org.jclouds.vcloud.terremark.compute.options.TerremarkVCloudTemplateOptions.Builder.*; |
| 39 | * <p/> |
| 40 | * ComputeService client = // get connection |
| 41 | * templateBuilder.options(inboundPorts(22, 80, 8080, 443)); |
| 42 | * Set<? extends NodeMetadata> set = client.runNodesWithTag(tag, 2, templateBuilder.build()); |
| 43 | * <code> |
| 44 | * |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | public class TerremarkVCloudTemplateOptions extends TemplateOptions implements Cloneable { |
| 48 | @Override |
| 49 | public TerremarkVCloudTemplateOptions clone() { |
| 50 | TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); |
| 51 | copyTo(options); |
| 52 | return options; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void copyTo(TemplateOptions to) { |
| 57 | super.copyTo(to); |
| 58 | if (to instanceof TerremarkVCloudTemplateOptions) { |
| 59 | TerremarkVCloudTemplateOptions eTo = TerremarkVCloudTemplateOptions.class.cast(to); |
| 60 | if (noKeyPair) |
| 61 | eTo.noKeyPair(); |
| 62 | if (keyPair != null) |
| 63 | eTo.keyPair = keyPair; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private String keyPair = null; |
| 68 | private boolean noKeyPair; |
| 69 | |
| 70 | public static final TerremarkVCloudTemplateOptions NONE = new TerremarkVCloudTemplateOptions(); |
| 71 | |
| 72 | /** |
| 73 | * Specifies the keypair used to run instances with |
| 74 | */ |
| 75 | public TerremarkVCloudTemplateOptions sshKeyFingerprint(String keyPair) { |
| 76 | checkNotNull(keyPair, "use noKeyPair option to request boot without a keypair"); |
| 77 | checkState(!noKeyPair, "you cannot specify both options keyPair and noKeyPair"); |
| 78 | Preconditions2.checkNotEmpty(keyPair, "keypair must be non-empty"); |
| 79 | this.keyPair = keyPair; |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Do not use a keypair on instances |
| 85 | */ |
| 86 | public TerremarkVCloudTemplateOptions noKeyPair() { |
| 87 | checkState(keyPair == null, "you cannot specify both options keyPair and noKeyPair"); |
| 88 | this.noKeyPair = true; |
| 89 | return this; |
| 90 | } |
| 91 | |
| 92 | public static class Builder { |
| 93 | |
| 94 | /** |
| 95 | * @see TerremarkVCloudTemplateOptions#sshKeyFingerprint |
| 96 | */ |
| 97 | public static TerremarkVCloudTemplateOptions sshKeyFingerprint(String keyPair) { |
| 98 | TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); |
| 99 | return TerremarkVCloudTemplateOptions.class.cast(options.sshKeyFingerprint(keyPair)); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @see TerremarkVCloudTemplateOptions#noKeyPair |
| 104 | */ |
| 105 | public static TerremarkVCloudTemplateOptions noKeyPair() { |
| 106 | TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); |
| 107 | return TerremarkVCloudTemplateOptions.class.cast(options.noKeyPair()); |
| 108 | } |
| 109 | |
| 110 | // methods that only facilitate returning the correct object type |
| 111 | /** |
| 112 | * @see TemplateOptions#inboundPorts |
| 113 | */ |
| 114 | public static TerremarkVCloudTemplateOptions inboundPorts(int... ports) { |
| 115 | TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); |
| 116 | return TerremarkVCloudTemplateOptions.class.cast(options.inboundPorts(ports)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @see TemplateOptions#port |
| 121 | */ |
| 122 | public static TerremarkVCloudTemplateOptions blockOnPort(int port, int seconds) { |
| 123 | TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); |
| 124 | return TerremarkVCloudTemplateOptions.class.cast(options.blockOnPort(port, seconds)); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @see TemplateOptions#blockUntilRunning |
| 129 | */ |
| 130 | public static TerremarkVCloudTemplateOptions blockUntilRunning(boolean blockUntilRunning) { |
| 131 | TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); |
| 132 | return TerremarkVCloudTemplateOptions.class.cast(options.blockUntilRunning(blockUntilRunning)); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @see TemplateOptions#runScript |
| 137 | */ |
| 138 | public static TerremarkVCloudTemplateOptions runScript(byte[] script) { |
| 139 | TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); |
| 140 | return TerremarkVCloudTemplateOptions.class.cast(options.runScript(script)); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @see TemplateOptions#installPrivateKey |
| 145 | */ |
| 146 | public static TerremarkVCloudTemplateOptions installPrivateKey(String rsaKey) { |
| 147 | TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); |
| 148 | return TerremarkVCloudTemplateOptions.class.cast(options.installPrivateKey(rsaKey)); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @see TemplateOptions#authorizePublicKey |
| 153 | */ |
| 154 | public static TerremarkVCloudTemplateOptions authorizePublicKey(String rsaKey) { |
| 155 | TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); |
| 156 | return TerremarkVCloudTemplateOptions.class.cast(options.authorizePublicKey(rsaKey)); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @see TemplateOptions#withDetails |
| 161 | */ |
| 162 | public static TerremarkVCloudTemplateOptions withDetails() { |
| 163 | TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); |
| 164 | return TerremarkVCloudTemplateOptions.class.cast(options.withMetadata()); |
| 165 | } |
| 166 | |
| 167 | } |
| 168 | |
| 169 | // methods that only facilitate returning the correct object type |
| 170 | |
| 171 | /** |
| 172 | * @see TemplateOptions#blockOnPort |
| 173 | */ |
| 174 | @Override |
| 175 | public TerremarkVCloudTemplateOptions blockOnPort(int port, int seconds) { |
| 176 | return TerremarkVCloudTemplateOptions.class.cast(super.blockOnPort(port, seconds)); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * |
| 181 | * special thing is that we do assume if you are passing groups that you have everything you need |
| 182 | * already defined. for example, our option inboundPorts normally creates ingress rules |
| 183 | * accordingly but if we notice you've specified securityGroups, we do not mess with rules at all |
| 184 | * |
| 185 | * @see TemplateOptions#inboundPorts |
| 186 | */ |
| 187 | @Override |
| 188 | public TerremarkVCloudTemplateOptions inboundPorts(int... ports) { |
| 189 | return TerremarkVCloudTemplateOptions.class.cast(super.inboundPorts(ports)); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @see TemplateOptions#authorizePublicKey(String) |
| 194 | */ |
| 195 | @Override |
| 196 | public TerremarkVCloudTemplateOptions authorizePublicKey(String publicKey) { |
| 197 | return TerremarkVCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey)); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @see TemplateOptions#authorizePublicKey(Payload) |
| 202 | */ |
| 203 | @Override |
| 204 | @Deprecated |
| 205 | public TerremarkVCloudTemplateOptions authorizePublicKey(Payload publicKey) { |
| 206 | return TerremarkVCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey)); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @see TemplateOptions#installPrivateKey(String) |
| 211 | */ |
| 212 | @Override |
| 213 | public TerremarkVCloudTemplateOptions installPrivateKey(String privateKey) { |
| 214 | return TerremarkVCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey)); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @see TemplateOptions#installPrivateKey(Payload) |
| 219 | */ |
| 220 | @Override |
| 221 | @Deprecated |
| 222 | public TerremarkVCloudTemplateOptions installPrivateKey(Payload privateKey) { |
| 223 | return TerremarkVCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey)); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @see TemplateOptions#runScript(Payload) |
| 228 | */ |
| 229 | @Override |
| 230 | public TerremarkVCloudTemplateOptions runScript(Payload script) { |
| 231 | return TerremarkVCloudTemplateOptions.class.cast(super.runScript(script)); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @see TemplateOptions#runScript(byte[]) |
| 236 | */ |
| 237 | @Override |
| 238 | @Deprecated |
| 239 | public TerremarkVCloudTemplateOptions runScript(byte[] script) { |
| 240 | return TerremarkVCloudTemplateOptions.class.cast(super.runScript(script)); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @see TemplateOptions#withMetadata |
| 245 | */ |
| 246 | @Override |
| 247 | public TerremarkVCloudTemplateOptions withMetadata() { |
| 248 | return TerremarkVCloudTemplateOptions.class.cast(super.withMetadata()); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @return keyPair to use when running the instance or null, to generate a keypair. |
| 253 | */ |
| 254 | public String getSshKeyFingerprint() { |
| 255 | return keyPair; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @return true (default) if we are supposed to use a keypair |
| 260 | */ |
| 261 | public boolean shouldAutomaticallyCreateKeyPair() { |
| 262 | return !noKeyPair; |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public int hashCode() { |
| 267 | final int prime = 31; |
| 268 | int result = super.hashCode(); |
| 269 | result = prime * result + ((keyPair == null) ? 0 : keyPair.hashCode()); |
| 270 | result = prime * result + (noKeyPair ? 1231 : 1237); |
| 271 | return result; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public boolean equals(Object obj) { |
| 276 | if (this == obj) |
| 277 | return true; |
| 278 | if (!super.equals(obj)) |
| 279 | return false; |
| 280 | if (getClass() != obj.getClass()) |
| 281 | return false; |
| 282 | TerremarkVCloudTemplateOptions other = (TerremarkVCloudTemplateOptions) obj; |
| 283 | if (keyPair == null) { |
| 284 | if (other.keyPair != null) |
| 285 | return false; |
| 286 | } else if (!keyPair.equals(other.keyPair)) |
| 287 | return false; |
| 288 | if (noKeyPair != other.noKeyPair) |
| 289 | return false; |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public String toString() { |
| 295 | return "TerremarkVCloudTemplateOptions [keyPair=" + keyPair + ", noKeyPair=" + noKeyPair + ", inboundPorts=" |
| 296 | + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey=" |
| 297 | + (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds |
| 298 | + ", metadata/details: " + includeMetadata + "]"; |
| 299 | } |
| 300 | |
| 301 | } |