Skip to content
Merged
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
@@ -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;
Expand All @@ -15,8 +16,13 @@ public interface CreateServiceCmd extends SyncDockerCmd<CreateServiceResponse> {
@CheckForNull
ServiceSpec getServiceSpec();

@CheckForNull
AuthConfig getAuthConfig();

CreateServiceCmd withServiceSpec(ServiceSpec serviceSpec);

CreateServiceCmd withAuthConfig(AuthConfig authConfig);

/**
* @throws ConflictException
* Named service already exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,8 @@ public class CreateServiceCmdImpl extends AbstrDockerCmd<CreateServiceCmd, Creat

private ServiceSpec serviceSpec;

private AuthConfig authConfig;

public CreateServiceCmdImpl(CreateServiceCmd.Exec exec, ServiceSpec serviceSpec) {
super(exec);
checkNotNull(serviceSpec, "serviceSpec was not specified");
Expand All @@ -25,10 +28,22 @@ public ServiceSpec getServiceSpec() {
return serviceSpec;
}

@Override
public AuthConfig getAuthConfig() {
return authConfig;
}

@Override
public CreateServiceCmd withServiceSpec(ServiceSpec serviceSpec) {
checkNotNull(serviceSpec, "serviceSpec was not specified");
this.serviceSpec = serviceSpec;
return this;
}

@Override
public CreateServiceCmd withAuthConfig(AuthConfig authConfig) {
checkNotNull(authConfig, "authConfig was not specified");
this.authConfig = authConfig;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.dockerjava.api.command.CreateServiceCmd;
import com.github.dockerjava.api.command.CreateServiceResponse;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.InvocationBuilder;
import com.github.dockerjava.core.MediaType;
import com.github.dockerjava.core.WebTarget;
import org.slf4j.Logger;
Expand All @@ -23,8 +24,11 @@ protected CreateServiceResponse execute(CreateServiceCmd command) {
WebTarget webResource = getBaseResource().path("/services/create");

LOGGER.trace("POST: {} ", webResource);
return webResource.request().accept(MediaType.APPLICATION_JSON)
.post(command.getServiceSpec(), new TypeReference<CreateServiceResponse>() {
});

InvocationBuilder builder = resourceWithOptionalAuthConfig(command.getAuthConfig(), webResource.request())
.accept(MediaType.APPLICATION_JSON);

return builder.post(command.getServiceSpec(), new TypeReference<CreateServiceResponse>() {
});
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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())
Expand Down Expand Up @@ -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<Service> 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();
}
}