View Javadoc

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 java.util.concurrent.ConcurrentMap;
22  
23  import javax.annotation.Resource;
24  import javax.inject.Inject;
25  import javax.inject.Named;
26  import javax.inject.Singleton;
27  
28  import org.jclouds.compute.reference.ComputeServiceConstants;
29  import org.jclouds.logging.Logger;
30  import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudClient;
31  import org.jclouds.trmk.vcloud_0_8.compute.domain.KeyPairCredentials;
32  import org.jclouds.trmk.vcloud_0_8.compute.domain.OrgAndName;
33  import org.jclouds.trmk.vcloud_0_8.domain.KeyPair;
34  
35  /**
36   * 
37   * @author Adrian Cole
38   * 
39   */
40  @Singleton
41  public class DeleteKeyPair {
42     @Resource
43     @Named(ComputeServiceConstants.COMPUTE_LOGGER)
44     protected Logger logger = Logger.NULL;
45  
46     final TerremarkVCloudClient terremarkClient;
47     final ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap;
48  
49     @Inject
50     DeleteKeyPair(TerremarkVCloudClient terremarkClient, ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap) {
51        this.terremarkClient = terremarkClient;
52        this.credentialsMap = credentialsMap;
53     }
54  
55     public void execute(OrgAndName orgTag) {
56        for (KeyPair keyPair : terremarkClient.listKeyPairsInOrg(orgTag.getOrg())) {
57           if (keyPair.getName().matches("jclouds#" + orgTag.getName() + "#[0-9a-f]+")) {
58              logger.debug(">> deleting keyPair(%s)", keyPair.getName());
59              terremarkClient.deleteKeyPair(keyPair.getId());
60              // TODO: test this clear happens
61              credentialsMap.remove(orgTag);
62              logger.debug("<< deleted keyPair(%s)", keyPair.getName());
63           }
64        }
65     }
66  }