| 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.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | import org.jclouds.javax.annotation.Nullable; |
| 25 | import org.jclouds.util.Preconditions2; |
| 26 | |
| 27 | /** |
| 28 | * |
| 29 | * @author Lili Nadar |
| 30 | */ |
| 31 | public class BlockDeviceMapping implements Comparable<BlockDeviceMapping>{ |
| 32 | public static Builder builder() { |
| 33 | return new Builder(); |
| 34 | } |
| 35 | |
| 36 | public static class Builder { |
| 37 | private String deviceName; |
| 38 | private String virtualName; |
| 39 | private String snapshotId; |
| 40 | private Integer sizeInGib; |
| 41 | private Boolean noDevice; |
| 42 | private Boolean deleteOnTermination; |
| 43 | |
| 44 | public Builder deviceName(String deviceName) { |
| 45 | this.deviceName = deviceName; |
| 46 | return this; |
| 47 | } |
| 48 | |
| 49 | public Builder virtualName(String virtualName) { |
| 50 | this.virtualName = virtualName; |
| 51 | return this; |
| 52 | } |
| 53 | |
| 54 | public Builder snapshotId(String snapshotId) { |
| 55 | this.snapshotId = snapshotId; |
| 56 | return this; |
| 57 | } |
| 58 | |
| 59 | public Builder sizeInGib(Integer sizeInGib) { |
| 60 | this.sizeInGib = sizeInGib; |
| 61 | return this; |
| 62 | } |
| 63 | |
| 64 | public Builder noDevice(Boolean noDevice) { |
| 65 | this.noDevice = noDevice; |
| 66 | return this; |
| 67 | } |
| 68 | |
| 69 | public Builder deleteOnTermination(Boolean deleteOnTermination) { |
| 70 | this.deleteOnTermination = deleteOnTermination; |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | public BlockDeviceMapping build() { |
| 75 | return new BlockDeviceMapping(deviceName, virtualName, snapshotId, sizeInGib, noDevice, deleteOnTermination); |
| 76 | } |
| 77 | |
| 78 | public Builder clear() { |
| 79 | this.deviceName = null; |
| 80 | this.virtualName = null; |
| 81 | this.snapshotId = null; |
| 82 | this.sizeInGib = null; |
| 83 | this.noDevice = null; |
| 84 | this.deleteOnTermination = null; |
| 85 | return this; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private final String deviceName; |
| 90 | private final String virtualName; |
| 91 | private final String snapshotId; |
| 92 | private final Integer sizeInGib; |
| 93 | private final Boolean noDevice; |
| 94 | private final Boolean deleteOnTermination; |
| 95 | |
| 96 | // values expressed in GB |
| 97 | private static final Integer VOLUME_SIZE_MIN_VALUE = 1; |
| 98 | private static final Integer VOLUME_SIZE_MAX_VALUE = 1000; |
| 99 | |
| 100 | BlockDeviceMapping(String deviceName, @Nullable String virtualName, @Nullable String snapshotId, |
| 101 | @Nullable Integer sizeInGib, @Nullable Boolean noDevice, @Nullable Boolean deleteOnTermination) { |
| 102 | |
| 103 | checkNotNull(deviceName, "deviceName cannot be null"); |
| 104 | Preconditions2.checkNotEmpty(deviceName, "the deviceName must be non-empty"); |
| 105 | |
| 106 | if (sizeInGib != null) { |
| 107 | checkArgument((sizeInGib >= VOLUME_SIZE_MIN_VALUE && sizeInGib <= VOLUME_SIZE_MAX_VALUE), |
| 108 | String.format("Size in Gib must be between %s and %s GB", VOLUME_SIZE_MIN_VALUE, VOLUME_SIZE_MAX_VALUE)); |
| 109 | } |
| 110 | this.deviceName = deviceName; |
| 111 | this.virtualName = virtualName; |
| 112 | this.snapshotId = snapshotId; |
| 113 | this.sizeInGib = sizeInGib; |
| 114 | this.noDevice = noDevice; |
| 115 | this.deleteOnTermination = deleteOnTermination; |
| 116 | } |
| 117 | |
| 118 | public String getDeviceName() { |
| 119 | return deviceName; |
| 120 | } |
| 121 | |
| 122 | public String getVirtualName() { |
| 123 | return virtualName; |
| 124 | } |
| 125 | |
| 126 | public String getEbsSnapshotId() { |
| 127 | return snapshotId; |
| 128 | } |
| 129 | |
| 130 | public Integer getEbsVolumeSize() { |
| 131 | return sizeInGib; |
| 132 | } |
| 133 | |
| 134 | public Boolean getEbsNoDevice() { |
| 135 | return noDevice; |
| 136 | } |
| 137 | |
| 138 | public Boolean getEbsDeleteOnTermination() { |
| 139 | return deleteOnTermination; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int hashCode() { |
| 144 | final int prime = 31; |
| 145 | int result = 1; |
| 146 | result = prime * result + ((deleteOnTermination == null) ? 0 : deleteOnTermination.hashCode()); |
| 147 | result = prime * result + ((deviceName == null) ? 0 : deviceName.hashCode()); |
| 148 | result = prime * result + ((noDevice == null) ? 0 : noDevice.hashCode()); |
| 149 | result = prime * result + ((sizeInGib == null) ? 0 : sizeInGib.hashCode()); |
| 150 | result = prime * result + ((snapshotId == null) ? 0 : snapshotId.hashCode()); |
| 151 | result = prime * result + ((virtualName == null) ? 0 : virtualName.hashCode()); |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public boolean equals(Object obj) { |
| 157 | if (this == obj) |
| 158 | return true; |
| 159 | if (obj == null) |
| 160 | return false; |
| 161 | if (getClass() != obj.getClass()) |
| 162 | return false; |
| 163 | BlockDeviceMapping other = (BlockDeviceMapping) obj; |
| 164 | if (deleteOnTermination == null) { |
| 165 | if (other.deleteOnTermination != null) |
| 166 | return false; |
| 167 | } else if (!deleteOnTermination.equals(other.deleteOnTermination)) |
| 168 | return false; |
| 169 | if (deviceName == null) { |
| 170 | if (other.deviceName != null) |
| 171 | return false; |
| 172 | } else if (!deviceName.equals(other.deviceName)) |
| 173 | return false; |
| 174 | if (noDevice == null) { |
| 175 | if (other.noDevice != null) |
| 176 | return false; |
| 177 | } else if (!noDevice.equals(other.noDevice)) |
| 178 | return false; |
| 179 | if (sizeInGib == null) { |
| 180 | if (other.sizeInGib != null) |
| 181 | return false; |
| 182 | } else if (!sizeInGib.equals(other.sizeInGib)) |
| 183 | return false; |
| 184 | if (snapshotId == null) { |
| 185 | if (other.snapshotId != null) |
| 186 | return false; |
| 187 | } else if (!snapshotId.equals(other.snapshotId)) |
| 188 | return false; |
| 189 | if (virtualName == null) { |
| 190 | if (other.virtualName != null) |
| 191 | return false; |
| 192 | } else if (!virtualName.equals(other.virtualName)) |
| 193 | return false; |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public String toString() { |
| 199 | return "[deviceName=" + deviceName + ", virtualName=" + virtualName + ", snapshotId=" + snapshotId |
| 200 | + ", sizeInGib=" + sizeInGib + ", noDevice=" + noDevice + ", deleteOnTermination=" + deleteOnTermination |
| 201 | + "]"; |
| 202 | } |
| 203 | |
| 204 | public static class MapEBSSnapshotToDevice extends BlockDeviceMapping { |
| 205 | public MapEBSSnapshotToDevice(String deviceName, String snapshotId, @Nullable Integer sizeInGib, |
| 206 | @Nullable Boolean deleteOnTermination) { |
| 207 | super(deviceName, null, snapshotId, sizeInGib, null, deleteOnTermination); |
| 208 | checkNotNull(snapshotId, "snapshotId cannot be null"); |
| 209 | Preconditions2.checkNotEmpty(snapshotId, "the snapshotId must be non-empty"); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | public static class MapNewVolumeToDevice extends BlockDeviceMapping { |
| 214 | public MapNewVolumeToDevice(String deviceName, Integer sizeInGib, @Nullable Boolean deleteOnTermination) { |
| 215 | super(deviceName, null, null, sizeInGib, null, deleteOnTermination); |
| 216 | checkNotNull(sizeInGib, "sizeInGib cannot be null"); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | public static class MapEphemeralDeviceToDevice extends BlockDeviceMapping { |
| 221 | public MapEphemeralDeviceToDevice(String deviceName, String virtualName) { |
| 222 | super(deviceName, virtualName, null, null, null, null); |
| 223 | checkNotNull(virtualName, "virtualName cannot be null"); |
| 224 | Preconditions2.checkNotEmpty(virtualName, "the virtualName must be non-empty"); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | public static class UnmapDeviceNamed extends BlockDeviceMapping { |
| 229 | public UnmapDeviceNamed(String deviceName) { |
| 230 | super(deviceName, null, null, null, true, null); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public int compareTo(BlockDeviceMapping arg0) { |
| 236 | return deviceName.compareTo(arg0.deviceName); |
| 237 | } |
| 238 | } |