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 | * @author Adrian Cole |
27 | * @see <a href= |
28 | * "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-CreateVolume.html" |
29 | * /> |
30 | */ |
31 | public class Attachment implements Comparable<Attachment> { |
32 | public static enum Status { |
33 | ATTACHING, ATTACHED, DETACHING, DETACHED, BUSY, UNRECOGNIZED; |
34 | |
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 | public static Builder builder() { |
54 | return new Builder(); |
55 | } |
56 | |
57 | public static class Builder { |
58 | private String region; |
59 | private String volumeId; |
60 | private String instanceId; |
61 | private String device; |
62 | private Status status; |
63 | private Date attachTime; |
64 | |
65 | public Builder region(String region) { |
66 | this.region = region; |
67 | return this; |
68 | } |
69 | |
70 | public Builder volumeId(String volumeId) { |
71 | this.volumeId = volumeId; |
72 | return this; |
73 | } |
74 | |
75 | public Builder instanceId(String instanceId) { |
76 | this.instanceId = instanceId; |
77 | return this; |
78 | } |
79 | |
80 | public Builder device(String device) { |
81 | this.device = device; |
82 | return this; |
83 | } |
84 | |
85 | public Builder status(Status status) { |
86 | this.status = status; |
87 | return this; |
88 | } |
89 | |
90 | public Builder attachTime(Date attachTime) { |
91 | this.attachTime = attachTime; |
92 | return this; |
93 | } |
94 | |
95 | public Attachment build() { |
96 | return new Attachment(region, volumeId, instanceId, device, status, attachTime); |
97 | } |
98 | } |
99 | |
100 | private final String region; |
101 | private final String volumeId; |
102 | private final String instanceId; |
103 | private final String device; |
104 | private final Status status; |
105 | private final Date attachTime; |
106 | |
107 | public Attachment(String region, String volumeId, String instanceId, String device, Status status, Date attachTime) { |
108 | this.region = checkNotNull(region, "region"); |
109 | this.volumeId = volumeId; |
110 | this.instanceId = instanceId; |
111 | this.device = device; |
112 | this.status = status; |
113 | this.attachTime = attachTime; |
114 | } |
115 | |
116 | /** |
117 | * Snapshots are tied to Regions and can only be used for volumes within the same Region. |
118 | */ |
119 | public String getRegion() { |
120 | return region; |
121 | } |
122 | |
123 | /** |
124 | * The ID of the volume. |
125 | */ |
126 | public String getVolumeId() { |
127 | return volumeId; |
128 | } |
129 | |
130 | /** |
131 | * The ID of the instance. |
132 | */ |
133 | public String getId() { |
134 | return instanceId; |
135 | } |
136 | |
137 | /** |
138 | * The device as it is exposed to the instance. |
139 | */ |
140 | public String getDevice() { |
141 | return device; |
142 | } |
143 | |
144 | /** |
145 | * Volume state. |
146 | */ |
147 | public Status getStatus() { |
148 | return status; |
149 | } |
150 | |
151 | /** |
152 | * Time stamp when the attachment initiated. |
153 | */ |
154 | public Date getAttachTime() { |
155 | return attachTime; |
156 | } |
157 | |
158 | @Override |
159 | public int hashCode() { |
160 | final int prime = 31; |
161 | int result = 1; |
162 | result = prime * result + ((attachTime == null) ? 0 : attachTime.hashCode()); |
163 | result = prime * result + ((device == null) ? 0 : device.hashCode()); |
164 | result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode()); |
165 | result = prime * result + ((region == null) ? 0 : region.hashCode()); |
166 | result = prime * result + ((status == null) ? 0 : status.hashCode()); |
167 | result = prime * result + ((volumeId == null) ? 0 : volumeId.hashCode()); |
168 | return result; |
169 | } |
170 | |
171 | @Override |
172 | public boolean equals(Object obj) { |
173 | if (this == obj) |
174 | return true; |
175 | if (obj == null) |
176 | return false; |
177 | if (getClass() != obj.getClass()) |
178 | return false; |
179 | Attachment other = (Attachment) obj; |
180 | if (attachTime == null) { |
181 | if (other.attachTime != null) |
182 | return false; |
183 | } else if (!attachTime.equals(other.attachTime)) |
184 | return false; |
185 | if (device == null) { |
186 | if (other.device != null) |
187 | return false; |
188 | } else if (!device.equals(other.device)) |
189 | return false; |
190 | if (instanceId == null) { |
191 | if (other.instanceId != null) |
192 | return false; |
193 | } else if (!instanceId.equals(other.instanceId)) |
194 | return false; |
195 | if (region == null) { |
196 | if (other.region != null) |
197 | return false; |
198 | } else if (!region.equals(other.region)) |
199 | return false; |
200 | if (status == null) { |
201 | if (other.status != null) |
202 | return false; |
203 | } else if (!status.equals(other.status)) |
204 | return false; |
205 | if (volumeId == null) { |
206 | if (other.volumeId != null) |
207 | return false; |
208 | } else if (!volumeId.equals(other.volumeId)) |
209 | return false; |
210 | return true; |
211 | } |
212 | |
213 | @Override |
214 | public String toString() { |
215 | return "Attachment [region=" + region + ", volumeId=" + volumeId + ", instanceId=" + instanceId + ", device=" |
216 | + device + ", attachTime=" + attachTime + ", status=" + status + "]"; |
217 | } |
218 | |
219 | @Override |
220 | public int compareTo(Attachment o) { |
221 | return attachTime.compareTo(o.attachTime); |
222 | } |
223 | |
224 | } |