Skip to content

Commit

Permalink
add record count to dns zone list
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed Dec 12, 2024
1 parent 695f7dd commit 7f5fb81
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
19 changes: 19 additions & 0 deletions pkg/roles/dns/api_zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dns
import (
"context"
"errors"
"slices"
"strings"

"beryju.io/gravity/pkg/roles/dns/types"
Expand All @@ -21,6 +22,7 @@ type APIZone struct {
DefaultTTL uint32 `json:"defaultTTL" required:"true"`
Authoritative bool `json:"authoritative" required:"true"`
Hook string `json:"hook" required:"true"`
RecordCount int `json:"recordCount" required:"true"`
}
type APIZonesGetOutput struct {
Zones []APIZone `json:"zones" required:"true"`
Expand All @@ -46,6 +48,15 @@ func (r *Role) APIZonesGet() usecase.Interactor {
r.log.Warn("failed to get zones", zap.Error(err))
return status.Wrap(errors.New("failed to get zones"), status.Internal)
}
rawRecords, err := r.i.KV().Get(
ctx,
r.i.KV().Key(types.KeyRole, types.KeyZones).Prefix(true).String(),
clientv3.WithPrefix(),
clientv3.WithKeysOnly(),
)
if err != nil {
return status.Wrap(errors.New("failed to get records"), status.Internal)
}
for _, rawZone := range rawZones.Kvs {
_zone, err := r.zoneFromKV(rawZone)
if err != nil {
Expand All @@ -55,12 +66,20 @@ func (r *Role) APIZonesGet() usecase.Interactor {
if strings.Contains(_zone.Name, "/") {
continue
}
recordCount := slices.Collect(func(yield func(int) bool) {
for _, v := range rawRecords.Kvs {
if strings.HasPrefix(string(v.Key), _zone.etcdKey+"/") {
yield(1)
}
}
})
output.Zones = append(output.Zones, APIZone{
Name: _zone.Name,
Authoritative: _zone.Authoritative,
DefaultTTL: _zone.DefaultTTL,
HandlerConfigs: _zone.HandlerConfigs,
Hook: _zone.Hook,
RecordCount: len(recordCount),
})
}
return nil
Expand Down
57 changes: 32 additions & 25 deletions pkg/roles/dns/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,37 +94,44 @@ func (r *Record) ToDNS(qname string) dns.RR {
var rr dns.RR
switch r.RRType() {
case dns.TypeA:
rr = &dns.A{}
rr.(*dns.A).Hdr = hdr
rr.(*dns.A).A = net.ParseIP(r.Data)
rr = &dns.A{
Hdr: hdr,
A: net.ParseIP(r.Data),
}
case dns.TypeAAAA:
rr = &dns.AAAA{}
rr.(*dns.AAAA).Hdr = hdr
rr.(*dns.AAAA).AAAA = net.ParseIP(r.Data)
rr = &dns.AAAA{
Hdr: hdr,
AAAA: net.ParseIP(r.Data),
}
case dns.TypePTR:
rr = &dns.PTR{}
rr.(*dns.PTR).Hdr = hdr
rr.(*dns.PTR).Ptr = r.Data
rr = &dns.PTR{
Hdr: hdr,
Ptr: r.Data,
}
case dns.TypeSRV:
rr = &dns.SRV{}
rr.(*dns.SRV).Hdr = hdr
rr.(*dns.SRV).Target = r.Data
rr.(*dns.SRV).Port = r.SRVPort
rr.(*dns.SRV).Priority = r.SRVPriority
rr.(*dns.SRV).Weight = r.SRVWeight
rr = &dns.SRV{
Hdr: hdr,
Target: r.Data,
Port: r.SRVPort,
Priority: r.SRVPriority,
Weight: r.SRVWeight,
}
case dns.TypeMX:
rr = &dns.MX{}
rr.(*dns.MX).Hdr = hdr
rr.(*dns.MX).Mx = r.Data
rr.(*dns.MX).Preference = r.MXPreference
rr = &dns.MX{
Hdr: hdr,
Mx: r.Data,
Preference: r.MXPreference,
}
case dns.TypeCNAME:
rr = &dns.CNAME{}
rr.(*dns.CNAME).Hdr = hdr
rr.(*dns.CNAME).Target = r.Data
rr = &dns.CNAME{
Hdr: hdr,
Target: r.Data,
}
case dns.TypeTXT:
rr = &dns.TXT{}
rr.(*dns.TXT).Hdr = hdr
rr.(*dns.TXT).Txt = strings.Split(r.Data, TXTSeparator)
rr = &dns.TXT{
Hdr: hdr,
Txt: strings.Split(r.Data, TXTSeparator),
}
}
return rr
}
Expand Down
3 changes: 3 additions & 0 deletions schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2301,12 +2301,15 @@ components:
type: string
name:
type: string
recordCount:
type: integer
required:
- name
- handlerConfigs
- defaultTTL
- authoritative
- hook
- recordCount
type: object
DnsAPIZonesGetOutput:
properties:
Expand Down
2 changes: 2 additions & 0 deletions web/src/pages/dns/DNSZonesPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class DNSZonesPage extends TablePage<DnsAPIZone> {
columns(): TableColumn[] {
return [
new TableColumn("Zone"),
new TableColumn("Records"),
new TableColumn("Authoritative"),
new TableColumn("Actions"),
];
Expand All @@ -55,6 +56,7 @@ export class DNSZonesPage extends TablePage<DnsAPIZone> {
html`<a href=${`#/dns/zones/${item.name}`}>
${item.name === "." ? "Root Zone" : item.name}
</a>`,
html`${item.recordCount}`,
html`${item.authoritative ? "Yes" : "No"}`,
html`<ak-forms-modal>
<span slot="submit"> ${"Update"} </span>
Expand Down

0 comments on commit 7f5fb81

Please sign in to comment.