| 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.compute.domain; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Predicates.not; |
| 23 | import static org.jclouds.compute.predicates.ImagePredicates.any; |
| 24 | import static org.jclouds.compute.predicates.ImagePredicates.idIn; |
| 25 | |
| 26 | import java.net.URI; |
| 27 | import java.util.List; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import org.jclouds.compute.domain.Hardware; |
| 31 | import org.jclouds.compute.domain.HardwareBuilder; |
| 32 | import org.jclouds.compute.domain.Image; |
| 33 | import org.jclouds.compute.domain.OsFamily; |
| 34 | import org.jclouds.compute.domain.Processor; |
| 35 | import org.jclouds.compute.domain.Volume; |
| 36 | import org.jclouds.compute.domain.internal.VolumeImpl; |
| 37 | import org.jclouds.compute.predicates.ImagePredicates; |
| 38 | import org.jclouds.domain.Location; |
| 39 | import org.jclouds.ec2.domain.InstanceType; |
| 40 | import org.jclouds.ec2.domain.RootDeviceType; |
| 41 | import org.jclouds.ec2.domain.VirtualizationType; |
| 42 | |
| 43 | import com.google.common.base.Predicate; |
| 44 | import com.google.common.base.Predicates; |
| 45 | import com.google.common.collect.ImmutableList; |
| 46 | |
| 47 | /** |
| 48 | * |
| 49 | * @author Adrian Cole |
| 50 | * @see <a |
| 51 | * href="http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html" |
| 52 | * /> |
| 53 | */ |
| 54 | public class EC2HardwareBuilder extends HardwareBuilder { |
| 55 | private Predicate<Image> rootDeviceType = any(); |
| 56 | private Predicate<Image> virtualizationType = Predicates.or(new IsWindows(), new RequiresVirtualizationType( |
| 57 | VirtualizationType.PARAVIRTUAL)); |
| 58 | private Predicate<Image> imageIds = any(); |
| 59 | private Predicate<Image> is64Bit = any(); |
| 60 | |
| 61 | public EC2HardwareBuilder() { |
| 62 | this.supportsImage = null; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * evaluates true if the Image has the following rootDeviceType |
| 67 | * |
| 68 | * @param type |
| 69 | * rootDeviceType of the image |
| 70 | * @return predicate |
| 71 | */ |
| 72 | public static class RequiresRootDeviceType implements Predicate<Image> { |
| 73 | final RootDeviceType type; |
| 74 | |
| 75 | public RequiresRootDeviceType(final RootDeviceType type) { |
| 76 | this.type = checkNotNull(type, "type must be defined"); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean apply(Image image) { |
| 81 | return image.getUserMetadata().containsKey("rootDeviceType") |
| 82 | && type == RootDeviceType.fromValue(image.getUserMetadata().get("rootDeviceType")); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| 87 | return "requiresRootDeviceType(" + type + ")"; |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | public static class IsWindows implements Predicate<Image> { |
| 93 | |
| 94 | @Override |
| 95 | public boolean apply(Image image) { |
| 96 | return image.getOperatingSystem() != null && OsFamily.WINDOWS == image.getOperatingSystem().getFamily(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String toString() { |
| 101 | return "isWindows()"; |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * evaluates true if the Image requires the following virtualizationType |
| 108 | * |
| 109 | * @param type |
| 110 | * virtualizationType of the image |
| 111 | * @return predicate |
| 112 | */ |
| 113 | public static class RequiresVirtualizationType implements Predicate<Image> { |
| 114 | final VirtualizationType type; |
| 115 | |
| 116 | public RequiresVirtualizationType(final VirtualizationType type) { |
| 117 | this.type = checkNotNull(type, "type must be defined"); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean apply(Image image) { |
| 122 | return image.getOperatingSystem() != null && image.getOperatingSystem().getArch() != null |
| 123 | && type == VirtualizationType.fromValue(image.getOperatingSystem().getArch()); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public String toString() { |
| 128 | return "requiresVirtualizationType(" + type + ")"; |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | |
| 133 | public EC2HardwareBuilder(String instanceType) { |
| 134 | ids(instanceType); |
| 135 | } |
| 136 | |
| 137 | public EC2HardwareBuilder virtualizationType(VirtualizationType virtualizationType) { |
| 138 | this.virtualizationType = new RequiresVirtualizationType(virtualizationType); |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | public EC2HardwareBuilder rootDeviceType(RootDeviceType rootDeviceType) { |
| 143 | this.rootDeviceType = new RequiresRootDeviceType(rootDeviceType); |
| 144 | return this; |
| 145 | } |
| 146 | |
| 147 | public EC2HardwareBuilder supportsImageIds(Iterable<String> ids) { |
| 148 | this.imageIds = idIn(ids); |
| 149 | return this; |
| 150 | } |
| 151 | |
| 152 | public EC2HardwareBuilder ids(String id) { |
| 153 | return EC2HardwareBuilder.class.cast(super.ids(id)); |
| 154 | } |
| 155 | |
| 156 | public EC2HardwareBuilder ram(int ram) { |
| 157 | return EC2HardwareBuilder.class.cast(super.ram(ram)); |
| 158 | } |
| 159 | |
| 160 | public EC2HardwareBuilder processors(List<Processor> processors) { |
| 161 | return EC2HardwareBuilder.class.cast(super.processors(processors)); |
| 162 | } |
| 163 | |
| 164 | public EC2HardwareBuilder volumes(List<Volume> volumes) { |
| 165 | return EC2HardwareBuilder.class.cast(super.volumes(volumes)); |
| 166 | } |
| 167 | |
| 168 | public EC2HardwareBuilder supportsImage(Predicate<Image> supportsImage) { |
| 169 | return EC2HardwareBuilder.class.cast(super.supportsImage(supportsImage)); |
| 170 | } |
| 171 | |
| 172 | public EC2HardwareBuilder is64Bit(boolean is64Bit) { |
| 173 | this.is64Bit = is64Bit ? ImagePredicates.is64Bit() : not(ImagePredicates.is64Bit()); |
| 174 | return this; |
| 175 | } |
| 176 | |
| 177 | public EC2HardwareBuilder id(String id) { |
| 178 | return EC2HardwareBuilder.class.cast(super.id(id)); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public EC2HardwareBuilder providerId(String providerId) { |
| 183 | return EC2HardwareBuilder.class.cast(super.providerId(providerId)); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public EC2HardwareBuilder name(String name) { |
| 188 | return EC2HardwareBuilder.class.cast(super.name(name)); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public EC2HardwareBuilder location(Location location) { |
| 193 | return EC2HardwareBuilder.class.cast(super.location(location)); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public EC2HardwareBuilder uri(URI uri) { |
| 198 | return EC2HardwareBuilder.class.cast(super.uri(uri)); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public EC2HardwareBuilder userMetadata(Map<String, String> userMetadata) { |
| 203 | return EC2HardwareBuilder.class.cast(super.userMetadata(userMetadata)); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @see InstanceType#M1_SMALL |
| 208 | */ |
| 209 | public static EC2HardwareBuilder m1_small32() { |
| 210 | return new EC2HardwareBuilder(InstanceType.M1_SMALL).ram(1740).processors( |
| 211 | ImmutableList.of(new Processor(1.0, 1.0))).volumes( |
| 212 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(150.0f, |
| 213 | "/dev/sda2", false, false))).is64Bit(false); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @see InstanceType#M1_SMALL |
| 218 | */ |
| 219 | public static EC2HardwareBuilder m1_small() { |
| 220 | return new EC2HardwareBuilder(InstanceType.M1_SMALL).ram(1740).processors( |
| 221 | ImmutableList.of(new Processor(1.0, 1.0))).volumes( |
| 222 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(150.0f, |
| 223 | "/dev/sda2", false, false))); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @see InstanceType#T1_MICRO |
| 228 | */ |
| 229 | public static EC2HardwareBuilder t1_micro() { |
| 230 | return new EC2HardwareBuilder(InstanceType.T1_MICRO).ram(630).processors( |
| 231 | ImmutableList.of(new Processor(1.0, 1.0))).rootDeviceType(RootDeviceType.EBS); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @see InstanceType#M1_LARGE |
| 236 | */ |
| 237 | public static EC2HardwareBuilder m1_large() { |
| 238 | return new EC2HardwareBuilder(InstanceType.M1_LARGE).ram(7680).processors( |
| 239 | ImmutableList.of(new Processor(2.0, 2.0))).volumes( |
| 240 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(420.0f, |
| 241 | "/dev/sdb", false, false), new VolumeImpl(420.0f, "/dev/sdc", false, false))).is64Bit(true); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @see InstanceType#M1_XLARGE |
| 246 | */ |
| 247 | public static EC2HardwareBuilder m1_xlarge() { |
| 248 | return new EC2HardwareBuilder(InstanceType.M1_XLARGE).ram(15360).processors( |
| 249 | ImmutableList.of(new Processor(4.0, 2.0))).volumes( |
| 250 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(420.0f, |
| 251 | "/dev/sdb", false, false), new VolumeImpl(420.0f, "/dev/sdc", false, false), new VolumeImpl( |
| 252 | 420.0f, "/dev/sdd", false, false), new VolumeImpl(420.0f, "/dev/sde", false, false))).is64Bit( |
| 253 | true); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @see InstanceType#M2_XLARGE |
| 258 | */ |
| 259 | public static EC2HardwareBuilder m2_xlarge() { |
| 260 | return new EC2HardwareBuilder(InstanceType.M2_XLARGE).ram(17510).processors( |
| 261 | ImmutableList.of(new Processor(2.0, 3.25))).volumes( |
| 262 | ImmutableList.<Volume> of(new VolumeImpl(420.0f, "/dev/sda1", true, false))).is64Bit(true); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @see InstanceType#M2_2XLARGE |
| 267 | */ |
| 268 | public static EC2HardwareBuilder m2_2xlarge() { |
| 269 | return new EC2HardwareBuilder(InstanceType.M2_2XLARGE).ram(35020).processors( |
| 270 | ImmutableList.of(new Processor(4.0, 3.25))).volumes( |
| 271 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(840.0f, |
| 272 | "/dev/sdb", false, false))).is64Bit(true); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @see InstanceType#M2_4XLARGE |
| 277 | */ |
| 278 | public static EC2HardwareBuilder m2_4xlarge() { |
| 279 | return new EC2HardwareBuilder(InstanceType.M2_4XLARGE).ram(70041).processors( |
| 280 | ImmutableList.of(new Processor(8.0, 3.25))).volumes( |
| 281 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(840.0f, |
| 282 | "/dev/sdb", false, false), new VolumeImpl(840.0f, "/dev/sdc", false, false))).is64Bit(true); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @see InstanceType#C1_MEDIUM |
| 287 | */ |
| 288 | public static EC2HardwareBuilder c1_medium() { |
| 289 | return new EC2HardwareBuilder(InstanceType.C1_MEDIUM).ram(1740).processors( |
| 290 | ImmutableList.of(new Processor(2.0, 2.5))).volumes( |
| 291 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(340.0f, |
| 292 | "/dev/sda2", false, false))).is64Bit(false); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * @see InstanceType#C1_XLARGE |
| 297 | */ |
| 298 | public static EC2HardwareBuilder c1_xlarge() { |
| 299 | return new EC2HardwareBuilder(InstanceType.C1_XLARGE).ram(7168).processors( |
| 300 | ImmutableList.of(new Processor(8.0, 2.5))).volumes( |
| 301 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(420.0f, |
| 302 | "/dev/sdb", false, false), new VolumeImpl(420.0f, "/dev/sdc", false, false), new VolumeImpl( |
| 303 | 420.0f, "/dev/sdd", false, false), new VolumeImpl(420.0f, "/dev/sde", false, false))).is64Bit( |
| 304 | true); |
| 305 | } |
| 306 | |
| 307 | public static EC2HardwareBuilder cc1_4xlarge() { |
| 308 | return new EC2HardwareBuilder(InstanceType.CC1_4XLARGE).ram(23 * 1024).processors( |
| 309 | ImmutableList.of(new Processor(4.0, 4.0), new Processor(4.0, 4.0))).volumes( |
| 310 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(840.0f, |
| 311 | "/dev/sdb", false, false), new VolumeImpl(840.0f, "/dev/sdc", false, false))) |
| 312 | .virtualizationType(VirtualizationType.HVM); |
| 313 | } |
| 314 | |
| 315 | @SuppressWarnings("unchecked") |
| 316 | @Override |
| 317 | public Hardware build() { |
| 318 | boolean reset = false; |
| 319 | if (this.supportsImage == null) |
| 320 | reset = true; |
| 321 | try { |
| 322 | supportsImage = Predicates.<Image> and(rootDeviceType, virtualizationType, imageIds, is64Bit); |
| 323 | return super.build(); |
| 324 | } finally { |
| 325 | if (reset) |
| 326 | this.supportsImage = null; |
| 327 | } |
| 328 | |
| 329 | } |
| 330 | |
| 331 | } |