allow images to get credentials by credentialstore

This commit is contained in:
Adrian Cole 2011-12-22 02:18:17 -08:00
parent c418edb140
commit c6d1f2d56e
2 changed files with 31 additions and 6 deletions

View File

@ -20,6 +20,8 @@ package org.jclouds.compute.strategy.impl;
import static com.google.common.base.Preconditions.checkState;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
@ -38,10 +40,13 @@ import org.jclouds.javax.annotation.Nullable;
public class ReturnCredentialsBoundToImage implements PopulateDefaultLoginCredentialsForImageStrategy {
protected final LoginCredentials creds;
protected Map<String, Credentials> credentialStore;
@Inject
public ReturnCredentialsBoundToImage(@Nullable @Named("image") LoginCredentials creds) {
public ReturnCredentialsBoundToImage(@Nullable @Named("image") LoginCredentials creds,
Map<String, Credentials> credentialStore) {
this.creds = creds;
this.credentialStore = credentialStore;
}
@Override
@ -50,6 +55,8 @@ public class ReturnCredentialsBoundToImage implements PopulateDefaultLoginCreden
if (creds != null)
return creds;
Image image = Image.class.cast(resourceToAuthenticate);
if (credentialStore.containsKey("image#" + image.getId()))
return LoginCredentials.fromCredentials(credentialStore.get("image#" + image.getId()));
if (image.getOperatingSystem() != null && OsFamily.WINDOWS.equals(image.getOperatingSystem().getFamily())) {
return LoginCredentials.builder().user("Administrator").build();
} else {

View File

@ -31,6 +31,8 @@ import org.jclouds.domain.Credentials;
import org.jclouds.domain.LoginCredentials;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
/**
*
* @author Adrian Cole
@ -43,7 +45,21 @@ public class ReturnCredentialsBoundToImageTest {
replay(image);
LoginCredentials creds = new LoginCredentials("ubuntu", "foo", null, false);
assertEquals(new ReturnCredentialsBoundToImage(creds).execute(image), creds);
assertEquals(new ReturnCredentialsBoundToImage(creds, ImmutableMap.<String, Credentials> of()).execute(image),
creds);
verify(image);
}
public void testDefaultIsToReturnConfiguredCredentialInStore() {
Image image = createMock(Image.class);
expect(image.getId()).andReturn("1").times(2);
replay(image);
LoginCredentials creds = new LoginCredentials("ubuntu", "foo", null, false);
assertEquals(new ReturnCredentialsBoundToImage(null, ImmutableMap.<String, Credentials> of("image#1",creds)).execute(image),
creds);
verify(image);
@ -51,12 +67,13 @@ public class ReturnCredentialsBoundToImageTest {
public void testReturnAdministratorOnWindows() {
Image image = createMock(Image.class);
expect(image.getId()).andReturn("1");
expect(image.getOperatingSystem()).andReturn(
OperatingSystem.builder().family(OsFamily.WINDOWS).description("foo").build()).atLeastOnce();
replay(image);
Credentials creds = new Credentials("Administrator", null);
assertEquals(new ReturnCredentialsBoundToImage(null).execute(image), creds);
assertEquals(new ReturnCredentialsBoundToImage(null, ImmutableMap.<String, Credentials> of()).execute(image), creds);
verify(image);
@ -64,12 +81,13 @@ public class ReturnCredentialsBoundToImageTest {
public void testReturnRootWhenNotOnWindows() {
Image image = createMock(Image.class);
expect(image.getId()).andReturn("1");
expect(image.getOperatingSystem()).andReturn(
OperatingSystem.builder().family(OsFamily.LINUX).description("foo").build()).atLeastOnce();
replay(image);
Credentials creds = new Credentials("root", null);
assertEquals(new ReturnCredentialsBoundToImage(null).execute(image), creds);
assertEquals(new ReturnCredentialsBoundToImage(null, ImmutableMap.<String, Credentials> of()).execute(image), creds);
verify(image);