-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHetznerCloud.java
More file actions
120 lines (103 loc) · 4.12 KB
/
HetznerCloud.java
File metadata and controls
120 lines (103 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package dev.tomr.hcloud;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import dev.tomr.hcloud.listener.ListenerManager;
import dev.tomr.hcloud.resources.server.Server;
import dev.tomr.hcloud.service.ServiceManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
/**
* Entrypoint to interact with the Hetzner Cloud API, via hcloud-java. Construct with an available construtor, alternatively call .getInstance()
*/
public class HetznerCloud {
protected static final Logger logger = LogManager.getLogger();
private static final ObjectMapper objectMapper = JsonMapper.builder().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true).build();
private static final String HETZNER_CLOUD_HOST = "https://api.hetzner.cloud/v1/";
private static HetznerCloud instance;
private final ListenerManager listenerManager = ListenerManager.getInstance();
private final ServiceManager serviceManager = ServiceManager.getInstance();
private String apiKey;
private final String host;
/**
* Constructs a new {@code HetznerCloud} Instance with the given API key.
* @param apiKey API Key to use from the Hetzner Cloud Dashboard
*/
public HetznerCloud(String apiKey) {
this.apiKey = apiKey;
this.host = HETZNER_CLOUD_HOST;
instance = this;
}
/**
* Constructs a new {@code HetznerCloud} Instance with the given API key and a different host. This is mainly a testing constructor, it's unlikely you should call this.
* @param apiKey API Key to use from the Hetzner Cloud Dashboard
* @param host Different host to use in REST calls. Should start with http(s)://
*/
public HetznerCloud(String apiKey, String host) {
this.apiKey = apiKey;
this.host = host;
instance = this;
}
/**
* Get the current instance of {@code HetznerCloud} to interact with the API. If an instance doesn't exist, this will create a new instance.
* @return The instance of {@code HetznerCloud} being used
*/
public static HetznerCloud getInstance() {
if (instance == null) {
instance = new HetznerCloud(null);
logger.warn("Hetzner Cloud API Key needs to be set");
}
return instance;
}
/**
* Set the API key for REST calls. Necessary if you create the instance with the .getInstance() method
* @param apiKey API Key to use from the Hetzner Cloud Dashboard
*/
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
/**
* Get the shared Jackson {@code ObjectMapper}
* @return The shared {@code ObjectMapper} instance
*/
public static ObjectMapper getObjectMapper() {
return objectMapper;
}
/**
* Get the internal {@code ListenerManager}. End users do not need to interact with the {@code ListenerManager}
* @return The {@code ListenerManager} Instance
*/
public ListenerManager getListenerManager() {
return listenerManager;
}
/**
* Get the internal {@code ServiceManager}. End users do not need to interact with the {@code ServiceManager}
* @return The {@code ServiceManager} Instance
*/
public ServiceManager getServiceManager() {
return serviceManager;
}
/**
* Get the HTTP host and Hetzner Cloud API Key. End users do not need to interact with this.
* @return A {@code List<String>}, with the HTTP host at 0, and the API key at 1.
*/
public List<String> getHttpDetails() {
return List.of(host, apiKey);
}
/**
* Whether we have an API Key supplied
* @return true if one is present, false if not
*/
public boolean hasApiKey() {
return apiKey != null && !apiKey.isEmpty();
}
/**
* Get a Hetzner Server Instance from the local cache
* @param id ID of the server
* @return A server object from the local cache
*/
public Server getServer(Integer id) {
return serviceManager.getServerService().getServer(id);
}
}