@Test void testCreateAndUpdateSshKeyWhenCreatedAndSet() { Environment environment = new Environment(); environment.setName("env-name"); environment.setResourceCrn("res-crn"); environment.setCloudPlatform("CP"); environment.setLocation("location"); EnvironmentAuthentication authentication = new EnvironmentAuthentication(); authentication.setPublicKey("ssh-key"); environment.setAuthentication(authentication); CloudConnector connector = mock(CloudConnector.class); PublicKeyConnector publicKeyConnector = mock(PublicKeyConnector.class); when(cloudPlatformConnectors.get(new CloudPlatformVariant(Platform.platform("CP"), Variant.variant("CP")))).thenReturn(connector); when(connector.publicKey()).thenReturn(publicKeyConnector); when(credentialToCloudCredentialConverter.convert(environment.getCredential())).thenReturn(mock(CloudCredential.class)); when(clock.getCurrentTimeMillis()).thenReturn(1L); environmentResourceServiceUnderTest.createAndUpdateSshKey(environment); assertEquals("env-name-res-crn-1", environment.getAuthentication().getPublicKeyId()); }
public boolean createAndUpdateSshKey(Environment environment) { LOGGER.debug("Environment {} requested managed public key. Creating.", environment.getName()); String publicKeyId = String.format("%s-%s", environment.getName(), environment.getResourceCrn()); if (publicKeyId.length() <= MAX_LENGTH_OF_PUBLIC_KEY_NAME) { LOGGER.debug("Append timestamp to public key id ({}).", publicKeyId); publicKeyId += "-" + clock.getCurrentTimeMillis(); } else { LOGGER.debug("The public key id ({}) is longer than {}.", publicKeyId, MAX_LENGTH_OF_PUBLIC_KEY_NAME); } if (publicKeyId.length() > MAX_LENGTH_OF_PUBLIC_KEY_NAME) { String substring = publicKeyId.substring(0, MAX_LENGTH_OF_PUBLIC_KEY_NAME); LOGGER.debug("The public key id ({}) is chopped to {}.", publicKeyId, substring); publicKeyId = substring; } boolean created = createPublicKey(environment, publicKeyId); if (created) { environment.getAuthentication().setPublicKeyId(publicKeyId); LOGGER.debug("The public key id ({}) is updated in {}.", publicKeyId, environment.getName()); } return created; }
EnvironmentResourceService { public boolean createAndUpdateSshKey(Environment environment) { LOGGER.debug("Environment {} requested managed public key. Creating.", environment.getName()); String publicKeyId = String.format("%s-%s", environment.getName(), environment.getResourceCrn()); if (publicKeyId.length() <= MAX_LENGTH_OF_PUBLIC_KEY_NAME) { LOGGER.debug("Append timestamp to public key id ({}).", publicKeyId); publicKeyId += "-" + clock.getCurrentTimeMillis(); } else { LOGGER.debug("The public key id ({}) is longer than {}.", publicKeyId, MAX_LENGTH_OF_PUBLIC_KEY_NAME); } if (publicKeyId.length() > MAX_LENGTH_OF_PUBLIC_KEY_NAME) { String substring = publicKeyId.substring(0, MAX_LENGTH_OF_PUBLIC_KEY_NAME); LOGGER.debug("The public key id ({}) is chopped to {}.", publicKeyId, substring); publicKeyId = substring; } boolean created = createPublicKey(environment, publicKeyId); if (created) { environment.getAuthentication().setPublicKeyId(publicKeyId); LOGGER.debug("The public key id ({}) is updated in {}.", publicKeyId, environment.getName()); } return created; } }
EnvironmentResourceService { public boolean createAndUpdateSshKey(Environment environment) { LOGGER.debug("Environment {} requested managed public key. Creating.", environment.getName()); String publicKeyId = String.format("%s-%s", environment.getName(), environment.getResourceCrn()); if (publicKeyId.length() <= MAX_LENGTH_OF_PUBLIC_KEY_NAME) { LOGGER.debug("Append timestamp to public key id ({}).", publicKeyId); publicKeyId += "-" + clock.getCurrentTimeMillis(); } else { LOGGER.debug("The public key id ({}) is longer than {}.", publicKeyId, MAX_LENGTH_OF_PUBLIC_KEY_NAME); } if (publicKeyId.length() > MAX_LENGTH_OF_PUBLIC_KEY_NAME) { String substring = publicKeyId.substring(0, MAX_LENGTH_OF_PUBLIC_KEY_NAME); LOGGER.debug("The public key id ({}) is chopped to {}.", publicKeyId, substring); publicKeyId = substring; } boolean created = createPublicKey(environment, publicKeyId); if (created) { environment.getAuthentication().setPublicKeyId(publicKeyId); LOGGER.debug("The public key id ({}) is updated in {}.", publicKeyId, environment.getName()); } return created; } EnvironmentResourceService(CredentialService credentialService, NetworkService networkService, CloudPlatformConnectors cloudPlatformConnectors,
            CredentialToCloudCredentialConverter credentialToCloudCredentialConverter, Clock clock, ProxyConfigService proxyConfigService); }
