| 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.compute.options; |
| 20 | |
| 21 | import java.util.Arrays; |
| 22 | |
| 23 | import org.jclouds.compute.options.TemplateOptions; |
| 24 | import org.jclouds.io.Payload; |
| 25 | import org.jclouds.util.Preconditions2; |
| 26 | import org.jclouds.vcloud.domain.network.IpAddressAllocationMode; |
| 27 | |
| 28 | /** |
| 29 | * Contains options supported in the {@code ComputeService#runNode} operation on |
| 30 | * the "vcloud" provider. <h2> |
| 31 | * Usage</h2> The recommended way to instantiate a VCloudTemplateOptions object |
| 32 | * is to statically import VCloudTemplateOptions.* and invoke a static creation |
| 33 | * method followed by an instance mutator (if needed): |
| 34 | * <p/> |
| 35 | * <code> |
| 36 | * import static org.jclouds.compute.options.VCloudTemplateOptions.Builder.*; |
| 37 | * <p/> |
| 38 | * ComputeService client = // get connection |
| 39 | * templateBuilder.options(inboundPorts(22, 80, 8080, 443)); |
| 40 | * Set<? extends NodeMetadata> set = client.runNodesWithTag(tag, 2, templateBuilder.build()); |
| 41 | * <code> |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | */ |
| 45 | public class VCloudTemplateOptions extends TemplateOptions implements Cloneable { |
| 46 | @Override |
| 47 | public VCloudTemplateOptions clone() { |
| 48 | VCloudTemplateOptions options = new VCloudTemplateOptions(); |
| 49 | copyTo(options); |
| 50 | return options; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void copyTo(TemplateOptions to) { |
| 55 | super.copyTo(to); |
| 56 | if (to instanceof VCloudTemplateOptions) { |
| 57 | VCloudTemplateOptions eTo = VCloudTemplateOptions.class.cast(to); |
| 58 | if (getCustomizationScript() != null) |
| 59 | eTo.customizationScript(getCustomizationScript()); |
| 60 | if (getDescription() != null) |
| 61 | eTo.description(getDescription()); |
| 62 | if (getIpAddressAllocationMode() != null) |
| 63 | eTo.ipAddressAllocationMode(getIpAddressAllocationMode()); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private String description = null; |
| 68 | private String customizationScript = null; |
| 69 | private IpAddressAllocationMode ipAddressAllocationMode = null; |
| 70 | |
| 71 | public static final VCloudTemplateOptions NONE = new VCloudTemplateOptions(); |
| 72 | |
| 73 | /** |
| 74 | * Optional description. Used for the Description of the vApp created by this |
| 75 | * instantiation. |
| 76 | */ |
| 77 | public VCloudTemplateOptions description(String description) { |
| 78 | this.description = description; |
| 79 | return this; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Specifies the customizationScript used to run instances with |
| 84 | */ |
| 85 | public VCloudTemplateOptions customizationScript(String customizationScript) { |
| 86 | Preconditions2.checkNotEmpty(customizationScript, "customizationScript must be non-empty"); |
| 87 | this.customizationScript = customizationScript; |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Specifies the ipAddressAllocationMode used to for network interfaces on |
| 93 | * the VMs |
| 94 | */ |
| 95 | public VCloudTemplateOptions ipAddressAllocationMode(IpAddressAllocationMode ipAddressAllocationMode) { |
| 96 | this.ipAddressAllocationMode = ipAddressAllocationMode; |
| 97 | return this; |
| 98 | } |
| 99 | |
| 100 | public static class Builder { |
| 101 | /** |
| 102 | * @see VCloudTemplateOptions#description |
| 103 | */ |
| 104 | public static VCloudTemplateOptions description(String description) { |
| 105 | VCloudTemplateOptions options = new VCloudTemplateOptions(); |
| 106 | return VCloudTemplateOptions.class.cast(options.description(description)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @see VCloudTemplateOptions#customizationScript |
| 111 | */ |
| 112 | public static VCloudTemplateOptions customizationScript(String customizationScript) { |
| 113 | VCloudTemplateOptions options = new VCloudTemplateOptions(); |
| 114 | return VCloudTemplateOptions.class.cast(options.customizationScript(customizationScript)); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @see VCloudTemplateOptions#ipAddressAllocationMode |
| 119 | */ |
| 120 | public static VCloudTemplateOptions ipAddressAllocationMode(IpAddressAllocationMode ipAddressAllocationMode) { |
| 121 | VCloudTemplateOptions options = new VCloudTemplateOptions(); |
| 122 | return VCloudTemplateOptions.class.cast(options.ipAddressAllocationMode(ipAddressAllocationMode)); |
| 123 | } |
| 124 | |
| 125 | // methods that only facilitate returning the correct object type |
| 126 | /** |
| 127 | * @see TemplateOptions#inboundPorts |
| 128 | */ |
| 129 | public static VCloudTemplateOptions inboundPorts(int... ports) { |
| 130 | VCloudTemplateOptions options = new VCloudTemplateOptions(); |
| 131 | return VCloudTemplateOptions.class.cast(options.inboundPorts(ports)); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @see TemplateOptions#port |
| 136 | */ |
| 137 | public static VCloudTemplateOptions blockOnPort(int port, int seconds) { |
| 138 | VCloudTemplateOptions options = new VCloudTemplateOptions(); |
| 139 | return VCloudTemplateOptions.class.cast(options.blockOnPort(port, seconds)); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @see TemplateOptions#runScript |
| 144 | */ |
| 145 | public static VCloudTemplateOptions runScript(Payload script) { |
| 146 | VCloudTemplateOptions options = new VCloudTemplateOptions(); |
| 147 | return VCloudTemplateOptions.class.cast(options.runScript(script)); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @see TemplateOptions#installPrivateKey |
| 152 | */ |
| 153 | public static VCloudTemplateOptions installPrivateKey(Payload rsaKey) { |
| 154 | VCloudTemplateOptions options = new VCloudTemplateOptions(); |
| 155 | return VCloudTemplateOptions.class.cast(options.installPrivateKey(rsaKey)); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @see TemplateOptions#authorizePublicKey |
| 160 | */ |
| 161 | public static VCloudTemplateOptions authorizePublicKey(Payload rsaKey) { |
| 162 | VCloudTemplateOptions options = new VCloudTemplateOptions(); |
| 163 | return VCloudTemplateOptions.class.cast(options.authorizePublicKey(rsaKey)); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @see TemplateOptions#withDetails |
| 168 | */ |
| 169 | public static VCloudTemplateOptions withDetails() { |
| 170 | VCloudTemplateOptions options = new VCloudTemplateOptions(); |
| 171 | return VCloudTemplateOptions.class.cast(options.withMetadata()); |
| 172 | } |
| 173 | |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @return description of the vApp |
| 178 | */ |
| 179 | public String getDescription() { |
| 180 | return description; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return customizationScript on the vms |
| 185 | */ |
| 186 | public String getCustomizationScript() { |
| 187 | return customizationScript; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @return ipAddressAllocationMode on the vms |
| 192 | */ |
| 193 | public IpAddressAllocationMode getIpAddressAllocationMode() { |
| 194 | return ipAddressAllocationMode; |
| 195 | } |
| 196 | |
| 197 | // methods that only facilitate returning the correct object type |
| 198 | |
| 199 | /** |
| 200 | * @see TemplateOptions#blockOnPort |
| 201 | */ |
| 202 | @Override |
| 203 | public VCloudTemplateOptions blockOnPort(int port, int seconds) { |
| 204 | return VCloudTemplateOptions.class.cast(super.blockOnPort(port, seconds)); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * |
| 209 | * special thing is that we do assume if you are passing groups that you have |
| 210 | * everything you need already defined. for example, our option inboundPorts |
| 211 | * normally creates ingress rules accordingly but if we notice you've |
| 212 | * specified securityGroups, we do not mess with rules at all |
| 213 | * |
| 214 | * @see TemplateOptions#inboundPorts |
| 215 | */ |
| 216 | @Override |
| 217 | public VCloudTemplateOptions inboundPorts(int... ports) { |
| 218 | return VCloudTemplateOptions.class.cast(super.inboundPorts(ports)); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @see TemplateOptions#authorizePublicKey(String) |
| 223 | */ |
| 224 | @Override |
| 225 | public VCloudTemplateOptions authorizePublicKey(String publicKey) { |
| 226 | return VCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey)); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @see TemplateOptions#authorizePublicKey(Payload) |
| 231 | */ |
| 232 | @Override |
| 233 | @Deprecated |
| 234 | public VCloudTemplateOptions authorizePublicKey(Payload publicKey) { |
| 235 | return VCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey)); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * @see TemplateOptions#installPrivateKey(String) |
| 240 | */ |
| 241 | @Override |
| 242 | public VCloudTemplateOptions installPrivateKey(String privateKey) { |
| 243 | return VCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey)); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @see TemplateOptions#installPrivateKey(Payload) |
| 248 | */ |
| 249 | @Override |
| 250 | @Deprecated |
| 251 | public VCloudTemplateOptions installPrivateKey(Payload privateKey) { |
| 252 | return VCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey)); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * @see TemplateOptions#runScript(Payload) |
| 257 | */ |
| 258 | @Override |
| 259 | public VCloudTemplateOptions runScript(Payload script) { |
| 260 | return VCloudTemplateOptions.class.cast(super.runScript(script)); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @see TemplateOptions#runScript(byte[]) |
| 265 | */ |
| 266 | @Override |
| 267 | @Deprecated |
| 268 | public VCloudTemplateOptions runScript(byte[] script) { |
| 269 | return VCloudTemplateOptions.class.cast(super.runScript(script)); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @see TemplateOptions#withMetadata |
| 274 | */ |
| 275 | @Override |
| 276 | public VCloudTemplateOptions withMetadata() { |
| 277 | return VCloudTemplateOptions.class.cast(super.withMetadata()); |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public int hashCode() { |
| 282 | final int prime = 31; |
| 283 | int result = super.hashCode(); |
| 284 | result = prime * result + ((customizationScript == null) ? 0 : customizationScript.hashCode()); |
| 285 | result = prime * result + ((description == null) ? 0 : description.hashCode()); |
| 286 | result = prime * result + ((ipAddressAllocationMode == null) ? 0 : ipAddressAllocationMode.hashCode()); |
| 287 | return result; |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public boolean equals(Object obj) { |
| 292 | if (this == obj) |
| 293 | return true; |
| 294 | if (!super.equals(obj)) |
| 295 | return false; |
| 296 | if (getClass() != obj.getClass()) |
| 297 | return false; |
| 298 | VCloudTemplateOptions other = (VCloudTemplateOptions) obj; |
| 299 | if (customizationScript == null) { |
| 300 | if (other.customizationScript != null) |
| 301 | return false; |
| 302 | } else if (!customizationScript.equals(other.customizationScript)) |
| 303 | return false; |
| 304 | if (description == null) { |
| 305 | if (other.description != null) |
| 306 | return false; |
| 307 | } else if (!description.equals(other.description)) |
| 308 | return false; |
| 309 | if (ipAddressAllocationMode != other.ipAddressAllocationMode) |
| 310 | return false; |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public String toString() { |
| 316 | return "[customizationScript=" + (customizationScript != null) + ", description=" + description |
| 317 | + ", ipAddressAllocationMode=" + ipAddressAllocationMode + ", inboundPorts=" |
| 318 | + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey=" |
| 319 | + (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds |
| 320 | + ", metadata/details: " + includeMetadata + "]"; |
| 321 | } |
| 322 | |
| 323 | } |