@Override public KeyPair load(ZoneAndName zoneAndName) { String zoneId = checkNotNull(zoneAndName, "zoneAndName").getZone(); String prefix = zoneAndName.getName(); Optional<? extends KeyPairApi> api = novaApi.getKeyPairExtensionForZone(zoneId); checkArgument(api.isPresent(), "Key pairs are required, but the extension is not available in zone %s!", zoneId); logger.debug(">> creating keyPair zone(%s) prefix(%s)", zoneId, prefix); KeyPair keyPair = null; while (keyPair == null) { try { keyPair = api.get().create(namingConvention.createWithoutPrefix().uniqueNameForGroup(prefix)); } catch (IllegalStateException e) { } } logger.debug("<< created keyPair(%s)", keyPair.getName()); return keyPair; }
@Override public KeyPair load(ZoneAndName zoneAndName) { String zoneId = checkNotNull(zoneAndName, "zoneAndName").getZone(); String prefix = zoneAndName.getName(); Optional<? extends KeyPairApi> api = novaApi.getKeyPairExtensionForZone(zoneId); checkArgument(api.isPresent(), "Key pairs are required, but the extension is not available in zone %s!", zoneId); logger.debug(">> creating keyPair zone(%s) prefix(%s)", zoneId, prefix); KeyPair keyPair = null; while (keyPair == null) { try { keyPair = api.get().create(namingConvention.createWithoutPrefix().uniqueNameForGroup(prefix)); } catch (IllegalStateException e) { } } logger.debug("<< created keyPair(%s)", keyPair.getName()); return keyPair; }
@Override public KeyPair load(RegionAndName regionAndName) { String regionId = checkNotNull(regionAndName, "regionAndName").getRegion(); String prefix = regionAndName.getName(); Optional<? extends KeyPairApi> api = novaApi.getKeyPairApi(regionId); checkArgument(api.isPresent(), "Key pairs are required, but the extension is not available in region %s!", regionId); logger.debug(">> creating keyPair region(%s) prefix(%s)", regionId, prefix); KeyPair keyPair = null; while (keyPair == null) { try { keyPair = api.get().create(namingConvention.createWithoutPrefix().uniqueNameForGroup(prefix)); } catch (IllegalStateException e) { } } logger.debug("<< created keyPair(%s)", keyPair.getName()); return keyPair; }
@Override public KeyPair load(ZoneAndName zoneAndName) { String zoneId = checkNotNull(zoneAndName, "zoneAndName").getZone(); String prefix = zoneAndName.getName(); Optional<? extends KeyPairApi> api = novaApi.getKeyPairExtensionForZone(zoneId); checkArgument(api.isPresent(), "Key pairs are required, but the extension is not available in zone %s!", zoneId); logger.debug(">> creating keyPair zone(%s) prefix(%s)", zoneId, prefix); KeyPair keyPair = null; while (keyPair == null) { try { keyPair = api.get().create(namingConvention.createWithoutPrefix().uniqueNameForGroup(prefix)); } catch (IllegalStateException e) { } } logger.debug("<< created keyPair(%s)", keyPair.getName()); return keyPair; }
private KeyPair generateKeyPair(String region, String prefix) { logger.debug(">> creating default keypair for node..."); KeyPair keyPair = novaApi.getKeyPairApi(region).get().create(namingConvention.createWithoutPrefix().uniqueNameForGroup(prefix)); logger.debug(">> keypair created! %s", keyPair.getName()); return keyPair; }
@Test public void testApply() throws UnknownHostException { final NovaApi api = createMock(NovaApi.class); KeyPairApi keyApi = createMock(KeyPairApi.class); KeyPair pair = createMock(KeyPair.class); Optional optKeyApi = Optional.of(keyApi); expect(api.getKeyPairExtensionForZone("zone")).andReturn(optKeyApi).atLeastOnce(); expect(keyApi.create("group-1")).andReturn(pair); replay(api, keyApi); CreateUniqueKeyPair parser = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(new TypeLiteral<Supplier<String>>() { }).toInstance(Suppliers.ofInstance("1")); bind(NovaApi.class).toInstance(api); } }).getInstance(CreateUniqueKeyPair.class); assertEquals(parser.load(ZoneAndName.fromZoneAndName("zone", "group")), pair); verify(api, keyApi); }
@Test public void testApplyWithIllegalStateException() throws UnknownHostException { final NovaApi api = createMock(NovaApi.class); KeyPairApi keyApi = createMock(KeyPairApi.class); @SuppressWarnings("unchecked") final Supplier<String> uniqueIdSupplier = createMock(Supplier.class); KeyPair pair = createMock(KeyPair.class); expect(api.getKeyPairExtensionForZone("zone")).andReturn((Optional) Optional.of(keyApi)).atLeastOnce(); expect(uniqueIdSupplier.get()).andReturn("1"); expect(keyApi.create("group-1")).andThrow(new IllegalStateException()); expect(uniqueIdSupplier.get()).andReturn("2"); expect(keyApi.create("group-2")).andReturn(pair); replay(api, keyApi, uniqueIdSupplier); CreateUniqueKeyPair parser = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(new TypeLiteral<Supplier<String>>() { }).toInstance(uniqueIdSupplier); bind(NovaApi.class).toInstance(api); } }).getInstance(CreateUniqueKeyPair.class); assertEquals(parser.load(ZoneAndName.fromZoneAndName("zone", "group")), pair); verify(api, keyApi, uniqueIdSupplier); }
private KeyPair generateKeyPair(String region, String prefix) { logger.debug(">> creating default keypair for node..."); KeyPair keyPair = novaApi.getKeyPairApi(region).get().create(namingConvention.createWithoutPrefix().uniqueNameForGroup(prefix)); logger.debug(">> keypair created! %s", keyPair.getName()); return keyPair; }
public void testCreateAndDeleteKeyPair() throws Exception { final String KEYPAIR_NAME = "testkp"; for (String zoneId : api.getConfiguredZones()) { KeyPairApi keyPairApi = api.getKeyPairExtensionForZone(zoneId).get(); KeyPair keyPair = null; try { keyPair = keyPairApi.create(KEYPAIR_NAME); assertNotNull(keyPair); } finally { if (keyPair != null) { keyPairApi.delete(KEYPAIR_NAME); } } } }
public void testCreateAndGetAndDeleteKeyPair() throws Exception { for (String regionId : api.getConfiguredRegions()) { KeyPairApi keyPairApi = api.getKeyPairApi(regionId).get(); KeyPair createdKeyPair = null; try { createdKeyPair = keyPairApi.create(KEYPAIR_NAME); assertNotNull(createdKeyPair); KeyPair keyPair = keyPairApi.get(KEYPAIR_NAME); assertEquals(keyPair.getName(), createdKeyPair.getName()); assertEquals(keyPair.getFingerprint(), createdKeyPair.getFingerprint()); assertEquals(keyPair.getPublicKey(), createdKeyPair.getPublicKey()); } finally { if (createdKeyPair != null) { keyPairApi.delete(KEYPAIR_NAME); } } } }
public void testCreateKeyPair() throws Exception { HttpRequest create = HttpRequest .builder() .method("POST") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/os-keypairs") .addHeader("Accept", "application/json") .addHeader("X-Auth-Token", authToken) .payload(payloadFromStringWithContentType("{\"keypair\":{\"name\":\"testkeypair\"}}", "application/json")) .build(); HttpResponse createResponse = HttpResponse.builder().statusCode(200) .payload(payloadFromResource("/keypair_created.json")).build(); NovaApi apiWhenServersExist = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, create, createResponse); assertEquals(apiWhenServersExist.getKeyPairExtensionForZone("az-1.region-a.geo-1").get().create("testkeypair") .toString(), new ParseKeyPairTest().expected().toString()); }
public void testCreateKeyPair() throws Exception { HttpRequest create = HttpRequest .builder() .method("POST") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v2/3456/os-keypairs") .addHeader("Accept", "application/json") .addHeader("X-Auth-Token", authToken) .payload(payloadFromStringWithContentType("{\"keypair\":{\"name\":\"testkeypair\"}}", "application/json")) .build(); HttpResponse createResponse = HttpResponse.builder().statusCode(200) .payload(payloadFromResource("/keypair_details.json")).build(); NovaApi apiWhenServersExist = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, responseWithKeystoneAccess, extensionsOfNovaRequest, extensionsOfNovaResponse, create, createResponse); assertEquals(apiWhenServersExist.getKeyPairApi("az-1.region-a.geo-1").get().create("testkeypair") .toString(), new ParseKeyPairTest().expected().toString()); }