// UpdateRecord updates a record to a resource(domain) func UpdateRecord(domain string, resource *sham.Resource) error { resource.Validate() sham.SanitizeDomain(&domain) // in case of some update to domain name... if domain != resource.Domain { // delete old domain err := DeleteRecord(domain) if err != nil { return fmt.Errorf("Failed to clean up old domain - %v", err) } } // store in cache config.Log.Trace("Updating record in persistent cache...") err := cache.UpdateRecord(domain, resource) if err != nil { return err } // set new resource to domain // todo: atomic Answers[resource.Domain] = *resource return nil }
// AddRecord adds a record to the persistent cache func AddRecord(resource *shaman.Resource) error { if storage == nil { return nil } resource.Validate() return storage.addRecord(*resource) }
// UpdateRecord updates a record in the persistent cache func UpdateRecord(domain string, resource *shaman.Resource) error { if storage == nil { return nil } shaman.SanitizeDomain(&domain) resource.Validate() return storage.updateRecord(domain, *resource) }
func (p postgresDb) addRecord(resource shaman.Resource) error { resources, err := p.listRecords() if err != nil { return err } for i := range resources { if resources[i].Domain == resource.Domain { // if domains match, check address for k := range resources[i].Records { next: for j := range resource.Records { // check if the record exists... if resource.Records[j].RType == resources[i].Records[k].RType && resource.Records[j].Address == resources[i].Records[k].Address && resource.Records[j].Class == resources[i].Records[k].Class { // if so, skip config.Log.Trace("Record exists in persistent, skipping...") resource.Records = append(resource.Records[:i], resource.Records[i+1:]...) goto next } } } } } // add records for i := range resource.Records { config.Log.Trace("Adding record to database...") _, err = p.pg.Exec(fmt.Sprintf(` INSERT INTO records(domain, address, ttl, class, type) VALUES('%v', '%v', '%v', '%v', '%v')`, resource.Domain, resource.Records[i].Address, resource.Records[i].TTL, resource.Records[i].Class, resource.Records[i].RType)) if err != nil { return fmt.Errorf("Failed to insert into records table - %v", err) } } return nil }