···
3
-
let authHeader = ["authorization" $"Bearer ($env.HETZNER_API_TOKEN)"]
3
+
def main [firewallId: number, rulesFile: path, --auth-token (-t): string] {
4
+
let auth_token: string = if $auth_token == null { $env.HETZNER_API_TOKEN? } else { $auth_token }
5
+
let authHeader: list<string> = ["authorization" $"Bearer ($auth_token)"]
5
-
def makeApiUrl [path: string] {
6
-
return $"https://api.hetzner.cloud/v1($path)"
8
-
def post [path: string] {
9
-
$in | http post -e --full -H $authHeader --content-type application/json (makeApiUrl $path)
11
-
def get [path: string] {
12
-
http get -e --full -H $authHeader (makeApiUrl $path)
7
+
def makeApiUrl [path: string] {
8
+
return $"https://api.hetzner.cloud/v1($path)"
10
+
def post [path: string] {
11
+
$in | http post -e --full -H $authHeader --content-type application/json (makeApiUrl $path)
13
+
def get [path: string] {
14
+
http get -e --full -H $authHeader (makeApiUrl $path)
15
-
# first fetch firewall to see if it even exists
16
-
let resp = get $"/firewalls/($firewallId)"
17
-
if $resp.status == 404 {
18
-
log error $"provided firewall \(id ($firewallId)\) does not exist"
21
-
let firewall = $resp.body | get firewall
17
+
# first fetch firewall to see if it even exists
18
+
let resp = get $"/firewalls/($firewallId)"
19
+
if $resp.status == 404 {
20
+
log error $"provided firewall \(id ($firewallId)\) does not exist"
22
+
} else if $resp.status != 200 {
23
+
log error $"could not get firewall \(id ($firewallId)\):\n($resp.body.error | to text -n)"
26
+
let firewall = $resp.body | get firewall
24
-
let backupPath = $".hetzner/($firewallId).json"
25
-
mkdir .hetzner; $firewall | to json | save -f $backupPath
26
-
log info $"backing up firewall ($firewallId) to ($backupPath)"
29
+
let backupPath = $".hetzner/($firewallId).json"
30
+
mkdir .hetzner; $firewall | to json | save -f $backupPath
31
+
log info $"backing up firewall ($firewallId) to ($backupPath)"
29
-
let resp = open $rulesFile | post $"/firewalls/($firewallId)/actions/set_rules"
30
-
if $resp.status != 201 {
31
-
log error $"could not apply firewall \(id ($firewallId)\):\n($resp.body | to text)"
34
+
let resp = open $rulesFile | post $"/firewalls/($firewallId)/actions/set_rules"
35
+
if $resp.status != 201 {
36
+
log error $"could not apply firewall \(id ($firewallId)\):\n($resp.body.error | to text -n)"
39
+
log info $"applied firewall ($firewallId)"
34
-
log info $"applied firewall ($firewallId)"