| 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.aws.ec2.options; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.jclouds.aws.ec2.domain.LaunchSpecification; |
| 26 | import org.jclouds.ec2.domain.BlockDeviceMapping; |
| 27 | import org.jclouds.ec2.domain.InstanceType; |
| 28 | import org.jclouds.ec2.options.RunInstancesOptions; |
| 29 | |
| 30 | import com.google.common.collect.ImmutableSet; |
| 31 | |
| 32 | /** |
| 33 | * Contains options supported in the Form API for the RunInstances operation. <h2> |
| 34 | * Usage</h2> The recommended way to instantiate a RunInstancesOptions object is to statically |
| 35 | * import RunInstancesOptions.Builder.* and invoke a static creation method followed by an instance |
| 36 | * mutator (if needed): |
| 37 | * <p/> |
| 38 | * <code> |
| 39 | * import static org.jclouds.aws.ec2.options.RunInstancesOptions.Builder.* |
| 40 | * <p/> |
| 41 | * EC2Client connection = // get connection |
| 42 | * Future<ReservationInfo> instances = connection.runInstances(executableBy("123125").imageIds(1000, 1004)); |
| 43 | * <code> |
| 44 | * |
| 45 | * @author Adrian Cole |
| 46 | * @see <a href= |
| 47 | * "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-form-RunInstances.html" |
| 48 | * /> |
| 49 | */ |
| 50 | public class AWSRunInstancesOptions extends RunInstancesOptions { |
| 51 | private LaunchSpecification.Builder launchSpecificationBuilder = LaunchSpecification.builder(); |
| 52 | public static final AWSRunInstancesOptions NONE = new AWSRunInstancesOptions(); |
| 53 | |
| 54 | /** |
| 55 | * Specifies the name of an existing placement group you want to launch the instance into (for |
| 56 | * cluster compute instances). |
| 57 | * |
| 58 | * @param placementGroup |
| 59 | * name of an existing placement group |
| 60 | */ |
| 61 | public AWSRunInstancesOptions inPlacementGroup(String placementGroup) { |
| 62 | formParameters.put("Placement.GroupName", checkNotNull(placementGroup, "placementGroup")); |
| 63 | return this; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Enables monitoring for the instance. |
| 68 | */ |
| 69 | public AWSRunInstancesOptions enableMonitoring() { |
| 70 | formParameters.put("Monitoring.Enabled", "true"); |
| 71 | launchSpecificationBuilder.monitoringEnabled(true); |
| 72 | return this; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Specifies the subnet ID within which to launch the instance(s) for Amazon Virtual Private |
| 77 | * Cloud. |
| 78 | */ |
| 79 | public AWSRunInstancesOptions withSubnetId(String subnetId) { |
| 80 | formParameters.put("SubnetId", checkNotNull(subnetId, "subnetId")); |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | public AWSRunInstancesOptions withSecurityGroupId(String securityGroup) { |
| 85 | return withSecurityGroupIds(securityGroup); |
| 86 | } |
| 87 | |
| 88 | public AWSRunInstancesOptions withSecurityGroupIds(Iterable<String> securityGroupIds) { |
| 89 | launchSpecificationBuilder.securityGroupIds(securityGroupIds); |
| 90 | indexFormValuesWithPrefix("SecurityGroupId", securityGroupIds); |
| 91 | return this; |
| 92 | } |
| 93 | |
| 94 | public AWSRunInstancesOptions withSecurityGroupIds(String... securityGroupIds) { |
| 95 | return withSecurityGroupIds(ImmutableSet.copyOf(securityGroupIds)); |
| 96 | } |
| 97 | |
| 98 | public static class Builder extends RunInstancesOptions.Builder { |
| 99 | |
| 100 | /** |
| 101 | * @see AWSRunInstancesOptions#withSecurityGroupId(String) |
| 102 | */ |
| 103 | public static AWSRunInstancesOptions withSecurityGroupId(String securityGroup) { |
| 104 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 105 | return options.withSecurityGroupId(securityGroup); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @see AWSRunInstancesOptions#inPlacementGroup(String) |
| 110 | */ |
| 111 | public static AWSRunInstancesOptions inPlacementGroup(String placementGroup) { |
| 112 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 113 | return options.inPlacementGroup(placementGroup); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @see AWSRunInstancesOptions#enableMonitoring() |
| 118 | */ |
| 119 | public static AWSRunInstancesOptions enableMonitoring() { |
| 120 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 121 | return options.enableMonitoring(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @see AWSRunInstancesOptions#withSubnetId(String) |
| 126 | */ |
| 127 | public static AWSRunInstancesOptions withSubnetId(String subnetId) { |
| 128 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 129 | return options.withSubnetId(subnetId); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @see AWSRunInstancesOptions#withKeyName(String) |
| 134 | */ |
| 135 | public static AWSRunInstancesOptions withKeyName(String keyName) { |
| 136 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 137 | return options.withKeyName(keyName); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @see AWSRunInstancesOptions#withSecurityGroup(String) |
| 142 | */ |
| 143 | public static AWSRunInstancesOptions withSecurityGroup(String securityGroup) { |
| 144 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 145 | return options.withSecurityGroup(securityGroup); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @see AWSRunInstancesOptions#withUserData(byte []) |
| 150 | */ |
| 151 | public static AWSRunInstancesOptions withUserData(byte[] unencodedData) { |
| 152 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 153 | return options.withUserData(unencodedData); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @see AWSRunInstancesOptions#asType(InstanceType) |
| 158 | */ |
| 159 | public static AWSRunInstancesOptions asType(String instanceType) { |
| 160 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 161 | return options.asType(instanceType); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @see AWSRunInstancesOptions#withKernelId(String) |
| 166 | */ |
| 167 | public static AWSRunInstancesOptions withKernelId(String kernelId) { |
| 168 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 169 | return options.withKernelId(kernelId); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @see AWSRunInstancesOptions#withRamdisk(String) |
| 174 | */ |
| 175 | public static AWSRunInstancesOptions withRamdisk(String ramdiskId) { |
| 176 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 177 | return options.withRamdisk(ramdiskId); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @see AWSRunInstancesOptions#withBlockDeviceMappings(Set<BlockDeviceMapping> mappings) |
| 182 | */ |
| 183 | public static AWSRunInstancesOptions withBlockDeviceMappings(Set<? extends BlockDeviceMapping> mappings) { |
| 184 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 185 | return options.withBlockDeviceMappings(mappings); |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public AWSRunInstancesOptions withBlockDeviceMappings(Set<? extends BlockDeviceMapping> mappings) { |
| 192 | launchSpecificationBuilder.blockDeviceMappings(mappings); |
| 193 | return AWSRunInstancesOptions.class.cast(super.withBlockDeviceMappings(mappings)); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public AWSRunInstancesOptions withKernelId(String kernelId) { |
| 198 | launchSpecificationBuilder.kernelId(kernelId); |
| 199 | return AWSRunInstancesOptions.class.cast(super.withKernelId(kernelId)); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public AWSRunInstancesOptions withKeyName(String keyName) { |
| 204 | launchSpecificationBuilder.keyName(keyName); |
| 205 | return AWSRunInstancesOptions.class.cast(super.withKeyName(keyName)); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public AWSRunInstancesOptions withRamdisk(String ramDiskId) { |
| 210 | launchSpecificationBuilder.ramdiskId(ramDiskId); |
| 211 | return AWSRunInstancesOptions.class.cast(super.withRamdisk(ramDiskId)); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public AWSRunInstancesOptions withSecurityGroup(String securityGroup) { |
| 216 | launchSpecificationBuilder.securityGroupName(securityGroup); |
| 217 | return AWSRunInstancesOptions.class.cast(super.withSecurityGroup(securityGroup)); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public AWSRunInstancesOptions withSecurityGroups(Iterable<String> securityGroups) { |
| 222 | launchSpecificationBuilder.securityGroupNames(securityGroups); |
| 223 | return AWSRunInstancesOptions.class.cast(super.withSecurityGroups(securityGroups)); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public AWSRunInstancesOptions withSecurityGroups(String... securityGroups) { |
| 228 | launchSpecificationBuilder.securityGroupNames(ImmutableSet.copyOf(securityGroups)); |
| 229 | return AWSRunInstancesOptions.class.cast(super.withSecurityGroups(securityGroups)); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public AWSRunInstancesOptions withUserData(byte[] unencodedData) { |
| 234 | launchSpecificationBuilder.userData(unencodedData); |
| 235 | return AWSRunInstancesOptions.class.cast(super.withUserData(unencodedData)); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public AWSRunInstancesOptions asType(String type) { |
| 240 | launchSpecificationBuilder.instanceType(type); |
| 241 | return AWSRunInstancesOptions.class.cast(super.asType(type)); |
| 242 | } |
| 243 | |
| 244 | public synchronized LaunchSpecification.Builder getLaunchSpecificationBuilder() { |
| 245 | try { |
| 246 | return launchSpecificationBuilder.imageId("fake").build().toBuilder().imageId(null); |
| 247 | } finally { |
| 248 | launchSpecificationBuilder.imageId(null); |
| 249 | } |
| 250 | } |
| 251 | } |