mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
HETZNER: create and modify multiple records in batches (#925)
Signed-off-by: Jakob Ackermann <das7pad@outlook.com>
This commit is contained in:
@@ -29,6 +29,32 @@ func checkIsLockedSystemRecord(record record) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (api *hetznerProvider) bulkCreateRecords(records []record) error {
|
||||||
|
for _, record := range records {
|
||||||
|
if err := checkIsLockedSystemRecord(record); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
request := bulkCreateRecordsRequest{
|
||||||
|
Records: records,
|
||||||
|
}
|
||||||
|
return api.request("/records/bulk", "POST", request, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *hetznerProvider) bulkUpdateRecords(records []record) error {
|
||||||
|
for _, record := range records {
|
||||||
|
if err := checkIsLockedSystemRecord(record); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
request := bulkUpdateRecordsRequest{
|
||||||
|
Records: records,
|
||||||
|
}
|
||||||
|
return api.request("/records/bulk", "PUT", request, nil)
|
||||||
|
}
|
||||||
|
|
||||||
func (api *hetznerProvider) createRecord(record record) error {
|
func (api *hetznerProvider) createRecord(record record) error {
|
||||||
if err := checkIsLockedSystemRecord(record); err != nil {
|
if err := checkIsLockedSystemRecord(record); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@@ -3,6 +3,7 @@ package hetzner
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/StackExchange/dnscontrol/v3/models"
|
"github.com/StackExchange/dnscontrol/v3/models"
|
||||||
"github.com/StackExchange/dnscontrol/v3/pkg/diff"
|
"github.com/StackExchange/dnscontrol/v3/pkg/diff"
|
||||||
@@ -107,25 +108,37 @@ func (api *hetznerProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*mo
|
|||||||
corrections = append(corrections, corr)
|
corrections = append(corrections, corr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var createRecords []record
|
||||||
|
createDescription := []string{"Batch creation of records:"}
|
||||||
for _, m := range create {
|
for _, m := range create {
|
||||||
record := fromRecordConfig(m.Desired, zone)
|
record := fromRecordConfig(m.Desired, zone)
|
||||||
|
createRecords = append(createRecords, *record)
|
||||||
|
createDescription = append(createDescription, m.String())
|
||||||
|
}
|
||||||
|
if len(createRecords) > 0 {
|
||||||
corr := &models.Correction{
|
corr := &models.Correction{
|
||||||
Msg: m.String(),
|
Msg: strings.Join(createDescription, "\n\t"),
|
||||||
F: func() error {
|
F: func() error {
|
||||||
return api.createRecord(*record)
|
return api.bulkCreateRecords(createRecords)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
corrections = append(corrections, corr)
|
corrections = append(corrections, corr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var modifyRecords []record
|
||||||
|
modifyDescription := []string{"Batch modification of records:"}
|
||||||
for _, m := range modify {
|
for _, m := range modify {
|
||||||
id := m.Existing.Original.(*record).ID
|
id := m.Existing.Original.(*record).ID
|
||||||
record := fromRecordConfig(m.Desired, zone)
|
record := fromRecordConfig(m.Desired, zone)
|
||||||
record.ID = id
|
record.ID = id
|
||||||
|
modifyRecords = append(modifyRecords, *record)
|
||||||
|
modifyDescription = append(modifyDescription, m.String())
|
||||||
|
}
|
||||||
|
if len(modifyRecords) > 0 {
|
||||||
corr := &models.Correction{
|
corr := &models.Correction{
|
||||||
Msg: m.String(),
|
Msg: strings.Join(modifyDescription, "\n\t"),
|
||||||
F: func() error {
|
F: func() error {
|
||||||
return api.updateRecord(*record)
|
return api.bulkUpdateRecords(modifyRecords)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
corrections = append(corrections, corr)
|
corrections = append(corrections, corr)
|
||||||
|
@@ -4,6 +4,14 @@ import (
|
|||||||
"github.com/StackExchange/dnscontrol/v3/models"
|
"github.com/StackExchange/dnscontrol/v3/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type bulkCreateRecordsRequest struct {
|
||||||
|
Records []record `json:"records"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type bulkUpdateRecordsRequest struct {
|
||||||
|
Records []record `json:"records"`
|
||||||
|
}
|
||||||
|
|
||||||
type createRecordRequest struct {
|
type createRecordRequest struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
TTL int `json:"ttl"`
|
TTL int `json:"ttl"`
|
||||||
|
Reference in New Issue
Block a user