EnvironmentResourceService { public boolean createAndUpdateSshKey(Environment environment) { LOGGER.debug("Environment {} requested managed public key. Creating.", environment.getName()); String publicKeyId = String.format("%s-%s", environment.getName(), environment.getResourceCrn()); if (publicKeyId.length() <= MAX_LENGTH_OF_PUBLIC_KEY_NAME) { LOGGER.debug("Append timestamp to public key id ({}).", publicKeyId); publicKeyId += "-" + clock.getCurrentTimeMillis(); } else { LOGGER.debug("The public key id ({}) is longer than {}.", publicKeyId, MAX_LENGTH_OF_PUBLIC_KEY_NAME); } if (publicKeyId.length() > MAX_LENGTH_OF_PUBLIC_KEY_NAME) { String substring = publicKeyId.substring(0, MAX_LENGTH_OF_PUBLIC_KEY_NAME); LOGGER.debug("The public key id ({}) is chopped to {}.", publicKeyId, substring); publicKeyId = substring; } boolean created = createPublicKey(environment, publicKeyId); if (created) { environment.getAuthentication().setPublicKeyId(publicKeyId); LOGGER.debug("The public key id ({}) is updated in {}.", publicKeyId, environment.getName()); } return created; } EnvironmentResourceService(CredentialService credentialService, NetworkService networkService, CloudPlatformConnectors cloudPlatformConnectors,
            CredentialToCloudCredentialConverter credentialToCloudCredentialConverter, Clock clock, ProxyConfigService proxyConfigService); Credential getCredentialFromRequest(CredentialAwareEnvRequest request, String accountId); Optional<ProxyConfig> getProxyConfig(String proxyConfigName, String accountId); boolean createAndUpdateSshKey(Environment environment); Optional<PublicKeyConnector> getPublicKeyConnector(String cloudPlatform); BaseNetwork createAndSetNetwork(Environment environment, NetworkDto networkDto, String accountId, Map<String, CloudSubnet> subnetMetas); void deletePublicKey(Environment environment); void deletePublicKey(Environment environment, String publicKeyId); boolean isPublicKeyIdExists(Environment environment, String publicKeyId); }
EnvironmentResourceService { public boolean createAndUpdateSshKey(Environment environment) { LOGGER.debug("Environment {} requested managed public key. Creating.", environment.getName()); String publicKeyId = String.format("%s-%s", environment.getName(), environment.getResourceCrn()); if (publicKeyId.length() <= MAX_LENGTH_OF_PUBLIC_KEY_NAME) { LOGGER.debug("Append timestamp to public key id ({}).", publicKeyId); publicKeyId += "-" + clock.getCurrentTimeMillis(); } else { LOGGER.debug("The public key id ({}) is longer than {}.", publicKeyId, MAX_LENGTH_OF_PUBLIC_KEY_NAME); } if (publicKeyId.length() > MAX_LENGTH_OF_PUBLIC_KEY_NAME) { String substring = publicKeyId.substring(0, MAX_LENGTH_OF_PUBLIC_KEY_NAME); LOGGER.debug("The public key id ({}) is chopped to {}.", publicKeyId, substring); publicKeyId = substring; } boolean created = createPublicKey(environment, publicKeyId); if (created) { environment.getAuthentication().setPublicKeyId(publicKeyId); LOGGER.debug("The public key id ({}) is updated in {}.", publicKeyId, environment.getName()); } return created; } EnvironmentResourceService(CredentialService credentialService, NetworkService networkService, CloudPlatformConnectors cloudPlatformConnectors,
            CredentialToCloudCredentialConverter credentialToCloudCredentialConverter, Clock clock, ProxyConfigService proxyConfigService); Credential getCredentialFromRequest(CredentialAwareEnvRequest request, String accountId); Optional<ProxyConfig> getProxyConfig(String proxyConfigName, String accountId); boolean createAndUpdateSshKey(Environment environment); Optional<PublicKeyConnector> getPublicKeyConnector(String cloudPlatform); BaseNetwork createAndSetNetwork(Environment environment, NetworkDto networkDto, String accountId, Map<String, CloudSubnet> subnetMetas); void deletePublicKey(Environment environment); void deletePublicKey(Environment environment, String publicKeyId); boolean isPublicKeyIdExists(Environment environment, String publicKeyId);  }