forked from lucasgolino/cloudflare-zone-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare.go
154 lines (120 loc) · 3.11 KB
/
cloudflare.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package czm
import (
cfgo "github.com/cloudflare/cloudflare-go"
"github.com/logrusorgru/aurora"
"github.com/quan-to/slog"
)
type Cloudflare struct {
Email string `yaml:"email"`
APIKey string `yaml:"api_key"`
SLog *slog.Instance
Api *cfgo.API
UserInfo cfgo.User
}
func (cf *Cloudflare) Initialize() (*Cloudflare) {
cf.SLog = slog.Scope("Cloudflare-API")
api, err := cfgo.New(cf.APIKey, cf.Email)
cf.Api = api
if err != nil {
cf.SLog.Fatal(err)
} else {
cf.SLog.Info(`Cloudflare connection is setting!`)
}
cf.SLog.Info(`Fetching User informations`)
cf.UserInfo, err = cf.Api.UserDetails()
if err != nil {
cf.SLog.Error(`Fail to fetch user informations`)
cf.SLog.Fatal(err)
}
return cf
}
func (cf *Cloudflare) ExistsZone(zone *Zone) (bool) {
zoneResponse, err := cf.Api.ZoneDetails(zone.Id)
if err != nil {
cf.SLog.Error(err)
return false
}
if zoneResponse.Name != zone.Hostname {
cf.SLog.Error(`Zone has a different hostname, mark skip`)
return false
}
return true
}
func (cf *Cloudflare) LoadDNSRecords(zone *Zone) {
cf.SLog.Info(`Loading DNS Records for %s zone`, zone.Hostname)
records, err := cf.Api.DNSRecords(zone.Id, cfgo.DNSRecord{})
if err != nil {
cf.SLog.Fatal(err)
}
zone.DNSRecords = records
}
func (cf *Cloudflare) ExistsDNSRecord(zone *Zone, dns *Dns) (bool) {
for _, value := range zone.DNSRecords {
if value.Name == dns.Name {
dns.ID = value.ID
return true
}
}
return false
}
func (cf *Cloudflare) CreateDNSRecord(zone *Zone, dns *Dns) (error) {
if dns.Content == "" {
cf.SLog.SubScope(dns.Name).Info("Resolving module")
dns.Content = dns.Module.Resolve()
}
dres, err := cf.Api.CreateDNSRecord(zone.Id, cfgo.DNSRecord{
ID: dns.ID,
Type: dns.Dtype,
Name: dns.Name,
Content: dns.Content,
Proxied: dns.Proxied,
})
if err == nil {
dns.ID = dres.Result.ID
}
cf.SLog.SubScope(dns.Name).Info("Zone created")
return err
}
func (cf *Cloudflare) DNSRecordHasDiff(zone *Zone, dns *Dns) (bool) {
dnsData, err := cf.Api.DNSRecord(zone.Id, dns.ID)
if err != nil {
cf.SLog.Error(err)
return false
}
if dnsData.Name != dns.Name {
cf.SLog.SubScope(dns.Name).Info("Has different name")
return true
}
if dnsData.Proxiable {
if dns.Proxied != dnsData.Proxied {
cf.SLog.SubScope(dns.Name).Info("Has mark with different Proxied")
return true
}
}
if dnsData.Content != dns.Content {
var log = cf.SLog.SubScope(dns.Name)
log.Info("Has mark with different Content")
log.Log(` diff(%s, %s%s)`, aurora.Red(dnsData.Content), aurora.Green(dns.Content), aurora.Cyan(""))
return true
}
if dnsData.Type != dns.Dtype {
cf.SLog.SubScope(dns.Name).Info("Has mark with different Type")
return true
}
if dnsData.TTL != dns.TTL {
cf.SLog.SubScope(dns.Name).Info("Has mark with different TTL")
return true
}
return false
}
func (cf *Cloudflare) UpdateDNSRecord(zone *Zone, dns *Dns) (error) {
cf.SLog.SubScope(dns.Name).Info(`Updating DNSRecord`)
err := cf.Api.UpdateDNSRecord(zone.Id, dns.ID, cfgo.DNSRecord{
Name: dns.Name,
Type: dns.Dtype,
Content: dns.Content,
TTL: dns.TTL,
Proxied: dns.Proxied,
})
return err
}