Merge pull request 'jpictl: add initial dns add command' (#29)
Reviewed-on: #29
This commit was merged in pull request #29.
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"darvaza.org/core"
|
||||
"github.com/libdns/libdns"
|
||||
)
|
||||
|
||||
// Add adds a machine to the DNS records
|
||||
func (mgr *Manager) Add(ctx context.Context, name string, addrs ...netip.Addr) error {
|
||||
// TODO: validate name
|
||||
|
||||
cur, err := mgr.GetRecords(ctx, name)
|
||||
if err != nil {
|
||||
return core.Wrap(err, "GetRecords")
|
||||
}
|
||||
|
||||
// merge []SyncAddr for name
|
||||
s := mgr.asSyncRecordsMap(cur)[name+mgr.suffix]
|
||||
for _, addr := range addrs {
|
||||
s = AppendSyncAddr(s, addr)
|
||||
}
|
||||
|
||||
return mgr.addSyncAddr(ctx, name, s)
|
||||
}
|
||||
|
||||
func (mgr *Manager) addSyncAddr(ctx context.Context, name string, s []SyncAddr) error {
|
||||
var recs []libdns.Record
|
||||
|
||||
for _, a := range s {
|
||||
recs = append(recs, libdns.Record{
|
||||
ID: a.ID,
|
||||
Name: name + mgr.suffix,
|
||||
Type: core.IIf(a.Addr.Is6(), "AAAA", "A"),
|
||||
TTL: time.Second,
|
||||
Value: a.Addr.String(),
|
||||
})
|
||||
}
|
||||
|
||||
SortRecords(recs)
|
||||
err := writeRecords(recs, os.Stdout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = mgr.p.SetRecords(ctx, mgr.domain, recs)
|
||||
return err
|
||||
}
|
||||
|
||||
// AppendSyncAddr appends a [netip.Addr] to a [SyncAddr] slice
|
||||
// if the address is new.
|
||||
func AppendSyncAddr(s []SyncAddr, addr netip.Addr) []SyncAddr {
|
||||
for _, se := range s {
|
||||
if se.Addr.Compare(addr) == 0 {
|
||||
// found
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
s = append(s, SyncAddr{
|
||||
Addr: addr,
|
||||
TTL: time.Second,
|
||||
})
|
||||
return s
|
||||
}
|
||||
+9
-4
@@ -54,7 +54,7 @@ func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mgr.filteredSyncRecords(recs)
|
||||
return mgr.asSyncRecords(recs)
|
||||
}
|
||||
|
||||
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
|
||||
@@ -89,9 +89,9 @@ func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
|
||||
return out, true, nil
|
||||
}
|
||||
|
||||
func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
|
||||
func (mgr *Manager) asSyncRecordsMap(recs []libdns.Record) map[string][]SyncAddr {
|
||||
// filter and convert
|
||||
cache := make(map[string][]SyncAddr)
|
||||
out := make(map[string][]SyncAddr)
|
||||
for _, rr := range recs {
|
||||
addr, ok, err := mgr.AsSyncAddr(rr)
|
||||
switch {
|
||||
@@ -106,9 +106,14 @@ func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord,
|
||||
Print()
|
||||
case ok:
|
||||
// store
|
||||
cache[rr.Name] = append(cache[rr.Name], addr)
|
||||
out[rr.Name] = append(out[rr.Name], addr)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (mgr *Manager) asSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
|
||||
cache := mgr.asSyncRecordsMap(recs)
|
||||
|
||||
// prepare records
|
||||
out := make([]SyncAddrRecord, len(cache))
|
||||
|
||||
Reference in New Issue
Block a user