Skip to content

Commit a371a6a

Browse files
authored
allow optional authConfig to be supplied to createServiceCmd (#1314)
Fixes #1205
1 parent e0ea277 commit a371a6a

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

docker-java-api/src/main/java/com/github/dockerjava/api/command/CreateServiceCmd.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.dockerjava.api.command;
22

33
import com.github.dockerjava.api.exception.ConflictException;
4+
import com.github.dockerjava.api.model.AuthConfig;
45
import com.github.dockerjava.api.model.ServiceSpec;
56

67
import javax.annotation.CheckForNull;
@@ -15,8 +16,13 @@ public interface CreateServiceCmd extends SyncDockerCmd<CreateServiceResponse> {
1516
@CheckForNull
1617
ServiceSpec getServiceSpec();
1718

19+
@CheckForNull
20+
AuthConfig getAuthConfig();
21+
1822
CreateServiceCmd withServiceSpec(ServiceSpec serviceSpec);
1923

24+
CreateServiceCmd withAuthConfig(AuthConfig authConfig);
25+
2026
/**
2127
* @throws ConflictException
2228
* Named service already exists

docker-java-core/src/main/java/com/github/dockerjava/core/command/CreateServiceCmdImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.dockerjava.api.command.CreateServiceCmd;
44
import com.github.dockerjava.api.command.CreateServiceResponse;
5+
import com.github.dockerjava.api.model.AuthConfig;
56
import com.github.dockerjava.api.model.ServiceSpec;
67

78
import static com.google.common.base.Preconditions.checkNotNull;
@@ -14,6 +15,8 @@ public class CreateServiceCmdImpl extends AbstrDockerCmd<CreateServiceCmd, Creat
1415

1516
private ServiceSpec serviceSpec;
1617

18+
private AuthConfig authConfig;
19+
1720
public CreateServiceCmdImpl(CreateServiceCmd.Exec exec, ServiceSpec serviceSpec) {
1821
super(exec);
1922
checkNotNull(serviceSpec, "serviceSpec was not specified");
@@ -25,10 +28,22 @@ public ServiceSpec getServiceSpec() {
2528
return serviceSpec;
2629
}
2730

31+
@Override
32+
public AuthConfig getAuthConfig() {
33+
return authConfig;
34+
}
35+
2836
@Override
2937
public CreateServiceCmd withServiceSpec(ServiceSpec serviceSpec) {
3038
checkNotNull(serviceSpec, "serviceSpec was not specified");
3139
this.serviceSpec = serviceSpec;
3240
return this;
3341
}
42+
43+
@Override
44+
public CreateServiceCmd withAuthConfig(AuthConfig authConfig) {
45+
checkNotNull(authConfig, "authConfig was not specified");
46+
this.authConfig = authConfig;
47+
return this;
48+
}
3449
}

docker-java-core/src/main/java/com/github/dockerjava/core/exec/CreateServiceCmdExec.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.github.dockerjava.api.command.CreateServiceCmd;
55
import com.github.dockerjava.api.command.CreateServiceResponse;
66
import com.github.dockerjava.core.DockerClientConfig;
7+
import com.github.dockerjava.core.InvocationBuilder;
78
import com.github.dockerjava.core.MediaType;
89
import com.github.dockerjava.core.WebTarget;
910
import org.slf4j.Logger;
@@ -23,8 +24,11 @@ protected CreateServiceResponse execute(CreateServiceCmd command) {
2324
WebTarget webResource = getBaseResource().path("/services/create");
2425

2526
LOGGER.trace("POST: {} ", webResource);
26-
return webResource.request().accept(MediaType.APPLICATION_JSON)
27-
.post(command.getServiceSpec(), new TypeReference<CreateServiceResponse>() {
28-
});
27+
28+
InvocationBuilder builder = resourceWithOptionalAuthConfig(command.getAuthConfig(), webResource.request())
29+
.accept(MediaType.APPLICATION_JSON);
30+
31+
return builder.post(command.getServiceSpec(), new TypeReference<CreateServiceResponse>() {
32+
});
2933
}
3034
}

docker-java/src/test/java/com/github/dockerjava/cmd/swarm/CreateServiceCmdExecIT.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.dockerjava.cmd.swarm;
22

3+
import com.github.dockerjava.api.exception.ConflictException;
34
import com.github.dockerjava.api.exception.DockerException;
5+
import com.github.dockerjava.api.model.AuthConfig;
46
import com.github.dockerjava.api.model.ContainerSpec;
57
import com.github.dockerjava.api.model.EndpointResolutionMode;
68
import com.github.dockerjava.api.model.EndpointSpec;
@@ -16,9 +18,14 @@
1618
import com.github.dockerjava.api.model.SwarmSpec;
1719
import com.github.dockerjava.api.model.TaskSpec;
1820
import com.github.dockerjava.api.model.TmpfsOptions;
21+
import com.github.dockerjava.junit.PrivateRegistryRule;
1922
import com.google.common.collect.ImmutableMap;
2023
import com.google.common.collect.Lists;
24+
import org.junit.Before;
25+
import org.junit.ClassRule;
26+
import org.junit.Rule;
2127
import org.junit.Test;
28+
import org.junit.rules.ExpectedException;
2229
import org.slf4j.Logger;
2330
import org.slf4j.LoggerFactory;
2431

@@ -35,6 +42,18 @@ public class CreateServiceCmdExecIT extends SwarmCmdIT {
3542
public static final Logger LOG = LoggerFactory.getLogger(CreateServiceCmdExecIT.class);
3643
private static final String SERVICE_NAME = "theservice";
3744

45+
@ClassRule
46+
public static PrivateRegistryRule REGISTRY = new PrivateRegistryRule();
47+
48+
@Rule
49+
public ExpectedException exception = ExpectedException.none();
50+
private AuthConfig authConfig;
51+
52+
@Before
53+
public void beforeTest() throws Exception {
54+
authConfig = REGISTRY.getAuthConfig();
55+
}
56+
3857
@Test
3958
public void testCreateService() throws DockerException {
4059
dockerRule.getClient().initializeSwarmCmd(new SwarmSpec())
@@ -132,4 +151,52 @@ public void testCreateServiceWithTmpfs() {
132151
assertThat(mounts.get(0), is(tmpMount));
133152
dockerRule.getClient().removeServiceCmd(SERVICE_NAME).exec();
134153
}
154+
155+
@Test
156+
public void testCreateServiceWithValidAuth() throws DockerException {
157+
dockerRule.getClient().initializeSwarmCmd(new SwarmSpec())
158+
.withListenAddr("127.0.0.1")
159+
.withAdvertiseAddr("127.0.0.1")
160+
.exec();
161+
162+
dockerRule.getClient().createServiceCmd(new ServiceSpec()
163+
.withName(SERVICE_NAME)
164+
.withTaskTemplate(new TaskSpec()
165+
.withContainerSpec(new ContainerSpec()
166+
.withImage(DEFAULT_IMAGE))))
167+
.withAuthConfig(authConfig)
168+
.exec();
169+
170+
List<Service> services = dockerRule.getClient().listServicesCmd()
171+
.withNameFilter(Lists.newArrayList(SERVICE_NAME))
172+
.exec();
173+
174+
assertThat(services, hasSize(1));
175+
176+
dockerRule.getClient().removeServiceCmd(SERVICE_NAME).exec();
177+
}
178+
179+
@Test
180+
public void testCreateServiceWithInvalidAuth() throws DockerException {
181+
dockerRule.getClient().initializeSwarmCmd(new SwarmSpec())
182+
.withListenAddr("127.0.0.1")
183+
.withAdvertiseAddr("127.0.0.1")
184+
.exec();
185+
186+
AuthConfig invalidAuthConfig = new AuthConfig()
187+
.withUsername("testuser")
188+
.withPassword("testwrongpassword")
189+
.withEmail("foo@bar.de")
190+
.withRegistryAddress(authConfig.getRegistryAddress());
191+
192+
exception.expect(ConflictException.class);
193+
194+
dockerRule.getClient().createServiceCmd(new ServiceSpec()
195+
.withName(SERVICE_NAME)
196+
.withTaskTemplate(new TaskSpec()
197+
.withContainerSpec(new ContainerSpec()
198+
.withImage(DEFAULT_IMAGE))))
199+
.withAuthConfig(invalidAuthConfig)
200+
.exec();
201+
}
135202
}

0 commit comments

Comments
 (0)