EMMA Coverage Report (generated Fri Apr 27 15:03:37 EDT 2012)
[all classes][org.jclouds.ec2.compute.loaders]

COVERAGE SUMMARY FOR SOURCE FILE [CreateSecurityGroupIfNeeded.java]

nameclass, %method, %block, %line, %
CreateSecurityGroupIfNeeded.java100% (1/1)100% (6/6)100% (238/238)100% (35/35)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateSecurityGroupIfNeeded100% (1/1)100% (6/6)100% (238/238)100% (35/35)
CreateSecurityGroupIfNeeded (EC2Client, Predicate): void 100% (1/1)100% (9/9)100% (2/2)
CreateSecurityGroupIfNeeded (SecurityGroupClient, Predicate): void 100% (1/1)100% (18/18)100% (5/5)
authorizeGroupToItself (String, String): void 100% (1/1)100% (50/50)100% (5/5)
createIngressRuleForTCPPort (String, String, int): void 100% (1/1)100% (39/39)100% (4/4)
createSecurityGroupInRegion (String, String, int []): void 100% (1/1)100% (106/106)100% (16/16)
load (RegionAndName): String 100% (1/1)100% (16/16)100% (3/3)

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 */
19package org.jclouds.ec2.compute.loaders;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import javax.annotation.Resource;
24import javax.inject.Inject;
25import javax.inject.Named;
26import javax.inject.Singleton;
27 
28import org.jclouds.compute.reference.ComputeServiceConstants;
29import org.jclouds.ec2.EC2Client;
30import org.jclouds.ec2.compute.domain.RegionAndName;
31import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules;
32import org.jclouds.ec2.domain.IpProtocol;
33import org.jclouds.ec2.domain.UserIdGroupPair;
34import org.jclouds.ec2.services.SecurityGroupClient;
35import org.jclouds.logging.Logger;
36 
37import com.google.common.base.Predicate;
38import com.google.common.cache.CacheLoader;
39import com.google.common.collect.Iterables;
40 
41/**
42 * 
43 * @author Adrian Cole
44 */
45@Singleton
46public class CreateSecurityGroupIfNeeded extends CacheLoader<RegionAndName, String> {
47   @Resource
48   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
49   protected Logger logger = Logger.NULL;
50   protected final SecurityGroupClient securityClient;
51   protected final Predicate<RegionAndName> securityGroupEventualConsistencyDelay;
52 
53   @Inject
54   public CreateSecurityGroupIfNeeded(EC2Client ec2Client,
55         @Named("SECURITY") Predicate<RegionAndName> securityGroupEventualConsistencyDelay) {
56      this(checkNotNull(ec2Client, "ec2Client").getSecurityGroupServices(), securityGroupEventualConsistencyDelay);
57   }
58 
59   public CreateSecurityGroupIfNeeded(SecurityGroupClient securityClient,
60         @Named("SECURITY") Predicate<RegionAndName> securityGroupEventualConsistencyDelay) {
61      this.securityClient = checkNotNull(securityClient, "securityClient");
62      this.securityGroupEventualConsistencyDelay = checkNotNull(securityGroupEventualConsistencyDelay,
63            "securityGroupEventualConsistencyDelay");
64   }
65 
66   @Override
67   public String load(RegionAndName from) {
68      RegionNameAndIngressRules realFrom = RegionNameAndIngressRules.class.cast(from);
69      createSecurityGroupInRegion(from.getRegion(), from.getName(), realFrom.getPorts());
70      return from.getName();
71   }
72 
73   private void createSecurityGroupInRegion(String region, String name, int... ports) {
74      checkNotNull(region, "region");
75      checkNotNull(name, "name");
76      logger.debug(">> creating securityGroup region(%s) name(%s)", region, name);
77      try {
78         securityClient.createSecurityGroupInRegion(region, name, name);
79         boolean created = securityGroupEventualConsistencyDelay.apply(new RegionAndName(region, name));
80         if (!created)
81            throw new RuntimeException(String.format("security group %s/%s is not available after creating", region,
82                  name));
83         logger.debug("<< created securityGroup(%s)", name);
84         for (int port : ports) {
85            createIngressRuleForTCPPort(region, name, port);
86         }
87         if (ports.length > 0) {
88            authorizeGroupToItself(region, name);
89         }
90      } catch (IllegalStateException e) {
91         logger.debug("<< reused securityGroup(%s)", name);
92      }
93   }
94 
95   protected void createIngressRuleForTCPPort(String region, String name, int port) {
96      logger.debug(">> authorizing securityGroup region(%s) name(%s) port(%s)", region, name, port);
97      securityClient.authorizeSecurityGroupIngressInRegion(region, name, IpProtocol.TCP, port, port, "0.0.0.0/0");
98      logger.debug("<< authorized securityGroup(%s)", name);
99   }
100 
101   protected void authorizeGroupToItself(String region, String name) {
102      logger.debug(">> authorizing securityGroup region(%s) name(%s) permission to itself", region, name);
103      String myOwnerId = Iterables.get(securityClient.describeSecurityGroupsInRegion(region, name), 0).getOwnerId();
104      securityClient.authorizeSecurityGroupIngressInRegion(region, name, new UserIdGroupPair(myOwnerId, name));
105      logger.debug("<< authorized securityGroup(%s)", name);
106   }
107 
108}

[all classes][org.jclouds.ec2.compute.loaders]
EMMA 2.0.5312 (C) Vladimir Roubtsov