View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
18   */
19  package org.jclouds.aws.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 javax.annotation.Nullable;
28  
29  import org.jclouds.ec2.domain.BlockDevice;
30  import org.jclouds.ec2.domain.InstanceState;
31  import org.jclouds.ec2.domain.RootDeviceType;
32  import org.jclouds.ec2.domain.RunningInstance;
33  
34  import com.google.common.collect.ImmutableSet;
35  import com.google.common.collect.Sets;
36  
37  /**
38   * 
39   * @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-RunningInstancesItemType.html"
40   *      />
41   * @author Adrian Cole
42   */
43  public class AWSRunningInstance extends RunningInstance {
44     public static Builder builder() {
45        return new Builder();
46     }
47  
48     public static class Builder extends org.jclouds.ec2.domain.RunningInstance.Builder {
49        private MonitoringState monitoringState;
50        private String placementGroup;
51        private Set<String> productCodes = Sets.newLinkedHashSet();
52        private String subnetId;
53        private String spotInstanceRequestId;
54        private String vpcId;
55  
56        public Builder monitoringState(MonitoringState monitoringState) {
57           this.monitoringState = monitoringState;
58           return this;
59        }
60  
61        public Builder placementGroup(String placementGroup) {
62           this.placementGroup = placementGroup;
63           return this;
64        }
65  
66        public Builder productCodes(Iterable<String> productCodes) {
67           this.productCodes = ImmutableSet.copyOf(checkNotNull(productCodes, "productCodes"));
68           return this;
69        }
70  
71        public Builder productCode(String productCode) {
72           if (productCode != null)
73              this.productCodes.add(productCode);
74           return this;
75        }
76  
77        public Builder subnetId(String subnetId) {
78           this.subnetId = subnetId;
79           return this;
80        }
81  
82        public Builder spotInstanceRequestId(String spotInstanceRequestId) {
83           this.spotInstanceRequestId = spotInstanceRequestId;
84           return this;
85        }
86  
87        public Builder vpcId(String vpcId) {
88           this.vpcId = vpcId;
89           return this;
90        }
91  
92        @Override
93        public Builder amiLaunchIndex(String amiLaunchIndex) {
94           return Builder.class.cast(super.amiLaunchIndex(amiLaunchIndex));
95        }
96  
97        @Override
98        public Builder availabilityZone(String availabilityZone) {
99           return Builder.class.cast(super.availabilityZone(availabilityZone));
100       }
101 
102       @Override
103       public Builder devices(Map<String, BlockDevice> ebsBlockDevices) {
104          return Builder.class.cast(super.devices(ebsBlockDevices));
105       }
106 
107       @Override
108       public Builder dnsName(String dnsName) {
109          return Builder.class.cast(super.dnsName(dnsName));
110       }
111 
112       @Override
113       public Builder imageId(String imageId) {
114          return Builder.class.cast(super.imageId(imageId));
115       }
116 
117       @Override
118       public Builder instanceId(String instanceId) {
119          return Builder.class.cast(super.instanceId(instanceId));
120       }
121 
122       @Override
123       public Builder instanceState(InstanceState instanceState) {
124          return Builder.class.cast(super.instanceState(instanceState));
125       }
126 
127       @Override
128       public Builder instanceType(String instanceType) {
129          return Builder.class.cast(super.instanceType(instanceType));
130       }
131 
132       @Override
133       public Builder ipAddress(String ipAddress) {
134          return Builder.class.cast(super.ipAddress(ipAddress));
135       }
136 
137       @Override
138       public Builder kernelId(String kernelId) {
139          return Builder.class.cast(super.kernelId(kernelId));
140       }
141 
142       @Override
143       public Builder keyName(String keyName) {
144          return Builder.class.cast(super.keyName(keyName));
145       }
146 
147       @Override
148       public Builder launchTime(Date launchTime) {
149          return Builder.class.cast(super.launchTime(launchTime));
150       }
151 
152       @Override
153       public Builder platform(String platform) {
154          return Builder.class.cast(super.platform(platform));
155       }
156 
157       @Override
158       public Builder privateDnsName(String privateDnsName) {
159          return Builder.class.cast(super.privateDnsName(privateDnsName));
160       }
161 
162       @Override
163       public Builder privateIpAddress(String privateIpAddress) {
164          return Builder.class.cast(super.privateIpAddress(privateIpAddress));
165       }
166 
167       @Override
168       public Builder ramdiskId(String ramdiskId) {
169          return Builder.class.cast(super.ramdiskId(ramdiskId));
170       }
171 
172       @Override
173       public Builder reason(String reason) {
174          return Builder.class.cast(super.reason(reason));
175       }
176 
177       @Override
178       public Builder region(String region) {
179          return Builder.class.cast(super.region(region));
180       }
181 
182       @Override
183       public Builder rootDeviceName(String rootDeviceName) {
184          return Builder.class.cast(super.rootDeviceName(rootDeviceName));
185       }
186 
187       @Override
188       public Builder rootDeviceType(RootDeviceType rootDeviceType) {
189          return Builder.class.cast(super.rootDeviceType(rootDeviceType));
190       }
191 
192       @Override
193       public Builder virtualizationType(String virtualizationType) {
194          return Builder.class.cast(super.virtualizationType(virtualizationType));
195       }
196 
197       @Override
198       public Builder device(String key, BlockDevice value) {
199          return Builder.class.cast(super.device(key, value));
200       }
201 
202       @Override
203       public Builder groupId(String groupId) {
204          return Builder.class.cast(super.groupId(groupId));
205       }
206 
207       @Override
208       public Builder groupIds(Iterable<String> groupIds) {
209          return Builder.class.cast(super.groupIds(groupIds));
210       }
211 
212       @Override
213       public AWSRunningInstance build() {
214          return new AWSRunningInstance(region, groupIds, amiLaunchIndex, dnsName, imageId, instanceId, instanceState,
215                   instanceType, ipAddress, kernelId, keyName, launchTime, availabilityZone, virtualizationType,
216                   platform, privateDnsName, privateIpAddress, ramdiskId, reason, rootDeviceType, rootDeviceName,
217                   ebsBlockDevices, monitoringState, placementGroup, productCodes, subnetId, spotInstanceRequestId,
218                   vpcId);
219       }
220 
221    }
222 
223    private final MonitoringState monitoringState;
224    @Nullable
225    private final String placementGroup;
226    private final Set<String> productCodes;
227    @Nullable
228    private final String subnetId;
229    @Nullable
230    private final String spotInstanceRequestId;
231    @Nullable
232    private final String vpcId;
233 
234    protected AWSRunningInstance(String region, Iterable<String> groupIds, String amiLaunchIndex, String dnsName,
235             String imageId, String instanceId, InstanceState instanceState, String instanceType, String ipAddress,
236             String kernelId, String keyName, Date launchTime, String availabilityZone, String virtualizationType,
237             String platform, String privateDnsName, String privateIpAddress, String ramdiskId, String reason,
238             RootDeviceType rootDeviceType, String rootDeviceName, Map<String, BlockDevice> ebsBlockDevices,
239             MonitoringState monitoringState, String placementGroup, Iterable<String> productCodes, String subnetId,
240             String spotInstanceRequestId, String vpcId) {
241       super(region, groupIds, amiLaunchIndex, dnsName, imageId, instanceId, instanceState, instanceType, ipAddress,
242                kernelId, keyName, launchTime, availabilityZone, virtualizationType, platform, privateDnsName,
243                privateIpAddress, ramdiskId, reason, rootDeviceType, rootDeviceName, ebsBlockDevices);
244       this.monitoringState = checkNotNull(monitoringState, "monitoringState");
245       this.placementGroup = placementGroup;
246       this.productCodes = ImmutableSet.copyOf(checkNotNull(groupIds, "groupIds"));
247       this.subnetId = subnetId;
248       this.spotInstanceRequestId = spotInstanceRequestId;
249       this.vpcId = vpcId;
250    }
251 
252    /**
253     * State of monitoring for the instance.
254     */
255    public MonitoringState getMonitoringState() {
256       return monitoringState;
257    }
258 
259    /**
260     * The name of the placement group the instance is in (for cluster compute instances).
261     */
262    public String getPlacementGroup() {
263       return placementGroup;
264    }
265 
266    /**
267     * Product codes attached to this instance.
268     */
269    public Set<String> getProductCodes() {
270       return productCodes;
271    }
272 
273    /**
274     * The ID of the Spot Instance request
275     */
276    public String getSpotInstanceRequestId() {
277       return spotInstanceRequestId;
278    }
279 
280    /**
281     * Specifies the VPC in which the instance is running (Amazon Virtual Private Cloud).
282     */
283    public String getVpcId() {
284       return vpcId;
285    }
286 
287    /**
288     * Specifies the subnet ID in which the instance is running (Amazon Virtual Private Cloud).
289     */
290    public String getSubnetId() {
291       return subnetId;
292    }
293 
294    @Override
295    public int hashCode() {
296       final int prime = 31;
297       int result = super.hashCode();
298       result = prime * result + ((placementGroup == null) ? 0 : placementGroup.hashCode());
299       result = prime * result + ((productCodes == null) ? 0 : productCodes.hashCode());
300       result = prime * result + ((spotInstanceRequestId == null) ? 0 : spotInstanceRequestId.hashCode());
301       result = prime * result + ((subnetId == null) ? 0 : subnetId.hashCode());
302       result = prime * result + ((vpcId == null) ? 0 : vpcId.hashCode());
303       return result;
304    }
305 
306    @Override
307    public boolean equals(Object obj) {
308       if (this == obj)
309          return true;
310       if (!super.equals(obj))
311          return false;
312       if (getClass() != obj.getClass())
313          return false;
314       AWSRunningInstance other = (AWSRunningInstance) obj;
315       if (placementGroup == null) {
316          if (other.placementGroup != null)
317             return false;
318       } else if (!placementGroup.equals(other.placementGroup))
319          return false;
320       if (productCodes == null) {
321          if (other.productCodes != null)
322             return false;
323       } else if (!productCodes.equals(other.productCodes))
324          return false;
325       if (spotInstanceRequestId == null) {
326          if (other.spotInstanceRequestId != null)
327             return false;
328       } else if (!spotInstanceRequestId.equals(other.spotInstanceRequestId))
329          return false;
330       if (subnetId == null) {
331          if (other.subnetId != null)
332             return false;
333       } else if (!subnetId.equals(other.subnetId))
334          return false;
335       if (vpcId == null) {
336          if (other.vpcId != null)
337             return false;
338       } else if (!vpcId.equals(other.vpcId))
339          return false;
340       return true;
341    }
342 
343    @Override
344    public String toString() {
345       return "[region=" + region + ", availabilityZone=" + availabilityZone + ", instanceId=" + instanceId
346                + ", instanceState=" + instanceState + ", instanceType=" + instanceType + ", virtualizationType="
347                + virtualizationType + ", imageId=" + imageId + ", ipAddress=" + ipAddress + ", dnsName=" + dnsName
348                + ", privateIpAddress=" + privateIpAddress + ", privateDnsName=" + privateDnsName + ", keyName="
349                + keyName + ", platform=" + platform + ", launchTime=" + launchTime + ", rootDeviceName="
350                + rootDeviceName + ", rootDeviceType=" + rootDeviceType + ", ebsBlockDevices=" + ebsBlockDevices
351                + ", monitoringState=" + monitoringState + ", placementGroup=" + placementGroup + ", productCodes="
352                + productCodes + ", spotInstanceRequestId=" + spotInstanceRequestId + ", subnetId=" + subnetId
353                + ", vpcId=" + vpcId + "]";
354    }
355 
356 }