moved the default credentials to ImageImpl#defaultCredentials, issue 157

This commit is contained in:
Alex Yarmula 2010-04-08 12:44:19 -07:00
parent 1bdf5eda18
commit 214aee6626
17 changed files with 80 additions and 49 deletions

View File

@ -22,7 +22,7 @@ import java.util.List;
import java.util.Properties;
import org.jclouds.aws.ec2.compute.config.EC2ComputeServiceContextModule;
import org.jclouds.aws.ec2.compute.config.internal.EC2AuthenticationModule;
import org.jclouds.aws.ec2.compute.config.internal.EC2ResolveImagesModule;
import org.jclouds.aws.ec2.config.EC2RestClientModule;
import org.jclouds.compute.ComputeServiceContextBuilder;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
@ -71,6 +71,6 @@ public class EC2ContextBuilder extends
@Override
protected void addImageResolutionModule() {
modules.add(new EC2AuthenticationModule());
modules.add(new EC2ResolveImagesModule());
}
}

View File

@ -19,19 +19,19 @@
package org.jclouds.aws.ec2.compute.config.internal;
import com.google.inject.AbstractModule;
import org.jclouds.aws.ec2.compute.strategy.EC2AuthenticateImagesStrategy;
import org.jclouds.aws.ec2.compute.strategy.EC2PopulateDefaultLoginCredentialsForImageStrategy;
import org.jclouds.compute.config.ResolvesImages;
import org.jclouds.compute.strategy.AuthenticateImagesStrategy;
import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
/**
* @author Oleksiy Yarmula
*/
@ResolvesImages
public class EC2AuthenticationModule extends AbstractModule {
public class EC2ResolveImagesModule extends AbstractModule {
@Override
protected void configure() {
bind(AuthenticateImagesStrategy.class).to(EC2AuthenticateImagesStrategy.class);
bind(PopulateDefaultLoginCredentialsForImageStrategy.class).to(EC2PopulateDefaultLoginCredentialsForImageStrategy.class);
}
}

View File

@ -23,6 +23,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
@ -32,6 +33,8 @@ import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.internal.ImageImpl;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
import org.jclouds.domain.Credentials;
import org.jclouds.logging.Logger;
import com.google.common.base.Function;
@ -54,6 +57,9 @@ public class ImageParser implements Function<org.jclouds.aws.ec2.domain.Image, I
"9.04").put("karmic", "9.10").put("lucid", "10.04").put("maverick", "10.10")
.build();
@Inject
private PopulateDefaultLoginCredentialsForImageStrategy authenticator;
@Override
public Image apply(org.jclouds.aws.ec2.domain.Image from) {
if (from.getImageLocation().indexOf("test") != -1) {
@ -83,6 +89,8 @@ public class ImageParser implements Function<org.jclouds.aws.ec2.domain.Image, I
logger.debug("<< didn't match os(%s)", matcher.group(1));
}
}
Credentials defaultCredentials = authenticator.execute(from);
return new ImageImpl(
from.getId(),
name,
@ -94,7 +102,7 @@ public class ImageParser implements Function<org.jclouds.aws.ec2.domain.Image, I
os,
osDescription,
from.getArchitecture() == org.jclouds.aws.ec2.domain.Image.Architecture.I386 ? Architecture.X86_32
: Architecture.X86_64);
: Architecture.X86_64,
defaultCredentials);
}
}

View File

@ -18,7 +18,6 @@ import org.jclouds.aws.ec2.services.AMIClient;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.domain.internal.NodeMetadataImpl;
import org.jclouds.compute.strategy.AuthenticateImagesStrategy;
import org.jclouds.domain.Credentials;
import com.google.common.base.Function;
@ -35,15 +34,15 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
private final AMIClient amiClient;
private final Map<RegionTag, KeyPairCredentials> credentialsMap;
private final AuthenticateImagesStrategy authenticator;
private final ImageParser imageParser;
@Inject
public RunningInstanceToNodeMetadata(AMIClient amiClient,
Map<RegionTag, KeyPairCredentials> credentialsMap,
AuthenticateImagesStrategy authenticator) {
ImageParser imageParser) {
this.amiClient = amiClient;
this.credentialsMap = credentialsMap;
this.authenticator = authenticator;
this.imageParser = imageParser;
}
@Override
@ -62,7 +61,12 @@ public class RunningInstanceToNodeMetadata implements Function<RunningInstance,
Image image = Iterables.getOnlyElement(amiClient.describeImagesInRegion(from.getRegion(),
DescribeImagesOptions.Builder.imageIds(from.getImageId())));
if(credentials == null) credentials = authenticator.execute(image);
// canonical/alestic images use the ubuntu user to login
// TODO: add this as a property of image
if (credentials != null && image.getImageOwnerId().matches("063491364108|099720109477"))
credentials = new Credentials("ubuntu", credentials.key);
if(credentials == null) credentials = imageParser.apply(image).getDefaultCredentials();
String locationId = from.getAvailabilityZone().toString();
Map<String, String> extra = ImmutableMap.<String, String> of();

