-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement container rename api #504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.github.dockerjava.api.command; | ||
|
|
||
| import com.github.dockerjava.api.exception.NotFoundException; | ||
| import com.github.dockerjava.core.RemoteApiVersion; | ||
|
|
||
| import javax.annotation.CheckForNull; | ||
| import javax.annotation.Nonnull; | ||
|
|
||
| /** | ||
| * rename a container. | ||
| * | ||
| * @since {@link RemoteApiVersion#VERSION_1_22} | ||
| */ | ||
| public interface RenameContainerCmd extends SyncDockerCmd<Void> { | ||
|
|
||
| @CheckForNull | ||
| String getContainerId(); | ||
|
|
||
| RenameContainerCmd withContainerId(@Nonnull String containerId); | ||
|
|
||
| @CheckForNull | ||
| String getName(); | ||
|
|
||
| RenameContainerCmd withName(@Nonnull String name); | ||
|
|
||
|
|
||
| /** | ||
| * @throws com.github.dockerjava.api.exception.NotFoundException No such container | ||
| */ | ||
| @Override | ||
| Void exec() throws NotFoundException; | ||
|
|
||
| interface Exec extends DockerCmdSyncExec<RenameContainerCmd, Void> { | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.github.dockerjava.core.command; | ||
|
|
||
| import com.github.dockerjava.api.command.RenameContainerCmd; | ||
| import com.github.dockerjava.api.exception.NotFoundException; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| /** | ||
| * rename a container. | ||
| */ | ||
| public class RenameContainerCmdImpl extends AbstrDockerCmd<RenameContainerCmd, Void> implements RenameContainerCmd { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one space between |
||
|
|
||
| private String containerId; | ||
|
|
||
| private String name; | ||
|
|
||
| public RenameContainerCmdImpl(RenameContainerCmd.Exec exec, String containerId) { | ||
| super(exec); | ||
| withContainerId(containerId); | ||
| } | ||
|
|
||
| @Override | ||
| public String getContainerId() { | ||
| return containerId; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public RenameContainerCmd withContainerId(String containerId) { | ||
| checkNotNull(containerId, "containerId was not specified"); | ||
| this.containerId = containerId; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public RenameContainerCmd withName(@Nonnull String name) { | ||
| checkNotNull(name, "name was not specified"); | ||
| this.name = name; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @throws NotFoundException | ||
| * No such container | ||
| */ | ||
| @Override | ||
| public Void exec() throws NotFoundException { | ||
| return super.exec(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.github.dockerjava.jaxrs; | ||
|
|
||
| import javax.ws.rs.client.WebTarget; | ||
| import javax.ws.rs.core.MediaType; | ||
|
|
||
| import com.github.dockerjava.api.command.RenameContainerCmd; | ||
| import com.github.dockerjava.core.DockerClientConfig; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class RenameContainerCmdExec extends AbstrSyncDockerCmdExec<RenameContainerCmd, Void> implements RenameContainerCmd.Exec { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(RenameContainerCmdExec.class); | ||
|
|
||
| public RenameContainerCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) { | ||
| super(baseResource, dockerClientConfig); | ||
| } | ||
|
|
||
| @Override | ||
| protected Void execute(RenameContainerCmd command) { | ||
| WebTarget webResource = getBaseResource().path("/containers/{id}/rename").resolveTemplate("id", command.getContainerId()) | ||
| .queryParam("name", command.getName()); | ||
|
|
||
|
|
||
| LOGGER.trace("POST: {}", webResource); | ||
| webResource.request().accept(MediaType.APPLICATION_JSON).post(null); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.github.dockerjava.netty.exec; | ||
|
|
||
| import com.github.dockerjava.api.command.RenameContainerCmd; | ||
| import com.github.dockerjava.core.DockerClientConfig; | ||
| import com.github.dockerjava.netty.MediaType; | ||
| import com.github.dockerjava.netty.WebTarget; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * rename a container. | ||
| */ | ||
| public class RenameContainerCmdExec extends AbstrSyncDockerCmdExec<RenameContainerCmd, Void> implements RenameContainerCmd.Exec { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you try wrote javadoc in interface and check that it inherited by default to classes? (in IDEA just check javadoc on this class) |
||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(RenameContainerCmdExec.class); | ||
|
|
||
| public RenameContainerCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) { | ||
| super(baseResource, dockerClientConfig); | ||
| } | ||
|
|
||
| @Override | ||
| protected Void execute(RenameContainerCmd command) { | ||
| WebTarget webResource = getBaseResource().path("/containers/{id}/rename").resolveTemplate("id", command.getContainerId()) | ||
| .queryParam("name", command.getName()); | ||
|
|
||
| LOGGER.trace("POST: {}", webResource); | ||
| webResource.request().accept(MediaType.APPLICATION_JSON).post(null); | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.github.dockerjava.core.command; | ||
|
|
||
| import com.github.dockerjava.api.command.CreateContainerResponse; | ||
| import com.github.dockerjava.api.command.InspectContainerResponse; | ||
| import com.github.dockerjava.api.exception.DockerException; | ||
| import com.github.dockerjava.api.exception.NotFoundException; | ||
| import com.github.dockerjava.client.AbstractDockerClientTest; | ||
| import org.testng.ITestResult; | ||
| import org.testng.annotations.*; | ||
|
|
||
| import java.lang.reflect.Method; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.*; | ||
|
|
||
| @Test(groups = "integration") | ||
| public class RenameContainerCmdImplTest extends AbstractDockerClientTest { | ||
| @BeforeTest | ||
| public void beforeTest() throws Exception { | ||
| super.beforeTest(); | ||
| } | ||
|
|
||
| @AfterTest | ||
| public void afterTest() { | ||
| super.afterTest(); | ||
| } | ||
|
|
||
| @BeforeMethod | ||
| public void beforeMethod(Method method) { | ||
| super.beforeMethod(method); | ||
| } | ||
|
|
||
| @AfterMethod | ||
| public void afterMethod(ITestResult result) { | ||
| super.afterMethod(result); | ||
| } | ||
|
|
||
| @Test | ||
| public void renameContainer() throws DockerException { | ||
|
|
||
| CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999").exec(); | ||
| LOG.info("Created container: {}", container.toString()); | ||
| assertThat(container.getId(), not(isEmptyString())); | ||
| dockerClient.startContainerCmd(container.getId()).exec(); | ||
|
|
||
| InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec(); | ||
| LOG.info("Container Inspect: {}", inspectContainerResponse.toString()); | ||
|
|
||
| String name1 = inspectContainerResponse.getName(); | ||
|
|
||
| dockerClient.renameContainerCmd(container.getId()).withName(String.valueOf(System.currentTimeMillis())+String.valueOf(System.nanoTime())).exec(); | ||
|
|
||
| InspectContainerResponse inspectContainerResponse2 = dockerClient.inspectContainerCmd(container.getId()).exec(); | ||
| LOG.info("Container Inspect After Rename: {}", inspectContainerResponse2.toString()); | ||
|
|
||
| String name2 = inspectContainerResponse2.getName(); | ||
|
|
||
| assertNotEquals(name1, name2); | ||
|
|
||
| dockerClient.killContainerCmd(container.getId()).exec(); | ||
| } | ||
|
|
||
| @Test | ||
| public void renameExistingContainer() throws DockerException, InterruptedException { | ||
| try { | ||
| dockerClient.renameContainerCmd("non-existing").withName(String.valueOf(System.currentTimeMillis())+String.valueOf(System.nanoTime())).exec(); | ||
| fail("expected NotFoundException"); | ||
| } catch (NotFoundException e) { | ||
| } | ||
|
|
||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please start with capital letter :)