diff --git a/docker-java-api/src/main/java/com/github/dockerjava/api/command/CreateServiceCmd.java b/docker-java-api/src/main/java/com/github/dockerjava/api/command/CreateServiceCmd.java index dc78642e3..bfcce27ad 100644 --- a/docker-java-api/src/main/java/com/github/dockerjava/api/command/CreateServiceCmd.java +++ b/docker-java-api/src/main/java/com/github/dockerjava/api/command/CreateServiceCmd.java @@ -1,6 +1,7 @@ package com.github.dockerjava.api.command; import com.github.dockerjava.api.exception.ConflictException; +import com.github.dockerjava.api.model.AuthConfig; import com.github.dockerjava.api.model.ServiceSpec; import javax.annotation.CheckForNull; @@ -15,8 +16,13 @@ public interface CreateServiceCmd extends SyncDockerCmd { @CheckForNull ServiceSpec getServiceSpec(); + @CheckForNull + AuthConfig getAuthConfig(); + CreateServiceCmd withServiceSpec(ServiceSpec serviceSpec); + CreateServiceCmd withAuthConfig(AuthConfig authConfig); + /** * @throws ConflictException * Named service already exists diff --git a/docker-java-core/src/main/java/com/github/dockerjava/core/command/CreateServiceCmdImpl.java b/docker-java-core/src/main/java/com/github/dockerjava/core/command/CreateServiceCmdImpl.java index 1b8e78e42..a664569a1 100644 --- a/docker-java-core/src/main/java/com/github/dockerjava/core/command/CreateServiceCmdImpl.java +++ b/docker-java-core/src/main/java/com/github/dockerjava/core/command/CreateServiceCmdImpl.java @@ -2,6 +2,7 @@ import com.github.dockerjava.api.command.CreateServiceCmd; import com.github.dockerjava.api.command.CreateServiceResponse; +import com.github.dockerjava.api.model.AuthConfig; import com.github.dockerjava.api.model.ServiceSpec; import static com.google.common.base.Preconditions.checkNotNull; @@ -14,6 +15,8 @@ public class CreateServiceCmdImpl extends AbstrDockerCmd() { - }); + + InvocationBuilder builder = resourceWithOptionalAuthConfig(command.getAuthConfig(), webResource.request()) + .accept(MediaType.APPLICATION_JSON); + + return builder.post(command.getServiceSpec(), new TypeReference() { + }); } } diff --git a/docker-java/src/test/java/com/github/dockerjava/cmd/swarm/CreateServiceCmdExecIT.java b/docker-java/src/test/java/com/github/dockerjava/cmd/swarm/CreateServiceCmdExecIT.java index 27eae0b6e..a28d0e83b 100644 --- a/docker-java/src/test/java/com/github/dockerjava/cmd/swarm/CreateServiceCmdExecIT.java +++ b/docker-java/src/test/java/com/github/dockerjava/cmd/swarm/CreateServiceCmdExecIT.java @@ -1,6 +1,8 @@ package com.github.dockerjava.cmd.swarm; +import com.github.dockerjava.api.exception.ConflictException; import com.github.dockerjava.api.exception.DockerException; +import com.github.dockerjava.api.model.AuthConfig; import com.github.dockerjava.api.model.ContainerSpec; import com.github.dockerjava.api.model.EndpointResolutionMode; import com.github.dockerjava.api.model.EndpointSpec; @@ -16,9 +18,14 @@ import com.github.dockerjava.api.model.SwarmSpec; import com.github.dockerjava.api.model.TaskSpec; import com.github.dockerjava.api.model.TmpfsOptions; +import com.github.dockerjava.junit.PrivateRegistryRule; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,6 +42,18 @@ public class CreateServiceCmdExecIT extends SwarmCmdIT { public static final Logger LOG = LoggerFactory.getLogger(CreateServiceCmdExecIT.class); private static final String SERVICE_NAME = "theservice"; + @ClassRule + public static PrivateRegistryRule REGISTRY = new PrivateRegistryRule(); + + @Rule + public ExpectedException exception = ExpectedException.none(); + private AuthConfig authConfig; + + @Before + public void beforeTest() throws Exception { + authConfig = REGISTRY.getAuthConfig(); + } + @Test public void testCreateService() throws DockerException { dockerRule.getClient().initializeSwarmCmd(new SwarmSpec()) @@ -132,4 +151,52 @@ public void testCreateServiceWithTmpfs() { assertThat(mounts.get(0), is(tmpMount)); dockerRule.getClient().removeServiceCmd(SERVICE_NAME).exec(); } + + @Test + public void testCreateServiceWithValidAuth() throws DockerException { + dockerRule.getClient().initializeSwarmCmd(new SwarmSpec()) + .withListenAddr("127.0.0.1") + .withAdvertiseAddr("127.0.0.1") + .exec(); + + dockerRule.getClient().createServiceCmd(new ServiceSpec() + .withName(SERVICE_NAME) + .withTaskTemplate(new TaskSpec() + .withContainerSpec(new ContainerSpec() + .withImage(DEFAULT_IMAGE)))) + .withAuthConfig(authConfig) + .exec(); + + List services = dockerRule.getClient().listServicesCmd() + .withNameFilter(Lists.newArrayList(SERVICE_NAME)) + .exec(); + + assertThat(services, hasSize(1)); + + dockerRule.getClient().removeServiceCmd(SERVICE_NAME).exec(); + } + + @Test + public void testCreateServiceWithInvalidAuth() throws DockerException { + dockerRule.getClient().initializeSwarmCmd(new SwarmSpec()) + .withListenAddr("127.0.0.1") + .withAdvertiseAddr("127.0.0.1") + .exec(); + + AuthConfig invalidAuthConfig = new AuthConfig() + .withUsername("testuser") + .withPassword("testwrongpassword") + .withEmail("foo@bar.de") + .withRegistryAddress(authConfig.getRegistryAddress()); + + exception.expect(ConflictException.class); + + dockerRule.getClient().createServiceCmd(new ServiceSpec() + .withName(SERVICE_NAME) + .withTaskTemplate(new TaskSpec() + .withContainerSpec(new ContainerSpec() + .withImage(DEFAULT_IMAGE)))) + .withAuthConfig(invalidAuthConfig) + .exec(); + } }