Skip to content

Commit 882441a

Browse files
committed
refactor: rename instanceType -> dbType
1 parent 3fb282d commit 882441a

File tree

8 files changed

+50
-29
lines changed

8 files changed

+50
-29
lines changed

pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@
8686
<artifactId>unirest-java</artifactId>
8787
<version>1.4.6</version>
8888
</dependency>
89+
<dependency>
90+
<groupId>org.apache.httpcomponents</groupId>
91+
<artifactId>httpclient</artifactId>
92+
<version>4.3.6</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.apache.httpcomponents</groupId>
96+
<artifactId>httpasyncclient</artifactId>
97+
<version>4.0.2</version>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.apache.httpcomponents</groupId>
101+
<artifactId>httpmime</artifactId>
102+
<version>4.3.6</version>
103+
</dependency>
104+
<dependency>
105+
<groupId>org.json</groupId>
106+
<artifactId>json</artifactId>
107+
<version>20140107</version>
108+
</dependency>
89109
<dependency>
90110
<groupId>com.github.tomakehurst</groupId>
91111
<artifactId>wiremock</artifactId>

src/main/java/org/jenkinsci/plugins/mktmpio/Mktmpio.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ public class Mktmpio extends SimpleBuildWrapper {
2121
public static final String DEFAULT_SERVER = "https://mktmp.io";
2222

2323
// Job config
24-
private String instanceType;
24+
private String dbType;
2525
private boolean shutdownWithBuild = false;
2626

2727
@DataBoundConstructor
28-
public Mktmpio(String instanceType) {
29-
this.instanceType = instanceType;
28+
public Mktmpio(String dbType) {
29+
this.dbType = dbType;
3030
}
3131

3232
static void dispose(final MktmpioEnvironment env, final Launcher launcher, final TaskListener listener) throws IOException, InterruptedException {
3333
final String instanceID = env.id;
3434
MktmpioInstance instance = new MktmpioInstance(env);
3535
instance.destroy();
36-
listener.getLogger().printf("mktmpio instance shutdown. type: %s, host: %s, port: %d\n", env.type, env.host, env.port);
36+
listener.getLogger().printf("mktmpio instance shutdown. type: %s, host: %s, port: %d\n", env.dbType, env.host, env.port);
3737
}
3838

39-
public String getInstanceType() {
40-
return instanceType;
39+
public String getDbType() {
40+
return dbType;
4141
}
4242

4343
@DataBoundSetter
44-
public void setInstanceType(String instanceType) {
45-
this.instanceType = instanceType;
44+
public void setDbType(String dbType) {
45+
this.dbType = dbType;
4646
}
4747

4848
public boolean isShutdownWithBuild() {
@@ -70,20 +70,20 @@ public void setUp(SimpleBuildWrapper.Context context,
7070
final MktmpioDescriptor config = getDescriptor();
7171
final String token = config.getToken();
7272
final String baseUrl = config.getServer();
73-
final String type = getInstanceType();
73+
final String dbType = getDbType();
7474
final MktmpioInstance instance;
7575
try {
7676
listener.getLogger().printf("Attempting to create instance (server: %s, token: %s, type: %s)",
77-
baseUrl, token.replaceAll(".", "*"), type);
78-
instance = MktmpioInstance.create(baseUrl, token, type, isShutdownWithBuild());
77+
baseUrl, token.replaceAll(".", "*"), dbType);
78+
instance = MktmpioInstance.create(baseUrl, token, dbType, isShutdownWithBuild());
7979
} catch (IOException ex) {
8080
listener.fatalError("mktmpio: " + ex.getMessage());
8181
throw new InterruptedException(ex.getMessage());
8282
}
8383
final MktmpioEnvironment env = instance.getEnv();
8484
final Map<String, String> envVars = env.envVars();
85-
listener.hyperlink(baseUrl + "/i/" + env.id, env.type + " instance " + env.id);
86-
listener.getLogger().printf("mktmpio instance created: %s\n", env.type);
85+
listener.hyperlink(baseUrl + "/i/" + env.id, env.dbType + " instance " + env.id);
86+
listener.getLogger().printf("mktmpio instance created: %s\n", env.dbType);
8787
for (Map.Entry<String, String> entry : envVars.entrySet()) {
8888
listener.getLogger().printf("setting %s=%s\n", entry.getKey(), entry.getValue());
8989
}
@@ -92,7 +92,7 @@ public void setUp(SimpleBuildWrapper.Context context,
9292
context.setDisposer(new MktmpioDisposer(env));
9393
}
9494

95-
public static ListBoxModel instanceTypes() {
95+
public static ListBoxModel supportedDbTypes() {
9696
ListBoxModel items = new ListBoxModel();
9797
items.add("MySQL", "mysql");
9898
items.add("PostgreSQL-9.4", "postgres");
@@ -121,7 +121,7 @@ public boolean configure(final StaplerRequest req, final JSONObject formData) {
121121
}
122122

123123
@Override
124-
public BuildWrapper newInstance(final StaplerRequest req, final JSONObject formData) throws hudson.model.Descriptor.FormException {
124+
public Mktmpio newInstance(final StaplerRequest req, final JSONObject formData) throws hudson.model.Descriptor.FormException {
125125
return req.bindJSON(Mktmpio.class, formData);
126126
}
127127

@@ -151,8 +151,8 @@ public String getDisplayName() {
151151
return "Create temporary database server for build";
152152
}
153153

154-
public ListBoxModel doFillInstanceTypeItems() {
155-
return instanceTypes();
154+
public ListBoxModel doFillDbTypeItems() {
155+
return supportedDbTypes();
156156
}
157157
}
158158
}

src/main/java/org/jenkinsci/plugins/mktmpio/MktmpioEnvironment.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ public class MktmpioEnvironment extends InvisibleAction implements Serializable
1414
public final int port;
1515
public final String username;
1616
public final String password;
17-
public final String type;
17+
public final String dbType;
1818
public final boolean shutdownWithBuild;
1919

20-
public MktmpioEnvironment(final String token, final String id, final String host, final int port, final String username, final String password, final String type, final boolean shutdownWithBuild) {
20+
public MktmpioEnvironment(final String token, final String id, final String host, final int port, final String username, final String password, final String dbType, final boolean shutdownWithBuild) {
2121
this.token = token;
2222
this.id = id;
2323
this.host = host;
2424
this.port = port;
2525
this.username = username;
2626
this.password = password;
27-
this.type = type;
27+
this.dbType = dbType;
2828
this.shutdownWithBuild = shutdownWithBuild;
2929
}
3030

@@ -35,7 +35,7 @@ public Map<String, String> envVars() {
3535
vars.put("MKTMPIO_USERNAME", username);
3636
vars.put("MKTMPIO_PASSWORD", password);
3737
vars.put("MKTMPIO_ID", id);
38-
vars.put("MKTMPIO_TYPE", type);
38+
vars.put("MKTMPIO_TYPE", dbType);
3939
return vars;
4040
}
4141
}

src/main/java/org/jenkinsci/plugins/mktmpio/MktmpioInstance.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ public MktmpioInstance(final MktmpioEnvironment env) {
1515
this.env = env;
1616
}
1717

18-
public static MktmpioInstance create(final String urlRoot, final String token, final String type, final boolean shutdownWithBuild) throws IOException, InterruptedException {
18+
public static MktmpioInstance create(final String urlRoot, final String token, final String dbType, final boolean shutdownWithBuild) throws IOException, InterruptedException {
19+
final String url = urlRoot + "/api/v1/new/" + dbType;
1920
HttpResponse<JsonNode> json;
2021
try {
21-
json = Unirest.post(urlRoot + "/api/v1/new/" + type)
22+
json = Unirest.post(url)
2223
.header("accept", "application/json")
2324
.header("X-Auth-Token", token)
2425
.asJson();
@@ -31,15 +32,15 @@ public static MktmpioInstance create(final String urlRoot, final String token, f
3132
System.err.println("Used token: " + token);
3233
System.err.println("error response: " + json.getStatusText());
3334
System.err.println("response body: " + json.getBody().toString());
34-
throw new IOException("Error creating " + type + " instance, " + message);
35+
throw new IOException("Error creating " + dbType + " instance, " + message);
3536
}
3637
JSONObject res = json.getBody().getObject();
3738
String id = res.getString("id");
3839
String host = res.getString("host");
3940
int port = res.getInt("port");
4041
String username = res.optString("username", "");
4142
String password = res.optString("password", "");
42-
final MktmpioEnvironment env = new MktmpioEnvironment(token, id, host, port, username, password, type, shutdownWithBuild);
43+
final MktmpioEnvironment env = new MktmpioEnvironment(token, id, host, port, username, password, dbType, shutdownWithBuild);
4344
return new MktmpioInstance(env);
4445
}
4546

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?jelly escape-by-default='true'?>
22
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
3-
<f:entry field="instanceType" title="Database Type">
3+
<f:entry field="dbType" title="Database Type">
44
<f:select />
55
</f:entry>
66
</j:jelly>

src/test/java/org/jenkinsci/plugins/mktmpio/MktmpioInstanceTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void succeedsWithGoodCredentials() throws Exception {
2626
prepareFakeInstance("totally-legit-token", "redis");
2727
MktmpioInstance redis = MktmpioInstance.create(mockedServer(), "totally-legit-token", "redis", true);
2828
assertThat("result is not null", redis != null);
29-
assertThat(redis.getEnv().type, is("redis"));
29+
assertThat(redis.getEnv().dbType, is("redis"));
3030
redis.destroy();
3131
}
3232
}

src/test/java/org/jenkinsci/plugins/mktmpio/MktmpioWorkflowTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void evaluate() throws Throwable {
5858

5959
workflowJob.setDefinition(new CpsFlowDefinition(""
6060
+ "node {\n"
61-
+ " wrap([$class: 'Mktmpio', instanceType: 'redis']) {\n"
61+
+ " wrap([$class: 'Mktmpio', dbType: 'redis']) {\n"
6262
+ " semaphore 'shouldAllowWorkflowRestarts'\n"
6363
+ " sh 'echo REDIS_HOST=$MKTMPIO_HOST'\n"
6464
+ " }\n"
@@ -104,7 +104,7 @@ public void evaluate() throws Throwable {
104104

105105
workflowJob.setDefinition(new CpsFlowDefinition(""
106106
+ "node {\n"
107-
+ " wrap([$class: 'Mktmpio', instanceType: 'redis']) {\n"
107+
+ " wrap([$class: 'Mktmpio', dbType: 'redis']) {\n"
108108
+ " sh 'echo REDIS_HOST=$MKTMPIO_HOST'\n"
109109
+ " }\n"
110110
+ "}", true));

0 commit comments

Comments
 (0)