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.options; |
20 | |
21 | import static com.google.common.base.Preconditions.checkArgument; |
22 | import static com.google.common.base.Preconditions.checkNotNull; |
23 | |
24 | import java.util.Set; |
25 | |
26 | import org.jclouds.ec2.domain.BlockDeviceMapping; |
27 | import org.jclouds.ec2.domain.InstanceType; |
28 | import org.jclouds.ec2.options.internal.BaseEC2RequestOptions; |
29 | import org.jclouds.encryption.internal.Base64; |
30 | |
31 | /** |
32 | * Contains options supported in the Form API for the RunInstances operation. <h2> |
33 | * Usage</h2> The recommended way to instantiate a RunInstancesOptions object is to statically |
34 | * import RunInstancesOptions.Builder.* and invoke a static creation method followed by an instance |
35 | * mutator (if needed): |
36 | * <p/> |
37 | * <code> |
38 | * import static org.jclouds.aws.ec2.options.RunInstancesOptions.Builder.* |
39 | * <p/> |
40 | * EC2Client connection = // get connection |
41 | * Future<ReservationInfo> instances = connection.runInstances(executableBy("123125").imageIds(1000, 1004)); |
42 | * <code> |
43 | * |
44 | * @author Adrian Cole |
45 | * @see <a href= |
46 | * "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-form-RunInstances.html" |
47 | * /> |
48 | */ |
49 | public class RunInstancesOptions extends BaseEC2RequestOptions { |
50 | public static final RunInstancesOptions NONE = new RunInstancesOptions(); |
51 | |
52 | /** |
53 | * The name of the key pair. |
54 | */ |
55 | public RunInstancesOptions withKeyName(String keyName) { |
56 | formParameters.put("KeyName", checkNotNull(keyName, "keyName")); |
57 | return this; |
58 | } |
59 | |
60 | /** |
61 | * Attach multiple security groups |
62 | */ |
63 | public RunInstancesOptions withSecurityGroups(String... securityGroups) { |
64 | indexFormValuesWithPrefix("SecurityGroup", securityGroups); |
65 | return this; |
66 | } |
67 | |
68 | /** |
69 | * Attach multiple security groups |
70 | */ |
71 | public RunInstancesOptions withSecurityGroups(Iterable<String> securityGroups) { |
72 | indexFormValuesWithPrefix("SecurityGroup", securityGroups); |
73 | return this; |
74 | } |
75 | |
76 | /** |
77 | * Attaches a single security group. Multiple calls to this method won't add more groups. |
78 | * |
79 | * @param securityGroup |
80 | * name of an existing security group |
81 | */ |
82 | public RunInstancesOptions withSecurityGroup(String securityGroup) { |
83 | return withSecurityGroups(securityGroup); |
84 | } |
85 | |
86 | /** |
87 | * Unencoded data |
88 | */ |
89 | public RunInstancesOptions withUserData(byte[] unencodedData) { |
90 | int length = checkNotNull(unencodedData, "unencodedData").length; |
91 | checkArgument(length > 0, "userData cannot be empty"); |
92 | checkArgument(length <= 16 * 1024, "userData cannot be larger than 16kb"); |
93 | formParameters.put("UserData", Base64.encodeBytes(unencodedData)); |
94 | return this; |
95 | } |
96 | |
97 | /** |
98 | * Specifies the instance type. default small; |
99 | */ |
100 | public RunInstancesOptions asType(String type) { |
101 | formParameters.put("InstanceType", checkNotNull(type, "type")); |
102 | return this; |
103 | } |
104 | |
105 | /** |
106 | * The ID of the kernel with which to launch the instance. |
107 | */ |
108 | public RunInstancesOptions withKernelId(String kernelId) { |
109 | formParameters.put("KernelId", checkNotNull(kernelId, "kernelId")); |
110 | return this; |
111 | } |
112 | |
113 | /** |
114 | * The ID of the RAM disk with which to launch the instance. Some kernels require additional |
115 | * drivers at l aunch. Check the kernel requirements for information on whether you need to |
116 | * specify a RAM disk. To find kernel requirements, go to th e Resource Center and search for the |
117 | * kernel ID. |
118 | */ |
119 | public RunInstancesOptions withRamdisk(String ramDiskId) { |
120 | formParameters.put("RamdiskId", checkNotNull(ramDiskId, "ramDiskId")); |
121 | return this; |
122 | } |
123 | |
124 | /** |
125 | * Specifies the Block Device Mapping for the instance |
126 | * |
127 | */ |
128 | public RunInstancesOptions withBlockDeviceMappings(Set<? extends BlockDeviceMapping> mappings) { |
129 | int i = 1; |
130 | for (BlockDeviceMapping mapping : checkNotNull(mappings, "mappings")) { |
131 | checkNotNull(mapping.getDeviceName(), "deviceName"); |
132 | formParameters.put(String.format("BlockDeviceMapping.%d.DeviceName", i), mapping.getDeviceName()); |
133 | if (mapping.getVirtualName() != null) |
134 | formParameters.put(String.format("BlockDeviceMapping.%d.VirtualName", i), mapping.getVirtualName()); |
135 | if (mapping.getEbsSnapshotId() != null) |
136 | formParameters.put(String.format("BlockDeviceMapping.%d.Ebs.SnapshotId", i), mapping.getEbsSnapshotId()); |
137 | if (mapping.getEbsVolumeSize() != null) |
138 | formParameters.put(String.format("BlockDeviceMapping.%d.Ebs.VolumeSize", i), |
139 | String.valueOf(mapping.getEbsVolumeSize())); |
140 | if (mapping.getEbsNoDevice() != null) |
141 | formParameters.put(String.format("BlockDeviceMapping.%d.Ebs.NoDevice", i), |
142 | String.valueOf(mapping.getEbsNoDevice())); |
143 | if (mapping.getEbsDeleteOnTermination() != null) |
144 | formParameters.put(String.format("BlockDeviceMapping.%d.Ebs.DeleteOnTermination", i), |
145 | String.valueOf(mapping.getEbsDeleteOnTermination())); |
146 | i++; |
147 | } |
148 | return this; |
149 | } |
150 | |
151 | public static class Builder { |
152 | /** |
153 | * @see RunInstancesOptions#withKeyName(String) |
154 | */ |
155 | public static RunInstancesOptions withKeyName(String keyName) { |
156 | RunInstancesOptions options = new RunInstancesOptions(); |
157 | return options.withKeyName(keyName); |
158 | } |
159 | |
160 | /** |
161 | * @see RunInstancesOptions#withSecurityGroup(String) |
162 | */ |
163 | public static RunInstancesOptions withSecurityGroup(String securityGroup) { |
164 | RunInstancesOptions options = new RunInstancesOptions(); |
165 | return options.withSecurityGroup(securityGroup); |
166 | } |
167 | |
168 | /** |
169 | * @see RunInstancesOptions#withUserData(byte []) |
170 | */ |
171 | public static RunInstancesOptions withUserData(byte[] unencodedData) { |
172 | RunInstancesOptions options = new RunInstancesOptions(); |
173 | return options.withUserData(unencodedData); |
174 | } |
175 | |
176 | /** |
177 | * @see RunInstancesOptions#asType(InstanceType) |
178 | */ |
179 | public static RunInstancesOptions asType(String instanceType) { |
180 | RunInstancesOptions options = new RunInstancesOptions(); |
181 | return options.asType(instanceType); |
182 | } |
183 | |
184 | /** |
185 | * @see RunInstancesOptions#withKernelId(String) |
186 | */ |
187 | public static RunInstancesOptions withKernelId(String kernelId) { |
188 | RunInstancesOptions options = new RunInstancesOptions(); |
189 | return options.withKernelId(kernelId); |
190 | } |
191 | |
192 | /** |
193 | * @see RunInstancesOptions#withRamdisk(String) |
194 | */ |
195 | public static RunInstancesOptions withRamdisk(String ramdiskId) { |
196 | RunInstancesOptions options = new RunInstancesOptions(); |
197 | return options.withRamdisk(ramdiskId); |
198 | } |
199 | |
200 | /** |
201 | * @see RunInstancesOptions#withBlockDeviceMappings(Set<BlockDeviceMapping> mappings) |
202 | */ |
203 | public static RunInstancesOptions withBlockDeviceMappings(Set<? extends BlockDeviceMapping> mappings) { |
204 | RunInstancesOptions options = new RunInstancesOptions(); |
205 | return options.withBlockDeviceMappings(mappings); |
206 | } |
207 | |
208 | } |
209 | } |