1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

VULTR: adopt diff2 in full mode (#2014)

Co-authored-by: Tom Limoncelli <tlimoncelli@stackoverflow.com>
This commit is contained in:
Nicolas Lorin
2023-01-30 04:04:07 +01:00
committed by GitHub
parent 2679630de5
commit fd393e39f5

View File

@ -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