private KeyPair getOrImportKeyPairForPublicKey()

in aliyun-ecs/src/main/java/org/jclouds/aliyun/ecs/compute/strategy/CreateResourcesThenCreateNodes.java [205:232]


   private KeyPair getOrImportKeyPairForPublicKey(ECSServiceTemplateOptions options, String regionId) {
      logger.debug(">> checking if the key pair already exists...");
      PublicKey userKey = readPublicKey(options.getPublicKey());
      final String fingerprint = computeFingerprint(userKey);
      KeyPair keyPair;

      synchronized (CreateResourcesThenCreateNodes.class) {
         Optional<KeyPair> keyPairOptional = Iterables
               .tryFind(api.sshKeyPairApi().list(regionId).concat(), new Predicate<KeyPair>() {
                  @Override
                  public boolean apply(KeyPair input) {
                     return input.keyPairFingerPrint().equals(fingerprint.replace(":", ""));
                  }
               });
         if (!keyPairOptional.isPresent()) {
            logger.debug(">> key pair not found. Importing a new key pair %s ...", fingerprint);
            keyPair = api.sshKeyPairApi().importKeyPair(
                    regionId,
                    options.getPublicKey(),
                    namingConvention.create().uniqueNameForGroup(JCLOUDS_KEYPAIR_IMPORTED));
            logger.debug(">> key pair imported! %s", keyPair);
         } else {
            logger.debug(">> key pair found for key %s", fingerprint);
            keyPair = keyPairOptional.get();
         }
         return keyPair;
      }
   }