View Javadoc

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.aws.ec2.domain;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  
23  import java.util.Arrays;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.jclouds.javax.annotation.Nullable;
28  
29  import org.jclouds.ec2.domain.BlockDeviceMapping;
30  import org.jclouds.ec2.domain.BlockDeviceMapping.MapEBSSnapshotToDevice;
31  import org.jclouds.ec2.domain.BlockDeviceMapping.MapEphemeralDeviceToDevice;
32  import org.jclouds.ec2.domain.BlockDeviceMapping.MapNewVolumeToDevice;
33  
34  import com.google.common.collect.ImmutableMap;
35  import com.google.common.collect.ImmutableSet;
36  import com.google.common.collect.ImmutableSortedSet;
37  
38  /**
39   * 
40   * @see <a href=
41   *      "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-RequestSpotInstances.html"
42   *      />
43   * @author Adrian Cole
44   */
45  public class LaunchSpecification {
46  
47     public static Builder builder() {
48        return new Builder();
49     }
50  
51     public static class Builder {
52        protected ImmutableMap.Builder<String, String> securityGroupIdToNames = ImmutableMap.<String, String> builder();
53        protected String imageId;
54        protected String instanceType;
55        protected String kernelId;
56        protected String keyName;
57        protected String availabilityZone;
58        protected String ramdiskId;
59        protected Boolean monitoringEnabled;
60        protected ImmutableSet.Builder<BlockDeviceMapping> blockDeviceMappings = ImmutableSet
61              .<BlockDeviceMapping> builder();
62        protected ImmutableSet.Builder<String> securityGroupIds = ImmutableSet.<String> builder();
63        protected ImmutableSet.Builder<String> securityGroupNames = ImmutableSet.<String> builder();
64        protected byte[] userData;
65  
66        public void clear() {
67           securityGroupIdToNames = ImmutableMap.<String, String> builder();
68           imageId = null;
69           instanceType = null;
70           kernelId = null;
71           keyName = null;
72           availabilityZone = null;
73           ramdiskId = null;
74           monitoringEnabled = false;
75           blockDeviceMappings = ImmutableSet.<BlockDeviceMapping> builder();
76           securityGroupIds = ImmutableSet.<String> builder();
77           securityGroupNames = ImmutableSet.<String> builder();
78           userData = null;
79        }
80  
81        public Builder securityGroupIdToNames(Map<String, String> securityGroupIdToNames) {
82           this.securityGroupIdToNames.putAll(checkNotNull(securityGroupIdToNames, "securityGroupIdToNames"));
83           return this;
84        }
85  
86        public Builder securityGroupIdToName(String groupId, String groupName) {
87           if (groupId != null && groupName != null)
88              this.securityGroupIdToNames.put(checkNotNull(groupId, "groupId"), checkNotNull(groupName, "groupName"));
89           return this;
90        }
91  
92        public Builder imageId(String imageId) {
93           this.imageId = imageId;
94           return this;
95        }
96  
97        public Builder monitoringEnabled(Boolean monitoringEnabled) {
98           this.monitoringEnabled = monitoringEnabled;
99           return this;
100       }
101 
102       public Builder instanceType(String instanceType) {
103          this.instanceType = instanceType;
104          return this;
105       }
106 
107       public Builder kernelId(String kernelId) {
108          this.kernelId = kernelId;
109          return this;
110       }
111 
112       public Builder keyName(String keyName) {
113          this.keyName = keyName;
114          return this;
115       }
116 
117       public Builder availabilityZone(String availabilityZone) {
118          this.availabilityZone = availabilityZone;
119          return this;
120       }
121 
122       public Builder ramdiskId(String ramdiskId) {
123          this.ramdiskId = ramdiskId;
124          return this;
125       }
126 
127       public Builder mapEBSSnapshotToDevice(String deviceName, String snapshotId, @Nullable Integer sizeInGib,
128             boolean deleteOnTermination) {
129          blockDeviceMappings.add(new MapEBSSnapshotToDevice(deviceName, snapshotId, sizeInGib, deleteOnTermination));
130          return this;
131       }
132 
133       public Builder mapNewVolumeToDevice(String deviceName, int sizeInGib, boolean deleteOnTermination) {
134          blockDeviceMappings.add(new MapNewVolumeToDevice(deviceName, sizeInGib, deleteOnTermination));
135          return this;
136       }
137 
138       public Builder mapEphemeralDeviceToDevice(String deviceName, String virtualName) {
139          blockDeviceMappings.add(new MapEphemeralDeviceToDevice(deviceName, virtualName));
140          return this;
141       }
142 
143       public Builder blockDeviceMapping(BlockDeviceMapping blockDeviceMapping) {
144          this.blockDeviceMappings.add(checkNotNull(blockDeviceMapping, "blockDeviceMapping"));
145          return this;
146       }
147 
148       public Builder blockDeviceMappings(Iterable<? extends BlockDeviceMapping> blockDeviceMappings) {
149          this.blockDeviceMappings.addAll(checkNotNull(blockDeviceMappings, "blockDeviceMappings"));
150          return this;
151       }
152 
153       public Builder securityGroupIds(Iterable<String> securityGroupIds) {
154          this.securityGroupIds.addAll(checkNotNull(securityGroupIds, "securityGroupIds"));
155          return this;
156       }
157 
158       public Builder securityGroupId(String securityGroupId) {
159          if (securityGroupId != null)
160             this.securityGroupIds.add(securityGroupId);
161          return this;
162       }
163 
164       public Builder securityGroupNames(Iterable<String> securityGroupNames) {
165          this.securityGroupNames.addAll(checkNotNull(securityGroupNames, "securityGroupNames"));
166          return this;
167       }
168 
169       public Builder securityGroupName(String securityGroupName) {
170          if (securityGroupName != null)
171             this.securityGroupNames.add(securityGroupName);
172          return this;
173       }
174 
175       public Builder userData(byte[] userData) {
176          this.userData = userData;
177          return this;
178       }
179 
180       public LaunchSpecification build() {
181          return new LaunchSpecification(instanceType, imageId, kernelId, ramdiskId, availabilityZone, keyName,
182                securityGroupIdToNames.build(), blockDeviceMappings.build(), monitoringEnabled,
183                securityGroupIds.build(), securityGroupNames.build(), userData);
184       }
185 
186       public static Builder fromLaunchSpecification(LaunchSpecification in) {
187          return new Builder().instanceType(in.getInstanceType()).imageId(in.getImageId()).kernelId(in.getKernelId())
188                .ramdiskId(in.getRamdiskId()).availabilityZone(in.getAvailabilityZone()).keyName(in.getKeyName())
189                .securityGroupIdToNames(in.getSecurityGroupIdToNames()).securityGroupIds(in.getSecurityGroupIds())
190                .securityGroupNames(in.getSecurityGroupNames()).blockDeviceMappings(in.getBlockDeviceMappings())
191                .monitoringEnabled(in.isMonitoringEnabled()).userData(in.getUserData());
192       }
193    }
194 
195    protected final String instanceType;
196    protected final String imageId;
197    protected final String kernelId;
198    protected final String ramdiskId;
199    protected final String availabilityZone;
200    protected final String keyName;
201    protected final Map<String, String> securityGroupIdToNames;
202    protected final Set<? extends BlockDeviceMapping> blockDeviceMappings;
203    protected final Set<String> securityGroupIds;
204    protected final Set<String> securityGroupNames;
205    protected final Boolean monitoringEnabled;
206    protected final byte[] userData;
207 
208    public LaunchSpecification(String instanceType, String imageId, String kernelId, String ramdiskId,
209          String availabilityZone, String keyName, Map<String, String> securityGroupIdToNames,
210          Iterable<? extends BlockDeviceMapping> blockDeviceMappings, Boolean monitoringEnabled,
211          Set<String> securityGroupIds, Set<String> securityGroupNames, byte[] userData) {
212       this.instanceType = checkNotNull(instanceType, "instanceType");
213       this.imageId = checkNotNull(imageId, "imageId");
214       this.kernelId = kernelId;
215       this.ramdiskId = ramdiskId;
216       this.availabilityZone = availabilityZone;
217       this.keyName = keyName;
218       this.securityGroupIdToNames = ImmutableMap.copyOf(checkNotNull(securityGroupIdToNames, "securityGroupIdToNames"));
219       this.blockDeviceMappings = ImmutableSortedSet.copyOf(checkNotNull(blockDeviceMappings, "blockDeviceMappings"));
220       this.securityGroupIds = ImmutableSortedSet.copyOf(checkNotNull(securityGroupIds, "securityGroupIds"));
221       this.securityGroupNames = ImmutableSortedSet.copyOf(checkNotNull(securityGroupNames, "securityGroupNames"));
222       this.monitoringEnabled = monitoringEnabled;
223       this.userData = userData;
224    }
225 
226    public Map<String, String> getSecurityGroupIdToNames() {
227       return securityGroupIdToNames;
228    }
229 
230    /**
231     * Image ID of the AMI used to launch the instance.
232     */
233    public String getImageId() {
234       return imageId;
235    }
236 
237    /**
238     * CloudWatch support
239     */
240    public Boolean isMonitoringEnabled() {
241       return monitoringEnabled;
242    }
243 
244    /**
245     * The instance type.
246     */
247    public String getInstanceType() {
248       return instanceType;
249    }
250 
251    /**
252     * Optional. Kernel associated with this instance.
253     */
254    public String getKernelId() {
255       return kernelId;
256    }
257 
258    /**
259     * If this instance was launched with an associated key pair, this displays
260     * the key pair name.
261     */
262    public String getKeyName() {
263       return keyName;
264    }
265 
266    /**
267     * The location where the instance launched.
268     */
269    public String getAvailabilityZone() {
270       return availabilityZone;
271    }
272 
273    /**
274     * Optional. RAM disk associated with this instance.
275     */
276    public String getRamdiskId() {
277       return ramdiskId;
278    }
279 
280    /**
281     * volumes mappings associated with the instance.
282     */
283    public Set<? extends BlockDeviceMapping> getBlockDeviceMappings() {
284       return blockDeviceMappings;
285    }
286 
287    /**
288     * Names of the security groups.
289     */
290    public Set<String> getSecurityGroupNames() {
291       return securityGroupNames;
292    }
293 
294    /**
295     * Ids of the security groups.
296     */
297    public Set<String> getSecurityGroupIds() {
298       return securityGroupIds;
299    }
300 
301    /**
302     * User Data
303     */
304    public byte[] getUserData() {
305       return userData;
306    }
307 
308    @Override
309    public int hashCode() {
310       final int prime = 31;
311       int result = 1;
312       result = prime * result + ((availabilityZone == null) ? 0 : availabilityZone.hashCode());
313       result = prime * result + ((blockDeviceMappings == null) ? 0 : blockDeviceMappings.hashCode());
314       result = prime * result + ((imageId == null) ? 0 : imageId.hashCode());
315       result = prime * result + ((instanceType == null) ? 0 : instanceType.hashCode());
316       result = prime * result + ((kernelId == null) ? 0 : kernelId.hashCode());
317       result = prime * result + ((keyName == null) ? 0 : keyName.hashCode());
318       result = prime * result + ((monitoringEnabled == null) ? 0 : monitoringEnabled.hashCode());
319       result = prime * result + ((ramdiskId == null) ? 0 : ramdiskId.hashCode());
320       result = prime * result + ((securityGroupIdToNames == null) ? 0 : securityGroupIdToNames.hashCode());
321       result = prime * result + ((securityGroupIds == null) ? 0 : securityGroupIds.hashCode());
322       result = prime * result + ((securityGroupNames == null) ? 0 : securityGroupNames.hashCode());
323       result = prime * result + Arrays.hashCode(userData);
324       return result;
325    }
326 
327    @Override
328    public boolean equals(Object obj) {
329       if (this == obj)
330          return true;
331       if (obj == null)
332          return false;
333       if (getClass() != obj.getClass())
334          return false;
335       LaunchSpecification other = (LaunchSpecification) obj;
336       if (availabilityZone == null) {
337          if (other.availabilityZone != null)
338             return false;
339       } else if (!availabilityZone.equals(other.availabilityZone))
340          return false;
341       if (blockDeviceMappings == null) {
342          if (other.blockDeviceMappings != null)
343             return false;
344       } else if (!blockDeviceMappings.equals(other.blockDeviceMappings))
345          return false;
346       if (imageId == null) {
347          if (other.imageId != null)
348             return false;
349       } else if (!imageId.equals(other.imageId))
350          return false;
351       if (instanceType == null) {
352          if (other.instanceType != null)
353             return false;
354       } else if (!instanceType.equals(other.instanceType))
355          return false;
356       if (kernelId == null) {
357          if (other.kernelId != null)
358             return false;
359       } else if (!kernelId.equals(other.kernelId))
360          return false;
361       if (keyName == null) {
362          if (other.keyName != null)
363             return false;
364       } else if (!keyName.equals(other.keyName))
365          return false;
366       if (monitoringEnabled == null) {
367          if (other.monitoringEnabled != null)
368             return false;
369       } else if (!monitoringEnabled.equals(other.monitoringEnabled))
370          return false;
371       if (ramdiskId == null) {
372          if (other.ramdiskId != null)
373             return false;
374       } else if (!ramdiskId.equals(other.ramdiskId))
375          return false;
376       if (securityGroupIdToNames == null) {
377          if (other.securityGroupIdToNames != null)
378             return false;
379       } else if (!securityGroupIdToNames.equals(other.securityGroupIdToNames))
380          return false;
381       if (securityGroupIds == null) {
382          if (other.securityGroupIds != null)
383             return false;
384       } else if (!securityGroupIds.equals(other.securityGroupIds))
385          return false;
386       if (securityGroupNames == null) {
387          if (other.securityGroupNames != null)
388             return false;
389       } else if (!securityGroupNames.equals(other.securityGroupNames))
390          return false;
391       if (!Arrays.equals(userData, other.userData))
392          return false;
393       return true;
394    }
395 
396    public Builder toBuilder() {
397       return Builder.fromLaunchSpecification(this);
398    }
399 
400    @Override
401    public String toString() {
402       return "[instanceType=" + instanceType + ", imageId=" + imageId + ", kernelId=" + kernelId + ", ramdiskId="
403             + ramdiskId + ", availabilityZone=" + availabilityZone + ", keyName=" + keyName
404             + ", securityGroupIdToNames=" + securityGroupIdToNames + ", blockDeviceMappings=" + blockDeviceMappings
405             + ", securityGroupIds=" + securityGroupIds + ", securityGroupNames=" + securityGroupNames
406             + ", monitoringEnabled=" + monitoringEnabled + ", userData=" + Arrays.toString(userData) + "]";
407    }
408 
409 }