| 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.binders; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.Map.Entry; |
| 26 | |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.aws.ec2.domain.LaunchSpecification; |
| 30 | import org.jclouds.aws.ec2.options.AWSRunInstancesOptions; |
| 31 | import org.jclouds.http.HttpRequest; |
| 32 | import org.jclouds.http.utils.ModifyRequest; |
| 33 | import org.jclouds.rest.Binder; |
| 34 | |
| 35 | import com.google.common.base.Function; |
| 36 | import com.google.common.collect.ImmutableMap; |
| 37 | import com.google.common.collect.ImmutableMap.Builder; |
| 38 | import com.google.common.collect.Multimaps; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @author Adrian Cole |
| 43 | */ |
| 44 | @Singleton |
| 45 | public class BindLaunchSpecificationToFormParams implements Binder, Function<LaunchSpecification, Map<String, String>> { |
| 46 | |
| 47 | @Override |
| 48 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 49 | checkArgument(input instanceof LaunchSpecification, "this binder is only valid for LaunchSpecifications!"); |
| 50 | LaunchSpecification launchSpec = LaunchSpecification.class.cast(input); |
| 51 | return ModifyRequest.putFormParams(request, Multimaps.forMap(apply(launchSpec))); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public Map<String, String> apply(LaunchSpecification launchSpec) { |
| 56 | Builder<String, String> builder = ImmutableMap.<String, String> builder(); |
| 57 | builder.put("LaunchSpecification.ImageId", checkNotNull(launchSpec.getImageId(), "imageId")); |
| 58 | if (launchSpec.getAvailabilityZone() != null) |
| 59 | builder.put("LaunchSpecification.Placement.AvailabilityZone", launchSpec.getAvailabilityZone()); |
| 60 | |
| 61 | AWSRunInstancesOptions options = new AWSRunInstancesOptions(); |
| 62 | if (launchSpec.getBlockDeviceMappings().size() > 0) |
| 63 | options.withBlockDeviceMappings(launchSpec.getBlockDeviceMappings()); |
| 64 | if (launchSpec.getSecurityGroupNames().size() > 0) |
| 65 | options.withSecurityGroups(launchSpec.getSecurityGroupNames()); |
| 66 | if (launchSpec.getSecurityGroupIds().size() > 0) |
| 67 | options.withSecurityGroupIds(launchSpec.getSecurityGroupIds()); |
| 68 | options.asType(checkNotNull(launchSpec.getInstanceType(), "instanceType")); |
| 69 | if (launchSpec.getKernelId() != null) |
| 70 | options.withKernelId(launchSpec.getKernelId()); |
| 71 | if (launchSpec.getKeyName() != null) |
| 72 | options.withKeyName(launchSpec.getKeyName()); |
| 73 | if (launchSpec.getRamdiskId() != null) |
| 74 | options.withRamdisk(launchSpec.getRamdiskId()); |
| 75 | if (Boolean.TRUE.equals(launchSpec.isMonitoringEnabled())) |
| 76 | options.enableMonitoring(); |
| 77 | if (launchSpec.getUserData() != null) |
| 78 | options.withUserData(launchSpec.getUserData()); |
| 79 | |
| 80 | for (Entry<String, String> entry : options.buildFormParameters().entries()) { |
| 81 | builder.put("LaunchSpecification." + entry.getKey(), entry.getValue()); |
| 82 | } |
| 83 | return builder.build(); |
| 84 | } |
| 85 | } |