1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.aws.ec2.model;
17
18 import static com.google.common.base.Optional.absent;
19 import static com.google.common.base.Optional.fromNullable;
20 import static com.google.common.base.Preconditions.checkArgument;
21 import static org.apache.commons.lang3.StringUtils.trimToNull;
22 import static org.kuali.common.util.base.Precondition.checkMax;
23 import static org.kuali.common.util.base.Precondition.checkMin;
24 import static org.kuali.common.util.base.Precondition.checkNotBlank;
25
26 import com.amazonaws.services.ec2.model.EbsBlockDevice;
27 import com.amazonaws.services.ec2.model.VolumeType;
28 import com.google.common.base.Optional;
29
30 public final class ImmutableEbsBlockDevice extends EbsBlockDevice {
31
32 private static final long serialVersionUID = 1L;
33 private static final String UOE_MSG = "Not supported for immutable ebs block devices";
34
35 private ImmutableEbsBlockDevice(Builder builder) {
36 super.setSnapshotId(builder.snapshotId);
37 super.setVolumeType(builder.volumeType);
38 super.setDeleteOnTermination(builder.deleteOnTermination);
39 super.setIops(builder.iops.orNull());
40 super.setVolumeSize(builder.volumeSizeInGigabytes.orNull());
41 }
42
43 public static ImmutableEbsBlockDevice copyOf(EbsBlockDevice ebs) {
44 if (ebs instanceof ImmutableEbsBlockDevice) {
45 return (ImmutableEbsBlockDevice) ebs;
46 }
47 Builder builder = builder(ebs.getSnapshotId());
48 if (trimToNull(ebs.getVolumeType()) != null) {
49 builder.withVolumeType(VolumeType.fromValue(ebs.getVolumeType()));
50 }
51 if (ebs.getDeleteOnTermination() != null) {
52 builder.withDeleteOnTermination(ebs.getDeleteOnTermination());
53 }
54 if (ebs.getIops() != null) {
55 builder.withIops(ebs.getIops());
56 }
57 if (ebs.getVolumeSize() != null) {
58 builder.withVolumeSizeInGigabytes(ebs.getVolumeSize());
59 }
60 return builder.build();
61 }
62
63 public static Builder builder(String snapshotId) {
64 return new Builder(snapshotId);
65 }
66
67 public static class Builder implements org.apache.commons.lang3.builder.Builder<ImmutableEbsBlockDevice> {
68
69 private static final int IOPS_MIN = 100;
70 private static final int IOPS_MIN_VOLUME_SIZE = 10;
71 private static final int IOPS_MAX = 4000;
72
73 private final String snapshotId;
74 private VolumeType volumeType = VolumeType.Standard;
75 private boolean deleteOnTermination = true;
76 private Optional<Integer> iops = absent();
77 private Optional<Integer> volumeSizeInGigabytes = absent();
78
79 public Builder(String snapshotId) {
80 this.snapshotId = snapshotId;
81 }
82
83 public Builder withDeleteOnTermination(boolean deleteOnTermination) {
84 this.deleteOnTermination = deleteOnTermination;
85 return this;
86 }
87
88 public Builder withVolumeType(VolumeType volumeType) {
89 this.volumeType = volumeType;
90 return this;
91 }
92
93 public Builder withIops(Optional<Integer> iops) {
94 this.iops = iops;
95 return this;
96 }
97
98 public Builder withIops(int iops) {
99 return withIops(Optional.of(iops));
100 }
101
102 public Builder withVolumeSizeInGigabytes(Optional<Integer> volumeSizeInGigabytes) {
103 this.volumeSizeInGigabytes = volumeSizeInGigabytes;
104 return this;
105 }
106
107 public Builder withVolumeSizeInGigabytes(int volumeSizeInGigabytes) {
108 return withVolumeSizeInGigabytes(volumeSizeInGigabytes);
109 }
110
111 @Override
112 public ImmutableEbsBlockDevice build() {
113 return validate(new ImmutableEbsBlockDevice(this));
114 }
115
116 private static ImmutableEbsBlockDevice validate(ImmutableEbsBlockDevice instance) {
117 checkNotBlank(instance.getSnapshotId(), "snapshotId");
118 checkNotBlank(instance.getVolumeType(), "volumeType");
119 VolumeType vt = VolumeType.fromValue(instance.getVolumeType());
120 if (vt.equals(VolumeType.Io1)) {
121 Optional<Integer> iops = fromNullable(instance.getIops());
122 Optional<Integer> volumeSize = fromNullable(instance.getVolumeSize());
123 checkArgument(iops.isPresent(), "iops is required for volume type %s", vt.toString());
124 checkArgument(volumeSize.isPresent(), "volume size is required for volume type %s", vt.toString());
125 checkMin(iops, IOPS_MIN, "iops");
126 checkMax(iops.get(), IOPS_MAX, "iops");
127 checkMin(volumeSize, IOPS_MIN_VOLUME_SIZE, "volumeSize");
128 }
129 return instance;
130 }
131
132 }
133
134 @Override
135 public void setSnapshotId(String snapshotId) {
136 throw new UnsupportedOperationException(UOE_MSG);
137 }
138
139 @Override
140 public EbsBlockDevice withSnapshotId(String snapshotId) {
141 throw new UnsupportedOperationException(UOE_MSG);
142 }
143
144 @Override
145 public void setVolumeSize(Integer volumeSize) {
146 throw new UnsupportedOperationException(UOE_MSG);
147 }
148
149 @Override
150 public EbsBlockDevice withVolumeSize(Integer volumeSize) {
151 throw new UnsupportedOperationException(UOE_MSG);
152 }
153
154 @Override
155 public void setDeleteOnTermination(Boolean deleteOnTermination) {
156 throw new UnsupportedOperationException(UOE_MSG);
157 }
158
159 @Override
160 public void setEncrypted(Boolean encrypted) {
161 throw new UnsupportedOperationException(UOE_MSG);
162 }
163
164 @Override
165 public EbsBlockDevice withEncrypted(Boolean encrypted) {
166 throw new UnsupportedOperationException(UOE_MSG);
167 }
168
169 @Override
170 public EbsBlockDevice withDeleteOnTermination(Boolean deleteOnTermination) {
171 throw new UnsupportedOperationException(UOE_MSG);
172 }
173
174 @Override
175 public void setVolumeType(String volumeType) {
176 throw new UnsupportedOperationException(UOE_MSG);
177 }
178
179 @Override
180 public EbsBlockDevice withVolumeType(String volumeType) {
181 throw new UnsupportedOperationException(UOE_MSG);
182 }
183
184 @Override
185 public void setVolumeType(VolumeType volumeType) {
186 throw new UnsupportedOperationException(UOE_MSG);
187 }
188
189 @Override
190 public EbsBlockDevice withVolumeType(VolumeType volumeType) {
191 throw new UnsupportedOperationException(UOE_MSG);
192 }
193
194 @Override
195 public void setIops(Integer iops) {
196 throw new UnsupportedOperationException(UOE_MSG);
197 }
198
199 @Override
200 public EbsBlockDevice withIops(Integer iops) {
201 throw new UnsupportedOperationException(UOE_MSG);
202 }
203 }