Skip to content

Commit

Permalink
Merge pull request #8 from qui-pham/main
Browse files Browse the repository at this point in the history
feat: delete unused dns record
  • Loading branch information
duythinht authored Oct 30, 2023
2 parents f027b24 + 54bdb8a commit a1bc0ff
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.envrc
.envrc
.idea
26 changes: 14 additions & 12 deletions cmd/cafe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func main() {
ctx := context.Background()

zones, err := api.ListZones(ctx)

if err != nil {
log.Fatalf("get zones: %s", err)
}
Expand All @@ -48,7 +47,7 @@ func main() {
log.Fatalf("get record for zone %s: %s", zone.Name, err)
}

cfRecords = append(cfRecords, records...)
cfRecords = append(cfRecords, records...) // Get All Records
}

vm := jsonnet.MakeVM()
Expand All @@ -59,13 +58,11 @@ func main() {
if zoneRoot == "" {
zoneRoot = "./zones"
}

err = filepath.Walk(zoneRoot, func(path string, info fs.FileInfo, err error) error {

if info.IsDir() {
return nil
}

if strings.HasSuffix(path, ".jsonnet") {
jsonData, err := vm.EvaluateFile(path)
if err != nil {
Expand Down Expand Up @@ -103,40 +100,45 @@ func main() {
managed := SetOf[string]()
stored := SetOf[string]()

//
deleting := make([]cloudflare.DNSRecord, 0)
adding := make([]cloudflare.DNSRecord, 0)

defined := SetOf[string]()
deprecate := SetOf[string]()

for _, record := range dfRecords {
managed.Add(nt(record))
}

for _, record := range dfRecords {
defined.Add(hash(record))
if record.Type == "CNAME" {
if cnames.Has(record.Name) {
log.Fatalf("ERROR: cname is duplicated: %s", record.Name)
}
cnames.Add(record.Name)
defined.Add(hash(record))
} else if record.Type == "DEPRECATED" {
deprecate.Add(record.Name)
} else {
defined.Add(hash(record))
}
}

for _, record := range cfRecords {
if managed.Has(nt(record)) {
h := hash(record)

if !defined.Has(h) {
if !defined.Has(h) || deprecate.Has(record.Name) {
deleting = append(deleting, record)
}
stored.Add(h)
}
}

for _, record := range dfRecords {
if !stored.Has(hash(record)) {
adding = append(adding, record)
if record.Type != "DEPRECATED" {
if !stored.Has(hash(record)) {
adding = append(adding, record)
}
}
}

Expand Down Expand Up @@ -191,7 +193,7 @@ func hash(r cloudflare.DNSRecord) string {

if r.Type == "MX" {
s = md5.Sum([]byte(fmt.Sprintf("%s-%s-%d-%s-%s-%d", r.ZoneName, r.Type, r.TTL, r.Name, r.Content, *r.Priority)))
} else if r.Type == "A" || r.Type == "CNAME" {
} else if r.Type == "A" || r.Type == "CNAME" || r.Type == "DEPRECATED" {
s = md5.Sum([]byte(fmt.Sprintf("%s-%s-%d-%s-%s-%v", r.ZoneName, "X", r.TTL, r.Name, r.Content, *r.Proxied)))
} else {
s = md5.Sum([]byte(fmt.Sprintf("%s-%s-%d-%s-%s", r.ZoneName, r.Type, r.TTL, r.Name, r.Content)))
Expand All @@ -201,7 +203,7 @@ func hash(r cloudflare.DNSRecord) string {
}

func nt(r cloudflare.DNSRecord) string {
if r.Type == "A" || r.Type == "CNAME" {
if r.Type == "A" || r.Type == "CNAME" || r.Type == "DEPRECATED" {
return fmt.Sprintf("%s-X", r.Name)
}
return fmt.Sprintf("%s-%s", r.Name, r.Type)
Expand Down
3 changes: 2 additions & 1 deletion zones/0x97a.com.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ local cafe = import 'cafe.libsonnet';
local records = cafe.zone('0x97a.com');

[
records.a('test-lb.0x97a.com', '34.160.220.95', ttl=1, proxied=false),
records.a('testing.0x97a.com', '34.160.220.95', ttl=1, proxied=false),
records.deprecated('testing.0x97a.com'),
]
1 change: 1 addition & 0 deletions zones/cafe.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ local record = function(zone_name, name, content, type='A', proxied=true, ttl=1,
cname(name, domain, proxied=true, ttl=1):: record(zone_name, name, domain, type='CNAME', proxied=proxied, ttl=ttl),
txt(name, content):: record(zone_name, name, content, type='TXT', proxied=false, ttl=1),
mx(name, content, priority=10):: record(zone_name, name, content, type='MX', proxied=false, ttl=1, priority=priority),
deprecated(name):: record(zone_name, name, content='', type="DEPRECATED")
}
}

0 comments on commit a1bc0ff

Please sign in to comment.