package models import ( "log" "rocketgit.ru/rsmon/worker/internal/netaddr" "rocketgit.ru/rsmon/worker/app/models/concerns" ) // DNSRecord provides functionality. type DNSRecord struct { concerns.Model MonitorID int64 `gorm:"type:bigint REFERENCES monitors(id)" json:"monitor_id,omitempty"` Monitor *Monitor `json:"-"` Name string `json:"name"` Kind string `json:"kind"` Value netaddr.Inet `json:"value" gorm:"type:bytea;"` } // SaveIps provides functionality. func SaveIps(m *Monitor, _ Check, ips []NSRecord) { //nolint:gocritic // hugeParam: accepted for interface compatibility // log.Println("save ips for monitor") // spew.Dump(m) var err error tx := DB().Begin() currentRecords := make([]DNSRecord, 0) _ = tx.Model(&m).Association("DNSRecords").Find(¤tRecords) recordHash := make(map[string]DNSRecord, 0) for _, record := range currentRecords { recordHash[record.Name] = record } nextRecords := make(map[string]bool, 0) for _, ip := range ips { if record, ok := recordHash[ip.Name]; ok { // log.Println("old value:", record) record.MonitorID = m.ID record.Value = ip.Value record.Kind = ip.Kind err = tx.Model(&m).Association("DNSRecords").Replace(&record) if err != nil { log.Println("fatal error in saveips", err) return } } else { record = DNSRecord{ MonitorID: m.ID, Name: ip.Name, Kind: ip.Kind, Value: ip.Value, } if _, ok := nextRecords[ip.Name]; !ok { nextRecords[ip.Name] = true err = tx.Model(&m).Association("DNSRecords").Append(&record) if err != nil { log.Println("fatal error in saveips", err) return } } } } // spew.Dump(ips_hash) err = tx.Commit().Error if err != nil { log.Println("fatal error in saveips", err) return } } // NSRecord provides functionality. type NSRecord struct { Name string Kind string Value netaddr.Inet }