| 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 | |
| 25 | /** |
| 26 | * |
| 27 | * @see <a href= |
| 28 | * "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-CreateVolume.html" |
| 29 | * /> |
| 30 | * @author Adrian Cole |
| 31 | */ |
| 32 | public class Attachment implements Comparable<Attachment> { |
| 33 | public static enum Status { |
| 34 | ATTACHING, ATTACHED, DETACHING, DETACHED, BUSY, UNRECOGNIZED; |
| 35 | public String value() { |
| 36 | return name().toLowerCase(); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public String toString() { |
| 41 | return value(); |
| 42 | } |
| 43 | |
| 44 | public static Status fromValue(String status) { |
| 45 | try { |
| 46 | return valueOf(checkNotNull(status, "status").toUpperCase()); |
| 47 | } catch (IllegalArgumentException e) { |
| 48 | return UNRECOGNIZED; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private final String region; |
| 54 | private final String volumeId; |
| 55 | private final String instanceId; |
| 56 | private final String device; |
| 57 | private final Status status; |
| 58 | private final Date attachTime; |
| 59 | |
| 60 | public Attachment(String region, String volumeId, String instanceId, String device, Status status, Date attachTime) { |
| 61 | this.region = checkNotNull(region, "region"); |
| 62 | this.volumeId = volumeId; |
| 63 | this.instanceId = instanceId; |
| 64 | this.device = device; |
| 65 | this.status = status; |
| 66 | this.attachTime = attachTime; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Snapshots are tied to Regions and can only be used for volumes within the same Region. |
| 71 | * |
| 72 | */ |
| 73 | public String getRegion() { |
| 74 | return region; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * The ID of the volume. |
| 79 | */ |
| 80 | public String getVolumeId() { |
| 81 | return volumeId; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * The ID of the instance. |
| 86 | */ |
| 87 | public String getId() { |
| 88 | return instanceId; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * The device as it is exposed to the instance. |
| 93 | */ |
| 94 | public String getDevice() { |
| 95 | return device; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Volume state. |
| 100 | */ |
| 101 | public Status getStatus() { |
| 102 | return status; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Time stamp when the attachment initiated. |
| 107 | */ |
| 108 | public Date getAttachTime() { |
| 109 | return attachTime; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int hashCode() { |
| 114 | final int prime = 31; |
| 115 | int result = 1; |
| 116 | result = prime * result + ((attachTime == null) ? 0 : attachTime.hashCode()); |
| 117 | result = prime * result + ((device == null) ? 0 : device.hashCode()); |
| 118 | result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode()); |
| 119 | result = prime * result + ((region == null) ? 0 : region.hashCode()); |
| 120 | result = prime * result + ((status == null) ? 0 : status.hashCode()); |
| 121 | result = prime * result + ((volumeId == null) ? 0 : volumeId.hashCode()); |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean equals(Object obj) { |
| 127 | if (this == obj) |
| 128 | return true; |
| 129 | if (obj == null) |
| 130 | return false; |
| 131 | if (getClass() != obj.getClass()) |
| 132 | return false; |
| 133 | Attachment other = (Attachment) obj; |
| 134 | if (attachTime == null) { |
| 135 | if (other.attachTime != null) |
| 136 | return false; |
| 137 | } else if (!attachTime.equals(other.attachTime)) |
| 138 | return false; |
| 139 | if (device == null) { |
| 140 | if (other.device != null) |
| 141 | return false; |
| 142 | } else if (!device.equals(other.device)) |
| 143 | return false; |
| 144 | if (instanceId == null) { |
| 145 | if (other.instanceId != null) |
| 146 | return false; |
| 147 | } else if (!instanceId.equals(other.instanceId)) |
| 148 | return false; |
| 149 | if (region == null) { |
| 150 | if (other.region != null) |
| 151 | return false; |
| 152 | } else if (!region.equals(other.region)) |
| 153 | return false; |
| 154 | if (status == null) { |
| 155 | if (other.status != null) |
| 156 | return false; |
| 157 | } else if (!status.equals(other.status)) |
| 158 | return false; |
| 159 | if (volumeId == null) { |
| 160 | if (other.volumeId != null) |
| 161 | return false; |
| 162 | } else if (!volumeId.equals(other.volumeId)) |
| 163 | return false; |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public String toString() { |
| 169 | return "Attachment [region=" + region + ", volumeId=" + volumeId + ", instanceId=" + instanceId + ", device=" |
| 170 | + device + ", attachTime=" + attachTime + ", status=" + status + "]"; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public int compareTo(Attachment o) { |
| 175 | return attachTime.compareTo(o.attachTime); |
| 176 | } |
| 177 | |
| 178 | } |