Skip to content

Commit c8edce9

Browse files
committed
Make AuthConfig fluent setters.
Resolves #453
1 parent 41bad01 commit c8edce9

File tree

7 files changed

+52
-53
lines changed

7 files changed

+52
-53
lines changed

src/main/java/com/github/dockerjava/api/model/AuthConfig.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
44
import org.apache.commons.lang.builder.ToStringStyle;
55

6-
import com.fasterxml.jackson.annotation.JsonIgnore;
76
import com.fasterxml.jackson.annotation.JsonInclude;
87
import com.fasterxml.jackson.annotation.JsonInclude.Include;
98
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -30,48 +29,52 @@ public class AuthConfig {
3029
@JsonProperty("serveraddress")
3130
private String serverAddress = DEFAULT_SERVER_ADDRESS;
3231

32+
@JsonProperty("auth")
3333
private String auth;
3434

3535
public String getUsername() {
3636
return username;
3737
}
3838

39-
public void setUsername(String username) {
39+
public AuthConfig withUsername(String username) {
4040
this.username = username;
41+
return this;
4142
}
4243

4344
public String getPassword() {
4445
return password;
4546
}
4647

47-
public void setPassword(String password) {
48+
public AuthConfig withPassword(String password) {
4849
this.password = password;
50+
return this;
4951
}
5052

5153
public String getEmail() {
5254
return email;
5355
}
5456

55-
public void setEmail(String email) {
57+
public AuthConfig withEmail(String email) {
5658
this.email = email;
59+
return this;
5760
}
5861

5962
public String getServerAddress() {
6063
return serverAddress;
6164
}
6265

63-
public void setServerAddress(String serverAddress) {
66+
public AuthConfig withServerAddress(String serverAddress) {
6467
this.serverAddress = serverAddress;
68+
return this;
6569
}
6670

67-
@JsonIgnore
6871
public String getAuth() {
6972
return auth;
7073
}
7174

72-
@JsonProperty("auth")
73-
public void setAuth(String auth) {
75+
public AuthConfig withAuth(String auth) {
7476
this.auth = auth;
77+
return this;
7578
}
7679

7780
@Override

src/main/java/com/github/dockerjava/core/AuthConfigFile.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
111111
for (Map.Entry<String, AuthConfig> entry : configMap.entrySet()) {
112112
AuthConfig authConfig = entry.getValue();
113113
decodeAuth(authConfig.getAuth(), authConfig);
114-
authConfig.setAuth(null);
115-
authConfig.setServerAddress(entry.getKey());
114+
authConfig.withAuth(null);
115+
authConfig.withServerAddress(entry.getKey());
116116
configFile.addConfig(authConfig);
117117
}
118118
} else {
@@ -131,7 +131,7 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
131131
if (origEmail.length != 2) {
132132
throw new IOException("Invalid Auth config file");
133133
}
134-
config.setEmail(origEmail[1]);
134+
config.withEmail(origEmail[1]);
135135
configFile.addConfig(config);
136136
}
137137
return configFile;
@@ -144,8 +144,8 @@ static void decodeAuth(String auth, AuthConfig config) throws IOException {
144144
if (parts.length != 2) {
145145
throw new IOException("Invalid auth configuration file");
146146
}
147-
config.setUsername(parts[0]);
148-
config.setPassword(parts[1]);
147+
config.withUsername(parts[0]);
148+
config.withPassword(parts[1]);
149149
}
150150

151151
static String convertToHostname(String server) {

src/main/java/com/github/dockerjava/core/DockerClientConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ private AuthConfig getAuthConfig() {
278278
AuthConfig authConfig = null;
279279
if (getRegistryUsername() != null && getRegistryPassword() != null && getRegistryEmail() != null
280280
&& getRegistryUrl() != null) {
281-
authConfig = new AuthConfig();
282-
authConfig.setUsername(getRegistryUsername());
283-
authConfig.setPassword(getRegistryPassword());
284-
authConfig.setEmail(getRegistryEmail());
285-
authConfig.setServerAddress(getRegistryUrl());
281+
authConfig = new AuthConfig()
282+
.withUsername(getRegistryUsername())
283+
.withPassword(getRegistryPassword())
284+
.withEmail(getRegistryEmail())
285+
.withServerAddress(getRegistryUrl());
286286
}
287287
return authConfig;
288288
}

src/main/java/com/github/dockerjava/core/DockerClientImpl.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,11 @@ public AuthConfig authConfig() {
164164
checkNotNull(dockerClientConfig.getRegistryUsername(), "Configured username is null.");
165165
checkNotNull(dockerClientConfig.getRegistryUrl(), "Configured serverAddress is null.");
166166

167-
AuthConfig authConfig = new AuthConfig();
168-
authConfig.setUsername(dockerClientConfig.getRegistryUsername());
169-
authConfig.setPassword(dockerClientConfig.getRegistryPassword());
170-
authConfig.setEmail(dockerClientConfig.getRegistryEmail());
171-
authConfig.setServerAddress(dockerClientConfig.getRegistryUrl());
172-
173-
return authConfig;
167+
return new AuthConfig()
168+
.withUsername(dockerClientConfig.getRegistryUsername())
169+
.withPassword(dockerClientConfig.getRegistryPassword())
170+
.withEmail(dockerClientConfig.getRegistryEmail())
171+
.withServerAddress(dockerClientConfig.getRegistryUrl());
174172
}
175173

176174
/**

src/test/java/com/github/dockerjava/core/AuthConfigFileTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ public void invalidLegacyEmailLine() throws IOException {
4848

4949
@Test
5050
public void validJson() throws IOException {
51-
AuthConfig authConfig1 = new AuthConfig();
52-
authConfig1.setEmail("foo@example.com");
53-
authConfig1.setUsername("foo");
54-
authConfig1.setPassword("bar");
55-
authConfig1.setServerAddress("quay.io");
56-
57-
AuthConfig authConfig2 = new AuthConfig();
58-
authConfig2.setEmail("moo@example.com");
59-
authConfig2.setUsername("foo1");
60-
authConfig2.setPassword("bar1");
61-
authConfig2.setServerAddress(AuthConfig.DEFAULT_SERVER_ADDRESS);
51+
AuthConfig authConfig1 = new AuthConfig()
52+
.withEmail("foo@example.com")
53+
.withUsername("foo")
54+
.withPassword("bar")
55+
.withServerAddress("quay.io");
56+
57+
AuthConfig authConfig2 = new AuthConfig()
58+
.withEmail("moo@example.com")
59+
.withUsername("foo1")
60+
.withPassword("bar1")
61+
.withServerAddress(AuthConfig.DEFAULT_SERVER_ADDRESS);
6262

6363
AuthConfigFile expected = new AuthConfigFile();
6464
expected.addConfig(authConfig1);
@@ -70,11 +70,11 @@ public void validJson() throws IOException {
7070

7171
@Test
7272
public void validLegacy() throws IOException {
73-
AuthConfig authConfig = new AuthConfig();
74-
authConfig.setEmail("foo@example.com");
75-
authConfig.setUsername("foo");
76-
authConfig.setPassword("bar");
77-
authConfig.setServerAddress(AuthConfig.DEFAULT_SERVER_ADDRESS);
73+
AuthConfig authConfig = new AuthConfig()
74+
.withEmail("foo@example.com")
75+
.withUsername("foo")
76+
.withPassword("bar")
77+
.withServerAddress(AuthConfig.DEFAULT_SERVER_ADDRESS);
7878

7979
AuthConfigFile expected = new AuthConfigFile();
8080
expected.addConfig(authConfig);

src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,12 @@ public void testBuildFromPrivateRegistry() throws Exception {
274274
// wait for registry to boot
275275
Thread.sleep(3000);
276276

277-
AuthConfig authConfig = new AuthConfig();
278-
279277
// credentials as configured in /auth/htpasswd
280-
authConfig.setUsername("testuser");
281-
authConfig.setPassword("testpassword");
282-
authConfig.setEmail("foo@bar.de");
283-
authConfig.setServerAddress("localhost:5000");
278+
AuthConfig authConfig = new AuthConfig()
279+
.withUsername("testuser")
280+
.withPassword("testpassword")
281+
.withEmail("foo@bar.de")
282+
.withServerAddress("localhost:5000");
284283

285284
dockerClient.authCmd().withAuthConfig(authConfig).exec();
286285
dockerClient.tagImageCmd("busybox:latest", "localhost:5000/testuser/busybox", "latest").withForce().exec();

src/test/java/com/github/dockerjava/netty/exec/BuildImageCmdExecTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,12 @@ public void testBuildFromPrivateRegistry() throws Exception {
266266
// wait for registry to boot
267267
Thread.sleep(3000);
268268

269-
AuthConfig authConfig = new AuthConfig();
270-
271269
// credentials as configured in /auth/htpasswd
272-
authConfig.setUsername("testuser");
273-
authConfig.setPassword("testpassword");
274-
authConfig.setEmail("foo@bar.de");
275-
authConfig.setServerAddress("localhost:5000");
270+
AuthConfig authConfig = new AuthConfig()
271+
.withUsername("testuser")
272+
.withPassword("testpassword")
273+
.withEmail("foo@bar.de")
274+
.withServerAddress("localhost:5000");
276275

277276
dockerClient.authCmd().withAuthConfig(authConfig).exec();
278277
dockerClient.tagImageCmd("busybox:latest", "localhost:5000/testuser/busybox", "latest").withForce().exec();

0 commit comments

Comments
 (0)