-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhetzner.js
More file actions
67 lines (56 loc) · 1.55 KB
/
Copy pathhetzner.js
File metadata and controls
67 lines (56 loc) · 1.55 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
const axios = require("axios")
class Hetzner {
constructor(token) {
this.axios = axios.create({
baseURL: 'https://api.hetzner.cloud/v1',
timeout: 50000,
headers: {
'Authorization': `Bearer ${token}`
}
})
}
getServers() {
return this.axios.get('/servers').then(r => r.data)
}
getLoadbalancers() {
return this.axios.get('/load_balancers').then(r => r.data)
}
getLoadbalancer(id) {
return this.axios.get(`/load_balancers/${id}`).then(r => r.data)
}
addTargetToLoadbalancer(loadbalancer_id, server_id) {
return this.axios.post(`/load_balancers/${loadbalancer_id}/actions/add_target`, {
"server": {
"id": server_id
},
"type": "server",
"use_private_ip": true
}).then(r => r.data)
}
removeLoadbalancerTarget(loadbalancer_id, server_id) {
return this.axios.post(`/load_balancers/${loadbalancer_id}/actions/remove_target`, {
"server": {
"id": server_id
},
"type": "server",
"use_private_ip": true
}).then(r => r.data)
}
}
const findServer = (hetznerServerResponse, ip) => {
return hetznerServerResponse.servers.find(s => s.public_net.ipv4.ip === ip)
}
const findLoadbalancersWithServer = (hetznerLoadbalancersResponse, serverId) => {
const lbs = []
for (const lb of hetznerLoadbalancersResponse.load_balancers) {
if(lb.targets.filter(t => t.type === "server").find(t => t.server.id === serverId)) {
lbs.push(lb)
}
}
return lbs
}
module.exports = {
Hetzner,
findServer,
findLoadbalancersWithServer
}