diff --git a/providers/vultr/vultrProvider.go b/providers/vultr/vultrProvider.go index 8e2fca3f0..9abc306ba 100644 --- a/providers/vultr/vultrProvider.go +++ b/providers/vultr/vultrProvider.go @@ -134,47 +134,81 @@ func (api *vultrProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*mode models.PostProcessRecords(curRecords) var corrections []*models.Correction - var create, delete, modify diff.Changeset if !diff2.EnableDiff2 { differ := diff.New(dc) - _, create, delete, modify, err = differ.IncrementalDiff(curRecords) - } else { - differ := diff.NewCompat(dc) - _, create, delete, modify, err = differ.IncrementalDiff(curRecords) + _, create, delete, modify, err := differ.IncrementalDiff(curRecords) + + if err != nil { + return nil, err + } + + for _, mod := range delete { + id := mod.Existing.Original.(govultr.DomainRecord).ID + corrections = append(corrections, &models.Correction{ + Msg: fmt.Sprintf("%s; Vultr RecordID: %v", mod.String(), id), + F: func() error { + return api.client.DomainRecord.Delete(context.Background(), dc.Name, id) + }, + }) + } + + for _, mod := range modify { + r := toVultrRecord(dc, mod.Desired, mod.Existing.Original.(govultr.DomainRecord).ID) + corrections = append(corrections, &models.Correction{ + Msg: fmt.Sprintf("%s; Vultr RecordID: %v", mod.String(), r.ID), + F: func() error { + return api.client.DomainRecord.Update(context.Background(), dc.Name, r.ID, &govultr.DomainRecordReq{Name: r.Name, Type: r.Type, Data: r.Data, TTL: r.TTL, Priority: &r.Priority}) + }, + }) + } + + for _, mod := range create { + r := toVultrRecord(dc, mod.Desired, "0") + corrections = append(corrections, &models.Correction{ + Msg: mod.String(), + F: func() error { + _, err := api.client.DomainRecord.Create(context.Background(), dc.Name, &govultr.DomainRecordReq{Name: r.Name, Type: r.Type, Data: r.Data, TTL: r.TTL, Priority: &r.Priority}) + return err + }, + }) + } + return corrections, nil } + + changes, err := diff2.ByRecord(curRecords, dc, nil) + if err != nil { return nil, err } - for _, mod := range delete { - id := mod.Existing.Original.(govultr.DomainRecord).ID - corrections = append(corrections, &models.Correction{ - Msg: fmt.Sprintf("%s; Vultr RecordID: %v", mod.String(), id), - F: func() error { - return api.client.DomainRecord.Delete(context.Background(), dc.Name, id) - }, - }) - } - - for _, mod := range modify { - r := toVultrRecord(dc, mod.Desired, mod.Existing.Original.(govultr.DomainRecord).ID) - corrections = append(corrections, &models.Correction{ - Msg: fmt.Sprintf("%s; Vultr RecordID: %v", mod.String(), r.ID), - F: func() error { - return api.client.DomainRecord.Update(context.Background(), dc.Name, r.ID, &govultr.DomainRecordReq{Name: r.Name, Type: r.Type, Data: r.Data, TTL: r.TTL, Priority: &r.Priority}) - }, - }) - } - - for _, mod := range create { - r := toVultrRecord(dc, mod.Desired, "0") - corrections = append(corrections, &models.Correction{ - Msg: mod.String(), - F: func() error { - _, err := api.client.DomainRecord.Create(context.Background(), dc.Name, &govultr.DomainRecordReq{Name: r.Name, Type: r.Type, Data: r.Data, TTL: r.TTL, Priority: &r.Priority}) - return err - }, - }) + for _, change := range changes { + switch change.Type { + case diff2.CREATE: + r := toVultrRecord(dc, change.New[0], "0") + corrections = append(corrections, &models.Correction{ + Msg: change.Msgs[0], + F: func() error { + _, err := api.client.DomainRecord.Create(context.Background(), dc.Name, &govultr.DomainRecordReq{Name: r.Name, Type: r.Type, Data: r.Data, TTL: r.TTL, Priority: &r.Priority}) + return err + }, + }) + case diff2.CHANGE: + r := toVultrRecord(dc, change.New[0], change.Old[0].Original.(govultr.DomainRecord).ID) + corrections = append(corrections, &models.Correction{ + Msg: fmt.Sprintf("%s; Vultr RecordID: %v", change.Msgs[0], r.ID), + F: func() error { + return api.client.DomainRecord.Update(context.Background(), dc.Name, r.ID, &govultr.DomainRecordReq{Name: r.Name, Type: r.Type, Data: r.Data, TTL: r.TTL, Priority: &r.Priority}) + }, + }) + case diff2.DELETE: + id := change.Old[0].Original.(govultr.DomainRecord).ID + corrections = append(corrections, &models.Correction{ + Msg: fmt.Sprintf("%s; Vultr RecordID: %v", change.Msgs[0], id), + F: func() error { + return api.client.DomainRecord.Delete(context.Background(), dc.Name, id) + }, + }) + } } return corrections, nil