-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.go
74 lines (60 loc) · 1.85 KB
/
migrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"fmt"
"github.com/digitalocean/godo"
"github.com/namedotcom/go/namecom"
)
func Migrate(ctx context.Context, doClient *godo.Client, ncClient *namecom.NameCom, domain string, dryRun bool) error {
sourceRecords, err := getAllDORecords(ctx, doClient, domain)
if err != nil {
return err
}
fmt.Printf("Found %d records to copy from DigitalOcean\n", len(sourceRecords))
for _, sourceRecord := range sourceRecords {
if sourceRecord.Type == "NS" || sourceRecord.Type == "SOA" {
fmt.Printf("Skipping %s record: %s (%s)\n", sourceRecord.Type, sourceRecord.Name, sourceRecord.Data)
continue
}
if sourceRecord.Type == "SRV" {
fmt.Printf("Skipping SRV record (not supported by this tool; patches are welcome): %s (%s)\n", sourceRecord.Name, sourceRecord.Data)
continue
}
if !canCreateNamecomRecordOfType(sourceRecord.Type) {
fmt.Printf("Skipping unsupported %s record (%s)\n", sourceRecord.Type, sourceRecord.Name)
continue
}
fmt.Println("Copying:", sourceRecord.Type, sourceRecord.Name)
newRec := doToNamecom(sourceRecord)
newRec.DomainName = domain
if dryRun {
fmt.Printf("\twould create record: %+v\n", newRec)
continue
}
resRec, err := ncClient.CreateRecord(&newRec)
if err != nil {
return err
}
fmt.Printf("\tcreated record %d: %+v\n", resRec.ID, newRec)
}
return nil
}
func getAllDORecords(ctx context.Context, client *godo.Client, domain string) ([]godo.DomainRecord, error) {
var retv []godo.DomainRecord
page := 0
more := true
for more {
page++
records, resp, err := client.Domains.Records(ctx, domain, &godo.ListOptions{
Page: page,
})
if err != nil {
return nil, fmt.Errorf("failed listing DO DNS records (page %d): %w", page, err)
}
retv = append(retv, records...)
if resp.Links == nil || resp.Links.IsLastPage() {
more = false
}
}
return retv, nil
}