| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.ec2.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import javax.annotation.Resource; |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Named; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.ec2.EC2Client; |
| 29 | import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules; |
| 30 | import org.jclouds.ec2.domain.IpProtocol; |
| 31 | import org.jclouds.ec2.domain.UserIdGroupPair; |
| 32 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 33 | import org.jclouds.logging.Logger; |
| 34 | |
| 35 | import com.google.common.base.Function; |
| 36 | import com.google.common.collect.Iterables; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class CreateSecurityGroupIfNeeded implements Function<RegionNameAndIngressRules, String> { |
| 44 | @Resource |
| 45 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 46 | protected Logger logger = Logger.NULL; |
| 47 | protected final EC2Client ec2Client; |
| 48 | |
| 49 | @Inject |
| 50 | public CreateSecurityGroupIfNeeded(EC2Client ec2Client) { |
| 51 | this.ec2Client = ec2Client; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public String apply(RegionNameAndIngressRules from) { |
| 56 | createSecurityGroupInRegion(from.getRegion(), from.getName(), from.getPorts()); |
| 57 | return from.getName(); |
| 58 | } |
| 59 | |
| 60 | private void createSecurityGroupInRegion(String region, String name, int... ports) { |
| 61 | checkNotNull(region, "region"); |
| 62 | checkNotNull(name, "name"); |
| 63 | logger.debug(">> creating securityGroup region(%s) name(%s)", region, name); |
| 64 | try { |
| 65 | ec2Client.getSecurityGroupServices().createSecurityGroupInRegion(region, name, name); |
| 66 | logger.debug("<< created securityGroup(%s)", name); |
| 67 | for (int port : ports) { |
| 68 | createIngressRuleForTCPPort(region, name, port); |
| 69 | } |
| 70 | if (ports.length > 0) { |
| 71 | authorizeGroupToItself(region, name); |
| 72 | } |
| 73 | } catch (IllegalStateException e) { |
| 74 | logger.debug("<< reused securityGroup(%s)", name); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private void createIngressRuleForTCPPort(String region, String name, int port) { |
| 79 | logger.debug(">> authorizing securityGroup region(%s) name(%s) port(%s)", region, name, port); |
| 80 | ec2Client.getSecurityGroupServices().authorizeSecurityGroupIngressInRegion(region, name, IpProtocol.TCP, port, |
| 81 | port, "0.0.0.0/0"); |
| 82 | logger.debug("<< authorized securityGroup(%s)", name); |
| 83 | } |
| 84 | |
| 85 | private void authorizeGroupToItself(String region, String name) { |
| 86 | logger.debug(">> authorizing securityGroup region(%s) name(%s) permission to itself", region, name); |
| 87 | String myOwnerId = Iterables.get(ec2Client.getSecurityGroupServices().describeSecurityGroupsInRegion(region), 0) |
| 88 | .getOwnerId(); |
| 89 | ec2Client.getSecurityGroupServices().authorizeSecurityGroupIngressInRegion(region, name, |
| 90 | new UserIdGroupPair(myOwnerId, name)); |
| 91 | logger.debug("<< authorized securityGroup(%s)", name); |
| 92 | } |
| 93 | |
| 94 | } |