-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_create_domain.go
527 lines (486 loc) · 16.4 KB
/
command_create_domain.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// 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"
configgtm "github.com/akamai/AkamaiOPEN-edgegrid-golang/configgtm-v1_4"
akamai "github.com/akamai/cli-common-golang"
"github.com/fatih/color"
"github.com/urfave/cli"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
)
var defaultDCs = []int{5400, 5401, 5402}
// Terraform resource names
var domainResource = "akamai_gtm_domain"
var datacenterResource = "akamai_gtm_datacenter"
var defaultDatacenterDataSource = "akamai_gtm_default_datacenter"
var propertyResource = "akamai_gtm_property"
var resourceResource = "akamai_gtm_resource"
var asResource = "akamai_gtm_asmap"
var geoResource = "akamai_gtm_geomap"
var cidrResource = "akamai_gtm_cidrmap"
// Import List Struct
type importListStruct struct {
Domain string
Datacenters map[int]string
Properties map[string][]int
Resources map[string][]int
Cidrmaps map[string][]int
Geomaps map[string][]int
Asmaps map[string][]int
}
var tfWorkPath = "./"
var createImportList = false
var createConfig = false
var domainName string
var fullImportList *importListStruct
// text for gtmvars.tf construction
var gtmvarsContent = fmt.Sprint(`variable "gtmsection" {
default = "default"
}
variable "contractid" {
default = ""
}
variable "groupid" {
default = ""
}
`)
var nullFieldMap = &configgtm.NullFieldMapStruct{}
// retrieve Null Values for Domain
func getDomainNullValues() configgtm.NullPerObjectAttributeStruct {
return nullFieldMap.Domain
}
// retrieve Null Values for Object Type
func getNullValuesList(objType string) map[string]configgtm.NullPerObjectAttributeStruct {
switch objType {
case "Properties":
return nullFieldMap.Properties
case "Datacenters":
return nullFieldMap.Datacenters
case "Resources":
return nullFieldMap.Resources
case "CidrMaps":
return nullFieldMap.CidrMaps
case "GeoMaps":
return nullFieldMap.GeoMaps
case "AsMaps":
return nullFieldMap.AsMaps
}
// unknown
return map[string]configgtm.NullPerObjectAttributeStruct{}
}
// command function create-domain
func cmdCreateDomain(c *cli.Context) error {
config, err := akamai.GetEdgegridConfig(c)
if err != nil {
return err
}
configgtm.Init(config)
if c.NArg() < 1 {
cli.ShowCommandHelp(c, c.Command.Name)
return cli.NewExitError(color.RedString("domain is required"), 1)
}
domainName = c.Args().Get(0)
if c.IsSet("tfworkpath") {
tfWorkPath = c.String("tfworkpath")
}
tfWorkPath = filepath.FromSlash(tfWorkPath)
if c.IsSet("resources") {
createImportList = true
}
if c.IsSet("createconfig") {
createConfig = true
}
akamai.StartSpinner(
"Fetching domain entity ...",
fmt.Sprintf("Fetching domain entity ...... [%s]", color.GreenString("OK")))
domain, err := configgtm.GetDomain(domainName)
if err != nil {
akamai.StopSpinnerFail()
fmt.Println("Error: " + err.Error())
return cli.NewExitError(color.RedString("Domain retrieval failed"), 1)
}
// use domain name sans suffix for domain resource name
resourceDomainName := normalizeResourceName(strings.TrimSuffix(domainName, ".akadns.net"))
if createImportList {
fmt.Println("Inventorying domain objects ...")
// Inventory datacenters
datacenters := make(map[int]string)
for _, dc := range domain.Datacenters {
// include Default DCs. Special handling elsewhere.
datacenters[dc.DatacenterId] = dc.Nickname
}
// inventory properties and targets
propTargets := make(map[string][]int)
for _, p := range domain.Properties {
targets := make([]int, 0)
for _, tt := range p.TrafficTargets {
targets = append(targets, tt.DatacenterId)
}
propTargets[p.Name] = targets
}
// inventory Resources
resources := make(map[string][]int)
for _, r := range domain.Resources {
targets := make([]int, 0)
for _, ri := range r.ResourceInstances {
targets = append(targets, ri.DatacenterId)
}
resources[r.Name] = targets
}
// inventory CidrMaps
cidrmaps := make(map[string][]int)
for _, c := range domain.CidrMaps {
targets := make([]int, 0)
for _, a := range c.Assignments {
targets = append(targets, a.DatacenterId)
}
cidrmaps[c.Name] = targets
}
// inventory GeoMaps
geomaps := make(map[string][]int)
for _, g := range domain.GeographicMaps {
targets := make([]int, 0)
for _, a := range g.Assignments {
targets = append(targets, a.DatacenterId)
}
geomaps[g.Name] = targets
}
// inventory ASMaps
asmaps := make(map[string][]int)
for _, as := range domain.AsMaps {
targets := make([]int, 0)
for _, a := range as.Assignments {
targets = append(targets, a.DatacenterId)
}
asmaps[as.Name] = targets
}
fmt.Println("Creating Resources list file...")
// pathname and exists?
if stat, err := os.Stat(tfWorkPath); err == nil && stat.IsDir() {
importListFilename := createImportListFilename(resourceDomainName)
if _, err := os.Stat(importListFilename); err == nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Resource list file exists. Remove to continue."), 1)
}
fullImportList = &importListStruct{}
fullImportList.Domain = domainName
fullImportList.Properties = propTargets
fullImportList.Datacenters = datacenters
fullImportList.Resources = resources
fullImportList.Cidrmaps = cidrmaps
fullImportList.Geomaps = geomaps
fullImportList.Asmaps = asmaps
json, err := json.MarshalIndent(fullImportList, "", " ")
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Unable to generate json formatted resource list"), 1)
}
f, err := os.Create(importListFilename)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Unable to create resources file"), 1)
}
defer f.Close()
_, err = f.WriteString(string(json))
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Unable to write resources file"), 1)
}
f.Sync()
} else {
// Path doesnt exist. Bail
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Destination work path is not accessible."), 1)
}
}
if createConfig {
// Read in resources list
importList, err := retrieveImportList(resourceDomainName)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Failed to read json resources file"), 1)
}
fmt.Println("Creating domain configuration file ...")
// see if configuration file already exists and exclude any resources already represented.
domainTFfileHandle, tfConfig, configImportList, err := reconcileResourceTargets(importList, resourceDomainName)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Failed to open/create config file."), 1)
}
defer domainTFfileHandle.Close()
//initialize Null Fields Struct
nullFieldMap, err = domain.NullFieldMap()
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Failed to initialize Domain null fields map"), 1)
}
// build tf file
if len(tfConfig) == 0 {
// if tf pre existed, domain has to exist by definition
tfConfig = processDomain(domain, resourceDomainName)
}
tfConfig += processDatacenters(domain.Datacenters, configImportList.Datacenters, resourceDomainName)
tfConfig += processProperties(domain.Properties, configImportList.Properties, importList.Datacenters, resourceDomainName)
tfConfig += processResources(domain.Resources, configImportList.Resources, importList.Datacenters, resourceDomainName)
tfConfig += processCidrmaps(domain.CidrMaps, configImportList.Cidrmaps, importList.Datacenters, resourceDomainName)
tfConfig += processGeomaps(domain.GeographicMaps, configImportList.Geomaps, importList.Datacenters, resourceDomainName)
tfConfig += processAsmaps(domain.AsMaps, configImportList.Asmaps, importList.Datacenters, resourceDomainName)
tfConfig += "\n"
_, err = domainTFfileHandle.Write([]byte(tfConfig))
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Failed to save domain configuration file."), 1)
}
domainTFfileHandle.Sync()
// Need create gtmvars.tf dependency
gtmvarsFilename := filepath.Join(tfWorkPath, "gtmvars.tf")
gtmvarsHandle, err := os.Create(gtmvarsFilename)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Unable to create gtmvars config file"), 1)
}
defer gtmvarsHandle.Close()
_, err = gtmvarsHandle.WriteString(gtmvarsContent)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Unable to write gtmvars config file"), 1)
}
gtmvarsHandle.Sync()
fmt.Println("Creating domain import script file...")
importScriptFilename := filepath.Join(tfWorkPath, resourceDomainName+"_resource_import.script")
if _, err := os.Stat(importScriptFilename); err == nil {
// File exists. Bail
akamai.StopSpinnerOk()
}
scriptContent, err := buildImportScript(configImportList, resourceDomainName)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Import script content generation failed"), 1)
}
f, err := os.Create(importScriptFilename)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Unable to create import script file"), 1)
}
defer f.Close()
_, err = f.WriteString(scriptContent)
if err != nil {
akamai.StopSpinnerFail()
return cli.NewExitError(color.RedString("Unable to write import script file"), 1)
}
f.Sync()
}
return nil
}
func retrieveImportList(rscName string) (*importListStruct, error) {
// check if createImportList set. If so, already have ....
if createImportList {
return fullImportList, nil
}
importListFilename := createImportListFilename(rscName)
if _, err := os.Stat(importListFilename); err != nil {
return nil, err
}
importData, err := ioutil.ReadFile(importListFilename)
if err != nil {
return nil, err
}
importList := &importListStruct{}
err = json.Unmarshal(importData, importList)
if err != nil {
return nil, err
}
return importList, nil
}
// Utility method to create full import list file path
func createImportListFilename(resourceName string) string {
return filepath.Join(tfWorkPath, resourceName+"_resources.json")
}
// Utility method to create full tf file path
func createTFFilename(resourceName string) string {
return filepath.Join(tfWorkPath, resourceName+".tf")
}
func buildImportScript(importList *importListStruct, resourceDomainName string) (string, error) {
// build import script
var import_prefix = "terraform import "
var import_file = ""
// Init TF
import_file += "terraform init\n"
// domain
if !checkForResource(domainResource, resourceDomainName) {
// Assuming a domain name cannot contain spaces ....
import_file += import_prefix + domainResource + "." + resourceDomainName + " " + importList.Domain + "\n"
}
// datacenters
for id, nickname := range importList.Datacenters {
normalName := normalizeResourceName(nickname)
// default datacenters special case.
ddcfound := false
for _, ddc := range defaultDCs {
if id == ddc {
ddcfound = true
}
}
if ddcfound {
continue
}
if !checkForResource(datacenterResource, normalName) {
import_file += import_prefix + datacenterResource + "." + normalName + " " + importList.Domain + ":" + strconv.Itoa(id) + "\n"
}
}
// properties
for name, _ := range importList.Properties {
normalName := normalizeResourceName(name)
if !checkForResource(propertyResource, normalName) {
import_file += import_prefix + propertyResource + "." + normalName + " "
if strings.Contains(name, " ") {
import_file += `"` + importList.Domain + ":" + name + `"` + "\n"
} else {
import_file += importList.Domain + ":" + name + "\n"
}
}
}
// resources
for name, _ := range importList.Resources {
normalName := normalizeResourceName(name)
if !checkForResource(resourceResource, normalName) {
import_file += import_prefix + resourceResource + "." + normalName + " "
if strings.Contains(name, " ") {
import_file += `"` + importList.Domain + ":" + name + `"` + "\n"
} else {
import_file += importList.Domain + ":" + name + "\n"
}
}
}
// cidrmaps
for name, _ := range importList.Cidrmaps {
normalName := normalizeResourceName(name)
if !checkForResource(cidrResource, normalName) {
import_file += import_prefix + cidrResource + "." + normalName + " "
if strings.Contains(name, " ") {
import_file += `"` + importList.Domain + ":" + name + `"` + "\n"
} else {
import_file += importList.Domain + ":" + name + "\n"
}
}
}
// geomaps
for name, _ := range importList.Geomaps {
normalName := normalizeResourceName(name)
if !checkForResource(geoResource, normalName) {
import_file += import_prefix + geoResource + "." + normalName + " "
if strings.Contains(name, " ") {
import_file += `"` + importList.Domain + ":" + name + `"` + "\n"
} else {
import_file += importList.Domain + ":" + name + "\n"
}
}
}
// asmaps
for name, _ := range importList.Asmaps {
normalName := normalizeResourceName(name)
if !checkForResource(asResource, normalName) {
import_file += import_prefix + asResource + "." + normalName + " "
if strings.Contains(name, " ") {
import_file += `"` + importList.Domain + ":" + name + `"` + "\n"
} else {
import_file += importList.Domain + ":" + name + "\n"
}
}
}
return import_file, nil
}
// remove any resources already present in existing domain tf configuration
func reconcileResourceTargets(importList *importListStruct, domainName string) (*os.File, string, *importListStruct, error) {
var tfScratchLen int64
tfFilename := createTFFilename(domainName)
if tfInfo, err := os.Stat(tfFilename); err != nil && os.IsExist(err) {
tfScratchLen = tfInfo.Size()
}
tfScratch := make([]byte, tfScratchLen)
var tfHandle *os.File
tfHandle, err := os.OpenFile(tfFilename, os.O_CREATE|os.O_RDWR, 0644)
if err != nil && err != io.EOF {
fmt.Println(err.Error())
return nil, "", importList, err
}
if tfScratchLen == 0 {
return tfHandle, "", importList, nil
}
charsRead, err := tfHandle.Read(tfScratch)
if err != nil && err != io.EOF {
fmt.Println(err.Error())
return nil, "", importList, err
}
_, err = tfHandle.Seek(0, 0)
if err != nil {
fmt.Println(err.Error())
return nil, "", importList, err
}
if charsRead == 0 {
return tfHandle, "", importList, err
}
tfConfig := fmt.Sprintf("%s", tfScratch[0:charsRead-1])
// need walk thru each resource type
for id, nickname := range importList.Datacenters {
normalName := normalizeResourceName(nickname)
if strings.Contains(tfConfig, "\""+normalName+"\"") {
fmt.Println("Datacenter " + nickname + " found in existing tf file")
delete(importList.Datacenters, id)
}
}
for name, _ := range importList.Properties {
normalName := normalizeResourceName(name)
if strings.Contains(tfConfig, "\""+normalName+"\"") {
fmt.Println("Property " + name + " found in existing tf file")
delete(importList.Properties, name)
}
}
for name, _ := range importList.Resources {
normalName := normalizeResourceName(name)
if strings.Contains(tfConfig, "\""+normalName+"\"") {
fmt.Println("Resource " + name + " found in existing tf file")
delete(importList.Resources, name)
}
}
for name, _ := range importList.Cidrmaps {
normalName := normalizeResourceName(name)
if strings.Contains(tfConfig, "\""+normalName+"\"") {
fmt.Println("Cidrmap " + name + " found in existing tf file")
delete(importList.Cidrmaps, name)
}
}
for name, _ := range importList.Geomaps {
normalName := normalizeResourceName(name)
if strings.Contains(tfConfig, "\""+normalName+"\"") {
fmt.Println("Geomap " + name + " found in existing tf file")
delete(importList.Geomaps, name)
}
}
for name, _ := range importList.Asmaps {
normalName := normalizeResourceName(name)
if strings.Contains(tfConfig, "\""+normalName+"\"") {
fmt.Println("Asmap " + name + " found in existing tf file")
delete(importList.Asmaps, name)
}
}
return tfHandle, tfConfig, importList, err
}