View Javadoc

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.vcloud.terremark.compute.strategy;
20  
21  import static com.google.common.base.Predicates.notNull;
22  import static com.google.common.collect.Iterables.all;
23  import static com.google.common.collect.Iterables.filter;
24  import static com.google.common.collect.Iterables.size;
25  import static com.google.common.collect.Iterables.transform;
26  import static org.jclouds.compute.predicates.NodePredicates.TERMINATED;
27  import static org.jclouds.compute.predicates.NodePredicates.parentLocationId;
28  import static org.jclouds.compute.predicates.NodePredicates.inGroup;
29  
30  import java.util.Map;
31  
32  import javax.inject.Inject;
33  import javax.inject.Singleton;
34  
35  import org.jclouds.compute.domain.NodeMetadata;
36  import org.jclouds.compute.strategy.ListNodesStrategy;
37  import org.jclouds.domain.Credentials;
38  import org.jclouds.vcloud.terremark.compute.domain.OrgAndName;
39  
40  import com.google.common.base.Function;
41  
42  /**
43   * 
44   * @author Adrian Cole
45   * 
46   */
47  @Singleton
48  public class CleanupOrphanKeys {
49     final Function<NodeMetadata, OrgAndName> nodeToOrgAndName;
50     final DeleteKeyPair deleteKeyPair;
51     final ListNodesStrategy listNodes;
52     final Map<String, Credentials> credentialStore;
53  
54     @Inject
55     CleanupOrphanKeys(Function<NodeMetadata, OrgAndName> nodeToOrgAndName, DeleteKeyPair deleteKeyPair,
56              Map<String, Credentials> credentialStore, ListNodesStrategy listNodes) {
57        this.nodeToOrgAndName = nodeToOrgAndName;
58        this.deleteKeyPair = deleteKeyPair;
59        this.listNodes = listNodes;
60        this.credentialStore = credentialStore;
61     }
62  
63     public void execute(Iterable<? extends NodeMetadata> deadOnes) {
64        // TODO refactor so that admin passwords are cached properly, probably as a list value in the
65        // credentialStore
66        for (NodeMetadata node : deadOnes){
67           credentialStore.remove("node#" + node.getId());
68           credentialStore.remove("node#" + node.getId() + "#adminPassword");
69        }
70        Iterable<OrgAndName> orgGroups = filter(transform(deadOnes, nodeToOrgAndName), notNull());
71        for (OrgAndName orgGroup : orgGroups) {
72           Iterable<? extends NodeMetadata> nodesInOrg = listNodes.listDetailsOnNodesMatching(parentLocationId(orgGroup
73                    .getOrg().toASCIIString()));
74           Iterable<? extends NodeMetadata> nodesInGroup = filter(nodesInOrg, inGroup(orgGroup.getName()));
75           if (size(nodesInGroup) == 0 || all(nodesInGroup, TERMINATED))
76              deleteKeyPair.execute(orgGroup);
77        }
78     }
79  
80  }