From 87c32df0c9cc38bfd5fb1bf735ad95592fc580f7 Mon Sep 17 00:00:00 2001 From: imlonghao Date: Tue, 13 Dec 2022 00:49:32 +0800 Subject: [PATCH] PORKBUN: support diff2 and two small changes (#1855) --- providers/porkbun/api.go | 2 +- providers/porkbun/porkbunProvider.go | 46 ++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/providers/porkbun/api.go b/providers/porkbun/api.go index 02d389506..9c083f926 100644 --- a/providers/porkbun/api.go +++ b/providers/porkbun/api.go @@ -54,7 +54,7 @@ func (c *porkbunProvider) post(endpoint string, params requestParams) ([]byte, e // If request sending too fast, the server will fail with the following error: // porkbun API error: Create error: We were unable to create the DNS record. - time.Sleep(300 * time.Millisecond) + time.Sleep(500 * time.Millisecond) resp, err := client.Do(req) if err != nil { return []byte{}, err diff --git a/providers/porkbun/porkbunProvider.go b/providers/porkbun/porkbunProvider.go index 16d6ac9e5..abe3fbcdd 100644 --- a/providers/porkbun/porkbunProvider.go +++ b/providers/porkbun/porkbunProvider.go @@ -100,7 +100,7 @@ func (c *porkbunProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*mode } var corrections []*models.Correction - if !diff2.EnableDiff2 || true { // Remove "|| true" when diff2 version arrives + if !diff2.EnableDiff2 { differ := diff.New(dc) _, create, del, modify, err := differ.IncrementalDiff(existingRecords) @@ -143,7 +143,7 @@ func (c *porkbunProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*mode } corr := &models.Correction{ - Msg: fmt.Sprintf("%s, porkbun ID: %s: ", m.String(), id), + Msg: fmt.Sprintf("%s, porkbun ID: %s", m.String(), id), F: func() error { return c.modifyRecord(dc.Name, id, req) }, @@ -154,7 +154,47 @@ func (c *porkbunProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*mode return corrections, nil } - // Insert Future diff2 version here. + changes, err := diff2.ByRecord(existingRecords, dc, nil) + if err != nil { + return nil, err + } + for _, change := range changes { + var corr *models.Correction + switch change.Type { + case diff2.CREATE: + req, err := toReq(change.New[0]) + if err != nil { + return nil, err + } + corr = &models.Correction{ + Msg: change.Msgs[0], + F: func() error { + return c.createRecord(dc.Name, req) + }, + } + case diff2.CHANGE: + id := change.Old[0].Original.(*domainRecord).ID + req, err := toReq(change.New[0]) + if err != nil { + return nil, err + } + corr = &models.Correction{ + Msg: fmt.Sprintf("%s, porkbun ID: %s", change.Msgs[0], id), + F: func() error { + return c.modifyRecord(dc.Name, id, req) + }, + } + case diff2.DELETE: + id := change.Old[0].Original.(*domainRecord).ID + corr = &models.Correction{ + Msg: fmt.Sprintf("%s, porkbun ID: %s", change.Msgs[0], id), + F: func() error { + return c.deleteRecord(dc.Name, id) + }, + } + } + corrections = append(corrections, corr) + } return corrections, nil }