/
set.go
220 lines (183 loc) · 5.67 KB
/
set.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package cmd
import (
"context"
"fmt"
"net"
"strings"
"github.com/Azure/azure-sdk-for-go/profiles/latest/dns/mgmt/dns"
"github.com/elyscape/az-dns/helpers"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// setCmd represents the set command
var setCmd = &cobra.Command{
Use: "set TYPE HOSTNAME VALUES",
Short: "Create or update a DNS record set",
Long: `Create or update a record set in Azure DNS
This will create or update a record set on Azure DNS, depending on whether a
record of the same type already exists for the provided value of HOSTNAME. The
currently-supported record types are A, AAAA, CAA, and TXT. HOSTNAME may be a
fully-qualified domain name contained within the zone, a record name relative
to the zone, or either the empty string or @ for the apex. If a record name
contains the zone name (e.g. example.com.example.com), you should either
provide the FQDN or use the --relative flag.
Examples:
az-dns set A example.com 1.1.1.1 -z example.com
Creates an A record at the apex of example.com pointing to 1.1.1.1
az-dns set A sub 1.1.1.1 2.2.2.2 -z example.com
Creates an A record for sub.example.com pointing to 1.1.1.1 and 2.2.2.2
az-dns set AAAA local.example.com ::1 -t 600 -r -z example.com
Creates an AAAA record for local.example.com.example.com with TTL of
600 pointing at ::1
az-dns set CAA example.com 0 issue letsencrypt.org -z example.com
Creates a CAA record at the apex of example.com with value:
0 issue "letsencrypt.org"
az-dns set CAA @ 0 issue letsencrypt.org 0 issuewild ';' -z example.com
Creates CAA records at the apex of example.com with values:
0 issue "letsencrypt.org"
0 issuewild ";"`,
Args: cobra.MinimumNArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
recordType := dns.RecordType(strings.ToUpper(args[0]))
hostname := args[1]
records := args[2:]
client, err := helpers.NewRecordSetClient(dns.DefaultBaseURI)
if err != nil {
return
}
resourceGroup := viper.GetString("resource-group")
if resourceGroup == "" {
err = fmt.Errorf("a resource group name is required")
return
}
zone := viper.GetString("zone")
if zone == "" {
err = fmt.Errorf("a DNS zone name is required")
return
}
relative := viper.GetBool("relative")
ttl := viper.GetInt64("ttl")
recordName := helpers.GenerateRecordName(hostname, zone, relative)
var rrparams *dns.RecordSet = nil
switch recordType {
case dns.A:
rrparams, err = generateARecordParams(ttl, records)
if err != nil {
return
}
case dns.AAAA:
rrparams, err = generateAaaaRecordParams(ttl, records)
if err != nil {
return
}
case dns.CAA:
rrparams, err = generateCaaRecordParams(ttl, records)
if err != nil {
return
}
case dns.TXT:
rrparams, err = generateTxtRecordParams(ttl, records)
if err != nil {
return
}
default:
err = fmt.Errorf("unsupported record type %v", recordType)
return
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err = client.CreateOrUpdate(ctx, resourceGroup, zone, recordName, recordType, *rrparams, "", "")
if err != nil {
return
}
fmt.Println("success")
return
},
}
func init() {
rootCmd.AddCommand(setCmd)
setCmd.PersistentFlags().BoolP("relative", "r", false, "HOSTNAME is a zone-relative label")
setCmd.PersistentFlags().Int64P("ttl", "t", 300, "Record set TTL")
viper.BindPFlags(setCmd.PersistentFlags())
}
func generateARecordParams(ttl int64, values []string) (rrparams *dns.RecordSet, err error) {
records := []dns.ARecord{}
for _, addr := range values {
if ip := net.ParseIP(addr); ip == nil || ip.To4() == nil {
err = fmt.Errorf(`invalid IP address "%v"`, addr)
return
}
records = append(records, dns.ARecord{Ipv4Address: &addr})
}
rrparams = &dns.RecordSet{
RecordSetProperties: &dns.RecordSetProperties{
TTL: &ttl,
ARecords: &records,
},
}
return
}
func generateAaaaRecordParams(ttl int64, values []string) (rrparams *dns.RecordSet, err error) {
records := []dns.AaaaRecord{}
for _, addr := range values {
if ip := net.ParseIP(addr); ip == nil || ip.To16() == nil {
err = fmt.Errorf(`invalid IP address "%v"`, addr)
return
}
records = append(records, dns.AaaaRecord{Ipv6Address: &addr})
}
rrparams = &dns.RecordSet{
RecordSetProperties: &dns.RecordSetProperties{
TTL: &ttl,
AaaaRecords: &records,
},
}
return
}
func generateCaaRecordParams(ttl int64, values []string) (rrparams *dns.RecordSet, err error) {
records := []dns.CaaRecord{}
const recordSize = 3
for min := 0; min < len(values); min += recordSize {
max := min + recordSize
if max > len(values) {
err = fmt.Errorf(`incomplete CAA record %v`, values[min:])
return
}
fields := values[min:max]
var flags int32
flags, err = cast.ToInt32E(fields[0])
if err != nil || flags > 255 || flags < 0 {
err = fmt.Errorf(`invalid CAA flags "%v" must be an integer between 0 and 255`, fields[0])
return
}
tag := fields[1]
value := fields[2]
records = append(records, dns.CaaRecord{
Flags: &flags,
Tag: &tag,
Value: &value,
})
}
rrparams = &dns.RecordSet{
RecordSetProperties: &dns.RecordSetProperties{
TTL: &ttl,
CaaRecords: &records,
},
}
return
}
func generateTxtRecordParams(ttl int64, values []string) (rrparams *dns.RecordSet, err error) {
records := []dns.TxtRecord{}
for _, value := range values {
valueArr := []string{value}
records = append(records, dns.TxtRecord{Value: &valueArr})
}
rrparams = &dns.RecordSet{
RecordSetProperties: &dns.RecordSetProperties{
TTL: &ttl,
TxtRecords: &records,
},
}
return
}