Search in sources :

Example 1 with IPortForwardable

use of com.openshift.restclient.capability.resources.IPortForwardable in project jbosstools-openshift by jbosstools.

the class PortForwardingWizardModel method startPortForwarding.

/**
 * Starts the port-forwarding for the current pod.
 */
public void startPortForwarding() {
    final MessageConsole console = ConsoleUtils.findMessageConsole(getMessageConsoleName());
    try (MessageConsoleStream stream = console.newMessageStream()) {
        ConsoleUtils.registerConsoleListener(consoleListener);
        ConsoleUtils.displayConsoleView(console);
        stream.println("Starting port-forwarding...");
        final IPortForwardable portForwardable = PortForwardingUtils.startPortForwarding(this.pod, this.ports, IBinaryCapability.SKIP_TLS_VERIFY);
        portForwardable.getPortPairs().stream().forEach(port -> stream.println(NLS.bind("{0} {1} -> {2}", new Object[] { port.getName(), port.getLocalPort(), port.getRemotePort() })));
        stream.println("done.");
        firePropertyChange(PROPERTY_PORT_FORWARDING, false, getPortForwarding());
        updatePortForwardingAllowed();
    } catch (IOException e) {
        OpenShiftUIActivator.getDefault().getLogger().logError("Error while closing the console inputstream", e);
    }
}
Also used : IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable) MessageConsole(org.eclipse.ui.console.MessageConsole) MessageConsoleStream(org.eclipse.ui.console.MessageConsoleStream) IOException(java.io.IOException)

Example 2 with IPortForwardable

use of com.openshift.restclient.capability.resources.IPortForwardable in project jbosstools-openshift by jbosstools.

the class PortForwardingUtils method stopPortForwarding.

/**
 * Stops all port-forwarding for the given {@code pod}
 *
 * @param pod
 *            the pod on which port-forwarding is to be stopped
 * @param stream the MessageConsoleStream to use to print messages
 * @return the {@link IPortForwardable} referencing all ports that were
 *         forwarded, or <code>null</code> if port-forwarding was not already
 *         started on the given pod.
 * @throws IOException when writing into the given {@code stream} fails
 */
public static IPortForwardable stopPortForwarding(final IPod pod, final OutputStream stream) throws IOException {
    if (!PortForwardingUtils.isPortForwardingStarted(pod)) {
        return null;
    }
    final IPortForwardable portForwarding = REGISTRY.remove(pod);
    if (portForwarding != null) {
        portForwarding.stop();
    }
    waitForPortsToGetFree(portForwarding.getPortPairs(), 5, stream);
    return portForwarding;
}
Also used : IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable)

Example 3 with IPortForwardable

use of com.openshift.restclient.capability.resources.IPortForwardable in project jbosstools-openshift by jbosstools.

the class PortForwardingUtilsTest method shouldStartPortForwardingOnSinglePort.

@Test
@SuppressWarnings("unchecked")
public void shouldStartPortForwardingOnSinglePort() {
    // given
    final IPod pod = Mockito.mock(IPod.class);
    final PortPair port = Mockito.mock(PortPair.class);
    final IPortForwardable portForwardable = Mockito.mock(IPortForwardable.class);
    Mockito.when(pod.accept(Mockito.any(CapabilityVisitor.class), Mockito.any(IPortForwardable.class))).thenReturn(portForwardable);
    Mockito.when(portForwardable.isForwarding()).thenReturn(true);
    // when
    final IPortForwardable startPortForwarding = PortForwardingUtils.startPortForwarding(pod, port);
    // then
    assertThat(startPortForwarding).isNotNull().isEqualTo(portForwardable);
    assertThat(PortForwardingUtils.isPortForwardingStarted(pod)).isTrue();
}
Also used : IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable) PortPair(com.openshift.restclient.capability.resources.IPortForwardable.PortPair) CapabilityVisitor(com.openshift.restclient.capability.CapabilityVisitor) IPod(com.openshift.restclient.model.IPod) Test(org.junit.Test)

Example 4 with IPortForwardable

use of com.openshift.restclient.capability.resources.IPortForwardable in project jbosstools-openshift by jbosstools.

the class PortForwardingUtilsTest method shouldGetForwardablePortsOnStartedStopped.

@Test
public void shouldGetForwardablePortsOnStartedStopped() {
    // given
    final IPod pod = Mockito.mock(IPod.class);
    final IPort port = Mockito.mock(IPort.class);
    final IPortForwardable portForwardable = Mockito.mock(IPortForwardable.class);
    Mockito.when(pod.getContainerPorts()).thenReturn(toSet(port));
    // when
    final Set<PortPair> forwardablePorts = PortForwardingUtils.getForwardablePorts(pod);
    // then
    assertThat(forwardablePorts).isNotNull().hasSize(1);
}
Also used : IPort(com.openshift.restclient.model.IPort) IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable) PortPair(com.openshift.restclient.capability.resources.IPortForwardable.PortPair) IPod(com.openshift.restclient.model.IPod) Test(org.junit.Test)

Example 5 with IPortForwardable

use of com.openshift.restclient.capability.resources.IPortForwardable in project jbosstools-openshift by jbosstools.

the class PortForwardingWizardModel method stopPortForwarding.

/**
 * Stops the port-forwarding for the current pod.
 * @throws IOException
 */
public void stopPortForwarding() throws IOException {
    if (!PortForwardingUtils.isPortForwardingStarted(pod)) {
        return;
    }
    final MessageConsole console = ConsoleUtils.findMessageConsole(getMessageConsoleName());
    try (final MessageConsoleStream stream = console.newMessageStream()) {
        stream.println("Stopping port-forwarding...");
        final IPortForwardable cap = PortForwardingUtils.stopPortForwarding(pod, stream);
        if (cap != null) {
            cap.getPortPairs().stream().forEach(port -> stream.println(NLS.bind("{0} {1} -> {2}", new Object[] { port.getName(), port.getLocalPort(), port.getRemotePort() })));
        }
        if (!PortForwardingUtils.hasPortInUse(this.ports)) {
            stream.println("done.");
        } else {
            stream.println("Ports remain in use yet. Stopping ports is requested and eventually will be completed.");
        }
        ConsoleUtils.displayConsoleView(console);
        firePropertyChange(PROPERTY_PORT_FORWARDING, true, getPortForwarding());
        updatePortForwardingAllowed();
    } finally {
        ConsoleUtils.deregisterConsoleListener(consoleListener);
    }
}
Also used : IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable) MessageConsole(org.eclipse.ui.console.MessageConsole) MessageConsoleStream(org.eclipse.ui.console.MessageConsoleStream)

Aggregations

IPortForwardable (com.openshift.restclient.capability.resources.IPortForwardable)8 PortPair (com.openshift.restclient.capability.resources.IPortForwardable.PortPair)5 IPod (com.openshift.restclient.model.IPod)5 CapabilityVisitor (com.openshift.restclient.capability.CapabilityVisitor)4 Test (org.junit.Test)4 IOException (java.io.IOException)2 MessageConsole (org.eclipse.ui.console.MessageConsole)2 MessageConsoleStream (org.eclipse.ui.console.MessageConsoleStream)2 OpenShiftBinaryOption (com.openshift.restclient.capability.IBinaryCapability.OpenShiftBinaryOption)1 IPort (com.openshift.restclient.model.IPort)1 OutputStream (java.io.OutputStream)1 ServerSocket (java.net.ServerSocket)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 MultiStatus (org.eclipse.core.runtime.MultiStatus)1