Added RedirectRule to wrap parameters for AttachNATRedirectRuleToMachine.

This commit is contained in:
Mattias Holmqvist 2011-11-27 14:26:36 +01:00
parent a006d63b44
commit ebe75e3ff2
4 changed files with 78 additions and 5 deletions

View File

@ -0,0 +1,62 @@
/*
* 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.domain;
import org.virtualbox_4_1.NATProtocol;
/**
* @author Mattias Holmqvist
*/
public class RedirectRule {
private final NATProtocol protocol;
private final String host;
private final int hostPort;
private final String guest;
private final int guestPort;
public RedirectRule(NATProtocol protocol, String host, int hostPort, String guest, int guestPort) {
this.protocol = protocol;
this.host = host;
this.hostPort = hostPort;
this.guest = guest;
this.guestPort = guestPort;
}
public NATProtocol getProtocol() {
return protocol;
}
public String getHost() {
return host;
}
public int getHostPort() {
return hostPort;
}
public String getGuest() {
return guest;
}
public int getGuestPort() {
return guestPort;
}
}

View File

@ -24,6 +24,7 @@ import static org.virtualbox_4_1.NetworkAttachmentType.NAT;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.jclouds.virtualbox.domain.RedirectRule;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.INetworkAdapter; import org.virtualbox_4_1.INetworkAdapter;
@ -35,16 +36,23 @@ import com.google.common.base.Function;
public class AttachNATRedirectRuleToMachine implements Function<IMachine, Void> { public class AttachNATRedirectRuleToMachine implements Function<IMachine, Void> {
private long adapterIndex; private long adapterIndex;
private RedirectRule redirectRule;
public AttachNATRedirectRuleToMachine(long adapterSlot) { public AttachNATRedirectRuleToMachine(long adapterSlot, RedirectRule redirectRule) {
this.adapterIndex = adapterSlot; this.adapterIndex = adapterSlot;
this.redirectRule = redirectRule;
} }
@Override @Override
public Void apply(@Nullable IMachine machine) { public Void apply(@Nullable IMachine machine) {
INetworkAdapter networkAdapter = machine.getNetworkAdapter(adapterIndex); INetworkAdapter networkAdapter = machine.getNetworkAdapter(adapterIndex);
networkAdapter.setAttachmentType(NAT); networkAdapter.setAttachmentType(NAT);
networkAdapter.getNatDriver().addRedirect("guestssh", TCP, "127.0.0.1", 2222, "", 22); networkAdapter.getNatDriver().addRedirect("guestssh",
redirectRule.getProtocol(),
redirectRule.getHost(),
redirectRule.getHostPort(),
redirectRule.getGuest(),
redirectRule.getGuestPort());
networkAdapter.setEnabled(true); networkAdapter.setEnabled(true);
machine.saveSettings(); machine.saveSettings();
return null; return null;

View File

@ -31,6 +31,7 @@ import org.jclouds.logging.Logger;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.ssh.SshException; import org.jclouds.ssh.SshException;
import org.jclouds.virtualbox.domain.ExecutionType; import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.domain.RedirectRule;
import org.jclouds.virtualbox.settings.KeyboardScancodes; import org.jclouds.virtualbox.settings.KeyboardScancodes;
import org.virtualbox_4_1.*; import org.virtualbox_4_1.*;
@ -49,6 +50,7 @@ import static org.virtualbox_4_1.DeviceType.DVD;
import static org.virtualbox_4_1.DeviceType.HardDisk; import static org.virtualbox_4_1.DeviceType.HardDisk;
import static org.virtualbox_4_1.LockType.Shared; import static org.virtualbox_4_1.LockType.Shared;
import static org.virtualbox_4_1.LockType.Write; import static org.virtualbox_4_1.LockType.Write;
import static org.virtualbox_4_1.NATProtocol.TCP;
public class IsoToIMachine implements Function<String, IMachine> { public class IsoToIMachine implements Function<String, IMachine> {
@ -197,7 +199,7 @@ public class IsoToIMachine implements Function<String, IMachine> {
} }
private void ensureNATNetworkingIsAppliedToMachine(String vmName) { private void ensureNATNetworkingIsAppliedToMachine(String vmName) {
lockMachineAndApply(manager, Write, vmName, new AttachNATRedirectRuleToMachine(0l)); lockMachineAndApply(manager, Write, vmName, new AttachNATRedirectRuleToMachine(0l, new RedirectRule(TCP, "127.0.0.1", 2222, "", 22)));
} }
private void ensureMachineHasAttachedDistroMedium(String isoName, String workingDir, String controllerIDE) { private void ensureMachineHasAttachedDistroMedium(String isoName, String workingDir, String controllerIDE) {

View File

@ -27,6 +27,7 @@ import static org.easymock.classextension.EasyMock.verify;
import static org.virtualbox_4_1.NATProtocol.TCP; import static org.virtualbox_4_1.NATProtocol.TCP;
import static org.virtualbox_4_1.NetworkAttachmentType.NAT; import static org.virtualbox_4_1.NetworkAttachmentType.NAT;
import org.jclouds.virtualbox.domain.RedirectRule;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.INATEngine; import org.virtualbox_4_1.INATEngine;
@ -55,7 +56,7 @@ public class AttachNATRedirectRuleToMachineTest {
replay(machine, networkAdapter, natEngine); replay(machine, networkAdapter, natEngine);
new AttachNATRedirectRuleToMachine(adapterId).apply(machine); new AttachNATRedirectRuleToMachine(adapterId, new RedirectRule(TCP, "127.0.0.1", 2222, "", 22)).apply(machine);
verify(machine, networkAdapter, natEngine); verify(machine, networkAdapter, natEngine);
} }
@ -75,7 +76,7 @@ public class AttachNATRedirectRuleToMachineTest {
replay(machine, networkAdapter, natEngine); replay(machine, networkAdapter, natEngine);
new AttachNATRedirectRuleToMachine(adapterId).apply(machine); new AttachNATRedirectRuleToMachine(adapterId, new RedirectRule(TCP, "127.0.0.1", 2222, "", 22)).apply(machine);
verify(machine, networkAdapter, natEngine); verify(machine, networkAdapter, natEngine);
} }