View File

@ -19,7 +19,7 @@
package org.jclouds.aws.ec2.compute.strategy;
import org.jclouds.aws.ec2.domain.Image;
import org.jclouds.compute.strategy.AuthenticateImagesStrategy;
import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
import org.jclouds.domain.Credentials;
import static com.google.common.base.Preconditions.checkArgument;
@ -28,7 +28,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* @author Oleksiy Yarmula
*/
public class EC2AuthenticateImagesStrategy implements AuthenticateImagesStrategy {
public class EC2PopulateDefaultLoginCredentialsForImageStrategy implements PopulateDefaultLoginCredentialsForImageStrategy {
@Override
public Credentials execute(Object resourceToAuthenticate) {
@ -36,12 +36,12 @@ public class EC2AuthenticateImagesStrategy implements AuthenticateImagesStrategy
checkArgument(resourceToAuthenticate instanceof Image, "Resource must be an image (for EC2)");
Image image = (Image) resourceToAuthenticate;
Credentials credentials = null;
Credentials credentials;
// canonical/alestic images use the ubuntu user to login
if (image.getImageOwnerId().matches("063491364108|099720109477"))
credentials = new Credentials("ubuntu", "");
else credentials = new Credentials("root", "");
credentials = new Credentials("ubuntu", null);
else credentials = new Credentials("root", null);
return credentials;
}

View File

@ -63,7 +63,7 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
@Test
public void testCredentialsMapping() {
// Template simpleTemplate = client.templateBuilder().smallest().build();
Template simpleTemplate = client.templateBuilder().smallest().build();
// client.runNodesWithTag("ec2", 1, simpleTemplate);
Map<String, ? extends NodeMetadata> map = client.getNodesWithTag("ec2");
int a = 5;

View File

@ -34,6 +34,7 @@ import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.domain.internal.ImageImpl;
import org.jclouds.compute.internal.TemplateBuilderImpl;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationScope;
import org.jclouds.domain.internal.LocationImpl;
@ -113,7 +114,7 @@ public class EC2ComputeServiceTest {
Location location = new LocationImpl(LocationScope.REGION, "us-east-1", "us east", null, true);
Image image = new ImageImpl("ami-image", "image", "us-east-1", null, Maps
.<String, String> newHashMap(), "description", "1.0", null, "ubuntu",
Architecture.X86_64);
Architecture.X86_64, new Credentials("root", null));
return new TemplateBuilderImpl(ImmutableMap.of("us-east-1", location), ImmutableMap.of(
"ami-image", image), Maps.uniqueIndex(ImmutableSet.of(EC2Size.C1_MEDIUM,

View File

@ -26,6 +26,7 @@ package org.jclouds.compute.domain;
import org.jclouds.compute.domain.internal.ImageImpl;
import com.google.inject.ImplementedBy;
import org.jclouds.domain.Credentials;
/**
* Running Operating system
@ -59,4 +60,10 @@ public interface Image extends ComputeMetadata {
* Operating System
*/
Architecture getArchitecture();
/**
* Default credentials for the current image
*/
Credentials getDefaultCredentials();
}

View File

@ -34,6 +34,7 @@ import org.jclouds.compute.domain.Architecture;
import org.jclouds.compute.domain.ComputeType;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.domain.Credentials;
/**
* @author Adrian Cole
@ -48,16 +49,20 @@ public class ImageImpl extends ComputeMetadataImpl implements Image {
private final OsFamily osFamily;
private final String osDescription;
private final Architecture architecture;
private final Credentials defaultCredentials;
public ImageImpl(String id, String name, String locationId, URI uri,
Map<String, String> userMetadata, String description, String version,
@Nullable OsFamily osFamily, String osDescription, Architecture architecture) {
@Nullable OsFamily osFamily, String osDescription, Architecture architecture,
Credentials defaultCredentials) {
super(ComputeType.IMAGE, id, name, locationId, uri, userMetadata);
this.version = checkNotNull(version, "version");
this.osFamily = osFamily;
this.description = checkNotNull(description, "description");
this.osDescription = checkNotNull(osDescription, "osDescription");
this.architecture = checkNotNull(architecture, "architecture");
this.defaultCredentials = defaultCredentials;
}
/**
@ -100,7 +105,15 @@ public class ImageImpl extends ComputeMetadataImpl implements Image {
return architecture;
}
/**
* {@inheritDoc}
*/
@Override
public Credentials getDefaultCredentials() {
return defaultCredentials;
}
@Override
public String toString() {
return "[id=" + getId() + ", name=" + getName() + ", locationId=" + getLocationId()
+ ", architecture=" + architecture + ", osDescription=" + osDescription

View File

@ -23,7 +23,7 @@ import org.jclouds.domain.Credentials;
/**
* @author Oleksiy Yarmula
*/
public interface AuthenticateImagesStrategy {
public interface PopulateDefaultLoginCredentialsForImageStrategy {
/**
* Processes the resource to determine credentials.

View File

@ -34,7 +34,7 @@ import org.jclouds.compute.ComputeServiceContextBuilder;
import org.jclouds.compute.internal.ComputeServiceContextImpl;
import org.jclouds.gogrid.config.GoGridComputeServiceContextModule;
import org.jclouds.gogrid.config.GoGridRestClientModule;
import org.jclouds.gogrid.config.internal.GoGridAuthenticationModule;
import org.jclouds.gogrid.config.internal.GoGridResolveImagesModule;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
@ -62,7 +62,7 @@ public class GoGridContextBuilder extends ComputeServiceContextBuilder<GoGridAsy
@Override
protected void addImageResolutionModule() {
modules.add(new GoGridAuthenticationModule());
modules.add(new GoGridResolveImagesModule());
}
@Override

View File

@ -36,7 +36,6 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import com.google.inject.AbstractModule;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.Architecture;
@ -323,17 +322,14 @@ public class GoGridComputeServiceContextModule extends GoGridContextModule {
private final Map<String, NodeState> serverStateToNodeState;
private final Function<String, InetAddress> stringIpToInetAddress;
private final GoGridClient client;
private final AuthenticateImagesStrategy authenticator;
@SuppressWarnings("unused")
@Inject
ServerToNodeMetadata(Map<String, NodeState> serverStateToNodeState,
Function<String, InetAddress> stringIpToInetAddress, GoGridClient client,
AuthenticateImagesStrategy authenticator) {
Function<String, InetAddress> stringIpToInetAddress, GoGridClient client) {
this.serverStateToNodeState = serverStateToNodeState;
this.stringIpToInetAddress = stringIpToInetAddress;
this.client = client;
this.authenticator = authenticator;
}
@Override
@ -343,7 +339,8 @@ public class GoGridComputeServiceContextModule extends GoGridContextModule {
Set<InetAddress> ipSet = ImmutableSet
.of(stringIpToInetAddress.apply(from.getIp().getIp()));
NodeState state = serverStateToNodeState.get(from.getState().getName());
Credentials creds = authenticator.execute(from);
Credentials creds = client.getServerServices().getServerCredentialsList().get(
from.getName());
return new NodeMetadataImpl(from.getId() + "", from.getName(), locationId, null,
ImmutableMap.<String, String> of(), tag, state, ipSet, ImmutableList
.<InetAddress> of(), ImmutableMap.<String, String> of(), creds);
@ -434,8 +431,9 @@ public class GoGridComputeServiceContextModule extends GoGridContextModule {
@Provides
@Singleton
protected Map<String, ? extends Image> provideImages(final GoGridClient sync, LogHolder holder,
Function<ComputeMetadata, String> indexer, Location location)
throws InterruptedException, ExecutionException, TimeoutException {
Function<ComputeMetadata, String> indexer, Location location,
PopulateDefaultLoginCredentialsForImageStrategy authenticator)
throws InterruptedException, ExecutionException, TimeoutException {
final Set<Image> images = Sets.newHashSet();
holder.logger.debug(">> providing images");
Set<ServerImage> allImages = sync.getImageServices().getImageList();
@ -455,10 +453,10 @@ public class GoGridComputeServiceContextModule extends GoGridContextModule {
} catch (IllegalArgumentException e) {
holder.logger.debug("<< didn't match os(%s)", matchedOs);
}
Credentials defaultCredentials = authenticator.execute(from);
images.add(new ImageImpl(from.getId() + "", from.getFriendlyName(), location.getId(),
null, ImmutableMap.<String, String> of(), from.getDescription(), version, os,
osDescription, arch));
osDescription, arch, defaultCredentials));
}
holder.logger.debug("<< images(%d)", images.size());
return Maps.uniqueIndex(images, indexer);

View File

@ -20,10 +20,10 @@ package org.jclouds.gogrid.config.internal;
import com.google.inject.AbstractModule;
import org.jclouds.compute.config.ResolvesImages;
import org.jclouds.compute.strategy.AuthenticateImagesStrategy;
import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
import org.jclouds.domain.Credentials;
import org.jclouds.gogrid.GoGridClient;
import org.jclouds.gogrid.domain.Server;
import org.jclouds.gogrid.domain.ServerImage;
import javax.inject.Inject;
@ -34,27 +34,25 @@ import static com.google.common.base.Preconditions.checkNotNull;
* @author Oleksiy Yarmula
*/
@ResolvesImages
public class GoGridAuthenticationModule extends AbstractModule {
public class GoGridResolveImagesModule extends AbstractModule {
@Override
protected void configure() {
bind(AuthenticateImagesStrategy.class).to(GoGridAuthenticateImagesStrategy.class);
bind(PopulateDefaultLoginCredentialsForImageStrategy.class).
to(GoGridPopulateDefaultLoginCredentialsForImageStrategy.class);
}
public static class GoGridAuthenticateImagesStrategy implements AuthenticateImagesStrategy {
public static class GoGridPopulateDefaultLoginCredentialsForImageStrategy
implements PopulateDefaultLoginCredentialsForImageStrategy {
private final GoGridClient client;
@Inject
protected GoGridAuthenticateImagesStrategy(GoGridClient client) {
protected GoGridPopulateDefaultLoginCredentialsForImageStrategy(GoGridClient client) {
this.client = client;
}
@Override
public Credentials execute(Object resourceToAuthenticate) {
checkNotNull(resourceToAuthenticate);
checkArgument(resourceToAuthenticate instanceof Server, "Resource must be a server (for GoGrid)");
Server server = (Server) resourceToAuthenticate;
return client.getServerServices().getServerCredentialsList().get(
server.getName());
return new Credentials("root", null);
}
}

View File

@ -379,7 +379,7 @@ public class CloudServersComputeServiceContextModule extends CloudServersContext
}
images.add(new ImageImpl(from.getId() + "", from.getName(), location.getId(), null,
ImmutableMap.<String, String> of(), from.getName(), version, os, osDescription,
arch));
arch, new Credentials("root", null)));
}
holder.logger.debug("<< images(%d)", images.size());
return Maps.uniqueIndex(images, indexer);

View File

@ -416,7 +416,8 @@ public class RimuHostingComputeServiceContextModule extends RimuHostingContextMo
}
images.add(new ImageImpl(from.getId(), from.getDescription(), null, null, ImmutableMap
.<String, String> of(), from.getDescription(), version, os, osDescription, arch));
.<String, String> of(), from.getDescription(), version, os, osDescription, arch,
new Credentials("root", null)));
}
holder.logger.debug("<< images(%d)", images.size());
return Maps.uniqueIndex(images, indexer);

View File

@ -366,7 +366,7 @@ public class VCloudComputeServiceContextModule extends VCloudContextModule {
images.add(new ImageImpl(resource.getId(), template.getName(), vDC
.getId(), template.getLocation(), ImmutableMap
.<String, String> of(), template.getDescription(), "", myOs,
template.getName(), arch));
template.getName(), arch, new Credentials("root", null)));
return null;
}
}), executor));

View File

@ -41,6 +41,7 @@ import org.jclouds.compute.domain.internal.ImageImpl;
import org.jclouds.compute.domain.internal.SizeImpl;
import org.jclouds.compute.internal.TemplateBuilderImpl;
import org.jclouds.concurrent.ConcurrentUtils;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.vcloud.VCloudClient;
import org.jclouds.vcloud.VCloudMediaType;
@ -135,7 +136,7 @@ public class TerremarkVCloudComputeServiceContextModule extends VCloudComputeSer
images.add(new ImageImpl(resource.getId(), template.getName(), vDC
.getId(), template.getLocation(), ImmutableMap
.<String, String> of(), template.getDescription(), "", myOs,
template.getName(), arch));
template.getName(), arch, new Credentials("root", null)));
return null;
}
}), executor));