| 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.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Preconditions.checkState; |
| 23 | |
| 24 | import javax.annotation.Resource; |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Named; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.aws.AWSResponseException; |
| 30 | import org.jclouds.aws.ec2.AWSEC2Client; |
| 31 | import org.jclouds.aws.ec2.domain.PlacementGroup; |
| 32 | import org.jclouds.aws.ec2.domain.PlacementGroup.State; |
| 33 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 34 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 35 | import org.jclouds.logging.Logger; |
| 36 | |
| 37 | import com.google.common.base.Predicate; |
| 38 | import com.google.common.cache.CacheLoader; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @author Adrian Cole |
| 43 | */ |
| 44 | @Singleton |
| 45 | public class CreatePlacementGroupIfNeeded extends CacheLoader<RegionAndName, String> { |
| 46 | @Resource |
| 47 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 48 | protected Logger logger = Logger.NULL; |
| 49 | protected final AWSEC2Client ec2Client; |
| 50 | protected final Predicate<PlacementGroup> placementGroupAvailable; |
| 51 | |
| 52 | @Inject |
| 53 | public CreatePlacementGroupIfNeeded(AWSEC2Client ec2Client, |
| 54 | @Named("AVAILABLE") Predicate<PlacementGroup> placementGroupAvailable) { |
| 55 | this.ec2Client = ec2Client; |
| 56 | this.placementGroupAvailable = placementGroupAvailable; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public String load(RegionAndName from) { |
| 61 | createPlacementGroupInRegion(from.getRegion(), from.getName()); |
| 62 | return from.getName(); |
| 63 | } |
| 64 | |
| 65 | private void createPlacementGroupInRegion(String region, String name) { |
| 66 | checkNotNull(region, "region"); |
| 67 | checkNotNull(name, "name"); |
| 68 | logger.debug(">> creating placementGroup region(%s) name(%s)", region, name); |
| 69 | try { |
| 70 | ec2Client.getPlacementGroupServices().createPlacementGroupInRegion(region, name); |
| 71 | logger.debug("<< created placementGroup(%s)", name); |
| 72 | checkState(placementGroupAvailable.apply(new PlacementGroup(region, name, "cluster", State.PENDING)), String |
| 73 | .format("placementGroup region(%s) name(%s) failed to become available", region, name)); |
| 74 | } catch (AWSResponseException e) { |
| 75 | if (e.getError().getCode().equals("InvalidPlacementGroup.Duplicate")) { |
| 76 | logger.debug("<< reused placementGroup(%s)", name); |
| 77 | } else { |
| 78 | throw e; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } |