EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.ec2.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [CreateSecurityGroupIfNeeded.java]

nameclass, %method, %block, %line, %
CreateSecurityGroupIfNeeded.java100% (1/1)0%   (0/5)0%   (0/188)0%   (0/28)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateSecurityGroupIfNeeded100% (1/1)0%   (0/5)0%   (0/188)0%   (0/28)
CreateSecurityGroupIfNeeded (EC2Client): void 0%   (0/1)0%   (0/9)0%   (0/4)
apply (RegionNameAndIngressRules): String 0%   (0/1)0%   (0/11)0%   (0/2)
authorizeGroupToItself (String, String): void 0%   (0/1)0%   (0/48)0%   (0/5)
createIngressRuleForTCPPort (String, String, int): void 0%   (0/1)0%   (0/40)0%   (0/4)
createSecurityGroupInRegion (String, String, int []): void 0%   (0/1)0%   (0/80)0%   (0/13)

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 */
19package org.jclouds.ec2.compute.functions;
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.ec2.EC2Client;
29import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules;
30import org.jclouds.ec2.domain.IpProtocol;
31import org.jclouds.ec2.domain.UserIdGroupPair;
32import org.jclouds.compute.reference.ComputeServiceConstants;
33import org.jclouds.logging.Logger;
34 
35import com.google.common.base.Function;
36import com.google.common.collect.Iterables;
37 
38/**
39 * 
40 * @author Adrian Cole
41 */
42@Singleton
43public 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}

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