| 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.trmk.vcloud_0_8.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.trmk.vcloud_0_8.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 | } |