| 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.ec2.compute.options; |
| 20 | |
| 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.Preconditions.checkState; |
| 24 | |
| 25 | import java.util.Arrays; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import javax.annotation.Nullable; |
| 29 | |
| 30 | import org.jclouds.compute.options.TemplateOptions; |
| 31 | import org.jclouds.domain.Credentials; |
| 32 | import org.jclouds.ec2.domain.BlockDeviceMapping; |
| 33 | import org.jclouds.ec2.domain.BlockDeviceMapping.MapEBSSnapshotToDevice; |
| 34 | import org.jclouds.ec2.domain.BlockDeviceMapping.MapEphemeralDeviceToDevice; |
| 35 | import org.jclouds.ec2.domain.BlockDeviceMapping.MapNewVolumeToDevice; |
| 36 | import org.jclouds.ec2.domain.BlockDeviceMapping.UnmapDeviceNamed; |
| 37 | import org.jclouds.io.Payload; |
| 38 | import org.jclouds.scriptbuilder.domain.Statement; |
| 39 | import org.jclouds.util.Preconditions2; |
| 40 | |
| 41 | import com.google.common.collect.ImmutableSet; |
| 42 | import com.google.common.collect.Iterables; |
| 43 | |
| 44 | /** |
| 45 | * Contains options supported in the {@code ComputeService#runNode} operation on the "ec2" provider. |
| 46 | * <h2> |
| 47 | * Usage</h2> The recommended way to instantiate a EC2TemplateOptions object is to statically import |
| 48 | * EC2TemplateOptions.* and invoke a static creation method followed by an instance mutator (if |
| 49 | * needed): |
| 50 | * <p/> |
| 51 | * <code> |
| 52 | * import static org.jclouds.aws.ec2.compute.options.EC2TemplateOptions.Builder.*; |
| 53 | * <p/> |
| 54 | * ComputeService client = // get connection |
| 55 | * templateBuilder.options(inboundPorts(22, 80, 8080, 443)); |
| 56 | * Set<? extends NodeMetadata> set = client.runNodesWithTag(tag, 2, templateBuilder.build()); |
| 57 | * <code> |
| 58 | * |
| 59 | * @author Adrian Cole |
| 60 | */ |
| 61 | public class EC2TemplateOptions extends TemplateOptions implements Cloneable { |
| 62 | @Override |
| 63 | public EC2TemplateOptions clone() { |
| 64 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 65 | copyTo(options); |
| 66 | return options; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void copyTo(TemplateOptions to) { |
| 71 | super.copyTo(to); |
| 72 | if (to instanceof EC2TemplateOptions) { |
| 73 | EC2TemplateOptions eTo = EC2TemplateOptions.class.cast(to); |
| 74 | if (getGroupIds().size() > 0) |
| 75 | eTo.securityGroups(getGroupIds()); |
| 76 | if (getKeyPair() != null) |
| 77 | eTo.keyPair(getKeyPair()); |
| 78 | if (getBlockDeviceMappings().size() > 0) |
| 79 | eTo.blockDeviceMappings(getBlockDeviceMappings()); |
| 80 | if (!shouldAutomaticallyCreateKeyPair()) |
| 81 | eTo.noKeyPair(); |
| 82 | if (getUserData() != null) |
| 83 | eTo.userData(getUserData()); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private Set<String> groupIds = ImmutableSet.of(); |
| 88 | private String keyPair = null; |
| 89 | private boolean noKeyPair; |
| 90 | private byte[] userData; |
| 91 | private ImmutableSet.Builder<BlockDeviceMapping> blockDeviceMappings = ImmutableSet.<BlockDeviceMapping> builder(); |
| 92 | |
| 93 | public static final EC2TemplateOptions NONE = new EC2TemplateOptions(); |
| 94 | |
| 95 | /** |
| 96 | * |
| 97 | * @see EC2TemplateOptions#securityGroups(Iterable<String>) |
| 98 | */ |
| 99 | public EC2TemplateOptions securityGroups(String... groupIds) { |
| 100 | return securityGroups(ImmutableSet.copyOf(groupIds)); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Specifies the security groups to be used for nodes with this template |
| 105 | */ |
| 106 | public EC2TemplateOptions securityGroups(Iterable<String> groupIds) { |
| 107 | checkArgument(Iterables.size(groupIds) > 0, "you must specify at least one security group"); |
| 108 | for (String groupId : groupIds) |
| 109 | Preconditions2.checkNotEmpty(groupId, "all security groups must be non-empty"); |
| 110 | this.groupIds = ImmutableSet.copyOf(groupIds); |
| 111 | return this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Unencoded data |
| 116 | */ |
| 117 | public EC2TemplateOptions userData(byte[] unencodedData) { |
| 118 | checkArgument(checkNotNull(unencodedData, "unencodedData").length <= 16 * 1024, |
| 119 | "userData cannot be larger than 16kb"); |
| 120 | this.userData = unencodedData; |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Specifies the keypair used to run instances with |
| 126 | */ |
| 127 | public EC2TemplateOptions keyPair(String keyPair) { |
| 128 | checkNotNull(keyPair, "use noKeyPair option to request boot without a keypair"); |
| 129 | checkState(!noKeyPair, "you cannot specify both options keyPair and noKeyPair"); |
| 130 | Preconditions2.checkNotEmpty(keyPair, "keypair must be non-empty"); |
| 131 | this.keyPair = keyPair; |
| 132 | return this; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Do not use a keypair on instances |
| 137 | */ |
| 138 | public EC2TemplateOptions noKeyPair() { |
| 139 | checkState(keyPair == null, "you cannot specify both options keyPair and noKeyPair"); |
| 140 | this.noKeyPair = true; |
| 141 | return this; |
| 142 | } |
| 143 | |
| 144 | public EC2TemplateOptions mapEBSSnapshotToDeviceName(String deviceName, String snapshotId, |
| 145 | @Nullable Integer sizeInGib, boolean deleteOnTermination) { |
| 146 | blockDeviceMappings.add(new MapEBSSnapshotToDevice(deviceName, snapshotId, sizeInGib, deleteOnTermination)); |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | public EC2TemplateOptions mapNewVolumeToDeviceName(String deviceName, int sizeInGib, boolean deleteOnTermination) { |
| 151 | blockDeviceMappings.add(new MapNewVolumeToDevice(deviceName, sizeInGib, deleteOnTermination)); |
| 152 | return this; |
| 153 | } |
| 154 | |
| 155 | public EC2TemplateOptions mapEphemeralDeviceToDeviceName(String deviceName, String virtualName) { |
| 156 | blockDeviceMappings.add(new MapEphemeralDeviceToDevice(deviceName, virtualName)); |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | public EC2TemplateOptions unmapDeviceNamed(String deviceName) { |
| 161 | blockDeviceMappings.add(new UnmapDeviceNamed(deviceName)); |
| 162 | return this; |
| 163 | } |
| 164 | |
| 165 | public EC2TemplateOptions blockDeviceMappings(Iterable<? extends BlockDeviceMapping> blockDeviceMappings) { |
| 166 | this.blockDeviceMappings.addAll(checkNotNull(blockDeviceMappings, "blockDeviceMappings")); |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | public static class Builder { |
| 171 | /** |
| 172 | * @see EC2TemplateOptions#blockDeviceMappings |
| 173 | */ |
| 174 | public static EC2TemplateOptions blockDeviceMappings(Set<? extends BlockDeviceMapping> blockDeviceMappings) { |
| 175 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 176 | return options.blockDeviceMappings(blockDeviceMappings); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @see EC2TemplateOptions#mapEBSSnapshotToDeviceName |
| 181 | */ |
| 182 | public static EC2TemplateOptions mapEBSSnapshotToDeviceName(String deviceName, String snapshotId, |
| 183 | @Nullable Integer sizeInGib, boolean deleteOnTermination) { |
| 184 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 185 | return options.mapEBSSnapshotToDeviceName(deviceName, snapshotId, sizeInGib, deleteOnTermination); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @see EC2TemplateOptions#mapNewVolumeToDeviceName |
| 190 | */ |
| 191 | public static EC2TemplateOptions mapNewVolumeToDeviceName(String deviceName, int sizeInGib, |
| 192 | boolean deleteOnTermination) { |
| 193 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 194 | return options.mapNewVolumeToDeviceName(deviceName, sizeInGib, deleteOnTermination); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @see EC2TemplateOptions#mapEphemeralDeviceToDeviceName |
| 199 | */ |
| 200 | public static EC2TemplateOptions mapEphemeralDeviceToDeviceName(String deviceName, String virtualName) { |
| 201 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 202 | return options.mapEphemeralDeviceToDeviceName(deviceName, virtualName); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @see EC2TemplateOptions#unmapDeviceNamed |
| 207 | */ |
| 208 | public static EC2TemplateOptions unmapDeviceNamed(String deviceName) { |
| 209 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 210 | return options.unmapDeviceNamed(deviceName); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @see EC2TemplateOptions#securityGroups(Iterable<String>) |
| 215 | */ |
| 216 | public static EC2TemplateOptions securityGroups(String... groupIds) { |
| 217 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 218 | return EC2TemplateOptions.class.cast(options.securityGroups(groupIds)); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @see EC2TemplateOptions#securityGroups(Iterable<String>) |
| 223 | */ |
| 224 | public static EC2TemplateOptions securityGroups(Iterable<String> groupIds) { |
| 225 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 226 | return EC2TemplateOptions.class.cast(options.securityGroups(groupIds)); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @see EC2TemplateOptions#keyPair |
| 231 | */ |
| 232 | public static EC2TemplateOptions keyPair(String keyPair) { |
| 233 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 234 | return EC2TemplateOptions.class.cast(options.keyPair(keyPair)); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @see EC2TemplateOptions#userData |
| 239 | */ |
| 240 | public static EC2TemplateOptions userData(byte[] unencodedData) { |
| 241 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 242 | return EC2TemplateOptions.class.cast(options.userData(unencodedData)); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * @see EC2TemplateOptions#noKeyPair |
| 247 | */ |
| 248 | public static EC2TemplateOptions noKeyPair() { |
| 249 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 250 | return EC2TemplateOptions.class.cast(options.noKeyPair()); |
| 251 | } |
| 252 | |
| 253 | // methods that only facilitate returning the correct object type |
| 254 | /** |
| 255 | * @see TemplateOptions#inboundPorts |
| 256 | */ |
| 257 | public static EC2TemplateOptions inboundPorts(int... ports) { |
| 258 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 259 | return EC2TemplateOptions.class.cast(options.inboundPorts(ports)); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * @see TemplateOptions#port |
| 264 | */ |
| 265 | public static EC2TemplateOptions blockOnPort(int port, int seconds) { |
| 266 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 267 | return EC2TemplateOptions.class.cast(options.blockOnPort(port, seconds)); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * @see TemplateOptions#runScript |
| 272 | */ |
| 273 | public static EC2TemplateOptions runScript(byte[] script) { |
| 274 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 275 | return EC2TemplateOptions.class.cast(options.runScript(script)); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * @see TemplateOptions#installPrivateKey |
| 280 | */ |
| 281 | public static EC2TemplateOptions installPrivateKey(String rsaKey) { |
| 282 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 283 | return EC2TemplateOptions.class.cast(options.installPrivateKey(rsaKey)); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @see TemplateOptions#authorizePublicKey |
| 288 | */ |
| 289 | public static EC2TemplateOptions authorizePublicKey(String rsaKey) { |
| 290 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 291 | return EC2TemplateOptions.class.cast(options.authorizePublicKey(rsaKey)); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * @see TemplateOptions#withDetails |
| 296 | */ |
| 297 | public static EC2TemplateOptions withDetails() { |
| 298 | EC2TemplateOptions options = new EC2TemplateOptions(); |
| 299 | return EC2TemplateOptions.class.cast(options.withMetadata()); |
| 300 | } |
| 301 | |
| 302 | } |
| 303 | |
| 304 | // methods that only facilitate returning the correct object type |
| 305 | |
| 306 | /** |
| 307 | * {@inheritDoc} |
| 308 | */ |
| 309 | @Override |
| 310 | public EC2TemplateOptions blockOnPort(int port, int seconds) { |
| 311 | return EC2TemplateOptions.class.cast(super.blockOnPort(port, seconds)); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * {@inheritDoc} |
| 316 | */ |
| 317 | @Override |
| 318 | public EC2TemplateOptions inboundPorts(int... ports) { |
| 319 | return EC2TemplateOptions.class.cast(super.inboundPorts(ports)); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * {@inheritDoc} |
| 324 | */ |
| 325 | @Override |
| 326 | public EC2TemplateOptions authorizePublicKey(String publicKey) { |
| 327 | return EC2TemplateOptions.class.cast(super.authorizePublicKey(publicKey)); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * {@inheritDoc} |
| 332 | */ |
| 333 | @Override |
| 334 | @Deprecated |
| 335 | public EC2TemplateOptions authorizePublicKey(Payload publicKey) { |
| 336 | return EC2TemplateOptions.class.cast(super.authorizePublicKey(publicKey)); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * {@inheritDoc} |
| 341 | */ |
| 342 | @Override |
| 343 | public EC2TemplateOptions installPrivateKey(String privateKey) { |
| 344 | return EC2TemplateOptions.class.cast(super.installPrivateKey(privateKey)); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * {@inheritDoc} |
| 349 | */ |
| 350 | @Override |
| 351 | @Deprecated |
| 352 | public EC2TemplateOptions installPrivateKey(Payload privateKey) { |
| 353 | return EC2TemplateOptions.class.cast(super.installPrivateKey(privateKey)); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * {@inheritDoc} |
| 358 | */ |
| 359 | @Override |
| 360 | public EC2TemplateOptions runScript(Payload script) { |
| 361 | return EC2TemplateOptions.class.cast(super.runScript(script)); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * {@inheritDoc} |
| 366 | */ |
| 367 | @Override |
| 368 | @Deprecated |
| 369 | public EC2TemplateOptions runScript(byte[] script) { |
| 370 | return EC2TemplateOptions.class.cast(super.runScript(script)); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * {@inheritDoc} |
| 375 | */ |
| 376 | @Override |
| 377 | public EC2TemplateOptions withMetadata() { |
| 378 | return EC2TemplateOptions.class.cast(super.withMetadata()); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * {@inheritDoc} |
| 383 | */ |
| 384 | @Override |
| 385 | public EC2TemplateOptions blockUntilRunning(boolean blockUntilRunning) { |
| 386 | return EC2TemplateOptions.class.cast(super.blockUntilRunning(blockUntilRunning)); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * {@inheritDoc} |
| 391 | */ |
| 392 | @Override |
| 393 | public EC2TemplateOptions dontAuthorizePublicKey() { |
| 394 | return EC2TemplateOptions.class.cast(super.dontAuthorizePublicKey()); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * {@inheritDoc} |
| 399 | */ |
| 400 | @Override |
| 401 | public EC2TemplateOptions nameTask(String name) { |
| 402 | return EC2TemplateOptions.class.cast(super.nameTask(name)); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * {@inheritDoc} |
| 407 | */ |
| 408 | @Override |
| 409 | public EC2TemplateOptions runAsRoot(boolean runAsRoot) { |
| 410 | return EC2TemplateOptions.class.cast(super.runAsRoot(runAsRoot)); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * {@inheritDoc} |
| 415 | */ |
| 416 | @Override |
| 417 | public EC2TemplateOptions runScript(Statement script) { |
| 418 | return EC2TemplateOptions.class.cast(super.runScript(script)); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * {@inheritDoc} |
| 423 | */ |
| 424 | @Override |
| 425 | public EC2TemplateOptions overrideCredentialsWith(Credentials overridingCredentials) { |
| 426 | return EC2TemplateOptions.class.cast(super.overrideCredentialsWith(overridingCredentials)); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * @return groupIds the user specified to run instances with, or zero length set to create an |
| 431 | * implicit group |
| 432 | */ |
| 433 | public Set<String> getGroupIds() { |
| 434 | return groupIds; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * @return keyPair to use when running the instance or null, to generate a keypair. |
| 439 | */ |
| 440 | public String getKeyPair() { |
| 441 | return keyPair; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * @return true (default) if we are supposed to use a keypair |
| 446 | */ |
| 447 | public boolean shouldAutomaticallyCreateKeyPair() { |
| 448 | return !noKeyPair; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * @return unencoded user data. |
| 453 | */ |
| 454 | public byte[] getUserData() { |
| 455 | return userData; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * @return BlockDeviceMapping to use when running the instance or null. |
| 460 | */ |
| 461 | public Set<BlockDeviceMapping> getBlockDeviceMappings() { |
| 462 | return blockDeviceMappings.build(); |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public int hashCode() { |
| 467 | |
| 468 | final int prime = 31; |
| 469 | int result = super.hashCode(); |
| 470 | result = prime * result + ((blockDeviceMappings == null) ? 0 : blockDeviceMappings.hashCode()); |
| 471 | result = prime * result + ((groupIds == null) ? 0 : groupIds.hashCode()); |
| 472 | result = prime * result + ((keyPair == null) ? 0 : keyPair.hashCode()); |
| 473 | result = prime * result + (noKeyPair ? 1231 : 1237); |
| 474 | result = prime * result + Arrays.hashCode(userData); |
| 475 | return result; |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public boolean equals(Object obj) { |
| 480 | if (this == obj) |
| 481 | return true; |
| 482 | if (!super.equals(obj)) |
| 483 | return false; |
| 484 | if (getClass() != obj.getClass()) |
| 485 | return false; |
| 486 | EC2TemplateOptions other = (EC2TemplateOptions) obj; |
| 487 | if (blockDeviceMappings == null) { |
| 488 | if (other.blockDeviceMappings != null) |
| 489 | return false; |
| 490 | } else if (!blockDeviceMappings.equals(other.blockDeviceMappings)) |
| 491 | return false; |
| 492 | if (groupIds == null) { |
| 493 | if (other.groupIds != null) |
| 494 | return false; |
| 495 | } else if (!groupIds.equals(other.groupIds)) |
| 496 | return false; |
| 497 | if (keyPair == null) { |
| 498 | if (other.keyPair != null) |
| 499 | return false; |
| 500 | } else if (!keyPair.equals(other.keyPair)) |
| 501 | return false; |
| 502 | |
| 503 | if (!Arrays.equals(userData, other.userData)) |
| 504 | return false; |
| 505 | |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public String toString() { |
| 511 | return "[groupIds=" + groupIds + ", keyPair=" + keyPair + ", noKeyPair=" + noKeyPair + ", userData=" |
| 512 | + Arrays.toString(userData) + ", blockDeviceMappings=" + blockDeviceMappings.build() + "]"; |
| 513 | } |
| 514 | } |