Skip to content

Commit 1736f88

Browse files
committed
feat: Setup deleting a server from hetzner
- Need to setup an Action Service to track and notify when Action's are complete - Refactor Error subclass to be it's own - HTTP Client handles DELETE now
1 parent f93c6a2 commit 1736f88

File tree

18 files changed

+444
-42
lines changed

18 files changed

+444
-42
lines changed

src/main/java/dev/tomr/hcloud/http/HetznerCloudHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.tomr.hcloud.http;
22

3-
import dev.tomr.hcloud.HetznerCloud;
43
import dev.tomr.hcloud.http.exception.HetznerApiException;
54
import dev.tomr.hcloud.http.model.HetznerErrorResponse;
65
import org.slf4j.Logger;
@@ -98,6 +97,7 @@ private HttpRequest createHttpRequest(String uri, RequestVerb requestVerb, Strin
9897
case GET -> builder.GET();
9998
case POST -> builder.POST(HttpRequest.BodyPublishers.ofString(body));
10099
case PUT -> builder.PUT(HttpRequest.BodyPublishers.ofString(body));
100+
case DELETE -> builder.DELETE();
101101
};
102102
return builder.build();
103103
}

src/main/java/dev/tomr/hcloud/http/RequestVerb.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* Request Verb's for the HTTP Client
55
*/
66
public enum RequestVerb {
7-
GET, POST, PUT
7+
GET, POST, PUT, DELETE
88
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package dev.tomr.hcloud.http.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonRootName;
6+
import dev.tomr.hcloud.http.HetznerJsonObject;
7+
import dev.tomr.hcloud.http.model.enums.Status;
8+
9+
import java.util.List;
10+
11+
@JsonIgnoreProperties(ignoreUnknown = true)
12+
@JsonInclude(JsonInclude.Include.NON_NULL)
13+
@JsonRootName("action")
14+
public class Action extends HetznerJsonObject {
15+
16+
private String command;
17+
private String finished;
18+
private Integer id;
19+
private Integer progress;
20+
private String started;
21+
private Error error;
22+
private Status status;
23+
private List<Resource> resources;
24+
25+
public Action(String command, String finished, Integer id, Integer progress, String started, Error error, Status status, List<Resource> resources) {
26+
this.command = command;
27+
this.finished = finished;
28+
this.id = id;
29+
this.progress = progress;
30+
this.started = started;
31+
this.error = error;
32+
this.status = status;
33+
this.resources = resources;
34+
}
35+
36+
public Action() {
37+
}
38+
39+
public String getCommand() {
40+
return command;
41+
}
42+
43+
public void setCommand(String command) {
44+
this.command = command;
45+
}
46+
47+
public String getFinished() {
48+
return finished;
49+
}
50+
51+
public void setFinished(String finished) {
52+
this.finished = finished;
53+
}
54+
55+
public Integer getId() {
56+
return id;
57+
}
58+
59+
public void setId(Integer id) {
60+
this.id = id;
61+
}
62+
63+
public Integer getProgress() {
64+
return progress;
65+
}
66+
67+
public void setProgress(Integer progress) {
68+
this.progress = progress;
69+
}
70+
71+
public String getStarted() {
72+
return started;
73+
}
74+
75+
public void setStarted(String started) {
76+
this.started = started;
77+
}
78+
79+
public Error getError() {
80+
return error;
81+
}
82+
83+
public void setError(Error error) {
84+
this.error = error;
85+
}
86+
87+
public Status getStatus() {
88+
return status;
89+
}
90+
91+
public void setStatus(Status status) {
92+
this.status = status;
93+
}
94+
95+
public List<Resource> getResources() {
96+
return resources;
97+
}
98+
99+
public void setResources(List<Resource> resources) {
100+
this.resources = resources;
101+
}
102+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package dev.tomr.hcloud.http.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
6+
@JsonIgnoreProperties(ignoreUnknown = true)
7+
@JsonInclude(JsonInclude.Include.NON_NULL)
8+
public class Error {
9+
private String code;
10+
private String message;
11+
12+
public Error(String code, String message) {
13+
this.code = code;
14+
this.message = message;
15+
}
16+
17+
public Error() {}
18+
19+
public String getCode() {
20+
return code;
21+
}
22+
public String getMessage() {
23+
return message;
24+
}
25+
26+
public void setCode(String code) {
27+
this.code = code;
28+
}
29+
public void setMessage(String message) {
30+
this.message = message;
31+
}
32+
}
Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package dev.tomr.hcloud.http.model;
22

3-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4-
import com.fasterxml.jackson.annotation.JsonInclude;
53
import dev.tomr.hcloud.http.HetznerJsonObject;
64

75
public class HetznerErrorResponse extends HetznerJsonObject {
@@ -21,31 +19,4 @@ public String toString() {
2119
return "HetznerErrorResponse [code=" + error.getCode() + ", message=" + error.getMessage() + "]";
2220
}
2321

24-
@JsonIgnoreProperties(ignoreUnknown = true)
25-
@JsonInclude(JsonInclude.Include.NON_NULL)
26-
public static class Error {
27-
private String code;
28-
private String message;
29-
30-
public Error(String code, String message) {
31-
this.code = code;
32-
this.message = message;
33-
}
34-
35-
public Error() {}
36-
37-
public String getCode() {
38-
return code;
39-
}
40-
public String getMessage() {
41-
return message;
42-
}
43-
44-
public void setCode(String code) {
45-
this.code = code;
46-
}
47-
public void setMessage(String message) {
48-
this.message = message;
49-
}
50-
}
5122
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package dev.tomr.hcloud.http.model;
2+
3+
public class Resource {
4+
private Integer id;
5+
private String type;
6+
7+
public Resource(Integer id, String type) {
8+
this.id = id;
9+
this.type = type;
10+
}
11+
12+
public Resource() {
13+
}
14+
15+
public Integer getId() {
16+
return id;
17+
}
18+
19+
public void setId(Integer id) {
20+
this.id = id;
21+
}
22+
23+
public String getType() {
24+
return type;
25+
}
26+
27+
public void setType(String type) {
28+
this.type = type;
29+
}
30+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package dev.tomr.hcloud.http.model.enums;
2+
3+
public enum Status {
4+
RUNNING, SUCCESS, ERROR
5+
}

src/main/java/dev/tomr/hcloud/listener/ListenerManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private ListenerManager() {
1111
}
1212

1313
/**
14-
* Get's the ListenerManager Instance, creates one if it doesn't exist
14+
* Gets the ListenerManager Instance, creates one if it doesn't exist
1515
* @return {@code ListenerManager} in use
1616
*/
1717
public static ListenerManager getInstance() {

src/main/java/dev/tomr/hcloud/listener/ServerChangeListener.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ public class ServerChangeListener implements PropertyChangeListener {
1919
@Override
2020
public void propertyChange(PropertyChangeEvent evt) {
2121
Server server = (Server) evt.getSource();
22-
logger.info("Server changed: " + evt.getPropertyName());
23-
logger.info("Server: " + evt.getOldValue() + " -> " + evt.getNewValue());
24-
HetznerCloud.getInstance().getServiceManager().getServerService().serverNameOrLabelUpdate(evt.getPropertyName(), evt.getNewValue(), server);
22+
if (evt.getPropertyName().equals("delete")) {
23+
logger.warn("Server delete has been called. Instructing Hetzner to delete");
24+
HetznerCloud.getInstance().getServiceManager().getServerService().deleteServerFromHetzner(server);
25+
} else {
26+
logger.info("Server changed: " + evt.getPropertyName());
27+
logger.info("Server: " + evt.getOldValue() + " -> " + evt.getNewValue());
28+
HetznerCloud.getInstance().getServiceManager().getServerService().serverNameOrLabelUpdate(evt.getPropertyName(), evt.getNewValue(), server);
29+
}
2530
}
2631
}

src/main/java/dev/tomr/hcloud/resources/server/Server.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ private void setupPropertyChangeListener() {
9797
propertyChangeSupport.addPropertyChangeListener(HetznerCloud.getInstance().getListenerManager().getServerChangeListener());
9898
}
9999

100+
/**
101+
* Deletes a Server from Hetzner. Note, this is immediate and destructive. Ensure you want to delete the server before calling.
102+
*/
103+
public void delete() {
104+
propertyChangeSupport.firePropertyChange("delete", null, null);
105+
}
106+
100107

101108
// These are the current setters that will send an API request (PUT /servers) when actions begin to be added, they will also likely be triggered by setters
102109

0 commit comments

Comments
 (0)