| 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.Date; |
| 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.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.base.Strings; |
| 35 | import com.google.common.collect.ImmutableMap; |
| 36 | import com.google.common.collect.ImmutableSet; |
| 37 | import com.google.common.collect.Maps; |
| 38 | import com.google.common.collect.Sets; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @see <a href= |
| 43 | * "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-RunningInstancesItemType.html" |
| 44 | * /> |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | public class AWSRunningInstance extends RunningInstance { |
| 48 | public static Builder builder() { |
| 49 | return new Builder(); |
| 50 | } |
| 51 | |
| 52 | public static class Builder extends org.jclouds.ec2.domain.RunningInstance.Builder { |
| 53 | private MonitoringState monitoringState; |
| 54 | private String placementGroup; |
| 55 | private Set<String> productCodes = Sets.newLinkedHashSet(); |
| 56 | private String subnetId; |
| 57 | private String spotInstanceRequestId; |
| 58 | private String vpcId; |
| 59 | private Map<String, String> securityGroupIdToNames = Maps.newLinkedHashMap(); |
| 60 | private Map<String, String> tags = Maps.newLinkedHashMap(); |
| 61 | |
| 62 | public Builder tags(Map<String, String> tags) { |
| 63 | this.tags = ImmutableMap.copyOf(checkNotNull(tags, "tags")); |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | public Builder tag(String key, String value) { |
| 68 | if (key != null) |
| 69 | this.tags.put(key, Strings.nullToEmpty(value)); |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | public Builder securityGroupIdToNames(Map<String, String> securityGroupIdToNames) { |
| 74 | this.securityGroupIdToNames = ImmutableMap.copyOf(checkNotNull(securityGroupIdToNames, |
| 75 | "securityGroupIdToNames")); |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | public Builder securityGroupIdToName(String key, String value) { |
| 80 | if (key != null && value != null) |
| 81 | this.securityGroupIdToNames.put(key, value); |
| 82 | return this; |
| 83 | } |
| 84 | |
| 85 | public Builder monitoringState(MonitoringState monitoringState) { |
| 86 | this.monitoringState = monitoringState; |
| 87 | return this; |
| 88 | } |
| 89 | |
| 90 | public Builder placementGroup(String placementGroup) { |
| 91 | this.placementGroup = placementGroup; |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | public Builder productCodes(Iterable<String> productCodes) { |
| 96 | this.productCodes = ImmutableSet.copyOf(checkNotNull(productCodes, "productCodes")); |
| 97 | return this; |
| 98 | } |
| 99 | |
| 100 | public Builder productCode(String productCode) { |
| 101 | if (productCode != null) |
| 102 | this.productCodes.add(productCode); |
| 103 | return this; |
| 104 | } |
| 105 | |
| 106 | public Builder subnetId(String subnetId) { |
| 107 | this.subnetId = subnetId; |
| 108 | return this; |
| 109 | } |
| 110 | |
| 111 | public Builder spotInstanceRequestId(String spotInstanceRequestId) { |
| 112 | this.spotInstanceRequestId = spotInstanceRequestId; |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | public Builder vpcId(String vpcId) { |
| 117 | this.vpcId = vpcId; |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Builder amiLaunchIndex(String amiLaunchIndex) { |
| 123 | return Builder.class.cast(super.amiLaunchIndex(amiLaunchIndex)); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Builder availabilityZone(String availabilityZone) { |
| 128 | return Builder.class.cast(super.availabilityZone(availabilityZone)); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public Builder devices(Map<String, BlockDevice> ebsBlockDevices) { |
| 133 | return Builder.class.cast(super.devices(ebsBlockDevices)); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Builder dnsName(String dnsName) { |
| 138 | return Builder.class.cast(super.dnsName(dnsName)); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Builder imageId(String imageId) { |
| 143 | return Builder.class.cast(super.imageId(imageId)); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Builder instanceId(String instanceId) { |
| 148 | return Builder.class.cast(super.instanceId(instanceId)); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Builder instanceState(InstanceState instanceState) { |
| 153 | return Builder.class.cast(super.instanceState(instanceState)); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Builder instanceType(String instanceType) { |
| 158 | return Builder.class.cast(super.instanceType(instanceType)); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Builder ipAddress(String ipAddress) { |
| 163 | return Builder.class.cast(super.ipAddress(ipAddress)); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Builder kernelId(String kernelId) { |
| 168 | return Builder.class.cast(super.kernelId(kernelId)); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public Builder keyName(String keyName) { |
| 173 | return Builder.class.cast(super.keyName(keyName)); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public Builder launchTime(Date launchTime) { |
| 178 | return Builder.class.cast(super.launchTime(launchTime)); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public Builder platform(String platform) { |
| 183 | return Builder.class.cast(super.platform(platform)); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Builder privateDnsName(String privateDnsName) { |
| 188 | return Builder.class.cast(super.privateDnsName(privateDnsName)); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public Builder privateIpAddress(String privateIpAddress) { |
| 193 | return Builder.class.cast(super.privateIpAddress(privateIpAddress)); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public Builder ramdiskId(String ramdiskId) { |
| 198 | return Builder.class.cast(super.ramdiskId(ramdiskId)); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Builder reason(String reason) { |
| 203 | return Builder.class.cast(super.reason(reason)); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public Builder region(String region) { |
| 208 | return Builder.class.cast(super.region(region)); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Builder rootDeviceName(String rootDeviceName) { |
| 213 | return Builder.class.cast(super.rootDeviceName(rootDeviceName)); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public Builder rootDeviceType(RootDeviceType rootDeviceType) { |
| 218 | return Builder.class.cast(super.rootDeviceType(rootDeviceType)); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public Builder virtualizationType(String virtualizationType) { |
| 223 | return Builder.class.cast(super.virtualizationType(virtualizationType)); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public Builder device(String key, BlockDevice value) { |
| 228 | return Builder.class.cast(super.device(key, value)); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public Builder groupId(String groupId) { |
| 233 | return Builder.class.cast(super.groupId(groupId)); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public Builder groupIds(Iterable<String> groupIds) { |
| 238 | return Builder.class.cast(super.groupIds(groupIds)); |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public AWSRunningInstance build() { |
| 243 | return new AWSRunningInstance(region, securityGroupIdToNames, amiLaunchIndex, dnsName, imageId, instanceId, |
| 244 | instanceState, instanceType, ipAddress, kernelId, keyName, launchTime, availabilityZone, |
| 245 | virtualizationType, platform, privateDnsName, privateIpAddress, ramdiskId, reason, rootDeviceType, |
| 246 | rootDeviceName, ebsBlockDevices, monitoringState, placementGroup, productCodes, subnetId, |
| 247 | spotInstanceRequestId, vpcId, tags); |
| 248 | } |
| 249 | |
| 250 | } |
| 251 | |
| 252 | private final MonitoringState monitoringState; |
| 253 | @Nullable |
| 254 | private final String placementGroup; |
| 255 | private final Set<String> productCodes; |
| 256 | @Nullable |
| 257 | private final String subnetId; |
| 258 | @Nullable |
| 259 | private final String spotInstanceRequestId; |
| 260 | @Nullable |
| 261 | private final String vpcId; |
| 262 | private final Map<String, String> securityGroupIdToNames; |
| 263 | private final Map<String, String> tags; |
| 264 | |
| 265 | protected AWSRunningInstance(String region, Map<String, String> securityGroupIdToNames, String amiLaunchIndex, |
| 266 | String dnsName, String imageId, String instanceId, InstanceState instanceState, String instanceType, |
| 267 | String ipAddress, String kernelId, String keyName, Date launchTime, String availabilityZone, |
| 268 | String virtualizationType, String platform, String privateDnsName, String privateIpAddress, String ramdiskId, |
| 269 | String reason, RootDeviceType rootDeviceType, String rootDeviceName, Map<String, BlockDevice> ebsBlockDevices, |
| 270 | MonitoringState monitoringState, String placementGroup, Iterable<String> productCodes, String subnetId, |
| 271 | String spotInstanceRequestId, String vpcId, Map<String, String> tags) { |
| 272 | super(region, securityGroupIdToNames.values(), amiLaunchIndex, dnsName, imageId, instanceId, instanceState, |
| 273 | instanceType, ipAddress, kernelId, keyName, launchTime, availabilityZone, virtualizationType, platform, |
| 274 | privateDnsName, privateIpAddress, ramdiskId, reason, rootDeviceType, rootDeviceName, ebsBlockDevices); |
| 275 | this.monitoringState = checkNotNull(monitoringState, "monitoringState"); |
| 276 | this.placementGroup = placementGroup; |
| 277 | this.productCodes = ImmutableSet.copyOf(checkNotNull(productCodes, "productCodes")); |
| 278 | this.subnetId = subnetId; |
| 279 | this.spotInstanceRequestId = spotInstanceRequestId; |
| 280 | this.vpcId = vpcId; |
| 281 | this.securityGroupIdToNames = ImmutableMap.<String, String> copyOf(checkNotNull(securityGroupIdToNames, |
| 282 | "securityGroupIdToNames")); |
| 283 | this.tags = ImmutableMap.<String, String> copyOf(checkNotNull(tags, "tags")); |
| 284 | } |
| 285 | |
| 286 | public Map<String, String> getSecurityGroupIdToNames() { |
| 287 | return securityGroupIdToNames; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * State of monitoring for the instance. |
| 292 | */ |
| 293 | public MonitoringState getMonitoringState() { |
| 294 | return monitoringState; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * The name of the placement group the instance is in (for cluster compute |
| 299 | * instances). |
| 300 | */ |
| 301 | public String getPlacementGroup() { |
| 302 | return placementGroup; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Product codes attached to this instance. |
| 307 | */ |
| 308 | public Set<String> getProductCodes() { |
| 309 | return productCodes; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * The ID of the Spot Instance request |
| 314 | */ |
| 315 | public String getSpotInstanceRequestId() { |
| 316 | return spotInstanceRequestId; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Specifies the VPC in which the instance is running (Amazon Virtual Private |
| 321 | * Cloud). |
| 322 | */ |
| 323 | public String getVpcId() { |
| 324 | return vpcId; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Specifies the subnet ID in which the instance is running (Amazon Virtual |
| 329 | * Private Cloud). |
| 330 | */ |
| 331 | public String getSubnetId() { |
| 332 | return subnetId; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * tags that are present in the instance |
| 337 | */ |
| 338 | public Map<String, String> getTags() { |
| 339 | return tags; |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public int hashCode() { |
| 344 | final int prime = 31; |
| 345 | int result = super.hashCode(); |
| 346 | result = prime * result + ((placementGroup == null) ? 0 : placementGroup.hashCode()); |
| 347 | result = prime * result + ((productCodes == null) ? 0 : productCodes.hashCode()); |
| 348 | result = prime * result + ((spotInstanceRequestId == null) ? 0 : spotInstanceRequestId.hashCode()); |
| 349 | result = prime * result + ((subnetId == null) ? 0 : subnetId.hashCode()); |
| 350 | result = prime * result + ((vpcId == null) ? 0 : vpcId.hashCode()); |
| 351 | result = prime * result + ((tags == null) ? 0 : tags.hashCode()); |
| 352 | return result; |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public boolean equals(Object obj) { |
| 357 | if (this == obj) |
| 358 | return true; |
| 359 | if (!super.equals(obj)) |
| 360 | return false; |
| 361 | if (getClass() != obj.getClass()) |
| 362 | return false; |
| 363 | AWSRunningInstance other = (AWSRunningInstance) obj; |
| 364 | if (placementGroup == null) { |
| 365 | if (other.placementGroup != null) |
| 366 | return false; |
| 367 | } else if (!placementGroup.equals(other.placementGroup)) |
| 368 | return false; |
| 369 | if (productCodes == null) { |
| 370 | if (other.productCodes != null) |
| 371 | return false; |
| 372 | } else if (!productCodes.equals(other.productCodes)) |
| 373 | return false; |
| 374 | if (spotInstanceRequestId == null) { |
| 375 | if (other.spotInstanceRequestId != null) |
| 376 | return false; |
| 377 | } else if (!spotInstanceRequestId.equals(other.spotInstanceRequestId)) |
| 378 | return false; |
| 379 | if (subnetId == null) { |
| 380 | if (other.subnetId != null) |
| 381 | return false; |
| 382 | } else if (!subnetId.equals(other.subnetId)) |
| 383 | return false; |
| 384 | if (vpcId == null) { |
| 385 | if (other.vpcId != null) |
| 386 | return false; |
| 387 | } else if (!vpcId.equals(other.vpcId)) |
| 388 | return false; |
| 389 | if (tags == null) { |
| 390 | if (other.tags != null) |
| 391 | return false; |
| 392 | } else if (!tags.equals(other.tags)) |
| 393 | return false; |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public String toString() { |
| 399 | return "[region=" + region + ", availabilityZone=" + availabilityZone + ", instanceId=" + instanceId |
| 400 | + ", instanceState=" + instanceState + ", instanceType=" + instanceType + ", virtualizationType=" |
| 401 | + virtualizationType + ", imageId=" + imageId + ", ipAddress=" + ipAddress + ", dnsName=" + dnsName |
| 402 | + ", privateIpAddress=" + privateIpAddress + ", privateDnsName=" + privateDnsName + ", keyName=" + keyName |
| 403 | + ", platform=" + platform + ", launchTime=" + launchTime + ", rootDeviceName=" + rootDeviceName |
| 404 | + ", rootDeviceType=" + rootDeviceType + ", ebsBlockDevices=" + ebsBlockDevices + ", monitoringState=" |
| 405 | + monitoringState + ", placementGroup=" + placementGroup + ", productCodes=" + productCodes |
| 406 | + ", spotInstanceRequestId=" + spotInstanceRequestId + ", subnetId=" + subnetId + ", vpcId=" + vpcId |
| 407 | + ", tags=" + tags + "]"; |
| 408 | } |
| 409 | |
| 410 | } |