diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/RedirectRule.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/RedirectRule.java new file mode 100644 index 0000000000..7444d0124c --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/RedirectRule.java @@ -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; + } +} \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNATRedirectRuleToMachine.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNATRedirectRuleToMachine.java index 34c3e7130f..578fe38034 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNATRedirectRuleToMachine.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNATRedirectRuleToMachine.java @@ -24,6 +24,7 @@ import static org.virtualbox_4_1.NetworkAttachmentType.NAT; import javax.annotation.Nullable; +import org.jclouds.virtualbox.domain.RedirectRule; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.INetworkAdapter; @@ -35,16 +36,23 @@ import com.google.common.base.Function; public class AttachNATRedirectRuleToMachine implements Function { private long adapterIndex; + private RedirectRule redirectRule; - public AttachNATRedirectRuleToMachine(long adapterSlot) { + public AttachNATRedirectRuleToMachine(long adapterSlot, RedirectRule redirectRule) { this.adapterIndex = adapterSlot; + this.redirectRule = redirectRule; } @Override public Void apply(@Nullable IMachine machine) { INetworkAdapter networkAdapter = machine.getNetworkAdapter(adapterIndex); 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); machine.saveSettings(); return null; diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java index 3714284053..1fa341b379 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java @@ -31,6 +31,7 @@ import org.jclouds.logging.Logger; import org.jclouds.net.IPSocket; import org.jclouds.ssh.SshException; import org.jclouds.virtualbox.domain.ExecutionType; +import org.jclouds.virtualbox.domain.RedirectRule; import org.jclouds.virtualbox.settings.KeyboardScancodes; 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.LockType.Shared; import static org.virtualbox_4_1.LockType.Write; +import static org.virtualbox_4_1.NATProtocol.TCP; public class IsoToIMachine implements Function { @@ -197,7 +199,7 @@ public class IsoToIMachine implements Function { } 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) { diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachNATRedirectRuleToMachineTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachNATRedirectRuleToMachineTest.java index 22321e9aad..ce571877f2 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachNATRedirectRuleToMachineTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachNATRedirectRuleToMachineTest.java @@ -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.NetworkAttachmentType.NAT; +import org.jclouds.virtualbox.domain.RedirectRule; import org.testng.annotations.Test; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.INATEngine; @@ -55,7 +56,7 @@ public class AttachNATRedirectRuleToMachineTest { 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); } @@ -75,7 +76,7 @@ public class AttachNATRedirectRuleToMachineTest { 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); }