-
Notifications
You must be signed in to change notification settings - Fork 171
/
run.go
144 lines (119 loc) · 3.47 KB
/
run.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
package internal
import (
"context"
"fmt"
"os"
"strings"
internalmeta "github.com/Azure/aztfexport/internal/meta"
"github.com/Azure/aztfexport/internal/config"
"github.com/Azure/aztfexport/pkg/meta"
"github.com/Azure/aztfexport/internal/ui/common"
bspinner "github.com/charmbracelet/bubbles/spinner"
"github.com/magodo/spinner"
)
func BatchImport(ctx context.Context, cfg config.NonInteractiveModeConfig) error {
var c meta.Meta = internalmeta.NewGroupMetaDummy(cfg.ResourceGroupName, cfg.ProviderName)
if !cfg.MockMeta {
var err error
c, err = meta.NewMeta(cfg.Config)
if err != nil {
return err
}
}
var errors []string
f := func(msg Messager) error {
msg.SetStatus("Initializing...")
if err := c.Init(ctx); err != nil {
return err
}
defer func() {
msg.SetStatus("DeInitializing...")
// #nosec G104
c.DeInit(ctx)
}()
msg.SetStatus("Listing resources...")
list, err := c.ListResource(ctx)
if err != nil {
return err
}
msg.SetStatus("Exporting Skipped Resource file...")
if err := c.ExportSkippedResources(ctx, list); err != nil {
return fmt.Errorf("exporting Skipped Resource file: %v", err)
}
msg.SetStatus("Exporting Resource Mapping file...")
if err := c.ExportResourceMapping(ctx, list); err != nil {
return fmt.Errorf("exporting Resource Mapping file: %v", err)
}
// Return early if only generating mapping file
if cfg.GenMappingFileOnly {
return nil
}
for i := 0; i < len(list); i += cfg.Parallelism {
n := cfg.Parallelism
if i+cfg.Parallelism > len(list) {
n = len(list) - i
}
var importList []*meta.ImportItem
messages := []string{"Importing resources..."}
for j := 0; j < n; j++ {
idx := i + j
if list[idx].Skip() {
messages = append(messages, fmt.Sprintf("(%d/%d) Skipping %s", idx+1, len(list), list[idx].TFResourceId))
} else {
messages = append(messages, fmt.Sprintf("(%d/%d) Importing %s as %s", idx+1, len(list), list[idx].TFResourceId, list[idx].TFAddr))
}
importList = append(importList, &list[idx])
}
msg.SetStatus(strings.Join(messages, "\n"))
if err := c.ParallelImport(ctx, importList); err != nil {
return fmt.Errorf("parallel importing: %v", err)
}
var thisErrors []string
for j := 0; j < n; j++ {
idx := i + j
item := list[idx]
if err := item.ImportError; err != nil {
msg := fmt.Sprintf("Failed to import %s as %s: %v", item.TFResourceId, item.TFAddr, err)
thisErrors = append(thisErrors, msg)
}
}
if len(thisErrors) != 0 {
errors = append(errors, thisErrors...)
if !cfg.ContinueOnError {
return fmt.Errorf(strings.Join(thisErrors, "\n"))
}
}
}
if err := c.PushState(ctx); err != nil {
return fmt.Errorf("failed to push state: %v", err)
}
msg.SetStatus("Generating Terraform configurations...")
if err := c.GenerateCfg(ctx, list); err != nil {
return fmt.Errorf("generating Terraform configuration: %v", err)
}
msg.SetStatus("Cleaning up...")
if err := c.CleanUpWorkspace(ctx); err != nil {
return fmt.Errorf("cleaning up main workspace: %v", err)
}
return nil
}
var err error
if cfg.PlainUI {
err = f(NewStdoutMessager())
} else {
s := bspinner.NewModel()
s.Spinner = common.Spinner
sf := func(msg spinner.Messager) error {
return f(&msg)
}
err = spinner.Run(s, sf)
}
if err != nil {
return err
}
// Print out the errors, if any
if len(errors) != 0 {
fmt.Fprintln(os.Stderr, "Errors:\n"+strings.Join(errors, "\n"))
}
return nil
}