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.ec2.compute.strategy; |
20 | � |
21 | import�static�com.google.common.base.Preconditions.checkNotNull; |
22 | � |
23 | import�java.util.concurrent.ExecutionException; |
24 | � |
25 | import�javax.annotation.Resource; |
26 | import�javax.inject.Inject; |
27 | import�javax.inject.Named; |
28 | import�javax.inject.Singleton; |
29 | � |
30 | import�org.jclouds.aws.util.AWSUtils; |
31 | import�org.jclouds.compute.domain.NodeMetadata; |
32 | import�org.jclouds.compute.reference.ComputeServiceConstants; |
33 | import�org.jclouds.compute.strategy.DestroyNodeStrategy; |
34 | import�org.jclouds.compute.strategy.GetNodeMetadataStrategy; |
35 | import�org.jclouds.ec2.EC2Client; |
36 | import�org.jclouds.ec2.compute.domain.RegionAndName; |
37 | import�org.jclouds.ec2.reference.EC2Constants; |
38 | import�org.jclouds.logging.Logger; |
39 | � |
40 | import�com.google.common.annotations.VisibleForTesting; |
41 | import�com.google.common.cache.CacheLoader; |
42 | import�com.google.common.cache.LoadingCache; |
43 | � |
44 | /** |
45 | �*� |
46 | �*�@author�Adrian�Cole |
47 | �*/ |
48 | @Singleton |
49 | public�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 | } |