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