-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhosttechClient.go
76 lines (64 loc) · 1.85 KB
/
hosttechClient.go
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
package main
import (
"context"
"github.com/libdns/hosttech"
"github.com/libdns/libdns"
"log"
"os"
)
func DoesRecordAlreadyExist(domain string) (bool, libdns.Record) {
record, err := getRecord(domain)
if err == nil && record.ID != "" {
return true, record
} else {
return false, libdns.Record{}
}
}
func UpdateRecord(ip string, existingRecord libdns.Record) {
log.Printf("Updating A record for zone %s, domain %s with IP %s", os.Getenv("ZONE"), os.Getenv("DOMAIN"), ip)
provider := hosttech.Provider{APIToken: os.Getenv("API_KEY")}
_, err := provider.SetRecords(context.Background(), os.Getenv("ZONE"), []libdns.Record{
{
ID: existingRecord.ID,
Type: existingRecord.Type,
Name: existingRecord.Name,
Value: ip,
TTL: 3600,
Priority: 0,
},
})
if err != nil {
log.Printf("Could not update IP because of an error %s", err)
}
}
func CreateNewRecord(ip string, domain string) {
log.Printf("Creating a new A record for zone %s, domain %s with IP %s", os.Getenv("ZONE"), domain, ip)
provider := hosttech.Provider{APIToken: os.Getenv("API_KEY")}
_, err := provider.AppendRecords(context.Background(), os.Getenv("ZONE"), []libdns.Record{
{
ID: "",
Type: "A",
Name: domain,
Value: ip,
TTL: 3600,
Priority: 0,
},
})
if err != nil {
log.Printf("Could not create new record because of an error %s", err)
}
}
func getRecord(domain string) (libdns.Record, error) {
provider := hosttech.Provider{APIToken: os.Getenv("API_KEY")}
records, err := provider.GetRecords(context.Background(), os.Getenv("ZONE"))
if err != nil {
log.Printf("Could not fetch existing records from API because of an error %s", err)
return libdns.Record{}, err
}
for _, record := range records {
if record.Type == "A" && record.Name == domain {
return record, nil
}
}
return libdns.Record{}, nil
}