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

COVERAGE SUMMARY FOR SOURCE FILE [EC2DestroyNodeStrategy.java]

nameclass, %method, %block, %line, %
EC2DestroyNodeStrategy.java100% (1/1)100% (4/4)100% (158/158)100% (31/31)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class EC2DestroyNodeStrategy100% (1/1)100% (4/4)100% (158/158)100% (31/31)
EC2DestroyNodeStrategy (EC2Client, GetNodeMetadataStrategy, LoadingCache): void 100% (1/1)100% (27/27)100% (7/7)
destroyInstanceInRegion (String, String): void 100% (1/1)100% (13/13)100% (2/2)
destroyNode (String): NodeMetadata 100% (1/1)100% (24/24)100% (6/6)
releaseAnyPublicIpForInstanceInRegion (String, String): void 100% (1/1)100% (94/94)100% (16/16)

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.strategy;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.util.concurrent.ExecutionException;
24 
25import javax.annotation.Resource;
26import javax.inject.Inject;
27import javax.inject.Named;
28import javax.inject.Singleton;
29 
30import org.jclouds.aws.util.AWSUtils;
31import org.jclouds.compute.domain.NodeMetadata;
32import org.jclouds.compute.reference.ComputeServiceConstants;
33import org.jclouds.compute.strategy.DestroyNodeStrategy;
34import org.jclouds.compute.strategy.GetNodeMetadataStrategy;
35import org.jclouds.ec2.EC2Client;
36import org.jclouds.ec2.compute.domain.RegionAndName;
37import org.jclouds.ec2.reference.EC2Constants;
38import org.jclouds.logging.Logger;
39 
40import com.google.common.annotations.VisibleForTesting;
41import com.google.common.cache.CacheLoader;
42import com.google.common.cache.LoadingCache;
43 
44/**
45 * 
46 * @author Adrian Cole
47 */
48@Singleton
49public class EC2DestroyNodeStrategy implements DestroyNodeStrategy {
50   @Resource
51   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
52   protected Logger logger = Logger.NULL;
53   protected final EC2Client client;
54   protected final GetNodeMetadataStrategy getNode;
55   protected final LoadingCache<RegionAndName, String> elasticIpCache;
56 
57   @Inject
58   @Named(EC2Constants.PROPERTY_EC2_AUTO_ALLOCATE_ELASTIC_IPS)
59   @VisibleForTesting
60   boolean autoAllocateElasticIps = false;
61 
62   @Inject
63   protected EC2DestroyNodeStrategy(EC2Client client, GetNodeMetadataStrategy getNode,
64            @Named("ELASTICIP") LoadingCache<RegionAndName, String> elasticIpCache) {
65      this.client = checkNotNull(client, "client");
66      this.getNode = checkNotNull(getNode, "getNode");
67      this.elasticIpCache = checkNotNull(elasticIpCache, "elasticIpCache");
68   }
69 
70   @Override
71   public NodeMetadata destroyNode(String id) {
72      String[] parts = AWSUtils.parseHandle(id);
73      String region = parts[0];
74      String instanceId = parts[1];
75 
76      // TODO: can there be multiple?
77      releaseAnyPublicIpForInstanceInRegion(instanceId, region);
78      destroyInstanceInRegion(instanceId, region);
79      return getNode.getNode(id);
80   }
81 
82   protected void releaseAnyPublicIpForInstanceInRegion(String instanceId, String region) {
83      if (!autoAllocateElasticIps)
84         return;
85      try {
86         String ip = elasticIpCache.get(new RegionAndName(region, instanceId));
87         logger.debug(">> disassociating elastic IP %s", ip);
88         client.getElasticIPAddressServices().disassociateAddressInRegion(region, ip);
89         logger.trace("<< disassociated elastic IP %s", ip);
90         elasticIpCache.invalidate(new RegionAndName(region, instanceId));
91         logger.debug(">> releasing elastic IP %s", ip);
92         client.getElasticIPAddressServices().releaseAddressInRegion(region, ip);
93         logger.trace("<< released elastic IP %s", ip);
94      } catch (CacheLoader.InvalidCacheLoadException e) {
95         // no ip was found
96         return;
97      } catch (ExecutionException e) {
98         // don't propagate as we need to clean up the node regardless
99         logger.warn(e, "error cleaning up elastic ip for instance %s/%s", region, instanceId);
100      }
101 
102   }
103 
104   protected void destroyInstanceInRegion(String instanceId, String region) {
105      client.getInstanceServices().terminateInstancesInRegion(region, instanceId);
106   }
107}

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