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