mirror of https://github.com/apache/jclouds.git
refactored IsLinkedClone
This commit is contained in:
parent
4e69321e2e
commit
4e76ad0b46
|
@ -0,0 +1,70 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds licenses this file
|
||||||
|
* to you 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.virtualbox.predicates;
|
||||||
|
|
||||||
|
import static com.google.common.base.Predicates.and;
|
||||||
|
import static com.google.common.collect.Iterables.any;
|
||||||
|
import static com.google.common.collect.Iterables.transform;
|
||||||
|
import static org.jclouds.virtualbox.predicates.IMediumPredicates.deviceTypeEquals;
|
||||||
|
import static org.jclouds.virtualbox.predicates.IMediumPredicates.hasParent;
|
||||||
|
import static org.jclouds.virtualbox.predicates.IMediumPredicates.machineIdsContain;
|
||||||
|
import static org.jclouds.virtualbox.util.IMediumAttachments.toMedium;
|
||||||
|
import static org.virtualbox_4_1.DeviceType.HardDisk;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.virtualbox_4_1.IMachine;
|
||||||
|
import org.virtualbox_4_1.IMedium;
|
||||||
|
|
||||||
|
import com.google.common.base.Predicate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
public class IMachinePredicates {
|
||||||
|
/**
|
||||||
|
* TODO: andrea please explain
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static enum IsLinkedClone implements Predicate<IMachine> {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public boolean apply(@Nullable IMachine machine) {
|
||||||
|
Iterable<IMedium> mediumsOnMachine = transform(machine.getMediumAttachments(), toMedium());
|
||||||
|
// TODO: previous impl walked the parent medium back to the child
|
||||||
|
// before checking machine ids. Determine if that extra walk was really
|
||||||
|
// necessary
|
||||||
|
return any(mediumsOnMachine, and(deviceTypeEquals(HardDisk), hasParent(), machineIdsContain(machine.getId())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "isLinkedClone()";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Predicate<IMachine> isLinkedClone() {
|
||||||
|
return IsLinkedClone.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,97 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds licenses this file
|
||||||
|
* to you 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.virtualbox.predicates;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
import static com.google.common.base.Predicates.equalTo;
|
||||||
|
import static com.google.common.collect.Iterables.any;
|
||||||
|
|
||||||
|
import org.virtualbox_4_1.DeviceType;
|
||||||
|
import org.virtualbox_4_1.IMedium;
|
||||||
|
|
||||||
|
import com.google.common.base.Predicate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
public class IMediumPredicates {
|
||||||
|
public static class DeviceTypeEquals implements Predicate<IMedium> {
|
||||||
|
private final DeviceType deviceType;
|
||||||
|
|
||||||
|
public DeviceTypeEquals(DeviceType deviceType) {
|
||||||
|
this.deviceType = checkNotNull(deviceType, "deviceType");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(IMedium arg0) {
|
||||||
|
return deviceType.equals(arg0.getDeviceType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "deviceTypeEquals(" + deviceType + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Predicate<IMedium> deviceTypeEquals(DeviceType deviceType) {
|
||||||
|
return new DeviceTypeEquals(deviceType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static enum HasParent implements Predicate<IMedium> {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(IMedium arg0) {
|
||||||
|
return arg0.getParent() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "hasParent()";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Predicate<IMedium> hasParent() {
|
||||||
|
return HasParent.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MachineIdsContain implements Predicate<IMedium> {
|
||||||
|
private final String id;
|
||||||
|
|
||||||
|
public MachineIdsContain(String id) {
|
||||||
|
this.id = checkNotNull(id, "id");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(IMedium arg0) {
|
||||||
|
return any(arg0.getMachineIds(), equalTo(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "machineIdsContain(" + id + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Predicate<IMedium> machineIdsContain(String id) {
|
||||||
|
return new MachineIdsContain(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,92 +0,0 @@
|
||||||
/**
|
|
||||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
|
||||||
* contributor license agreements. See the NOTICE file
|
|
||||||
* distributed with this work for additional information
|
|
||||||
* regarding copyright ownership. jclouds licenses this file
|
|
||||||
* to you 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.virtualbox.predicates;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Named;
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
|
|
||||||
import org.jclouds.compute.reference.ComputeServiceConstants;
|
|
||||||
import org.jclouds.logging.Logger;
|
|
||||||
import org.virtualbox_4_1.DeviceType;
|
|
||||||
import org.virtualbox_4_1.IMachine;
|
|
||||||
import org.virtualbox_4_1.IMedium;
|
|
||||||
import org.virtualbox_4_1.IMediumAttachment;
|
|
||||||
import org.virtualbox_4_1.IStorageController;
|
|
||||||
import org.virtualbox_4_1.VirtualBoxManager;
|
|
||||||
|
|
||||||
import com.google.common.base.Predicate;
|
|
||||||
import com.google.common.base.Supplier;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author Andrea Turli
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class IsLinkedClone implements Predicate<IMachine> {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
|
||||||
protected Logger logger = Logger.NULL;
|
|
||||||
|
|
||||||
private final Supplier<VirtualBoxManager> manager;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public IsLinkedClone(Supplier<VirtualBoxManager> manager) {
|
|
||||||
this.manager = manager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean apply(@Nullable IMachine machine) {
|
|
||||||
|
|
||||||
for (IStorageController iStorageController : machine
|
|
||||||
.getStorageControllers()) {
|
|
||||||
|
|
||||||
for (IMediumAttachment iMediumAttachment : machine
|
|
||||||
.getMediumAttachmentsOfController(iStorageController.getName())) {
|
|
||||||
IMedium iMedium = iMediumAttachment.getMedium();
|
|
||||||
if (iMedium.getDeviceType().equals(DeviceType.HardDisk)) {
|
|
||||||
if (iMedium.getParent() != null) {
|
|
||||||
// more than one machine is attached to this hd
|
|
||||||
for (IMedium child : iMedium.getParent().getChildren()) {
|
|
||||||
for (String machineId : child.getMachineIds()) {
|
|
||||||
IMachine iMachine = manager.get().getVBox().findMachine(
|
|
||||||
machineId);
|
|
||||||
if (!iMachine.getName().equals(machine.getName())) {
|
|
||||||
logger.debug("Machine %s is a linked clone",
|
|
||||||
machine.getName());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "IsLinkedClone()";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds licenses this file
|
||||||
|
* to you 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.virtualbox.util;
|
||||||
|
|
||||||
|
import org.virtualbox_4_1.IMedium;
|
||||||
|
import org.virtualbox_4_1.IMediumAttachment;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
public class IMediumAttachments {
|
||||||
|
static enum ToMedium implements Function<IMediumAttachment, IMedium> {
|
||||||
|
INSTANCE;
|
||||||
|
@Override
|
||||||
|
public IMedium apply(IMediumAttachment arg0) {
|
||||||
|
return arg0.getMedium();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "toMedium()";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static Function<IMediumAttachment, IMedium> toMedium() {
|
||||||
|
return ToMedium.INSTANCE;
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,7 @@
|
||||||
package org.jclouds.virtualbox.predicates;
|
package org.jclouds.virtualbox.predicates;
|
||||||
|
|
||||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
|
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
|
||||||
|
import static org.jclouds.virtualbox.predicates.IMachinePredicates.isLinkedClone;
|
||||||
import static org.testng.Assert.assertTrue;
|
import static org.testng.Assert.assertTrue;
|
||||||
|
|
||||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||||
|
@ -46,10 +47,9 @@ import com.google.inject.Injector;
|
||||||
*
|
*
|
||||||
* @author Andrea Turli
|
* @author Andrea Turli
|
||||||
*/
|
*/
|
||||||
@Test(groups = "live", singleThreaded = true, testName = "IsLinkedClonesLiveTest")
|
@Test(groups = "live", singleThreaded = true, testName = "IMachinePredicatesLiveTest")
|
||||||
public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest {
|
public class IMachinePredicatesLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
|
|
||||||
private static final boolean IS_LINKED_CLONE = true;
|
|
||||||
private String osTypeId = "";
|
private String osTypeId = "";
|
||||||
private String ideControllerName = "IDE Controller";
|
private String ideControllerName = "IDE Controller";
|
||||||
private String cloneName;
|
private String cloneName;
|
||||||
|
@ -62,11 +62,10 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
@BeforeClass(groups = "live")
|
@BeforeClass(groups = "live")
|
||||||
public void setupClient() {
|
public void setupClient() {
|
||||||
super.setupClient();
|
super.setupClient();
|
||||||
vmName = VIRTUALBOX_IMAGE_PREFIX
|
vmName = VIRTUALBOX_IMAGE_PREFIX + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
|
||||||
|
|
||||||
cloneName = VIRTUALBOX_IMAGE_PREFIX
|
cloneName = VIRTUALBOX_IMAGE_PREFIX + "Clone#"
|
||||||
+ "Clone#" + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||||
|
|
||||||
HardDisk hardDisk = HardDisk.builder().diskpath(adminDisk).autoDelete(true).controllerPort(0).deviceSlot(1)
|
HardDisk hardDisk = HardDisk.builder().diskpath(adminDisk).autoDelete(true).controllerPort(0).deviceSlot(1)
|
||||||
.build();
|
.build();
|
||||||
|
@ -89,23 +88,23 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
public void testLinkedClone() {
|
public void testLinkedClone() {
|
||||||
|
|
||||||
Injector injector = context.utils().injector();
|
Injector injector = context.utils().injector();
|
||||||
IMachine master = injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class)
|
IMachine master = injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(
|
||||||
.apply(masterMachineSpec);
|
masterMachineSpec);
|
||||||
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, cloneSpec,
|
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, cloneSpec, true)
|
||||||
IS_LINKED_CLONE).apply(master);
|
.apply(master);
|
||||||
|
|
||||||
assertTrue(new IsLinkedClone(manager).apply(clone));
|
assertTrue(isLinkedClone().apply(clone));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
public void testFullClone() {
|
* public void testFullClone() { IMachine master =
|
||||||
IMachine master = context.utils().injector().getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class)
|
* context.utils().injector().
|
||||||
.apply(masterSpec);
|
* getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class)
|
||||||
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, cloneSpec,
|
* .apply(masterSpec); IMachine clone = new
|
||||||
!IS_LINKED_CLONE).apply(master);
|
* CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir,
|
||||||
|
* cloneSpec, !IS_LINKED_CLONE).apply(master);
|
||||||
assertFalse(new IsLinkedClone(manager).apply(clone));
|
*
|
||||||
}
|
* assertFalse(new IsLinkedClone(manager).apply(clone)); }
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@BeforeMethod
|
@BeforeMethod
|
Loading…
Reference in New Issue