| 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.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.net.URI; |
| 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.compute.reference.ComputeServiceConstants; |
| 31 | import org.jclouds.http.HttpResponseException; |
| 32 | import org.jclouds.logging.Logger; |
| 33 | import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudClient; |
| 34 | import org.jclouds.trmk.vcloud_0_8.compute.domain.OrgAndName; |
| 35 | import org.jclouds.trmk.vcloud_0_8.domain.KeyPair; |
| 36 | import org.jclouds.util.Throwables2; |
| 37 | |
| 38 | import com.google.common.base.Function; |
| 39 | import com.google.common.base.Supplier; |
| 40 | |
| 41 | /** |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | */ |
| 45 | @Singleton |
| 46 | public class CreateUniqueKeyPair implements Function<OrgAndName, KeyPair> { |
| 47 | @Resource |
| 48 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 49 | protected Logger logger = Logger.NULL; |
| 50 | protected final TerremarkVCloudClient trmkClient; |
| 51 | protected Supplier<String> randomSuffix; |
| 52 | |
| 53 | @Inject |
| 54 | CreateUniqueKeyPair(TerremarkVCloudClient trmkClient, Supplier<String> randomSuffix) { |
| 55 | this.trmkClient = trmkClient; |
| 56 | this.randomSuffix = randomSuffix; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public KeyPair apply(OrgAndName from) { |
| 61 | return createNewKeyPairInOrg(from.getOrg(), from.getName()); |
| 62 | } |
| 63 | |
| 64 | private KeyPair createNewKeyPairInOrg(URI org, String keyPairName) { |
| 65 | checkNotNull(org, "org"); |
| 66 | checkNotNull(keyPairName, "keyPairName"); |
| 67 | logger.debug(">> creating keyPair org(%s) name(%s)", org, keyPairName); |
| 68 | KeyPair keyPair = null; |
| 69 | while (keyPair == null) { |
| 70 | try { |
| 71 | keyPair = trmkClient.generateKeyPairInOrg(org, getNextName(keyPairName), false); |
| 72 | logger.debug("<< created keyPair(%s)", keyPair.getName()); |
| 73 | } catch (RuntimeException e) { |
| 74 | HttpResponseException ht = Throwables2.getFirstThrowableOfType(e, HttpResponseException.class); |
| 75 | if (ht == null || ht.getContent() == null |
| 76 | || ht.getContent().indexOf("Security key with same name exists") == -1) |
| 77 | throw e; |
| 78 | } |
| 79 | } |
| 80 | return keyPair; |
| 81 | } |
| 82 | |
| 83 | private String getNextName(String keyPairName) { |
| 84 | return "jclouds#" + keyPairName + "#" + randomSuffix.get(); |
| 85 | } |
| 86 | } |