| 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.options; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | import org.jclouds.ec2.domain.Image.Architecture; |
| 25 | import org.jclouds.javax.annotation.Nullable; |
| 26 | |
| 27 | /** |
| 28 | * Contains options supported in the Form API for the RegisterImage operation. <h2> |
| 29 | * Usage</h2> The recommended way to instantiate a RegisterImageBackedByEbsOptions object is to statically |
| 30 | * import RegisterImageBackedByEbsOptions.Builder.* and invoke a static creation method followed by an instance |
| 31 | * mutator (if needed): |
| 32 | * <p/> |
| 33 | * <code> |
| 34 | * import static org.jclouds.ec2.options.RegisterImageBackedByEbsOptions.Builder.* |
| 35 | * <p/> |
| 36 | * EC2Client connection = // get connection |
| 37 | * String imageId = connection.getImageServices().registerImageBackedByEbs(...addEphemeralBlockDeviceFromSnapshot("/dev/sda2","virtual-1","snapshot-id")); |
| 38 | * <code> |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | * @see <a |
| 42 | * href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-form-RegisterImage.html" |
| 43 | * /> |
| 44 | */ |
| 45 | public class RegisterImageBackedByEbsOptions extends RegisterImageOptions { |
| 46 | |
| 47 | private int deviceIndex = 1; |
| 48 | |
| 49 | /** |
| 50 | * |
| 51 | * adds a block device to the image from an ebs snapshot. This device is deleted on instance |
| 52 | * termination. |
| 53 | * |
| 54 | * @param name |
| 55 | * The device name (e.g., /dev/sdh). |
| 56 | * @param virtualName |
| 57 | * The virtual device name. (nullable) |
| 58 | * @param snapshotId |
| 59 | * The ID of the snapshot. |
| 60 | */ |
| 61 | public RegisterImageBackedByEbsOptions addEphemeralBlockDeviceFromSnapshot(String deviceName, |
| 62 | @Nullable String virtualName, String snapshotId) { |
| 63 | formParameters.put("BlockDeviceMapping." + deviceIndex + ".DeviceName", checkNotNull( |
| 64 | deviceName, "deviceName")); |
| 65 | if (virtualName != null) |
| 66 | formParameters.put("BlockDeviceMapping." + deviceIndex + ".VirtualName", checkNotNull( |
| 67 | virtualName, "virtualName")); |
| 68 | formParameters.put("BlockDeviceMapping." + deviceIndex + ".Ebs.SnapshotId", checkNotNull( |
| 69 | snapshotId, "snapshotId")); |
| 70 | deviceIndex++; |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * |
| 76 | * adds a new block device to the image. This device is deleted on instance termination. |
| 77 | * |
| 78 | * @param name |
| 79 | * The device name (e.g., /dev/sdh). |
| 80 | * @param virtualName |
| 81 | * The virtual device name. |
| 82 | * @param volumeSize |
| 83 | * The size of the volume, in GiBs. |
| 84 | */ |
| 85 | public RegisterImageBackedByEbsOptions addNewEphemeralBlockDevice(String deviceName, |
| 86 | @Nullable String virtualName, int volumeSize) { |
| 87 | checkArgument(volumeSize > 0 && volumeSize < 1025, "volumeSize must be between 1 and 1024 gb"); |
| 88 | formParameters.put("BlockDeviceMapping." + deviceIndex + ".DeviceName", checkNotNull( |
| 89 | deviceName, "deviceName")); |
| 90 | if (virtualName != null) |
| 91 | formParameters.put("BlockDeviceMapping." + deviceIndex + ".VirtualName", checkNotNull( |
| 92 | virtualName, "virtualName")); |
| 93 | formParameters.put("BlockDeviceMapping." + deviceIndex + ".Ebs.VolumeSize", volumeSize + ""); |
| 94 | deviceIndex++; |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * |
| 100 | * adds a block device to the image from an ebs snapshot. This device is retained on instance |
| 101 | * termination. |
| 102 | * |
| 103 | * @param name |
| 104 | * The device name (e.g., /dev/sdh). |
| 105 | * @param virtualName |
| 106 | * The virtual device name. (nullable) |
| 107 | * @param snapshotId |
| 108 | * The ID of the snapshot. |
| 109 | */ |
| 110 | public RegisterImageBackedByEbsOptions addBlockDeviceFromSnapshot(String deviceName, |
| 111 | @Nullable String virtualName, String snapshotId) { |
| 112 | formParameters.put("BlockDeviceMapping." + deviceIndex + ".Ebs.DeleteOnTermination", "false"); |
| 113 | addEphemeralBlockDeviceFromSnapshot(deviceName, virtualName, snapshotId); |
| 114 | return this; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * |
| 119 | * adds a new block device to the image. This device is retained on instance termination. |
| 120 | * |
| 121 | * @param name |
| 122 | * The device name (e.g., /dev/sdh). |
| 123 | * @param virtualName |
| 124 | * The virtual device name. |
| 125 | * @param volumeSize |
| 126 | * The size of the volume, in GiBs.. |
| 127 | */ |
| 128 | public RegisterImageBackedByEbsOptions addNewBlockDevice(String deviceName, |
| 129 | @Nullable String virtualName, int volumeSize) { |
| 130 | formParameters.put("BlockDeviceMapping." + deviceIndex + ".Ebs.DeleteOnTermination", "false"); |
| 131 | addNewEphemeralBlockDevice(deviceName, virtualName, volumeSize); |
| 132 | return this; |
| 133 | } |
| 134 | |
| 135 | public static class Builder { |
| 136 | /** |
| 137 | * @see RegisterImageBackedByEbsOptions#asArchitecture(Architecture) |
| 138 | */ |
| 139 | public static RegisterImageBackedByEbsOptions asArchitecture(Architecture architecture) { |
| 140 | RegisterImageBackedByEbsOptions options = new RegisterImageBackedByEbsOptions(); |
| 141 | return options.asArchitecture(architecture); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @see RegisterImageBackedByEbsOptions#withDescription(String) |
| 146 | */ |
| 147 | public static RegisterImageBackedByEbsOptions withDescription(String additionalInfo) { |
| 148 | RegisterImageBackedByEbsOptions options = new RegisterImageBackedByEbsOptions(); |
| 149 | return options.withDescription(additionalInfo); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @see RegisterImageBackedByEbsOptions#withKernelId(String) |
| 154 | */ |
| 155 | public static RegisterImageBackedByEbsOptions withKernelId(String kernelId) { |
| 156 | RegisterImageBackedByEbsOptions options = new RegisterImageBackedByEbsOptions(); |
| 157 | return options.withKernelId(kernelId); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @see RegisterImageBackedByEbsOptions#withRamdisk(String) |
| 162 | */ |
| 163 | public static RegisterImageBackedByEbsOptions withRamdisk(String ramdiskId) { |
| 164 | RegisterImageBackedByEbsOptions options = new RegisterImageBackedByEbsOptions(); |
| 165 | return options.withRamdisk(ramdiskId); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @see RegisterImageBackedByEbsOptions#addBlockDeviceFromSnapshot(String, String, String) |
| 170 | */ |
| 171 | public static RegisterImageBackedByEbsOptions addBlockDeviceFromSnapshot(String deviceName, |
| 172 | @Nullable String virtualName, String snapshotId) { |
| 173 | RegisterImageBackedByEbsOptions options = new RegisterImageBackedByEbsOptions(); |
| 174 | return options.addBlockDeviceFromSnapshot(deviceName, virtualName, snapshotId); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @see RegisterImageBackedByEbsOptions#addEphemeralBlockDeviceFromSnapshot(String, String, |
| 179 | * String) |
| 180 | */ |
| 181 | public static RegisterImageBackedByEbsOptions addEphemeralBlockDeviceFromSnapshot( |
| 182 | String deviceName, @Nullable String virtualName, String snapshotId) { |
| 183 | RegisterImageBackedByEbsOptions options = new RegisterImageBackedByEbsOptions(); |
| 184 | return options.addEphemeralBlockDeviceFromSnapshot(deviceName, virtualName, snapshotId); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @see RegisterImageBackedByEbsOptions#addNewBlockDevice(String, String, int) |
| 189 | */ |
| 190 | public static RegisterImageBackedByEbsOptions addNewBlockDevice(String deviceName, |
| 191 | @Nullable String virtualName, int volumeSize) { |
| 192 | RegisterImageBackedByEbsOptions options = new RegisterImageBackedByEbsOptions(); |
| 193 | return options.addNewBlockDevice(deviceName, virtualName, volumeSize); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @see RegisterImageBackedByEbsOptions#addNewEphemeralBlockDevice(String, String, int) |
| 198 | */ |
| 199 | public static RegisterImageBackedByEbsOptions addNewEphemeralBlockDevice(String deviceName, |
| 200 | @Nullable String virtualName, int volumeSize) { |
| 201 | RegisterImageBackedByEbsOptions options = new RegisterImageBackedByEbsOptions(); |
| 202 | return options.addNewEphemeralBlockDevice(deviceName, virtualName, volumeSize); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * {@inheritDoc} |
| 208 | */ |
| 209 | @Override |
| 210 | public RegisterImageBackedByEbsOptions asArchitecture(Architecture architecture) { |
| 211 | return (RegisterImageBackedByEbsOptions) super.asArchitecture(architecture); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * {@inheritDoc} |
| 216 | */ |
| 217 | @Override |
| 218 | public RegisterImageBackedByEbsOptions withDescription(String info) { |
| 219 | return (RegisterImageBackedByEbsOptions) super.withDescription(info); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * {@inheritDoc} |
| 224 | */ |
| 225 | @Override |
| 226 | public RegisterImageBackedByEbsOptions withKernelId(String kernelId) { |
| 227 | return (RegisterImageBackedByEbsOptions) super.withKernelId(kernelId); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * {@inheritDoc} |
| 232 | */ |
| 233 | @Override |
| 234 | public RegisterImageBackedByEbsOptions withRamdisk(String ramDiskId) { |
| 235 | return (RegisterImageBackedByEbsOptions) super.withRamdisk(ramDiskId); |
| 236 | } |
| 237 | } |