From 19999a509077a7d9250644793fd7932cb4c70554 Mon Sep 17 00:00:00 2001 From: Tom Siewert Date: Sat, 21 Apr 2018 12:17:42 +0200 Subject: [PATCH] Added Server-,FloatingIP- and Image-Protection --- README.md | 6 +-- pom.xml | 2 +- .../hetznercloud/HetznerCloudAPI.java | 44 ++++++++++++++++--- .../hetznercloud/objects/general/Image.java | 6 +++ .../hetznercloud/objects/general/Server.java | 8 ++++ .../request/ChangeProtectionRequest.java | 16 +++++++ 6 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 src/main/java/me/tomsdevsn/hetznercloud/objects/request/ChangeProtectionRequest.java diff --git a/README.md b/README.md index 53312d01..db04fb5a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Hetzner Cloud API for Java Simple Java client for the Hetzner Cloud API. -The current version is **2.1.1**. +The current version is **2.2.0**. It would be nice, if you submit pull requests. @@ -37,7 +37,7 @@ Dependency: me.tomsdevsn hetznercloud-api - 2.1.1 + 2.2.0 compile @@ -55,7 +55,7 @@ repositories({ }) dependencies({ - compile "me.tomsdevsn:hetznercloud-api:2.1.1" + compile "me.tomsdevsn:hetznercloud-api:2.2.0" }) ``` diff --git a/pom.xml b/pom.xml index f39f9e42..daad4024 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.tomsdevsn hetznercloud-api - 2.1.1 + 2.2.0 HetznerCloud-API Java-client for the Hetzner Cloud diff --git a/src/main/java/me/tomsdevsn/hetznercloud/HetznerCloudAPI.java b/src/main/java/me/tomsdevsn/hetznercloud/HetznerCloudAPI.java index 75a6e22f..b7eec162 100644 --- a/src/main/java/me/tomsdevsn/hetznercloud/HetznerCloudAPI.java +++ b/src/main/java/me/tomsdevsn/hetznercloud/HetznerCloudAPI.java @@ -125,6 +125,17 @@ public ConsoleResponse requestConsole(long id) { return restTemplate.postForEntity(API_URL + "/servers/" + id + "/actions/request_console", httpEntity, ConsoleResponse.class).getBody(); } + /** + * Change the protection configuration from a server + * + * @param id ID of the server + * @param changeProtection Request Object (both optional) + * @return ActionResponse object + */ + public ActionResponse changeServerProtection(long id, ChangeProtectionRequest changeProtection) { + return restTemplate.postForEntity(API_URL + "/servers/" + id + "/actions/change_protection", new HttpEntity<>(changeProtection, httpHeaders), ActionResponse.class).getBody(); + } + /** * Get all performed Actions for a Server * @@ -174,7 +185,7 @@ public ActionResponse getActionOfFloatingIP(long floatingIPID, long actionID) { * @return respond */ public ActionResponse powerOnServer(long id) { - return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/poweron", HttpMethod.POST, httpEntity, ActionResponse.class).getBody(); + return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/poweron", HttpMethod.POST, new HttpEntity<>(httpHeaders), ActionResponse.class).getBody(); } /** @@ -184,7 +195,7 @@ public ActionResponse powerOnServer(long id) { * @return respond */ public ActionResponse powerOffServer(long id) { - return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/poweroff", HttpMethod.POST, httpEntity, ActionResponse.class).getBody(); + return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/poweroff", HttpMethod.POST, new HttpEntity<>(httpHeaders), ActionResponse.class).getBody(); } /** @@ -194,7 +205,7 @@ public ActionResponse powerOffServer(long id) { * @return respond */ public ActionResponse softRebootServer(long id) { - return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/reboot", HttpMethod.POST, httpEntity, ActionResponse.class).getBody(); + return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/reboot", HttpMethod.POST, new HttpEntity<>(httpHeaders), ActionResponse.class).getBody(); } /** @@ -204,7 +215,7 @@ public ActionResponse softRebootServer(long id) { * @return respond */ public ActionResponse resetServer(long id) { - return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/reset", HttpMethod.POST, httpEntity, ActionResponse.class).getBody(); + return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/reset", HttpMethod.POST, new HttpEntity<>(httpHeaders), ActionResponse.class).getBody(); } /** @@ -214,7 +225,7 @@ public ActionResponse resetServer(long id) { * @return respond */ public ActionResponse shutdownServer(long id) { - return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/shutdown", HttpMethod.POST, httpEntity, ActionResponse.class).getBody(); + return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/shutdown", HttpMethod.POST, new HttpEntity<>(httpHeaders), ActionResponse.class).getBody(); } /** @@ -339,6 +350,18 @@ public CreateImageResponse createImage(long id, CreateImageRequest createImageRe return restTemplate.exchange(API_URL + "/servers/" + id + "/actions/create_image", HttpMethod.POST, new HttpEntity<>(createImageRequest, httpHeaders), CreateImageResponse.class).getBody(); } + + /** + * Enable or disable the Protection of an Image + * + * @param id ID of the Image + * @param protectionRequest Only the delete parameter! + * @return ActionResponse object + */ + public ActionResponse changeImageProtection(long id, ChangeProtectionRequest protectionRequest) { + return restTemplate.postForEntity(API_URL + "/images/" + id + "/actions/change_protection", new HttpEntity<>(protectionRequest, httpHeaders), ActionResponse.class).getBody(); + } + /** * Enable the backups from a server *

@@ -487,6 +510,17 @@ public FloatingIPResponse createFloatingIP(FloatingIPRequest floatingIPRequest) return restTemplate.postForEntity(API_URL + "/floating_ips", new HttpEntity<>(floatingIPRequest, httpHeaders), FloatingIPResponse.class).getBody(); } + /** + * Enable or disable the Protection of a Floating IP + * + * @param id ID of the Floating IP + * @param protectionRequest Only the delete parameter! + * @return ActionResponse object + */ + public ActionResponse changeFloatingIPProtection(long id, ChangeProtectionRequest protectionRequest) { + return restTemplate.postForEntity(API_URL + "/floating_ips/" + id + "/actions/change_protection", new HttpEntity<>(protectionRequest, httpHeaders), ActionResponse.class).getBody(); + } + /** * Change the description of a Floating IP. *

diff --git a/src/main/java/me/tomsdevsn/hetznercloud/objects/general/Image.java b/src/main/java/me/tomsdevsn/hetznercloud/objects/general/Image.java index 5737ddd6..87ae46fc 100644 --- a/src/main/java/me/tomsdevsn/hetznercloud/objects/general/Image.java +++ b/src/main/java/me/tomsdevsn/hetznercloud/objects/general/Image.java @@ -1,6 +1,7 @@ package me.tomsdevsn.hetznercloud.objects.general; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import lombok.Data; import me.tomsdevsn.hetznercloud.deserialize.DateDeserializer; @@ -33,6 +34,7 @@ public class Image { private boolean rapidRedeploy; @JsonDeserialize(using = DateDeserializer.class) private Date deprecated; + private Protect protection; @Data public static class CreatedFrom { @@ -40,4 +42,8 @@ public static class CreatedFrom { private String name; } + @Data + public static class Protect { + private boolean delete; + } } diff --git a/src/main/java/me/tomsdevsn/hetznercloud/objects/general/Server.java b/src/main/java/me/tomsdevsn/hetznercloud/objects/general/Server.java index 3ed8c090..813db928 100644 --- a/src/main/java/me/tomsdevsn/hetznercloud/objects/general/Server.java +++ b/src/main/java/me/tomsdevsn/hetznercloud/objects/general/Server.java @@ -33,4 +33,12 @@ public class Server { private Long ingoingTraffic; @JsonProperty("included_traffic") private Long includedTraffic; + private Protect protection; + + @Data + public static class Protect { + private boolean delete; + private boolean rebuild; + } + } diff --git a/src/main/java/me/tomsdevsn/hetznercloud/objects/request/ChangeProtectionRequest.java b/src/main/java/me/tomsdevsn/hetznercloud/objects/request/ChangeProtectionRequest.java new file mode 100644 index 00000000..fda63e10 --- /dev/null +++ b/src/main/java/me/tomsdevsn/hetznercloud/objects/request/ChangeProtectionRequest.java @@ -0,0 +1,16 @@ +package me.tomsdevsn.hetznercloud.objects.request; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class ChangeProtectionRequest { + + private boolean delete; + private boolean rebuild; +}