-
Notifications
You must be signed in to change notification settings - Fork 7
/
command_create_zoneconfig.go
296 lines (281 loc) · 9.04 KB
/
command_create_zoneconfig.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2020. Akamai Technologies, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
dnsv2 "github.com/akamai/AkamaiOPEN-edgegrid-golang/configdns-v2"
akamai "github.com/akamai/cli-common-golang"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
)
func cmdCreateZoneconfig(c *cli.Context) error {
config, err := akamai.GetEdgegridConfig(c)
if err != nil {
return err
}
dnsv2.Init(config)
var (
zonename string
outputPath string
contractid string
groupid string
inputPath string
)
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return cli.NewExitError(color.RedString("zonename is required"), 1)
}
akamai.StartSpinner("Preparing zone for create ", "")
zonename = c.Args().First()
newZone := &dnsv2.ZoneCreate{}
queryArgs := dnsv2.ZoneQueryString{}
if c.IsSet("contractid") {
contractid = c.String("contractid")
newZone.ContractId = contractid
queryArgs.Contract = contractid
}
if c.IsSet("groupid") {
groupid = c.String("groupid")
queryArgs.Group = groupid
}
if c.IsSet("file") {
inputPath = c.String("file")
inputPath = filepath.FromSlash(inputPath)
}
if c.IsSet("output") {
outputPath = c.String("output")
outputPath = filepath.FromSlash(outputPath)
}
if c.IsSet("file") {
// Read in json file
data, err := ioutil.ReadFile(inputPath)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Failed to read input file"), 1)
}
// set local variables and Object
err = json.Unmarshal(data, &newZone)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Failed to parse json file content into zone object"), 1)
}
zonename = newZone.Zone
if len(newZone.ContractId) > 0 {
// overwrite command line arg
contractid = newZone.ContractId
queryArgs.Contract = contractid
}
} else if c.IsSet("type") {
// contractid already set
newZone.Zone = zonename
newZone.Type = strings.ToUpper(c.String("type"))
if c.IsSet("master") {
newZone.Masters = c.StringSlice("master")
}
if c.IsSet("comment") {
newZone.Comment = c.String("comment")
}
if c.IsSet("signandserve") {
newZone.SignAndServe = c.Bool("signandserve")
}
if c.IsSet("algorithm") {
newZone.SignAndServeAlgorithm = c.String("algorithm")
}
if c.IsSet("tsigname") {
newZone.TsigKey = &dnsv2.TSIGKey{}
newZone.TsigKey.Name = c.String("tsigname")
if c.IsSet("tsigalgorithm") {
newZone.TsigKey.Algorithm = c.String("tsigalgorithm")
}
if c.IsSet("tsigsecret") {
newZone.TsigKey.Secret = c.String("tsigsecret")
}
}
if c.IsSet("target") {
newZone.Target = c.String("target")
}
if c.IsSet("endcustomerid") {
newZone.EndCustomerId = c.String("endcustomerid")
}
} else {
akamai.StopSpinnerFail()
cli.ShowCommandHelp(c, c.Command.Name)
return cli.NewExitError(color.RedString("zone command line values or input file are required"), 1)
}
if len(contractid) == 0 {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("contractid is required"), 1)
}
err = dnsv2.ValidateZone(newZone)
if err != nil {
akamai.StopSpinnerFail()
cli.ShowCommandHelp(c, c.Command.Name)
return cli.NewExitError(color.RedString(fmt.Sprintf("Invalid value provided for zone. Error: %s", err.Error())), 1)
}
akamai.StartSpinner("Creating Zone ", "")
// See if already exists
zone, err := dnsv2.GetZone(zonename)
if err == nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Zone already exists"), 1)
} else {
if !dnsv2.IsConfigDNSError(err) || !err.(dnsv2.ConfigDNSError).NotFound() {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString(fmt.Sprintf("Failure while checking zone existance. Error: %s", err.Error())), 1)
}
}
// create
err = newZone.Save(queryArgs)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString(fmt.Sprintf("Zone create failed. Error: %s", err.Error())), 1)
}
if c.IsSet("initialize") && c.Bool("initialize") && strings.ToUpper(newZone.Type) == "PRIMARY" {
// Indirectly create NS and SOA records
err = newZone.SaveChangelist()
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Zone initialization failed. SOA and NS records need to be created "), 1)
}
err = newZone.SubmitChangelist()
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString(fmt.Sprintf("Zone create failed. Error: %s", err.Error())), 1)
}
}
akamai.StopSpinnerOk()
akamai.StartSpinner("Reading Zone Content ", "")
zone, err = dnsv2.GetZone(zonename)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString(fmt.Sprintf("Failed to read zone content. Error: %s", err.Error())), 1)
}
// suppress result output?
if c.IsSet("suppress") && c.Bool("suppress") {
return nil
}
results := ""
akamai.StartSpinner("Assembling Zone Content ", "")
// full output
if c.IsSet("json") && c.Bool("json") {
zjson, err := json.MarshalIndent(zone, "", " ")
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Unable to display zone"), 1)
}
results = string(zjson)
} else {
results = renderZoneconfigTable(zone, c)
}
akamai.StopSpinnerOk()
if len(outputPath) > 1 {
akamai.StartSpinner(fmt.Sprintf("Writing Output to %s ", outputPath), "")
// pathname and exists?
zfHandle, err := os.Create(outputPath)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString(fmt.Sprintf("Failed to create output file. Error: %s", err.Error())), 1)
}
defer zfHandle.Close()
_, err = zfHandle.WriteString(string(results))
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Unable to write zone output to file"), 1)
}
zfHandle.Sync()
akamai.StopSpinnerOk()
return nil
} else {
fmt.Fprintln(c.App.Writer, "")
fmt.Fprintln(c.App.Writer, results)
}
return nil
}
func renderZoneconfigTable(zone *dnsv2.ZoneResponse, c *cli.Context) string {
//bold := color.New(color.FgWhite, color.Bold)
outString := ""
outString += fmt.Sprintln(" ")
outString += fmt.Sprintln("Zone Configuration")
outString += fmt.Sprintln(" ")
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT})
table.SetHeader([]string{"ZONE", "ATTRIBUTE", "VALUE"})
table.SetReflowDuringAutoWrap(false)
table.SetAutoWrapText(false)
table.SetRowLine(true)
table.SetCenterSeparator(" ")
table.SetColumnSeparator(" ")
table.SetRowSeparator(" ")
table.SetBorder(false)
if zone == nil {
rowData := []string{"No zone info to display", " ", " "}
table.Append(rowData)
} else {
zname := zone.Zone
ztype := zone.Type
table.Append([]string{zname, "Type", ztype})
if len(zone.Comment) > 0 {
table.Append([]string{" ", "Comment", zone.Comment})
}
if len(zone.ContractId) > 0 {
table.Append([]string{" ", "ContractId", zone.ContractId})
}
if strings.ToUpper(ztype) == "SECONDARY" {
if len(zone.Masters) > 0 {
masters := strings.Join(zone.Masters, " ,")
table.Append([]string{" ", "Masters", masters})
}
if zone.TsigKey != nil {
if len(zone.TsigKey.Name) > 0 {
table.Append([]string{" ", "TsigKey:Name", zone.TsigKey.Name})
}
if len(zone.TsigKey.Algorithm) > 0 {
table.Append([]string{" ", "TsigKey:Algorithm", zone.TsigKey.Algorithm})
}
if len(zone.TsigKey.Secret) > 0 {
table.Append([]string{" ", "TsigKey:Secret", zone.TsigKey.Secret})
}
}
}
if strings.ToUpper(ztype) == "PRIMARY" || strings.ToUpper(ztype) == "SECONDARY" {
table.Append([]string{" ", "SignAndServe", fmt.Sprintf("%t", zone.SignAndServe)})
if len(zone.SignAndServeAlgorithm) > 0 {
table.Append([]string{" ", "SignAndServeAlgorithm", fmt.Sprintf("%s", zone.SignAndServeAlgorithm)})
}
}
if strings.ToUpper(ztype) == "ALIAS" {
table.Append([]string{" ", "Target", zone.Target})
table.Append([]string{" ", "AliasCount", strconv.FormatInt(zone.AliasCount, 10)})
}
table.Append([]string{" ", "ActivationState", zone.ActivationState})
if len(zone.LastActivationDate) > 0 {
table.Append([]string{" ", "LastActivationDate", zone.LastActivationDate})
}
if len(zone.LastModifiedDate) > 0 {
table.Append([]string{" ", "LastModifiedDate", zone.LastModifiedDate})
}
table.Append([]string{" ", "VersionId", zone.VersionId})
}
table.Render()
outString += fmt.Sprintln(tableString.String())
return outString
}