Skip to content

Commit b4805ec

Browse files
committed
feat(bump): cloudflare version and improve code and messages
1 parent a568798 commit b4805ec

File tree

9 files changed

+160
-132
lines changed

9 files changed

+160
-132
lines changed

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export CONFIG_MAP_PATH=CONFIG_MAP_PATH
2+
export CONFIG_MOD_PATH=CONFIG_MOD_PATH
3+
4+
source ./.envrc

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.so
33
.idea
44
vendor
5+
.envrc

cloudflare.go

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
11
package czm
22

33
import (
4-
cfgo "github.com/cloudflare/cloudflare-go"
4+
"context"
5+
"github.com/cloudflare/cloudflare-go"
56
"github.com/logrusorgru/aurora"
67
"github.com/quan-to/slog"
78
)
89

910
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"`
1213
SLog *slog.Instance
13-
Api *cfgo.API
14-
UserInfo cfgo.User
14+
Api *cloudflare.API
15+
UserInfo cloudflare.User
16+
ctx context.Context
1517
}
1618

17-
func (cf *Cloudflare) Initialize() (*Cloudflare) {
19+
func (cf *Cloudflare) Initialize() *Cloudflare {
20+
cf.ctx = context.Background()
1821
cf.SLog = slog.Scope("Cloudflare-API")
1922

20-
api, err := cfgo.New(cf.APIKey, cf.Email)
23+
api, err := cloudflare.New(cf.APIKey, cf.Email)
2124
cf.Api = api
2225

2326
if err != nil {
2427
cf.SLog.Fatal(err)
25-
} else {
26-
cf.SLog.Info(`Cloudflare connection is setting!`)
2728
}
2829

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)
3134

3235
if err != nil {
33-
cf.SLog.Error(`Fail to fetch user informations`)
36+
cf.SLog.Error(`Fail to fetch user information`)
3437
cf.SLog.Fatal(err)
3538
}
3639

3740
return cf
3841
}
3942

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)
4245

4346
if err != nil {
4447
cf.SLog.Error(err)
@@ -55,7 +58,8 @@ func (cf *Cloudflare) ExistsZone(zone *Zone) (bool) {
5558

5659
func (cf *Cloudflare) LoadDNSRecords(zone *Zone) {
5760
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{})
5963

6064
if err != nil {
6165
cf.SLog.Fatal(err)
@@ -64,8 +68,8 @@ func (cf *Cloudflare) LoadDNSRecords(zone *Zone) {
6468
zone.DNSRecords = records
6569
}
6670

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 {
6973
if value.Name == dns.Name {
7074
dns.ID = value.ID
7175
return true
@@ -75,80 +79,85 @@ func (cf *Cloudflare) ExistsDNSRecord(zone *Zone, dns *Dns) (bool) {
7579
return false
7680
}
7781

78-
func (cf *Cloudflare) CreateDNSRecord(zone *Zone, dns *Dns) (error) {
82+
func (cf *Cloudflare) CreateDNSRecord(zone *Zone, dns *Dns) error {
7983
if dns.Content == "" {
8084
cf.SLog.SubScope(dns.Name).Info("Resolving module")
8185
dns.Content = dns.Module.Resolve()
8286
}
8387

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,
8893
Content: dns.Content,
8994
Proxied: dns.Proxied,
9095
})
9196

9297
if err == nil {
93-
dns.ID = dres.Result.ID
98+
dns.ID = response.ID
9499
}
95100

96101
cf.SLog.SubScope(dns.Name).Info("Zone created")
97102

98103
return err
99104
}
100105

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
103112

104113
if err != nil {
105-
cf.SLog.Error(err)
114+
log.Error(err)
106115
return false
107116
}
108117

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
112121
}
113122

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
118127
}
119128
}
120129

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
126134
}
127135

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
131139
}
132140

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
136144
}
137145

138-
return false
146+
return hasDiff
139147
}
140148

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 {
144150

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,
148157
Content: dns.Content,
149-
TTL: dns.TTL,
158+
TTL: dns.TTL,
150159
Proxied: dns.Proxied,
151160
})
152161

153162
return err
154-
}
163+
}

config.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
package czm
22

33
import (
4-
cfgo "github.com/cloudflare/cloudflare-go"
4+
"fmt"
5+
"github.com/cloudflare/cloudflare-go"
56
SLog "github.com/quan-to/slog"
67
"gopkg.in/yaml.v2"
7-
"io/ioutil"
8+
"os"
89
)
910

1011
type DNSMetadata struct {
1112
Key string `yaml:"key"`
1213
Data string `yaml:"data"`
1314
}
1415

