use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.
the class EnvironmentResourceService method deletePublicKey.
public void deletePublicKey(Environment environment, String publicKeyId) {
try {
LOGGER.info("Try to delete the ssh key ({}) for {}", publicKeyId, environment.getName());
PublicKeyConnector publicKeyConnector = getPublicKeyConnector(environment.getCloudPlatform()).orElseThrow(() -> new BadRequestException("No public key connector for cloud platform: " + environment.getCloudPlatform()));
PublicKeyUnregisterRequest request = createPublicKeyUnregisterRequest(environment, publicKeyId);
publicKeyConnector.unregister(request);
LOGGER.info("the ssh key ({}) deleted successfully for {}", publicKeyId, environment.getName());
} catch (UnsupportedOperationException e) {
LOGGER.info("Cloud platform {} does not support public key services", environment.getCloudPlatform());
}
}
use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.
the class EnvironmentResourceServiceTest method testCreateAndUpdateSshKeyWhenTooLongWithoutTimestamp.
@Test
void testCreateAndUpdateSshKeyWhenTooLongWithoutTimestamp() {
Environment environment = new Environment();
environment.setName(StringUtils.repeat("*", 200));
environment.setResourceCrn(StringUtils.repeat("*", 100));
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);
String first = StringUtils.repeat("*", 200);
String second = StringUtils.repeat("*", 54);
assertEquals(first + "-" + second, environment.getAuthentication().getPublicKeyId());
// the split size should be equals 2, because we concat the name and crn only
assertEquals(2, environment.getAuthentication().getPublicKeyId().split("-").length);
}
use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.
the class EnvironmentResourceServiceTest method testCreateAndUpdateSshKeyWhenCreatedAndSet.
@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());
}
use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.
the class EnvironmentResourceService method createPublicKey.
private boolean createPublicKey(Environment environment, String publicKeyId) {
try {
PublicKeyConnector publicKeyConnector = getPublicKeyConnector(environment.getCloudPlatform()).orElseThrow(() -> new BadRequestException("No network connector for cloud platform: " + environment.getCloudPlatform()));
PublicKeyRegisterRequest request = createPublicKeyRegisterRequest(environment, publicKeyId);
publicKeyConnector.register(request);
LOGGER.info("Public key id is registered with name of {}", publicKeyId);
return true;
} catch (UnsupportedOperationException e) {
LOGGER.info("Cloud platform {} does not support public key services", environment.getCloudPlatform());
} catch (Exception e) {
LOGGER.info("Public key could not be registered. {}", e.getMessage(), e);
}
return false;
}
Aggregations