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.ec2.domain;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  
23  import java.util.Date;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.jclouds.javax.annotation.Nullable;
28  
29  import com.google.common.collect.ImmutableMap;
30  import com.google.common.collect.ImmutableSet;
31  import com.google.common.collect.Maps;
32  import com.google.common.collect.Sets;
33  
34  /**
35   * 
36   * @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-RunningInstancesItemType.html"
37   *      />
38   * @author Adrian Cole
39   */
40  public class RunningInstance implements Comparable<RunningInstance> {
41     public static Builder builder() {
42        return new Builder();
43     }
44  
45     public static class Builder {
46        protected String region;
47        protected Set<String> groupIds = Sets.newLinkedHashSet();
48        protected String amiLaunchIndex;
49        protected String dnsName;
50        protected String imageId;
51        protected String instanceId;
52        protected InstanceState instanceState;
53        protected String instanceType;
54        protected String ipAddress;
55        protected String kernelId;
56        protected String keyName;
57        protected Date launchTime;
58        protected String availabilityZone;
59        protected String virtualizationType = "paravirtual";
60        protected String platform;
61        protected String privateDnsName;
62        protected String privateIpAddress;
63        protected String ramdiskId;
64        protected String reason;
65        protected RootDeviceType rootDeviceType = RootDeviceType.INSTANCE_STORE;
66        protected String rootDeviceName;
67        protected Map<String, BlockDevice> ebsBlockDevices = Maps.newLinkedHashMap();
68  
69        public Builder region(String region) {
70           this.region = region;
71           return this;
72        }
73  
74        public Builder groupIds(Iterable<String> groupIds) {
75           this.groupIds = ImmutableSet.copyOf(checkNotNull(groupIds, "groupIds"));
76           return this;
77        }
78  
79        public Builder groupId(String groupId) {
80           if (groupId != null)
81              this.groupIds.add(groupId);
82           return this;
83        }
84  
85        public Builder amiLaunchIndex(String amiLaunchIndex) {
86           this.amiLaunchIndex = amiLaunchIndex;
87           return this;
88        }
89  
90        public Builder dnsName(String dnsName) {
91           this.dnsName = dnsName;
92           return this;
93        }
94  
95        public Builder imageId(String imageId) {
96           this.imageId = imageId;
97           return this;
98        }
99  
100       public Builder instanceId(String instanceId) {
101          this.instanceId = instanceId;
102          return this;
103       }
104 
105       public Builder instanceState(InstanceState instanceState) {
106          this.instanceState = instanceState;
107          return this;
108       }
109 
110       public Builder instanceType(String instanceType) {
111          this.instanceType = instanceType;
112          return this;
113       }
114 
115       public Builder ipAddress(String ipAddress) {
116          this.ipAddress = ipAddress;
117          return this;
118       }
119 
120       public Builder kernelId(String kernelId) {
121          this.kernelId = kernelId;
122          return this;
123       }
124 
125       public Builder keyName(String keyName) {
126          this.keyName = keyName;
127          return this;
128       }
129 
130       public Builder launchTime(Date launchTime) {
131          this.launchTime = launchTime;
132          return this;
133       }
134 
135       public Builder availabilityZone(String availabilityZone) {
136          this.availabilityZone = availabilityZone;
137          return this;
138       }
139 
140       public Builder virtualizationType(String virtualizationType) {
141          this.virtualizationType = virtualizationType;
142          return this;
143       }
144 
145       public Builder platform(String platform) {
146          this.platform = platform;
147          return this;
148       }
149 
150       public Builder privateDnsName(String privateDnsName) {
151          this.privateDnsName = privateDnsName;
152          return this;
153       }
154 
155       public Builder privateIpAddress(String privateIpAddress) {
156          this.privateIpAddress = privateIpAddress;
157          return this;
158       }
159 
160       public Builder ramdiskId(String ramdiskId) {
161          this.ramdiskId = ramdiskId;
162          return this;
163       }
164 
165       public Builder reason(String reason) {
166          this.reason = reason;
167          return this;
168       }
169 
170       public Builder rootDeviceType(RootDeviceType rootDeviceType) {
171          this.rootDeviceType = rootDeviceType;
172          return this;
173       }
174 
175       public Builder rootDeviceName(String rootDeviceName) {
176          this.rootDeviceName = rootDeviceName;
177          return this;
178       }
179 
180       public Builder devices(Map<String, BlockDevice> ebsBlockDevices) {
181          this.ebsBlockDevices = ImmutableMap.copyOf(checkNotNull(ebsBlockDevices, "ebsBlockDevices"));
182          return this;
183       }
184 
185       public Builder device(String key, BlockDevice value) {
186          if (key != null && value != null)
187             this.ebsBlockDevices.put(key, value);
188          return this;
189       }
190 
191       public RunningInstance build() {
192          return new RunningInstance(region, groupIds, amiLaunchIndex, dnsName, imageId, instanceId, instanceState,
193                   instanceType, ipAddress, kernelId, keyName, launchTime, availabilityZone, virtualizationType,
194                   platform, privateDnsName, privateIpAddress, ramdiskId, reason, rootDeviceType, rootDeviceName,
195                   ebsBlockDevices);
196       }
197 
198       public String getDnsName() {
199          return dnsName;
200       }
201 
202       public String getIpAddress() {
203          return ipAddress;
204       }
205 
206       public String getPrivateDnsName() {
207          return privateDnsName;
208       }
209 
210       public String getPrivateIpAddress() {
211          return privateIpAddress;
212       }
213 
214    }
215 
216    protected final String region;
217    protected final Set<String> groupIds;
218    protected final String amiLaunchIndex;
219    @Nullable
220    protected final String dnsName;
221    protected final String imageId;
222    protected final String instanceId;
223    protected final InstanceState instanceState;
224    protected final String instanceType;
225    @Nullable
226    protected final String ipAddress;
227    @Nullable
228    protected final String kernelId;
229    @Nullable
230    protected final String keyName;
231    protected final Date launchTime;
232    protected final String availabilityZone;
233    protected final String virtualizationType;
234    @Nullable
235    protected final String platform;
236    @Nullable
237    protected final String privateDnsName;
238    @Nullable
239    protected final String privateIpAddress;
240    @Nullable
241    protected final String ramdiskId;
242    @Nullable
243    protected final String reason;
244    protected final RootDeviceType rootDeviceType;
245    @Nullable
246    protected final String rootDeviceName;
247    protected final Map<String, BlockDevice> ebsBlockDevices;
248 
249    public int compareTo(RunningInstance o) {
250       return (this == o) ? 0 : getId().compareTo(o.getId());
251    }
252 
253    protected RunningInstance(String region, Iterable<String> groupIds, @Nullable String amiLaunchIndex,
254             @Nullable String dnsName, String imageId, String instanceId, InstanceState instanceState,
255             String instanceType, @Nullable String ipAddress, @Nullable String kernelId, @Nullable String keyName,
256             Date launchTime, String availabilityZone, String virtualizationType, @Nullable String platform,
257             @Nullable String privateDnsName, @Nullable String privateIpAddress, @Nullable String ramdiskId,
258             @Nullable String reason, RootDeviceType rootDeviceType, @Nullable String rootDeviceName,
259             Map<String, BlockDevice> ebsBlockDevices) {
260       this.region = checkNotNull(region, "region");
261       this.amiLaunchIndex = amiLaunchIndex; // nullable on runinstances.
262       this.dnsName = dnsName; // nullable on runinstances.
263       this.imageId = imageId; // nullable on runinstances.
264       this.instanceId = checkNotNull(instanceId, "instanceId");
265       this.instanceState = checkNotNull(instanceState, "instanceState");
266       this.instanceType = checkNotNull(instanceType, "instanceType");
267       this.ipAddress = ipAddress;
268       this.kernelId = kernelId;
269       this.keyName = keyName;
270       this.launchTime = launchTime;// nullable on spot.
271       this.availabilityZone = availabilityZone;// nullable on spot.
272       this.virtualizationType = virtualizationType;
273       this.platform = platform;
274       this.privateDnsName = privateDnsName;// nullable on runinstances.
275       this.privateIpAddress = privateIpAddress;// nullable on runinstances.
276       this.ramdiskId = ramdiskId;
277       this.reason = reason;
278       this.rootDeviceType = checkNotNull(rootDeviceType, "rootDeviceType");
279       this.rootDeviceName = rootDeviceName;
280       this.ebsBlockDevices = ImmutableMap.copyOf(checkNotNull(ebsBlockDevices, "ebsBlockDevices"));
281       this.groupIds = ImmutableSet.copyOf(checkNotNull(groupIds, "groupIds"));
282    }
283 
284    /**
285     * Instance Ids are scoped to the region.
286     */
287    public String getRegion() {
288       return region;
289    }
290 
291    /**
292     * The AMI launch index, which can be used to find this instance within the launch group. For
293     * more information, go to the Metadata section of the Amazon Elastic Compute Cloud Developer
294     * Guide.
295     * 
296     * @see <a href="http://docs.amazonwebservices.com/AWSEC2/2010-06-15/DeveloperGuide/" />
297     */
298    public String getAmiLaunchIndex() {
299       return amiLaunchIndex;
300    }
301 
302    /**
303     * The public DNS name assigned to the instance. This DNS name is contactable from outside the
304     * Amazon EC2 network. This element remains empty until the instance enters a running state.
305     */
306    public String getDnsName() {
307       return dnsName;
308    }
309 
310    /**
311     * Image ID of the AMI used to launch the instance.
312     */
313    public String getImageId() {
314       return imageId;
315    }
316 
317    /**
318     * Unique ID of the instance launched.
319     */
320    public String getId() {
321       return instanceId;
322    }
323 
324    /**
325     * The current state of the instance.
326     */
327    public InstanceState getInstanceState() {
328       return instanceState;
329    }
330 
331    /**
332     * The instance type.
333     */
334    public String getInstanceType() {
335       return instanceType;
336    }
337 
338    /**
339     * Specifies the IP address of the instance.
340     */
341    public String getIpAddress() {
342       return ipAddress;
343    }
344 
345    /**
346     * Optional. Kernel associated with this instance.
347     */
348    public String getKernelId() {
349       return kernelId;
350    }
351 
352    /**
353     * If this instance was launched with an associated key pair, this displays the key pair name.
354     */
355    public String getKeyName() {
356       return keyName;
357    }
358 
359    /**
360     * The time the instance launched.
361     */
362    public Date getLaunchTime() {
363       return launchTime;
364    }
365 
366    /**
367     * The location where the instance launched.
368     */
369    public String getAvailabilityZone() {
370       return availabilityZone;
371    }
372 
373    /**
374     * Specifies the instance's virtualization type. Valid values are paravirtual or hvm.
375     */
376    public String getVirtualizationType() {
377       return virtualizationType;
378    }
379 
380    /**
381     * Platform of the instance (e.g., Windows).
382     */
383    public String getPlatform() {
384       return platform;
385    }
386 
387    /**
388     * The private DNS name assigned to the instance. This DNS name can only be used inside the
389     * Amazon EC2 network. This element remains empty until the instance enters a running state.
390     */
391    public String getPrivateDnsName() {
392       return privateDnsName;
393    }
394 
395    /**
396     * Specifies the private IP address that is assigned to the instance (Amazon VPC).
397     */
398    public String getPrivateIpAddress() {
399       return privateIpAddress;
400    }
401 
402    /**
403     * Optional. RAM disk associated with this instance.
404     */
405    public String getRamdiskId() {
406       return ramdiskId;
407    }
408 
409    /**
410     * Reason for the most recent state transition. This might be an empty string.
411     */
412    public String getReason() {
413       return reason;
414    }
415 
416    public RootDeviceType getRootDeviceType() {
417       return rootDeviceType;
418    }
419 
420    public String getRootDeviceName() {
421       return rootDeviceName;
422    }
423 
424    /**
425     * EBS volumes associated with the instance.
426     */
427    public Map<String, BlockDevice> getEbsBlockDevices() {
428       return ebsBlockDevices;
429    }
430 
431    /**
432     * Names of the security groups.
433     */
434    public Set<String> getGroupIds() {
435       return groupIds;
436    }
437 
438    @Override
439    public int hashCode() {
440       final int prime = 31;
441       int result = 1;
442       result = prime * result + ((amiLaunchIndex == null) ? 0 : amiLaunchIndex.hashCode());
443       result = prime * result + ((availabilityZone == null) ? 0 : availabilityZone.hashCode());
444       result = prime * result + ((dnsName == null) ? 0 : dnsName.hashCode());
445       result = prime * result + ((ebsBlockDevices == null) ? 0 : ebsBlockDevices.hashCode());
446       result = prime * result + ((groupIds == null) ? 0 : groupIds.hashCode());
447       result = prime * result + ((imageId == null) ? 0 : imageId.hashCode());
448       result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode());
449       result = prime * result + ((instanceType == null) ? 0 : instanceType.hashCode());
450       result = prime * result + ((ipAddress == null) ? 0 : ipAddress.hashCode());
451       result = prime * result + ((kernelId == null) ? 0 : kernelId.hashCode());
452       result = prime * result + ((keyName == null) ? 0 : keyName.hashCode());
453       result = prime * result + ((launchTime == null) ? 0 : launchTime.hashCode());
454       result = prime * result + ((platform == null) ? 0 : platform.hashCode());
455       result = prime * result + ((privateDnsName == null) ? 0 : privateDnsName.hashCode());
456       result = prime * result + ((privateIpAddress == null) ? 0 : privateIpAddress.hashCode());
457       result = prime * result + ((ramdiskId == null) ? 0 : ramdiskId.hashCode());
458       result = prime * result + ((region == null) ? 0 : region.hashCode());
459       result = prime * result + ((rootDeviceName == null) ? 0 : rootDeviceName.hashCode());
460       result = prime * result + ((rootDeviceType == null) ? 0 : rootDeviceType.hashCode());
461       result = prime * result + ((virtualizationType == null) ? 0 : virtualizationType.hashCode());
462       return result;
463    }
464 
465    @Override
466    public boolean equals(Object obj) {
467       if (this == obj)
468          return true;
469       if (obj == null)
470          return false;
471       if (getClass() != obj.getClass())
472          return false;
473       RunningInstance other = (RunningInstance) obj;
474       if (amiLaunchIndex == null) {
475          if (other.amiLaunchIndex != null)
476             return false;
477       } else if (!amiLaunchIndex.equals(other.amiLaunchIndex))
478          return false;
479       if (availabilityZone == null) {
480          if (other.availabilityZone != null)
481             return false;
482       } else if (!availabilityZone.equals(other.availabilityZone))
483          return false;
484       if (dnsName == null) {
485          if (other.dnsName != null)
486             return false;
487       } else if (!dnsName.equals(other.dnsName))
488          return false;
489       if (ebsBlockDevices == null) {
490          if (other.ebsBlockDevices != null)
491             return false;
492       } else if (!ebsBlockDevices.equals(other.ebsBlockDevices))
493          return false;
494       if (groupIds == null) {
495          if (other.groupIds != null)
496             return false;
497       } else if (!groupIds.equals(other.groupIds))
498          return false;
499       if (imageId == null) {
500          if (other.imageId != null)
501             return false;
502       } else if (!imageId.equals(other.imageId))
503          return false;
504       if (instanceId == null) {
505          if (other.instanceId != null)
506             return false;
507       } else if (!instanceId.equals(other.instanceId))
508          return false;
509       if (instanceType == null) {
510          if (other.instanceType != null)
511             return false;
512       } else if (!instanceType.equals(other.instanceType))
513          return false;
514       if (ipAddress == null) {
515          if (other.ipAddress != null)
516             return false;
517       } else if (!ipAddress.equals(other.ipAddress))
518          return false;
519       if (kernelId == null) {
520          if (other.kernelId != null)
521             return false;
522       } else if (!kernelId.equals(other.kernelId))
523          return false;
524       if (keyName == null) {
525          if (other.keyName != null)
526             return false;
527       } else if (!keyName.equals(other.keyName))
528          return false;
529       if (launchTime == null) {
530          if (other.launchTime != null)
531             return false;
532       } else if (!launchTime.equals(other.launchTime))
533          return false;
534       if (platform == null) {
535          if (other.platform != null)
536             return false;
537       } else if (!platform.equals(other.platform))
538          return false;
539       if (privateDnsName == null) {
540          if (other.privateDnsName != null)
541             return false;
542       } else if (!privateDnsName.equals(other.privateDnsName))
543          return false;
544       if (privateIpAddress == null) {
545          if (other.privateIpAddress != null)
546             return false;
547       } else if (!privateIpAddress.equals(other.privateIpAddress))
548          return false;
549       if (ramdiskId == null) {
550          if (other.ramdiskId != null)
551             return false;
552       } else if (!ramdiskId.equals(other.ramdiskId))
553          return false;
554       if (region == null) {
555          if (other.region != null)
556             return false;
557       } else if (!region.equals(other.region))
558          return false;
559       if (rootDeviceName == null) {
560          if (other.rootDeviceName != null)
561             return false;
562       } else if (!rootDeviceName.equals(other.rootDeviceName))
563          return false;
564       if (rootDeviceType == null) {
565          if (other.rootDeviceType != null)
566             return false;
567       } else if (!rootDeviceType.equals(other.rootDeviceType))
568          return false;
569       if (virtualizationType == null) {
570          if (other.virtualizationType != null)
571             return false;
572       } else if (!virtualizationType.equals(other.virtualizationType))
573          return false;
574       return true;
575    }
576 
577    @Override
578    public String toString() {
579       return "[region=" + region + ", availabilityZone=" + availabilityZone + ", instanceId=" + instanceId
580                + ", instanceState=" + instanceState + ", instanceType=" + instanceType + ", virtualizationType="
581                + virtualizationType + ", imageId=" + imageId + ", ipAddress=" + ipAddress + ", dnsName=" + dnsName
582                + ", privateIpAddress=" + privateIpAddress + ", privateDnsName=" + privateDnsName + ", keyName="
583                + keyName + ", groupIds=" + groupIds  + ", platform=" + platform + ", launchTime=" + launchTime + ", rootDeviceName="
584                + rootDeviceName + ", rootDeviceType=" + rootDeviceType + ", ebsBlockDevices=" + ebsBlockDevices + "]";
585    }
586 
587 }