Skip to content

Commit e4a7f87

Browse files
committed
Merge pull request #454 from KostyaSha/authConfig
Make AuthConfig fluent, setters -> with.
2 parents 41bad01 + bd11042 commit e4a7f87

File tree

10 files changed

+69
-70
lines changed

10 files changed

+69
-70
lines changed

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

Lines changed: 19 additions & 16 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;
@@ -28,50 +27,54 @@ public class AuthConfig {
2827
private String email;
2928

3029
@JsonProperty("serveraddress")
31-
private String serverAddress = DEFAULT_SERVER_ADDRESS;
30+
private String registryAddress = 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

59-
public String getServerAddress() {
60-
return serverAddress;
62+
public String getRegistryAddress() {
63+
return registryAddress;
6164
}
6265

63-
public void setServerAddress(String serverAddress) {
64-
this.serverAddress = serverAddress;
66+
public AuthConfig withRegistryAddress(String registryAddress) {
67+
this.registryAddress = registryAddress;
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
@@ -87,7 +90,7 @@ public int hashCode() {
8790
result = prime * result + ((auth == null) ? 0 : auth.hashCode());
8891
result = prime * result + ((email == null) ? 0 : email.hashCode());
8992
result = prime * result + ((password == null) ? 0 : password.hashCode());
90-
result = prime * result + ((serverAddress == null) ? 0 : serverAddress.hashCode());
93+
result = prime * result + ((registryAddress == null) ? 0 : registryAddress.hashCode());
9194
result = prime * result + ((username == null) ? 0 : username.hashCode());
9295
return result;
9396
}
@@ -116,10 +119,10 @@ public boolean equals(Object obj) {
116119
return false;
117120
} else if (!password.equals(other.password))
118121
return false;
119-
if (serverAddress == null) {
120-
if (other.serverAddress != null)
122+
if (registryAddress == null) {
123+
if (other.registryAddress != null)
121124
return false;
122-
} else if (!serverAddress.equals(other.serverAddress))
125+
} else if (!registryAddress.equals(other.registryAddress))
123126
return false;
124127
if (username == null) {
125128
if (other.username != null)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class AuthConfigurations {
1111
private Map<String, AuthConfig> configs = new TreeMap<>();
1212

1313
public void addConfig(AuthConfig authConfig) {
14-
configs.put(authConfig.getServerAddress(), authConfig);
14+
configs.put(authConfig.getRegistryAddress(), authConfig);
1515
}
1616

1717
public Map<String, AuthConfig> getConfigs() {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public AuthConfigFile() {
2929
}
3030

3131
void addConfig(AuthConfig config) {
32-
authConfigMap.put(config.getServerAddress(), config);
32+
authConfigMap.put(config.getRegistryAddress(), config);
3333
}
3434

3535
public AuthConfig resolveAuthConfig(String hostname) {
@@ -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.withRegistryAddress(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+
.withRegistryAddress(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+
.withRegistryAddress(dockerClientConfig.getRegistryUrl());
174172
}
175173

176174
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* is malformed.
3838
*
3939
* On Windows, escaping is disabled. Instead, '\\' is treated as
40-
* path separator.
40+
AuthConfigTest * path separator.
4141
* </pre>
4242
*
4343
* @author tedo

src/test/java/com/github/dockerjava/api/model/AuthConfigTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public class AuthConfigTest {
1111

1212
@BeforeMethod
1313
public void setUp() throws Exception {
14-
authConfig = new AuthConfig();
15-
authConfig.setEmail("foo");
16-
authConfig.setPassword("bar");
17-
authConfig.setServerAddress("baz");
18-
authConfig.setUsername("qux");
14+
authConfig = new AuthConfig()
15+
.withEmail("foo")
16+
.withPassword("bar")
17+
.withRegistryAddress("baz")
18+
.withUsername("qux");
1919
}
2020

2121
@Test
2222
public void defaultServerAddress() throws Exception {
23-
assertEquals(new AuthConfig().getServerAddress(), "https://index.docker.io/v1/");
23+
assertEquals(new AuthConfig().getRegistryAddress(), "https://index.docker.io/v1/");
2424
}
2525
}

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+
.withRegistryAddress("quay.io");
56+
57+
AuthConfig authConfig2 = new AuthConfig()
58+
.withEmail("moo@example.com")
59+
.withUsername("foo1")
60+
.withPassword("bar1")
61+
.withRegistryAddress(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+
.withRegistryAddress(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+
.withRegistryAddress("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+
.withRegistryAddress("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)