@Override
public LoginCredentials apply(final RunningInstance instance) {
Optional<? extends WindowsApi> windowsOption = ec2Client.getWindowsApiForRegion(instance.getRegion());
checkState(windowsOption.isPresent(), "windows feature not present in region %s", instance.getRegion());
final WindowsApi windowsApi = windowsOption.get();
LoginCredentials credentials = LoginCredentials.builder().user("Administrator").noPrivateKey().build();
String privateKey = getPrivateKeyOrNull(instance);
if (privateKey == null) {
return credentials;
}
// The Administrator password will take some time before it is ready - Amazon says
// sometimes
// 15 minutes.
// So we create a predicate that tests if the password is ready, and wrap it in a retryable
// predicate.
final AtomicReference<PasswordData> data = Atomics.newReference();
Predicate<String> passwordReady = new Predicate<String>() {
@Override
public boolean apply(@Nullable String s) {
if (Strings.isNullOrEmpty(s))
return false;
data.set(windowsApi.getPasswordDataForInstance(instance.getId()));
if (data.get() == null)
return false;
return !Strings.isNullOrEmpty(data.get().getPasswordData());
}
};