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