NovaLauncher extends Launcher { public boolean uploadSshPublicKey(String keyPairName, Nova nova, boolean removeOld) { boolean uploadSuccesful; FluentIterable<KeyPair> keyPairs = novaContext.getKeyPairApi().list(); if (keyPairs.isEmpty()) { logger.info(String.format("New keypair '%s' is being uploaded to Nova OpenStack", keyPairName)); novaContext.getKeyPairApi().createWithPublicKey(keyPairName, sshKeyPair.getPublicKey()); uploadSuccesful = true; } else if (removeOld) { logger.info(String.format("Removing the old keypair '%s' and uploading the new one ...", keyPairName)); boolean deleteSuccesful = novaContext.getKeyPairApi().delete(keyPairName); KeyPair pair = novaContext.getKeyPairApi().createWithPublicKey(keyPairName, sshKeyPair.getPublicKey()); uploadSuccesful = deleteSuccesful && pair != null; } else { uploadSuccesful = false; } return uploadSuccesful; } NovaLauncher(NovaContext novaContext, SshKeyPair sshKeyPair); static NovaContext validateCredentials(NovaCredentials novaCredentials, ContextBuilder builder); static NovaCredentials readCredentials(Confs confs); String createSecurityGroup(String clusterName, String groupName, Nova nova, Set<String> ports); boolean uploadSshPublicKey(String keyPairName, Nova nova, boolean removeOld); boolean cleanupFailedNodes(Map<NodeMetadata, Throwable> failedNodes); @Override void cleanup(JsonCluster definition, ClusterRuntime runtime); void cleanup(String clusterName, Set<String> vmIds, Set<String> vmNames, Map<String, String> groupRegion); @Override String forkGroup(JsonCluster definition, ClusterRuntime runtime, String name); @Override List<MachineRuntime> forkMachines(JsonCluster definition, ClusterRuntime runtime, String name); static Predicate<NodeMetadata> withPredicate(final Set<String> ids, final Set<String> names,
                                                      final Set<String> groupNames);  }
@Test public void uploadSSHPublicKeyAndCreate() throws KaramelException { String keypairName = "pepeKeyPair"; KeyPair pair = mock(KeyPair.class); List<KeyPair> keyPairList = new ArrayList<>(); FluentIterable<KeyPair> keys = FluentIterable.from(keyPairList); when(keyPairApi.list()).thenReturn(keys); when(keyPairApi.createWithPublicKey(keypairName, sshKeyPair.getPublicKey())).thenReturn(pair); NovaLauncher novaLauncher = new NovaLauncher(novaContext, sshKeyPair); boolean uploadSuccessful = novaLauncher.uploadSshPublicKey(keypairName, nova, false); assertTrue(uploadSuccessful); }
@Test public void uploadSSHPublicKeyAndRecreateOld() throws KaramelException { String keypairName = "pepeKeyPair"; KeyPair pair = mock(KeyPair.class); List<KeyPair> keyPairList = new ArrayList<>(); keyPairList.add(pair); FluentIterable<KeyPair> keys = FluentIterable.from(keyPairList); when(keyPairApi.list()).thenReturn(keys); when(keyPairApi.delete(keypairName)).thenReturn(true); when(keyPairApi.createWithPublicKey(keypairName, sshKeyPair.getPublicKey())).thenReturn(pair); NovaLauncher novaLauncher = new NovaLauncher(novaContext, sshKeyPair); boolean uploadSuccessful = novaLauncher.uploadSshPublicKey(keypairName, nova, true); assertTrue(uploadSuccessful); }
@Test public void uploadSSHPublicKeyAndNotRecreateOldFail() throws KaramelException { String keypairName = "pepeKeyPair"; KeyPair pair = mock(KeyPair.class); List<KeyPair> keyPairList = new ArrayList<>(); keyPairList.add(pair); FluentIterable<KeyPair> keys = FluentIterable.from(keyPairList); when(keyPairApi.list()).thenReturn(keys); when(keyPairApi.delete(keypairName)).thenReturn(true); when(keyPairApi.createWithPublicKey(keypairName, sshKeyPair.getPublicKey())).thenReturn(pair); NovaLauncher novaLauncher = new NovaLauncher(novaContext, sshKeyPair); boolean uploadSuccessful = novaLauncher.uploadSshPublicKey(keypairName, nova, false); assertFalse(uploadSuccessful); }