-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
350 lines (304 loc) · 12.3 KB
/
main.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
package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-10-01/resources"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/aristosvo/aztfmove/state"
)
var (
tfVars state.ArrayVars
tfVarFiles state.ArrayVarFiles
// TODO: should probably refactor `-resource` and `-module` to `-target` to mimic terraform flags as much as possible
resourceFlag = flag.String("resource", "*", "Terraform resource to be moved. For example 'module.storage.azurerm_storage_account.example'.")
moduleFlag = flag.String("module", "*", "Terraform module to be moved. For example 'module.storage'.")
sourceResourceGroupFlag = flag.String("resource-group", "*", "Azure resource group to be moved. For example 'example-source-resource-group'.")
sourceSubscriptionFlag = flag.String("subscription-id", os.Getenv("ARM_SUBSCRIPTION_ID"), "subscription where resources are currently. Environment variable 'ARM_SUBSCRIPTION_ID' has the same functionality.")
targetResourceGroupFlag = flag.String("target-resource-group", "", "Azure resource group name where resources are moved. For example 'example-target-resource-group'. (required)")
targetSubscriptionFlag = flag.String("target-subscription-id", *sourceSubscriptionFlag, "Azure subscription ID where resources are moved. If not specified resources are moved within the subscription.")
autoApproveFlag = flag.Bool("auto-approve", false, "aztfmove first shows which resources are selected for a move and requires approval. If you want to approve automatically, use this flag.")
dryRunFlag = flag.Bool("dry-run", false, "if set to true, aztfmove only shows which resources are selected for a move.")
noColorFlag = flag.Bool("no-color", false, "if set to true, aztfmove prints without color.")
// TODO: var excludeResourcesFlag = flag.String("exclude-resources", "-", "Terraform resources to be excluded from moving. For example 'module.storage.azurerm_storage_account.example,module.storage.azurerm_storage_account.example'.")
// but..., this is not according to previously stated principle to mimic terraform flags as much as possible
)
func init() {
flag.Var(&tfVars, "var", "use this like you'd use Terraform \"-var\", i.e. \"-var 'test1=123' -var 'test2=312'\" ")
flag.Var(&tfVarFiles, "var-file", "use this like you'd use Terraform \"-var-file\", i.e. \"-var-file='tst.tfvars'\" ")
}
var (
Azure = TealBold
AzureCLI = Teal
Warn = Yellow
Fata = Red
Good = Green
Terraform = CyanBold
TerraformCLI = Cyan
)
var (
Red = Color("\033[1;31m%s\033[0m")
Green = Color("\033[1;32m%s\033[0m")
Yellow = Color("\033[1;33m%s\033[0m")
Teal = Color("\033[36m%s\033[0m")
TealBold = Color("\033[1;36m%s\033[0m")
CyanBold = Color("\033[1;35m%s\033[0m")
Cyan = Color("\033[35m%s\033[0m")
)
func Color(colorString string) func(...interface{}) string {
sprint := func(args ...interface{}) string {
if *noColorFlag && strings.Contains(colorString, "1;") {
return fmt.Sprintf("\033[1m%s\033[0m",
fmt.Sprint(args...))
} else if *noColorFlag {
return fmt.Sprint(args...)
}
return fmt.Sprintf(colorString,
fmt.Sprint(args...))
}
return sprint
}
func main() {
flag.Parse()
validateInput()
if *targetSubscriptionFlag == "" || *targetSubscriptionFlag == *sourceSubscriptionFlag {
fmt.Println(Good("No unique \"-target-subscription-id\" specified, move will be within the same subscription:"))
*targetSubscriptionFlag = *sourceSubscriptionFlag
} else {
fmt.Println(Good("Target subscription specified, move will be to a different subscription:"))
}
fmt.Printf(" %s -> %s \n", *sourceSubscriptionFlag, *targetSubscriptionFlag)
tfstate, err := state.PullRemote()
if err != nil {
fmt.Printf("%s Terraform state is not found. Try `terraform init`.", Fata("Error:"))
os.Exit(1)
}
resourceInstances, sourceResourceGroup, err := tfstate.Filter(*resourceFlag, *moduleFlag, *sourceResourceGroupFlag, *sourceSubscriptionFlag, *targetResourceGroupFlag, *targetSubscriptionFlag)
if err != nil {
fmt.Printf("%s %v", Fata("Error:"), err)
os.Exit(1)
}
printBlockingMovement(resourceInstances.BlockingMovement())
printNotSupported(resourceInstances.NotSupported())
printNotNeeded(resourceInstances.NoMovementNeeded())
printToMoveInAzure(resourceInstances.MovableOnAzure())
printToCorrectInTF(resourceInstances.ToCorrectInTFState())
if !*dryRunFlag && !*autoApproveFlag {
askConfirmation()
}
if tfIDsToRemove, azureIDsToDelete := resourceInstances.BlockingMovement(); len(azureIDsToDelete) > 0 {
fmt.Print(Azure("\nBlocking resources will be deleted in Azure."))
if *dryRunFlag {
fmt.Print(" (dry-run!)")
}
deleteAzureResources(azureIDsToDelete, sourceResourceGroup, *sourceSubscriptionFlag)
fmt.Print(Good("\n\nBlocking resources are deleted in Azure."))
if *dryRunFlag {
fmt.Print(" (dry-run!)")
}
fmt.Print(Terraform("\n\nResources in Terraform state will be removed:"))
if *dryRunFlag {
fmt.Print(" (dry-run!)")
}
removeTerraformResources(tfIDsToRemove)
}
if azureIDs := resourceInstances.MovableOnAzure(); len(azureIDs) > 0 {
fmt.Print(Azure("\nResources are on the move to the specified resource group."))
if *dryRunFlag {
fmt.Print(" (dry-run!)")
}
moveAzureResources(azureIDs, sourceResourceGroup, *sourceSubscriptionFlag, *targetSubscriptionFlag, *targetResourceGroupFlag)
fmt.Print(Good("\n\nResources are moved to the specified resource group."))
if *dryRunFlag {
fmt.Print(" (dry-run!)")
}
}
fmt.Print(Terraform("\n\nResources in Terraform state are enhanced:"))
if *dryRunFlag {
fmt.Print(" (dry-run!)")
}
reimportTerraformResources(resourceInstances.ToCorrectInTFState())
if *dryRunFlag {
fmt.Print(Good("\nDry-run complete!\n"))
fmt.Printf("Resources are not moved to the specified resource group, but the resources actions (and corresponding %s and %s commands) are visible above.\n", Azure("az cli"), Terraform("terraform"))
os.Exit(0)
}
fmt.Print(Good("\n\nCongratulations! Resources are moved in Azure and corrected in Terraform.\n"))
}
func validateInput() {
if *targetResourceGroupFlag == "" {
fmt.Printf("%s target-resource-group is a required variables\n", Fata("Error:"))
os.Exit(1)
}
if *sourceSubscriptionFlag == "" {
fmt.Printf("%s No resource subscription known, specify environment variable ARM_SUBSCRIPTION_ID or flag -subscription-id\n", Fata("Error:"))
os.Exit(1)
}
}
func printBlockingMovement(terraformIDs []string, azureIDs []string) {
if len(terraformIDs) == 0 {
return
}
fmt.Print(Warn("\nResources blocking movement of other resources:\n"))
for _, id := range terraformIDs {
fmt.Println(" -", id)
}
}
func printNotSupported(terraformIDs []string) {
if len(terraformIDs) == 0 {
return
}
fmt.Print(Warn("\nResources not supported for movement:\n"))
for _, id := range terraformIDs {
fmt.Println(" -", id)
}
}
func printNotNeeded(terraformIDs []string) {
if len(terraformIDs) == 0 {
return
}
fmt.Print(Good("\nResources with no need for movement:\n (mostly child resources)\n"))
for _, id := range terraformIDs {
fmt.Println(" -", id)
}
}
func printToCorrectInTF(resources map[string]string) {
fmt.Print(Terraform("\nResources to be corrected in Terraform:\n"))
for k, v := range resources {
fmt.Printf(" - %s: [id=%s]\n", k, v)
}
}
func printToMoveInAzure(azureIDs []string) {
fmt.Print(Azure("\nResources to be moved in Azure:\n"))
for _, id := range azureIDs {
fmt.Println(" -", id)
}
}
func askConfirmation() {
fmt.Print(Good("\nCan you confirm these resources should be moved?"))
if *dryRunFlag {
fmt.Print(" (dry-run!)")
}
fmt.Printf("\nCheck the Azure documentation on moving Azure resources (https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/move-resource-group-and-subscription) for all the details for your specific resources.")
fmt.Print(Good("\n\nType 'yes' to confirm: "))
reader := bufio.NewReader(os.Stdin)
inputString, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("%s confirmation of the import errored out\n", Fata("Error:"))
os.Exit(1)
}
inputString = strings.TrimSuffix(inputString, "\n")
if inputString != "yes" {
fmt.Printf("\nMove is canceled\n")
os.Exit(0)
}
}
func deleteAzureResources(azureIDs []string, sourceResourceGroup string, sourceSubscriptionID string) {
if *dryRunFlag {
fmt.Println("\nThe Azure delete actions when \"-dry-run=false\" are similar to the scripted action below:")
fmt.Printf(AzureCLI(" az resource delete --ids '%s'"), strings.Join(azureIDs, " "))
return
}
resourceClient := resources.NewClient(sourceSubscriptionID)
authorizer, err := auth.NewAuthorizerFromCLI()
if err == nil {
resourceClient.Authorizer = authorizer
}
for _, id := range azureIDs {
ctx, cancel := context.WithTimeout(context.Background(), 60*60*time.Second)
defer cancel()
// TODO: API Version hardcoded as only one category yet implemented
future, err := resourceClient.DeleteByID(ctx, id, "2021-02-01")
if err != nil {
fmt.Printf("\n%s cannot delete resources: %v", Fata("Error:"), err)
os.Exit(1)
}
err = future.WaitForCompletionRef(ctx, resourceClient.Client)
if err != nil {
fmt.Printf("\n%s cannot get the delete future response: %v", Fata("Error:"), err)
os.Exit(1)
}
}
}
func moveAzureResources(azureIDs []string, sourceResourceGroup string, sourceSubscriptionID string, targetSubscriptionID string, targetResourceGroup string) {
if *dryRunFlag {
fmt.Println("\nThe Azure move actions when \"-dry-run=false\" are similar to the scripted action below:")
fmt.Printf(AzureCLI(" az resource move --destination-group '%s' --destination-subscription-id '%s' --ids '%s'"), targetResourceGroup, targetSubscriptionID, strings.Join(azureIDs, " "))
return
}
fmt.Printf("\nIt can take some time before this is done, don't panic!")
targetResourceGroupID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s", targetSubscriptionID, targetResourceGroup)
moveInfo := resources.MoveInfo{
ResourcesProperty: &azureIDs,
TargetResourceGroup: &targetResourceGroupID,
}
resourceClient := resources.NewClient(sourceSubscriptionID)
authorizer, err := auth.NewAuthorizerFromCLI()
if err == nil {
resourceClient.Authorizer = authorizer
}
ctx, cancel := context.WithTimeout(context.Background(), 60*60*time.Second)
defer cancel()
future, err := resourceClient.MoveResources(ctx, sourceResourceGroup, moveInfo)
if err != nil {
fmt.Printf("\n%s cannot move resources: %v", Fata("Error:"), err)
os.Exit(1)
}
err = future.WaitForCompletionRef(ctx, resourceClient.Client)
if err != nil {
fmt.Printf("\n%s cannot get the move future response: %v", Fata("Error:"), err)
os.Exit(1)
}
}
func removeTerraformResources(tfIDs []string) {
if *dryRunFlag {
fmt.Println("\nThe Terraform actions taken when \"-dry-run=false\" are similar to the scripted actions below:")
for _, tfID := range tfIDs {
fmt.Println(" #", tfID)
fmt.Printf(TerraformCLI(" terraform state rm '%s'\n"), tfID)
}
return
}
for _, tfID := range tfIDs {
fmt.Println("\n -", tfID)
output, err := state.RemoveInstance(tfID)
if err != nil {
fmt.Printf("\n%s terraform resource is not removed, %v\n", Fata("Error:"), err)
fmt.Println(" ", output)
os.Exit(1)
}
fmt.Printf("\t✓ Removed")
}
}
func reimportTerraformResources(resources map[string]string) {
if *dryRunFlag {
fmt.Println("\nThe Terraform actions taken when \"-dry-run=false\" are similar to the scripted actions below:")
for tfID, newAzureID := range resources {
fmt.Println(" #", tfID)
fmt.Printf(TerraformCLI(" terraform state rm '%s'\n"), tfID)
fmt.Printf(TerraformCLI(" terraform import %s %s '%s' '%s'\n"), strings.Join(tfVarFiles, " "), strings.Join(tfVars, " "), tfID, newAzureID)
}
return
}
for tfID, newAzureID := range resources {
fmt.Println("\n -", tfID)
output, err := state.RemoveInstance(tfID)
if err != nil {
fmt.Printf("\n%s terraform resource is not removed, %v\n", Fata("Error:"), err)
fmt.Println(" ", output)
os.Exit(1)
}
fmt.Printf("\t✓ Removed")
output, err = state.ImportInstance(tfID, newAzureID, tfVars, tfVarFiles)
if err != nil {
fmt.Printf("\n%s terraform resource is not imported, %v\n", Fata("Error:"), err)
fmt.Println(" ", output)
os.Exit(1)
}
fmt.Printf("\t✓ Imported")
}
}