Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ public final class DockerConstants {
public static final String DOCKER_PUBLISH_ALL_PORTS = "CamelDockerPublishAllPorts";
public static final String DOCKER_RESTART_POLICY = "CamelDockerRestartPolicy";

/**
* Create Network *
* Attach to Network *
* Remove Network *
*/
public static final String DOCKER_NETWORK = "CamelDockerNetwork";

/**
* Exec *
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ public enum DockerOperation {
DockerConstants.DOCKER_PS_ARGS, String.class),
UNPAUSE_CONTAINER("containerunpause", false, true, false,
DockerConstants.DOCKER_CONTAINER_ID, String.class),
CREATE_NETWORK("networkcreate", false, true, false,
DockerConstants.DOCKER_NETWORK, String.class),
REMOVE_NETWORK("networkremove", false, true, false,
DockerConstants.DOCKER_NETWORK, String.class),
CONNECT_NETWORK("networkconnect", false, true, false,
DockerConstants.DOCKER_NETWORK, String.class,
DockerConstants.DOCKER_CONTAINER_ID, String.class),
WAIT_CONTAINER("containerwait", false, true, true,
DockerConstants.DOCKER_CONTAINER_ID, String.class),

Expand Down Expand Up @@ -268,4 +275,4 @@ public static DockerOperation getDockerOperation(String name) {
return null;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.AuthCmd;
import com.github.dockerjava.api.command.CommitCmd;
import com.github.dockerjava.api.command.ConnectToNetworkCmd;
import com.github.dockerjava.api.command.ContainerDiffCmd;
import com.github.dockerjava.api.command.CopyArchiveFromContainerCmd;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.CreateImageCmd;
import com.github.dockerjava.api.command.CreateNetworkCmd;
import com.github.dockerjava.api.command.ExecCreateCmd;
import com.github.dockerjava.api.command.InfoCmd;
import com.github.dockerjava.api.command.InspectContainerCmd;
Expand All @@ -36,6 +38,7 @@
import com.github.dockerjava.api.command.PingCmd;
import com.github.dockerjava.api.command.RemoveContainerCmd;
import com.github.dockerjava.api.command.RemoveImageCmd;
import com.github.dockerjava.api.command.RemoveNetworkCmd;
import com.github.dockerjava.api.command.RestartContainerCmd;
import com.github.dockerjava.api.command.SearchImagesCmd;
import com.github.dockerjava.api.command.StartContainerCmd;
Expand All @@ -49,6 +52,7 @@
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.ExposedPorts;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Mount;
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.api.model.Volumes;
import com.github.dockerjava.api.model.VolumesFrom;
Expand Down Expand Up @@ -167,6 +171,15 @@ public void process(Exchange exchange) throws Exception {
case UNPAUSE_CONTAINER:
result = executeUnpauseContainerRequest(client, message).exec();
break;
case CREATE_NETWORK:
result = executeCreateNetworkRequest(client, message).exec();
break;
case REMOVE_NETWORK:
result = executeRemoveNetworkRequest(client, message).exec();
break;
case CONNECT_NETWORK:
result = executeConnectToNetworkRequest(client, message).exec();
break;
case EXEC_CREATE:
result = executeExecCreateRequest(client, message).exec();
break;
Expand Down Expand Up @@ -1086,6 +1099,71 @@ private UnpauseContainerCmd executeUnpauseContainerRequest(DockerClient client,

}

/**
* Produces a network create request
*
* @param client
* @param message
* @return
*/
private CreateNetworkCmd executeCreateNetworkRequest(DockerClient client, Message message) {

LOGGER.debug("Executing Docker Network Create Request");

String networkName = DockerHelper.getProperty(DockerConstants.DOCKER_NETWORK, configuration, message, String.class);

ObjectHelper.notNull(networkName, "Network Name must be specified");

CreateNetworkCmd createNetworkCmd = client.createNetworkCmd().withName(networkName);

return createNetworkCmd;

}

/**
* Produces a network remove request
*
* @param client
* @param message
* @return
*/
private RemoveNetworkCmd executeRemoveNetworkRequest(DockerClient client, Message message) {

LOGGER.debug("Executing Docker Network Remove Request");

String networkId = DockerHelper.getProperty(DockerConstants.DOCKER_NETWORK, configuration, message, String.class);

ObjectHelper.notNull(networkId, "Network ID must be specified");

RemoveNetworkCmd removeNetworkCmd = client.removeNetworkCmd(networkId);

return removeNetworkCmd;

}

/**
* Produces a network connect request
*
* @param client
* @param message
* @return
*/
private ConnectToNetworkCmd executeConnectToNetworkRequest(DockerClient client, Message message) {

LOGGER.debug("Executing Docker Network Connect Request");

String networkId = DockerHelper.getProperty(DockerConstants.DOCKER_NETWORK, configuration, message, String.class);
String containerId = DockerHelper.getProperty(DockerConstants.DOCKER_CONTAINER_ID, configuration, message, String.class);

ObjectHelper.notNull(networkId, "Network ID must be specified");
ObjectHelper.notNull(containerId, "Container ID must be specified");

ConnectToNetworkCmd connectToNetworkCmd = client.connectToNetworkCmd().withNetworkId(networkId).withContainerId(containerId);

return connectToNetworkCmd;

}

/*********************
* Exec Requests
********************/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.camel.component.docker.headers;

import com.github.dockerjava.api.command.ConnectToNetworkCmd;
import com.github.dockerjava.api.model.ExposedPort;
import org.apache.camel.component.docker.DockerConstants;
import org.apache.camel.component.docker.DockerOperation;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.util.Map;

import static org.mockito.ArgumentMatchers.anyString;

/**
* Validates Connect To Network Request headers are applied properly
*/
public class ConnectToNetworkCmdHeaderTest extends BaseDockerHeaderTest<ConnectToNetworkCmd> {

@Mock
ExposedPort exposedPort;

@Mock
private ConnectToNetworkCmd mockObject;

@Test
public void createNetworkHeaderTest() {

String networkId = "ID1234";
String containerId = "CID1234";

Map<String, Object> headers = getDefaultParameters();
headers.put(DockerConstants.DOCKER_NETWORK, networkId);
headers.put(DockerConstants.DOCKER_CONTAINER_ID, containerId);

template.sendBodyAndHeaders("direct:in", "", headers);

Mockito.verify(dockerClient.connectToNetworkCmd(), Mockito.times(1)).withContainerId(containerId);
Mockito.verify(dockerClient.connectToNetworkCmd(), Mockito.times(1)).withNetworkId(networkId);

}

@Override
protected void setupMocks() {
Mockito.when(dockerClient.connectToNetworkCmd()).thenReturn(mockObject);
Mockito.when(mockObject.withContainerId(anyString())).thenReturn(mockObject);
Mockito.when(mockObject.withNetworkId(anyString())).thenReturn(mockObject);
}

@Override
protected DockerOperation getOperation() {
return DockerOperation.CONNECT_NETWORK;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.camel.component.docker.headers;

import com.github.dockerjava.api.command.CreateNetworkCmd;
import com.github.dockerjava.api.model.ExposedPort;
import org.apache.camel.component.docker.DockerConstants;
import org.apache.camel.component.docker.DockerOperation;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.util.Map;

import static org.mockito.ArgumentMatchers.anyString;

/**
* Validates Create Network Request headers are applied properly
*/
public class CreateNetworkCmdHeaderTest extends BaseDockerHeaderTest<CreateNetworkCmd> {

@Mock
ExposedPort exposedPort;

@Mock
private CreateNetworkCmd mockObject;

@Test
public void createNetworkHeaderTest() {

String networkName = "TestNetwork";

Map<String, Object> headers = getDefaultParameters();
headers.put(DockerConstants.DOCKER_NETWORK, networkName);

template.sendBodyAndHeaders("direct:in", "", headers);

Mockito.verify(dockerClient.createNetworkCmd(), Mockito.times(1)).withName(networkName);

}

@Override
protected void setupMocks() {
Mockito.when(dockerClient.createNetworkCmd()).thenReturn(mockObject);
Mockito.when(mockObject.withName(anyString())).thenReturn(mockObject);
}

@Override
protected DockerOperation getOperation() {
return DockerOperation.CREATE_NETWORK;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.camel.component.docker.headers;

import com.github.dockerjava.api.command.RemoveNetworkCmd;
import com.github.dockerjava.api.model.ExposedPort;
import org.apache.camel.component.docker.DockerConstants;
import org.apache.camel.component.docker.DockerOperation;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.util.Map;

import static org.mockito.ArgumentMatchers.anyString;

/**
* Validates Remove Network Request headers are applied properly
*/
public class RemoveNetworkCmdHeaderTest extends BaseDockerHeaderTest<RemoveNetworkCmd> {

@Mock
ExposedPort exposedPort;

@Mock
private RemoveNetworkCmd mockObject;

@Test
public void createNetworkHeaderTest() {

String networkId = "ID1234";

Map<String, Object> headers = getDefaultParameters();
headers.put(DockerConstants.DOCKER_NETWORK, networkId);

template.sendBodyAndHeaders("direct:in", "", headers);

Mockito.verify(dockerClient, Mockito.times(1)).removeNetworkCmd(networkId);

}

@Override
protected void setupMocks() {
Mockito.when(dockerClient.removeNetworkCmd(anyString())).thenReturn(mockObject);
}

@Override
protected DockerOperation getOperation() {
return DockerOperation.REMOVE_NETWORK;
}

}