Issue 539:expose templatebuilder parameter to prefer s3-backed images

This commit is contained in:
Adrian Cole 2011-08-04 11:25:49 +02:00
parent 8a2cbe1be5
commit 2b28c04c0f
7 changed files with 196 additions and 6 deletions

View File

@ -0,0 +1,52 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.ec2.compute.predicates;
import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.predicates.ImagePredicates;
import org.jclouds.ec2.domain.RootDeviceType;
import com.google.common.base.Predicate;
/**
* Container for image filters (predicates).
*
* This class has static methods that create customized predicates to use with
* {@link org.jclouds.compute.ComputeService}.
*
* @author Adrian Cole
*/
public class EC2ImagePredicates {
/**
* evaluates true if the Image has the specified root device type
*
* @param rootDeviceType
* rootDeviceType of the images
* @return predicate
*/
public static Predicate<Image> rootDeviceType(final RootDeviceType rootDeviceType) {
checkNotNull(rootDeviceType, "rootDeviceType must be defined");
return ImagePredicates.userMetadataContains("rootDeviceType", rootDeviceType.value());
}
}

View File

@ -23,6 +23,7 @@ import java.util.NoSuchElementException;
import org.jclouds.compute.domain.internal.TemplateBuilderImpl;
import org.jclouds.compute.options.TemplateOptions;
import com.google.common.base.Predicate;
import com.google.inject.ImplementedBy;
/**
@ -135,6 +136,11 @@ public interface TemplateBuilder {
*/
TemplateBuilder imageDescriptionMatches(String imageDescriptionRegex);
/**
* Configure this template to have an image description that matches the supplied condition
*/
TemplateBuilder imageMatches(Predicate<Image> condition);
/**
* Configure this template to require the minimum cores below
*/

View File

