-
Notifications
You must be signed in to change notification settings - Fork 20
/
dns_records_update.go
113 lines (96 loc) · 2.63 KB
/
dns_records_update.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cmd
import (
"fmt"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/egoscale"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
func init() {
for i := egoscale.A; i <= egoscale.URL; i++ {
recordType := egoscale.Record.String(i)
cmdUpdateRecord := &cobra.Command{
Use: fmt.Sprintf("%s DOMAIN-NAME|ID RECORD-NAME|ID", recordType),
Short: fmt.Sprintf("Update %s record type to a domain", recordType),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
var name, content *string
var ttl, priority *int64
if cmd.Flags().Changed("name") {
t, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
name = &t
}
if cmd.Flags().Changed("content") {
t, err := cmd.Flags().GetString("content")
if err != nil {
return err
}
content = &t
}
if cmd.Flags().Changed("ttl") {
t, err := cmd.Flags().GetInt64("ttl")
if err != nil {
return err
}
ttl = &t
}
if cmd.Flags().Changed("priority") {
t, err := cmd.Flags().GetInt64("priority")
if err != nil {
return err
}
priority = &t
}
return updateDomainRecord(args[0], args[1], recordType, name, content, ttl, priority)
},
}
cmdUpdateRecord.Flags().StringP("name", "n", "", "Update name")
cmdUpdateRecord.Flags().StringP("content", "c", "", "Update Content")
cmdUpdateRecord.Flags().Int64P("ttl", "t", 0, "Update ttl")
cmdUpdateRecord.Flags().Int64P("priority", "p", 0, "Update priority")
dnsUpdateCmd.AddCommand(cmdUpdateRecord)
}
}
func updateDomainRecord(
domainIdent, recordIdent, recordType string,
name, content *string,
ttl, priority *int64,
) error {
domain, err := domainFromIdent(domainIdent)
if err != nil {
return err
}
record, err := domainRecordFromIdent(*domain.ID, recordIdent, &recordType)
if err != nil {
return err
}
if name != nil {
record.Name = name
}
if content != nil {
record.Content = content
}
if ttl != nil {
record.TTL = ttl
}
if priority != nil {
record.Priority = priority
}
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone))
decorateAsyncOperation(fmt.Sprintf("Updating DNS record %q...", *record.ID), func() {
err = globalstate.EgoscaleClient.UpdateDNSDomainRecord(ctx, account.CurrentAccount.DefaultZone, *domain.ID, record)
})
if err != nil {
return err
}
if !globalstate.Quiet {
fmt.Printf("Record %q was updated successfully\n", *record.ID)
}
return nil
}