1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.aws.ec2.domain;
20
21 import static com.google.common.base.Preconditions.checkNotNull;
22
23 import java.util.Arrays;
24 import java.util.Set;
25
26 import javax.annotation.Nullable;
27
28 import org.jclouds.ec2.domain.BlockDeviceMapping;
29 import org.jclouds.ec2.domain.BlockDeviceMapping.MapEBSSnapshotToDevice;
30 import org.jclouds.ec2.domain.BlockDeviceMapping.MapEphemeralDeviceToDevice;
31 import org.jclouds.ec2.domain.BlockDeviceMapping.MapNewVolumeToDevice;
32
33 import com.google.common.collect.ImmutableSet;
34 import com.google.common.collect.ImmutableSortedSet;
35
36
37
38
39
40
41
42
43 public class LaunchSpecification {
44
45 public static Builder builder() {
46 return new Builder();
47 }
48
49 public static class Builder {
50 protected ImmutableSet.Builder<String> groupIds = ImmutableSet.<String> builder();
51 protected String imageId;
52 protected String instanceType;
53 protected String kernelId;
54 protected String keyName;
55 protected String availabilityZone;
56 protected String ramdiskId;
57 protected Boolean monitoringEnabled;
58 protected ImmutableSet.Builder<BlockDeviceMapping> blockDeviceMappings = ImmutableSet
59 .<BlockDeviceMapping> builder();
60 protected byte[] userData;
61
62 public void clear() {
63 groupIds = ImmutableSet.<String> builder();
64 imageId = null;
65 instanceType = null;
66 kernelId = null;
67 keyName = null;
68 availabilityZone = null;
69 ramdiskId = null;
70 monitoringEnabled = false;
71 blockDeviceMappings = ImmutableSet.<BlockDeviceMapping> builder();
72 userData = null;
73 }
74
75 public Builder groupIds(Iterable<String> groupIds) {
76 this.groupIds.addAll(checkNotNull(groupIds, "groupIds"));
77 return this;
78 }
79
80 public Builder groupId(String groupId) {
81 if (groupId != null)
82 this.groupIds.add(groupId);
83 return this;
84 }
85
86 public Builder imageId(String imageId) {
87 this.imageId = imageId;
88 return this;
89 }
90
91 public Builder monitoringEnabled(Boolean monitoringEnabled) {
92 this.monitoringEnabled = monitoringEnabled;
93 return this;
94 }
95
96 public Builder instanceType(String instanceType) {
97 this.instanceType = instanceType;
98 return this;
99 }
100
101 public Builder kernelId(String kernelId) {
102 this.kernelId = kernelId;
103 return this;
104 }
105
106 public Builder keyName(String keyName) {
107 this.keyName = keyName;
108 return this;
109 }
110
111 public Builder availabilityZone(String availabilityZone) {
112 this.availabilityZone = availabilityZone;
113 return this;
114 }
115
116 public Builder ramdiskId(String ramdiskId) {
117 this.ramdiskId = ramdiskId;
118 return this;
119 }
120
121 public Builder mapEBSSnapshotToDevice(String deviceName, String snapshotId, @Nullable Integer sizeInGib,
122 boolean deleteOnTermination) {
123 blockDeviceMappings.add(new MapEBSSnapshotToDevice(deviceName, snapshotId, sizeInGib, deleteOnTermination));
124 return this;
125 }
126
127 public Builder mapNewVolumeToDevice(String deviceName, int sizeInGib, boolean deleteOnTermination) {
128 blockDeviceMappings.add(new MapNewVolumeToDevice(deviceName, sizeInGib, deleteOnTermination));
129 return this;
130 }
131
132 public Builder mapEphemeralDeviceToDevice(String deviceName, String virtualName) {
133 blockDeviceMappings.add(new MapEphemeralDeviceToDevice(deviceName, virtualName));
134 return this;
135 }
136
137 public Builder blockDeviceMapping(BlockDeviceMapping blockDeviceMapping) {
138 this.blockDeviceMappings.add(checkNotNull(blockDeviceMapping, "blockDeviceMapping"));
139 return this;
140 }
141
142 public Builder blockDeviceMappings(Iterable<? extends BlockDeviceMapping> blockDeviceMappings) {
143 this.blockDeviceMappings.addAll(checkNotNull(blockDeviceMappings, "blockDeviceMappings"));
144 return this;
145 }
146
147 public Builder userData(byte[] userData) {
148 this.userData = userData;
149 return this;
150 }
151
152 public LaunchSpecification build() {
153 return new LaunchSpecification(instanceType, imageId, kernelId, ramdiskId, availabilityZone, keyName,
154 groupIds.build(), blockDeviceMappings.build(), monitoringEnabled, userData);
155 }
156
157 public static Builder fromLaunchSpecification(LaunchSpecification in) {
158 return new Builder().instanceType(in.getInstanceType()).imageId(in.getImageId()).kernelId(in.getKernelId())
159 .ramdiskId(in.getRamdiskId()).availabilityZone(in.getAvailabilityZone()).keyName(in.getKeyName())
160 .groupIds(in.getGroupIds()).blockDeviceMappings(in.getBlockDeviceMappings())
161 .monitoringEnabled(in.isMonitoringEnabled()).userData(in.getUserData());
162 }
163 }
164
165 protected final String instanceType;
166 protected final String imageId;
167 protected final String kernelId;
168 protected final String ramdiskId;
169 protected final String availabilityZone;
170 protected final String keyName;
171 protected final Set<String> groupIds;
172 protected final Set<? extends BlockDeviceMapping> blockDeviceMappings;
173 protected final Boolean monitoringEnabled;
174 protected final byte[] userData;
175
176 public LaunchSpecification(String instanceType, String imageId, String kernelId, String ramdiskId,
177 String availabilityZone, String keyName, Iterable<String> groupIds,
178 Iterable<? extends BlockDeviceMapping> blockDeviceMappings, Boolean monitoringEnabled, byte[] userData) {
179 this.instanceType = checkNotNull(instanceType, "instanceType");
180 this.imageId = checkNotNull(imageId, "imageId");
181 this.kernelId = kernelId;
182 this.ramdiskId = ramdiskId;
183 this.availabilityZone = availabilityZone;
184 this.keyName = keyName;
185 this.groupIds = ImmutableSortedSet.copyOf(checkNotNull(groupIds, "groupIds"));
186 this.blockDeviceMappings = ImmutableSortedSet.copyOf(checkNotNull(blockDeviceMappings, "blockDeviceMappings"));
187 this.monitoringEnabled = monitoringEnabled;
188 this.userData = userData;
189 }
190
191
192
193
194 public String getImageId() {
195 return imageId;
196 }
197
198
199
200
201 public Boolean isMonitoringEnabled() {
202 return monitoringEnabled;
203 }
204
205
206
207
208 public String getInstanceType() {
209 return instanceType;
210 }
211
212
213
214
215 public String getKernelId() {
216 return kernelId;
217 }
218
219
220
221
222 public String getKeyName() {
223 return keyName;
224 }
225
226
227
228
229 public String getAvailabilityZone() {
230 return availabilityZone;
231 }
232
233
234
235
236 public String getRamdiskId() {
237 return ramdiskId;
238 }
239
240
241
242
243 public Set<? extends BlockDeviceMapping> getBlockDeviceMappings() {
244 return blockDeviceMappings;
245 }
246
247
248
249
250 public Set<String> getGroupIds() {
251 return groupIds;
252 }
253
254
255
256
257 public byte[] getUserData() {
258 return userData;
259 }
260
261 @Override
262 public int hashCode() {
263 final int prime = 31;
264 int result = 1;
265 result = prime * result + ((availabilityZone == null) ? 0 : availabilityZone.hashCode());
266 result = prime * result + ((blockDeviceMappings == null) ? 0 : blockDeviceMappings.hashCode());
267 result = prime * result + ((groupIds == null) ? 0 : groupIds.hashCode());
268 result = prime * result + ((imageId == null) ? 0 : imageId.hashCode());
269 result = prime * result + ((instanceType == null) ? 0 : instanceType.hashCode());
270 result = prime * result + ((kernelId == null) ? 0 : kernelId.hashCode());
271 result = prime * result + ((keyName == null) ? 0 : keyName.hashCode());
272 result = prime * result + ((monitoringEnabled == null) ? 0 : monitoringEnabled.hashCode());
273 result = prime * result + ((ramdiskId == null) ? 0 : ramdiskId.hashCode());
274 result = prime * result + Arrays.hashCode(userData);
275 return result;
276 }
277
278 @Override
279 public boolean equals(Object obj) {
280 if (this == obj)
281 return true;
282 if (obj == null)
283 return false;
284 if (getClass() != obj.getClass())
285 return false;
286 LaunchSpecification other = (LaunchSpecification) obj;
287 if (availabilityZone == null) {
288 if (other.availabilityZone != null)
289 return false;
290 } else if (!availabilityZone.equals(other.availabilityZone))
291 return false;
292 if (blockDeviceMappings == null) {
293 if (other.blockDeviceMappings != null)
294 return false;
295 } else if (!blockDeviceMappings.equals(other.blockDeviceMappings))
296 return false;
297 if (groupIds == null) {
298 if (other.groupIds != null)
299 return false;
300 } else if (!groupIds.equals(other.groupIds))
301 return false;
302 if (imageId == null) {
303 if (other.imageId != null)
304 return false;
305 } else if (!imageId.equals(other.imageId))
306 return false;
307 if (instanceType == null) {
308 if (other.instanceType != null)
309 return false;
310 } else if (!instanceType.equals(other.instanceType))
311 return false;
312 if (kernelId == null) {
313 if (other.kernelId != null)
314 return false;
315 } else if (!kernelId.equals(other.kernelId))
316 return false;
317 if (keyName == null) {
318 if (other.keyName != null)
319 return false;
320 } else if (!keyName.equals(other.keyName))
321 return false;
322 if (monitoringEnabled == null) {
323 if (other.monitoringEnabled != null)
324 return false;
325 } else if (!monitoringEnabled.equals(other.monitoringEnabled))
326 return false;
327 if (ramdiskId == null) {
328 if (other.ramdiskId != null)
329 return false;
330 } else if (!ramdiskId.equals(other.ramdiskId))
331 return false;
332 if (!Arrays.equals(userData, other.userData))
333 return false;
334 return true;
335 }
336
337 public Builder toBuilder() {
338 return Builder.fromLaunchSpecification(this);
339 }
340
341 @Override
342 public String toString() {
343 return "[instanceType=" + instanceType + ", imageId=" + imageId + ", kernelId=" + kernelId + ", ramdiskId="
344 + ramdiskId + ", availabilityZone=" + availabilityZone + ", keyName=" + keyName + ", groupIds=" + groupIds
345 + ", blockDeviceMappings=" + blockDeviceMappings + ", monitoringEnabled=" + monitoringEnabled
346 + ", userData=" + (userData != null) + "]";
347 }
348
349 }