Skip to content

Commit d7a5984

Browse files
catalyst7193ssullivan
authored andcommitted
Updated the ContainerSpec to have support for Config bindings
Changed line endings back to fix large diff Signed-off-by: Stephen Sullivan <[email protected]>
1 parent ae7cefe commit d7a5984

File tree

5 files changed

+220
-1
lines changed

5 files changed

+220
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*-
2+
* -\-\-
3+
* docker-client
4+
* --
5+
* Copyright (C) 2016 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.docker.client.messages.swarm;
22+
23+
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
24+
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
25+
26+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
27+
import com.fasterxml.jackson.annotation.JsonCreator;
28+
import com.fasterxml.jackson.annotation.JsonProperty;
29+
import com.google.auto.value.AutoValue;
30+
31+
@AutoValue
32+
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
33+
public abstract class ConfigBind {
34+
@JsonProperty("File")
35+
public abstract ConfigFile file();
36+
37+
@JsonProperty("ConfigID")
38+
public abstract String configId();
39+
40+
@JsonProperty("ConfigName")
41+
public abstract String configName();
42+
43+
public static Builder builder() {
44+
return new AutoValue_ConfigBind.Builder();
45+
}
46+
47+
@AutoValue.Builder
48+
public abstract static class Builder {
49+
50+
public abstract Builder file(ConfigFile file);
51+
52+
public abstract Builder configId(String configId);
53+
54+
public abstract Builder configName(String configName);
55+
56+
public abstract ConfigBind build();
57+
}
58+
59+
@JsonCreator
60+
static ConfigBind create(
61+
@JsonProperty("File") ConfigFile file,
62+
@JsonProperty("ConfigID") String configId,
63+
@JsonProperty("ConfigName") String configName) {
64+
return builder()
65+
.file(file)
66+
.configId(configId)
67+
.configName(configName)
68+
.build();
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*-
2+
* -\-\-
3+
* docker-client
4+
* --
5+
* Copyright (C) 2016 - 2017 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.docker.client.messages.swarm;
22+
23+
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
24+
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
25+
26+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
27+
import com.fasterxml.jackson.annotation.JsonCreator;
28+
import com.fasterxml.jackson.annotation.JsonProperty;
29+
import com.google.auto.value.AutoValue;
30+
import javax.annotation.Nullable;
31+
32+
@AutoValue
33+
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
34+
public abstract class ConfigFile {
35+
@JsonProperty("Name")
36+
public abstract String name();
37+
38+
@Nullable
39+
@JsonProperty("UID")
40+
public abstract String uid();
41+
42+
@Nullable
43+
@JsonProperty("GID")
44+
public abstract String gid();
45+
46+
@Nullable
47+
@JsonProperty("Mode")
48+
public abstract Long mode();
49+
50+
public static Builder builder() {
51+
return new AutoValue_ConfigFile.Builder();
52+
}
53+
54+
@AutoValue.Builder
55+
public abstract static class Builder {
56+
57+
public abstract Builder name(String name);
58+
59+
public abstract Builder uid(String uid);
60+
61+
public abstract Builder gid(String gid);
62+
63+
public abstract Builder mode(Long mode);
64+
65+
public abstract ConfigFile build();
66+
}
67+
68+
@JsonCreator
69+
static ConfigFile create(
70+
@JsonProperty("Name") String name,
71+
@JsonProperty("UID") String uid,
72+
@JsonProperty("GID") String gid,
73+
@JsonProperty("Mode") Long mode) {
74+
return builder()
75+
.name(name)
76+
.uid(uid)
77+
.gid(gid)
78+
.mode(mode)
79+
.build();
80+
}
81+
}

src/main/java/com/spotify/docker/client/messages/swarm/ContainerSpec.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ public abstract class ContainerSpec {
111111
@JsonProperty("Secrets")
112112
public abstract ImmutableList<SecretBind> secrets();
113113

114+
/**
115+
* @since API 1.30
116+
*/
117+
@Nullable
118+
@JsonProperty("Configs")
119+
public abstract ImmutableList<ConfigBind> configs();
120+
114121
@Nullable
115122
@JsonProperty("DNSConfig")
116123
public abstract DnsConfig dnsConfig();
@@ -339,6 +346,8 @@ public Builder withStopGracePeriod(final long stopGracePeriod) {
339346

340347
public abstract Builder secrets(List<SecretBind> secrets);
341348

349+
public abstract Builder configs(List<ConfigBind> configs);
350+
342351
public abstract ContainerSpec build();
343352
}
344353

@@ -363,7 +372,8 @@ static ContainerSpec create(
363372
@JsonProperty("Healthcheck") final ContainerConfig.Healthcheck healthcheck,
364373
@JsonProperty("Hosts") final List<String> hosts,
365374
@JsonProperty("Secrets") final List<SecretBind> secrets,
366-
@JsonProperty("DNSConfig") final DnsConfig dnsConfig) {
375+
@JsonProperty("DNSConfig") final DnsConfig dnsConfig,
376+
@JsonProperty("Configs") final List<ConfigBind> configs) {
367377
final Builder builder = builder()
368378
.image(image)
369379
.hostname(hostname)
@@ -391,6 +401,10 @@ static ContainerSpec create(
391401
builder.secrets(secrets);
392402
}
393403

404+
if (configs != null) {
405+
builder.configs(configs);
406+
}
407+
394408
return builder.build();
395409
}
396410
}

src/test/java/com/spotify/docker/client/DefaultDockerClientUnitTest.java

+51
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static org.hamcrest.Matchers.notNullValue;
3232
import static org.hamcrest.Matchers.nullValue;
3333
import static org.junit.Assert.assertThat;
34+
import static org.mockito.Matchers.isNull;
3435
import static org.mockito.Mockito.mock;
3536
import static org.mockito.Mockito.when;
3637

@@ -56,14 +57,17 @@
5657
import com.spotify.docker.client.messages.RegistryConfigs;
5758
import com.spotify.docker.client.messages.ServiceCreateResponse;
5859
import com.spotify.docker.client.messages.swarm.Config;
60+
import com.spotify.docker.client.messages.swarm.ConfigBind;
5961
import com.spotify.docker.client.messages.swarm.ConfigCreateResponse;
62+
import com.spotify.docker.client.messages.swarm.ConfigFile;
6063
import com.spotify.docker.client.messages.swarm.ConfigSpec;
6164
import com.spotify.docker.client.messages.swarm.ContainerSpec;
6265
import com.spotify.docker.client.messages.swarm.EngineConfig;
6366
import com.spotify.docker.client.messages.swarm.Node;
6467
import com.spotify.docker.client.messages.swarm.NodeDescription;
6568
import com.spotify.docker.client.messages.swarm.NodeInfo;
6669
import com.spotify.docker.client.messages.swarm.NodeSpec;
70+
import com.spotify.docker.client.messages.swarm.SecretSpec;
6771
import com.spotify.docker.client.messages.swarm.ServiceSpec;
6872
import com.spotify.docker.client.messages.swarm.SwarmJoin;
6973
import com.spotify.docker.client.messages.swarm.TaskSpec;
@@ -617,6 +621,53 @@ private void enqueueServerApiEmptyResponse(final int statusCode) {
617621
);
618622
}
619623

624+
@Test
625+
public void testCreateServiceWithConfig() throws Exception {
626+
final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);
627+
628+
// build() calls /version to check what format of header to send
629+
enqueueServerApiVersion("1.30");
630+
enqueueServerApiResponse(201, "fixtures/1.30/configCreateResponse.json");
631+
632+
final ConfigSpec configSpec = ConfigSpec
633+
.builder()
634+
.data(Base64.encodeAsString("foobar"))
635+
.name("foo.yaml")
636+
.build();
637+
638+
final ConfigCreateResponse configCreateResponse = dockerClient.createConfig(configSpec);
639+
assertThat(configCreateResponse.id(), equalTo("ktnbjxoalbkvbvedmg1urrz8h"));
640+
641+
final ConfigBind configBind = ConfigBind.builder()
642+
.configName(configSpec.name())
643+
.configId(configCreateResponse.id())
644+
.file(ConfigFile.builder()
645+
.gid("1000")
646+
.uid("1000")
647+
.mode(600L)
648+
.name(configSpec.name())
649+
.build()
650+
).build();
651+
652+
final TaskSpec taskSpec = TaskSpec.builder()
653+
.containerSpec(ContainerSpec.builder()
654+
.image("this_image_is_found_in_the_registry")
655+
.configs(ImmutableList.of(configBind))
656+
.build())
657+
.build();
658+
659+
final ServiceSpec spec = ServiceSpec.builder()
660+
.name("test")
661+
.taskTemplate(taskSpec)
662+
.build();
663+
664+
enqueueServerApiVersion("1.30");
665+
enqueueServerApiResponse(201, "fixtures/1.30/createServiceResponse.json");
666+
667+
final ServiceCreateResponse response = dockerClient.createService(spec);
668+
assertThat(response.id(), equalTo("ak7w3gjqoa3kuz8xcpnyy0pvl"));
669+
}
670+
620671
@Test
621672
public void testListConfigs() throws Exception {
622673
final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ID": "ak7w3gjqoa3kuz8xcpnyy0pvl"
3+
}

0 commit comments

Comments
 (0)