1
1
package czm
2
2
3
3
import (
4
- cfgo "github.com/cloudflare/cloudflare-go"
4
+ "context"
5
+ "github.com/cloudflare/cloudflare-go"
5
6
"github.com/logrusorgru/aurora"
6
7
"github.com/quan-to/slog"
7
8
)
8
9
9
10
type Cloudflare struct {
10
- Email string `yaml:"email"`
11
- APIKey string `yaml:"api_key"`
11
+ Email string `yaml:"email"`
12
+ APIKey string `yaml:"api_key"`
12
13
SLog * slog.Instance
13
- Api * cfgo.API
14
- UserInfo cfgo.User
14
+ Api * cloudflare.API
15
+ UserInfo cloudflare.User
16
+ ctx context.Context
15
17
}
16
18
17
- func (cf * Cloudflare ) Initialize () (* Cloudflare ) {
19
+ func (cf * Cloudflare ) Initialize () * Cloudflare {
20
+ cf .ctx = context .Background ()
18
21
cf .SLog = slog .Scope ("Cloudflare-API" )
19
22
20
- api , err := cfgo .New (cf .APIKey , cf .Email )
23
+ api , err := cloudflare .New (cf .APIKey , cf .Email )
21
24
cf .Api = api
22
25
23
26
if err != nil {
24
27
cf .SLog .Fatal (err )
25
- } else {
26
- cf .SLog .Info (`Cloudflare connection is setting!` )
27
28
}
28
29
29
- cf .SLog .Info (`Fetching User informations` )
30
- cf .UserInfo , err = cf .Api .UserDetails ()
30
+ cf .SLog .Info (`Cloudflare connection successful created` )
31
+ cf .SLog .Info (`Fetching user information` )
32
+
33
+ cf .UserInfo , err = cf .Api .UserDetails (cf .ctx )
31
34
32
35
if err != nil {
33
- cf .SLog .Error (`Fail to fetch user informations ` )
36
+ cf .SLog .Error (`Fail to fetch user information ` )
34
37
cf .SLog .Fatal (err )
35
38
}
36
39
37
40
return cf
38
41
}
39
42
40
- func (cf * Cloudflare ) ExistsZone (zone * Zone ) ( bool ) {
41
- zoneResponse , err := cf .Api .ZoneDetails (zone .Id )
43
+ func (cf * Cloudflare ) ExistsZone (zone * Zone ) bool {
44
+ zoneResponse , err := cf .Api .ZoneDetails (cf . ctx , zone .Id )
42
45
43
46
if err != nil {
44
47
cf .SLog .Error (err )
@@ -55,7 +58,8 @@ func (cf *Cloudflare) ExistsZone(zone *Zone) (bool) {
55
58
56
59
func (cf * Cloudflare ) LoadDNSRecords (zone * Zone ) {
57
60
cf .SLog .Info (`Loading DNS Records for %s zone` , zone .Hostname )
58
- records , err := cf .Api .DNSRecords (zone .Id , cfgo.DNSRecord {})
61
+ zoneId := cloudflare .ZoneIdentifier (zone .Id )
62
+ records , _ , err := cf .Api .ListDNSRecords (cf .ctx , zoneId , cloudflare.ListDNSRecordsParams {})
59
63
60
64
if err != nil {
61
65
cf .SLog .Fatal (err )
@@ -64,8 +68,8 @@ func (cf *Cloudflare) LoadDNSRecords(zone *Zone) {
64
68
zone .DNSRecords = records
65
69
}
66
70
67
- func (cf * Cloudflare ) ExistsDNSRecord (zone * Zone , dns * Dns ) ( bool ) {
68
- for _ , value := range zone .DNSRecords {
71
+ func (cf * Cloudflare ) ExistsDNSRecord (zone * Zone , dns * Dns ) bool {
72
+ for _ , value := range zone .DNSRecords {
69
73
if value .Name == dns .Name {
70
74
dns .ID = value .ID
71
75
return true
@@ -75,80 +79,85 @@ func (cf *Cloudflare) ExistsDNSRecord(zone *Zone, dns *Dns) (bool) {
75
79
return false
76
80
}
77
81
78
- func (cf * Cloudflare ) CreateDNSRecord (zone * Zone , dns * Dns ) ( error ) {
82
+ func (cf * Cloudflare ) CreateDNSRecord (zone * Zone , dns * Dns ) error {
79
83
if dns .Content == "" {
80
84
cf .SLog .SubScope (dns .Name ).Info ("Resolving module" )
81
85
dns .Content = dns .Module .Resolve ()
82
86
}
83
87
84
- dres , err := cf .Api .CreateDNSRecord (zone .Id , cfgo.DNSRecord {
85
- ID : dns .ID ,
86
- Type : dns .Dtype ,
87
- Name : dns .Name ,
88
+ zoneId := cloudflare .ZoneIdentifier (zone .Id )
89
+ response , err := cf .Api .CreateDNSRecord (cf .ctx , zoneId , cloudflare.CreateDNSRecordParams {
90
+ ID : dns .ID ,
91
+ Type : dns .Dtype ,
92
+ Name : dns .Name ,
88
93
Content : dns .Content ,
89
94
Proxied : dns .Proxied ,
90
95
})
91
96
92
97
if err == nil {
93
- dns .ID = dres . Result .ID
98
+ dns .ID = response .ID
94
99
}
95
100
96
101
cf .SLog .SubScope (dns .Name ).Info ("Zone created" )
97
102
98
103
return err
99
104
}
100
105
101
- func (cf * Cloudflare ) DNSRecordHasDiff (zone * Zone , dns * Dns ) (bool ) {
102
- dnsData , err := cf .Api .DNSRecord (zone .Id , dns .ID )
106
+ func (cf * Cloudflare ) DNSRecordHasDiff (zone * Zone , dns * Dns ) bool {
107
+ log := cf .SLog .SubScope (dns .Name )
108
+ zoneId := cloudflare .ZoneIdentifier (zone .Id )
109
+ response , err := cf .Api .GetDNSRecord (cf .ctx , zoneId , dns .ID )
110
+
111
+ hasDiff := false
103
112
104
113
if err != nil {
105
- cf . SLog .Error (err )
114
+ log .Error (err )
106
115
return false
107
116
}
108
117
109
- if dnsData .Name != dns .Name {
110
- cf . SLog . SubScope ( dns . Name ). Info ("Has different name" )
111
- return true
118
+ if response .Name != dns .Name {
119
+ log . Info ("DNS Zone has different name" )
120
+ hasDiff = true
112
121
}
113
122
114
- if dnsData .Proxiable {
115
- if dns .Proxied != dnsData .Proxied {
116
- cf . SLog . SubScope ( dns . Name ). Info ("Has mark with different Proxied " )
117
- return true
123
+ if response .Proxiable {
124
+ if response .Proxied != dns .Proxied {
125
+ log . Info ("" )
126
+ hasDiff = true
118
127
}
119
128
}
120
129
121
- if dnsData .Content != dns .Content {
122
- var log = cf .SLog .SubScope (dns .Name )
123
- log .Info ("Has mark with different Content" )
124
- log .Log (` diff(%s, %s%s)` , aurora .Red (dnsData .Content ), aurora .Green (dns .Content ), aurora .Cyan ("" ))
125
- return true
130
+ if response .Content != dns .Content {
131
+ log .Info ("DNS Zone has different payload" )
132
+ log .Log (` diff(%s, %s)%s` , aurora .Red (response .Content ), aurora .Green (dns .Content ), aurora .Cyan ("" ))
133
+ hasDiff = true
126
134
}
127
135
128
- if dnsData .Type != dns .Dtype {
129
- cf . SLog . SubScope ( dns . Name ). Info ("Has mark with different Type " )
130
- return true
136
+ if response .Type != dns .Dtype {
137
+ log . Info ("DNS Zone has different type " )
138
+ hasDiff = true
131
139
}
132
140
133
- if dnsData .TTL != dns .TTL {
134
- cf . SLog . SubScope ( dns . Name ). Info ("Has mark with different TTL" )
135
- return true
141
+ if response .TTL != dns .TTL {
142
+ log . Info ("DNS Zone has different TTL" )
143
+ hasDiff = true
136
144
}
137
145
138
- return false
146
+ return hasDiff
139
147
}
140
148
141
- func (cf * Cloudflare ) UpdateDNSRecord (zone * Zone , dns * Dns ) (error ) {
142
-
143
- cf .SLog .SubScope (dns .Name ).Info (`Updating DNSRecord` )
149
+ func (cf * Cloudflare ) UpdateDNSRecord (zone * Zone , dns * Dns ) error {
144
150
145
- err := cf .Api .UpdateDNSRecord (zone .Id , dns .ID , cfgo.DNSRecord {
146
- Name : dns .Name ,
147
- Type : dns .Dtype ,
151
+ cf .SLog .SubScope (dns .Name ).Info (`Updating Record` )
152
+ zoneId := cloudflare .ZoneIdentifier (zone .Id )
153
+ _ , err := cf .Api .UpdateDNSRecord (cf .ctx , zoneId , cloudflare.UpdateDNSRecordParams {
154
+ ID : dns .ID ,
155
+ Name : dns .Name ,
156
+ Type : dns .Dtype ,
148
157
Content : dns .Content ,
149
- TTL : dns .TTL ,
158
+ TTL : dns .TTL ,
150
159
Proxied : dns .Proxied ,
151
160
})
152
161
153
162
return err
154
- }
163
+ }
0 commit comments