@ -106,6 +106,8 @@ public class TemplateBuilderImpl implements TemplateBuilder {
@VisibleForTesting
protected String imageDescription;
@VisibleForTesting
protected Predicate<Image> imagePredicate;
@VisibleForTesting
protected double minCores;
@VisibleForTesting
protected int minRam;
@ -719,6 +721,8 @@ public class TemplateBuilderImpl implements TemplateBuilder {
predicates.add(imageNamePredicate);
if (imageDescription != null)
predicates.add(imageDescriptionPredicate);
if (imagePredicate != null)
predicates.add(imagePredicate);
}
// looks verbose, but explicit <Image> type needed for this to compile
@ -736,6 +740,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
this.imageId = imageId;
this.imageName = null;
this.imageDescription = null;
this.imagePredicate = null;
this.imageVersion = null;
this.osFamily = null;
this.osName = null;
@ -763,6 +768,15 @@ public class TemplateBuilderImpl implements TemplateBuilder {
this.imageDescription = descriptionRegex;
return this;
}
/**
* {@inheritDoc}
*/
@Override
public TemplateBuilder imageMatches(Predicate<Image> condition) {
this.imagePredicate = condition;
return this;
}
/**
* {@inheritDoc}
@ -849,8 +863,9 @@ public class TemplateBuilderImpl implements TemplateBuilder {
@VisibleForTesting
boolean nothingChangedExceptOptions() {
return osFamily == null && location == null && imageId == null && hardwareId == null && osName == null
&& osDescription == null && imageVersion == null && osVersion == null && osArch == null && os64Bit == null
&& imageName == null && imageDescription == null && minCores == 0 && minRam == 0 && !biggest && !fastest;
&& imagePredicate == null && osDescription == null && imageVersion == null && osVersion == null
&& osArch == null && os64Bit == null && imageName == null && imageDescription == null && minCores == 0
&& minRam == 0 && !biggest && !fastest;
}
/**
@ -864,7 +879,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
@Override
public String toString() {
return "[biggest=" + biggest + ", fastest=" + fastest + ", imageName=" + imageName + ", imageDescription="
+ imageDescription + ", imageId=" + imageId + ", imageVersion=" + imageVersion + ", location=" + location
+ imageDescription + ", imageId=" + imageId + ", imagePredicate=" + imagePredicate + ", imageVersion=" + imageVersion + ", location=" + location
+ ", minCores=" + minCores + ", minRam=" + minRam + ", osFamily=" + osFamily + ", osName=" + osName
+ ", osDescription=" + osDescription + ", osVersion=" + osVersion + ", osArch=" + osArch + ", os64Bit="
+ os64Bit + ", hardwareId=" + hardwareId + "]";

View File

@ -58,7 +58,7 @@ public class ImagePredicates {
}
/**
* evaluates true if the Image
* evaluates true if the Image id is in the supplied set
*
* @param ids
* ids of the images
@ -79,6 +79,31 @@ public class ImagePredicates {
}
};
}
/**
* evaluates true if the Image metadata contains the following values
*
* @param key
* key in Image#getUserMetadata
* @param value
* value in Image#getUserMetadata
* @return predicate
*/
public static Predicate<Image> userMetadataContains(final String key, final String value) {
checkNotNull(key, "key must be defined");
checkNotNull(value, "value must be defined");
return new Predicate<Image>() {
@Override
public boolean apply(Image image) {
return value.equals(image.getUserMetadata().get(key));
}
@Override
public String toString() {
return "metadataContains(" + key +", "+value + ")";
}
};
}
/**
* evaluates true if the Image

View File

@ -278,7 +278,7 @@ public class TemplateBuilderImplTest {
// make sure big data is not in the exception message
assertEquals(
e.getMessage(),
"no hardware profiles support images matching params: [biggest=false, fastest=false, imageName=null, imageDescription=null, imageId=notImageId, imageVersion=null, location=EasyMock for interface org.jclouds.domain.Location, minCores=0.0, minRam=0, osFamily=null, osName=null, osDescription=null, osVersion=null, osArch=null, os64Bit=false, hardwareId=null]");
"no hardware profiles support images matching params: [biggest=false, fastest=false, imageName=null, imageDescription=null, imageId=notImageId, imagePredicate=null, imageVersion=null, location=EasyMock for interface org.jclouds.domain.Location, minCores=0.0, minRam=0, osFamily=null, osName=null, osDescription=null, osVersion=null, osArch=null, os64Bit=false, hardwareId=null]");
verify(image);
verify(os);
verify(defaultTemplate);

View File

@ -0,0 +1,56 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.compute.predicates;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
/**
* Tests possible uses of OperatingSystemPredicates
*
* @author Adrian Cole
*/
@Test
public class ImagePredicatesTest {
ComputeService computeService = new ComputeServiceContextFactory().createContext("stub", "foo", "bar")
.getComputeService();
public void testImageId() {
Image first = Iterables.get(computeService.listImages(), 0);
assert ImagePredicates.idEquals(first.getId()).apply(first);
Image second = Iterables.get(computeService.listImages(), 1);
assert !ImagePredicates.idEquals(first.getId()).apply(second);
}
public void testUserMetadataContains() {
Image first = Iterables.get(computeService.listImages(), 0);
first = ImageBuilder.fromImage(first).userMetadata(ImmutableMap.of("foo", "bar")).build();
assert ImagePredicates.userMetadataContains("foo", "bar").apply(first);
Image second = Iterables.get(computeService.listImages(), 1);
second = ImageBuilder.fromImage(second).userMetadata(ImmutableMap.of("foo", "baz")).build();
assert !ImagePredicates.userMetadataContains("foo", "bar").apply(second);
}
}

View File

@ -34,7 +34,9 @@ import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.OsFamilyVersion64Bit;
import org.jclouds.compute.domain.Template;
import org.jclouds.ec2.compute.predicates.EC2ImagePredicates;
import org.jclouds.ec2.domain.InstanceType;
import org.jclouds.ec2.domain.RootDeviceType;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.testng.annotations.Test;
@ -85,7 +87,7 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
.build();
assert (template.getImage().getProviderId().startsWith("ami-")) : template;
assertEquals(template.getImage().getOperatingSystem().getVersion(), "10.04");
assertEquals(template.getImage().getOperatingSystem().getVersion(), "11.10");
assertEquals(template.getImage().getOperatingSystem().is64Bit(), false);
assertEquals(template.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
assertEquals(template.getImage().getUserMetadata().get("rootDeviceType"), "instance-store");
@ -95,6 +97,24 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
assertEquals(template.getImage().getOperatingSystem().getArch(), "paravirtual");
}
@Test
public void testUbuntuInstanceStoreGoesM1Small() {
Template template = context.getComputeService().templateBuilder()
.imageMatches(EC2ImagePredicates.rootDeviceType(RootDeviceType.INSTANCE_STORE))
.osVersionMatches("1[10].[10][04]").imageDescriptionMatches("ubuntu-images").osFamily(OsFamily.UBUNTU)
.build();
assert (template.getImage().getProviderId().startsWith("ami-")) : template;
assertEquals(template.getImage().getOperatingSystem().getVersion(), "11.10");
assertEquals(template.getImage().getOperatingSystem().is64Bit(), false);
assertEquals(template.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
assertEquals(template.getImage().getUserMetadata().get("rootDeviceType"), "instance-store");
assertEquals(template.getLocation().getId(), "us-east-1");
assertEquals(getCores(template.getHardware()), 1.0d);
assertEquals(template.getHardware().getId(), InstanceType.M1_SMALL);
assertEquals(template.getImage().getOperatingSystem().getArch(), "paravirtual");
}
@Test
public void testTemplateBuilderCanUseImageIdAndhardwareIdAndAZ() {
@ -129,6 +149,22 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
assertEquals(defaultTemplate.getImage().getOperatingSystem().getArch(), "paravirtual");
}
@Test
public void testAmazonLinuxInstanceStore() throws IOException {
Template defaultTemplate = context.getComputeService().templateBuilder().osFamily(OsFamily.AMZN_LINUX)
.imageMatches(EC2ImagePredicates.rootDeviceType(RootDeviceType.INSTANCE_STORE)).build();
assert (defaultTemplate.getImage().getProviderId().startsWith("ami-")) : defaultTemplate;
assertEquals(defaultTemplate.getImage().getOperatingSystem().getVersion(), "2011.02.1");
assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), false);
assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.AMZN_LINUX);
assertEquals(defaultTemplate.getImage().getUserMetadata().get("rootDeviceType"), "instance-store");
assertEquals(defaultTemplate.getLocation().getId(), "us-east-1");
assertEquals(getCores(defaultTemplate.getHardware()), 1.0d);
assertEquals(defaultTemplate.getImage().getOperatingSystem().getArch(), "paravirtual");
}
@Test
public void testFastestTemplateBuilder() throws IOException {
Template fastestTemplate = context.getComputeService().templateBuilder().fastest().osFamily(OsFamily.AMZN_LINUX)