15-
1616
type Dns struct {
17-
ID string
18-
Name string `yaml:"name"`
19-
Dtype string `yaml:"dtype"`
17+
ID string
18+
Name string `yaml:"name"`
19+
Dtype string `yaml:"dtype"`
2020
Content string `yaml:"content"`
21-
Proxied bool `yaml:"proxied"`
22-
Rules Rules `yaml:"rules"`
23-
TTL int `yaml:"ttl"`
24-
Module Module `yaml:"module"`
21+
Proxied *bool `yaml:"proxied"`
22+
Rules Rules `yaml:"rules"`
23+
TTL int `yaml:"ttl"`
24+
Module Module `yaml:"module"`
2525
}
2626

2727
type Zone struct {
28-
Id string `yaml:"id"`
29-
Hostname string `yaml:"hostname"`
30-
Dns []Dns `yaml:"dns"`
31-
DNSRecords []cfgo.DNSRecord
32-
28+
Id string `yaml:"id"`
29+
Hostname string `yaml:"hostname"`
30+
Dns []Dns `yaml:"dns"`
31+
DNSRecords []cloudflare.DNSRecord
3332
}
3433

3534
type ConfigMap struct {
3635
Cloudflare Cloudflare `yaml:"cloudflare"`
37-
Zones []Zone `yaml:"zones"`
36+
Zones []Zone `yaml:"zones"`
3837
}
3938

4039
func (cMap *ConfigMap) ReadConfigMap() {
41-
yamlFile, err := ioutil.ReadFile(CONFIG_MAP_PATH)
40+
log := SLog.Scope(fmt.Sprintf("LoadConfig: %s", CONFIG_MAP_PATH))
41+
yamlFile, err := os.ReadFile(CONFIG_MAP_PATH)
4242

4343
if err != nil {
44-
SLog.Scope("ReadZones").Error(err)
44+
log.Fatal(err)
4545
}
4646

4747
err = yaml.Unmarshal(yamlFile, &cMap)
4848

4949
if err != nil {
50-
SLog.Scope("ReadZones").Error(err)
50+
log.Fatal(err)
5151
}
5252
}

consts.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package czm
33
import "os"
44

55
const (
6-
RULES_NEXISTS_TAG = "NOTEXIST"
7-
RULES_NEXISTS_CREATE = "create"
8-
RULES_NEXISTS_SKIP = "skip"
6+
RULES_NEXISTS_TAG = "NOTEXIST"
7+
RULES_NEXISTS_CREATE = "create"
8+
RULES_NEXISTS_SKIP = "skip"
99

10-
RULES_UPDATE_TAG = "UPDATE"
11-
RULES_UPDATE_ALWAYS = "always"
12-
RULES_UPDATE_NEVER = "never"
10+
RULES_UPDATE_TAG = "UPDATE"
11+
RULES_UPDATE_ALWAYS = "always"
12+
RULES_UPDATE_NEVER = "never"
1313
)
1414

1515
var (
1616
CONFIG_MAP_PATH = os.Getenv("CONFIG_MAP_PATH")
1717
CONFIG_MOD_PATH = os.Getenv("CONFIG_MOD_PATH")
18-
)
18+
)

czm.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77

88
type CloudflareZoneManager struct {
99
ConfigMap ConfigMap
10-
SLog *slog.Instance
11-
Srv struct {
12-
CF *Cloudflare
10+
SLog *slog.Instance
11+
Srv struct {
12+
CF *Cloudflare
1313
Reporter string
14-
Mods Modules
14+
Mods Modules
1515
}
1616
}
1717

@@ -21,7 +21,7 @@ func (e *CloudflareZoneManager) Init() {
2121
e.ConfigMap.ReadConfigMap()
2222
e.InitServices()
2323

24-
for true {
24+
for {
2525
e.VerifyAndUpdateZones()
2626
time.Sleep(time.Second * 60)
2727
}
@@ -89,5 +89,3 @@ func (e *CloudflareZoneManager) VerifyAndUpdateZones() {
8989
}
9090
}
9191
}
92-
93-

go.mod

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
module github.com/lucasgolino/cloudflare-zone-manager
22

3-
go 1.12
3+
go 1.22
44

55
require (
6-
github.com/cloudflare/cloudflare-go v0.9.4
6+
github.com/cloudflare/cloudflare-go v0.96.0
77
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b
88
github.com/quan-to/slog v0.0.0-20190414172229-8bce0937f2c1
9-
github.com/stretchr/objx v0.2.0 // indirect
10-
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
11-
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
12-
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3 // indirect
13-
golang.org/x/text v0.3.2 // indirect
14-
golang.org/x/tools v0.0.0-20190802220118-1d1727260058
159
gopkg.in/yaml.v2 v2.2.2
1610
)
11+
12+
require (
13+
github.com/goccy/go-json v0.10.2 // indirect
14+
github.com/google/go-querystring v1.1.0 // indirect
15+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
16+
github.com/hashicorp/go-retryablehttp v0.7.6 // indirect
17+
github.com/kr/text v0.2.0 // indirect
18+
golang.org/x/net v0.25.0 // indirect
19+
golang.org/x/text v0.15.0 // indirect
20+
golang.org/x/time v0.5.0 // indirect
21+
)

0 commit comments

Comments
 (0)