From c8b71dce364f626351e3e3b902b7e2c5c2b01b8c Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Thu, 10 Oct 2024 16:28:37 +0800 Subject: [PATCH] update aztfmigrate --- go.mod | 2 +- go.sum | 4 +++ .../handlers/command/aztfmigrate_command.go | 29 ++++++++++++++++--- .../aztfmigrate/types/azurerm_resource.go | 1 + .../github.com/Azure/aztfmigrate/types/hcl.go | 6 ++-- .../Azure/aztfmigrate/types/utils.go | 7 ++++- vendor/modules.txt | 2 +- 7 files changed, 41 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 6c5b2ab0..a75d0f87 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Azure/azapi-lsp go 1.22.0 require ( - github.com/Azure/aztfmigrate v1.15.1-0.20241010052938-f55ac2833cd3 + github.com/Azure/aztfmigrate v1.15.1-0.20241010081637-fcc6b9d0f7dc github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0 github.com/apparentlymart/go-textseg v1.0.0 github.com/creachadair/jrpc2 v0.32.0 diff --git a/go.sum b/go.sum index 256ca218..bb024949 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,10 @@ github.com/Azure/aztfmigrate v1.15.1-0.20241008023126-5d1f089351ad h1:b09oirToeb github.com/Azure/aztfmigrate v1.15.1-0.20241008023126-5d1f089351ad/go.mod h1:XqyslKnttA4clKBJ837VOTSAsj0nBvr6MUIOnvrIRIQ= github.com/Azure/aztfmigrate v1.15.1-0.20241010052938-f55ac2833cd3 h1:ou4lFpnCzxTCtEtwCvIPRIYS+iRS4yXKGUQj/hpsnX0= github.com/Azure/aztfmigrate v1.15.1-0.20241010052938-f55ac2833cd3/go.mod h1:bQOrvrR/2/X6rtJDGH7ARJXZT3cmTMGC6QXIDb4rMxE= +github.com/Azure/aztfmigrate v1.15.1-0.20241010074100-daaa4f34b510 h1:ncxS1gQGJWL2ki1WUWfYe9rU61pv9kjKBNGgmTPGQKc= +github.com/Azure/aztfmigrate v1.15.1-0.20241010074100-daaa4f34b510/go.mod h1:bQOrvrR/2/X6rtJDGH7ARJXZT3cmTMGC6QXIDb4rMxE= +github.com/Azure/aztfmigrate v1.15.1-0.20241010081637-fcc6b9d0f7dc h1:xh3lnKKz2irii5amvw3endArzp5tylwOv2AGnjYu8os= +github.com/Azure/aztfmigrate v1.15.1-0.20241010081637-fcc6b9d0f7dc/go.mod h1:bQOrvrR/2/X6rtJDGH7ARJXZT3cmTMGC6QXIDb4rMxE= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0 h1:nyQWyZvwGTvunIMxi1Y9uXkcyr+I7TeNrr/foo4Kpk8= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0/go.mod h1:l38EPgmsp71HHLq9j7De57JcKOWPyhrsW1Awm1JS6K0= github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0 h1:tfLQ34V6F7tVSwoTf/4lH5sE0o6eCJuNDTmH09nDpbc= diff --git a/internal/langserver/handlers/command/aztfmigrate_command.go b/internal/langserver/handlers/command/aztfmigrate_command.go index 76dfe247..e61ec5cb 100644 --- a/internal/langserver/handlers/command/aztfmigrate_command.go +++ b/internal/langserver/handlers/command/aztfmigrate_command.go @@ -52,6 +52,7 @@ func (c AztfMigrateCommand) Handle(ctx context.Context, arguments []json.RawMess } reportProgress(ctx, "Parsing Terraform configurations...", 0) + defer reportProgress(ctx, "Migration completed.", 100) fs, err := lsctx.DocumentStorage(ctx) if err != nil { @@ -100,8 +101,14 @@ func (c AztfMigrateCommand) Handle(ctx context.Context, arguments []json.RawMess } // parsing the document - syntaxDoc, _ := hclsyntax.ParseConfig(data, "", hcl.InitialPos) - writeDoc, _ := hclwrite.ParseConfig(data, "", hcl.InitialPos) + syntaxDoc, diags := hclsyntax.ParseConfig(data, "", hcl.InitialPos) + if diags.HasErrors() { + return nil, fmt.Errorf("parsing the HCL file: %s", diags.Error()) + } + writeDoc, diags := hclwrite.ParseConfig(data, "", hcl.InitialPos) + if diags.HasErrors() { + return nil, fmt.Errorf("parsing the HCL file: %s", diags.Error()) + } syntaxBlockMap := map[string]*hclsyntax.Block{} writeBlockMap := map[string]*hclwrite.Block{} @@ -109,12 +116,14 @@ func (c AztfMigrateCommand) Handle(ctx context.Context, arguments []json.RawMess if !ok { return nil, fmt.Errorf("failed to parse HCL syntax") } + addresses := make([]string, 0) for _, block := range body.Blocks { if startPos.Position().Byte <= block.Range().Start.Byte && block.Range().End.Byte <= endPos.Position().Byte { if block.Type != "resource" { continue } address := strings.Join(block.Labels, ".") + addresses = append(addresses, address) syntaxBlockMap[address] = block } } @@ -249,7 +258,20 @@ func (c AztfMigrateCommand) Handle(ctx context.Context, arguments []json.RawMess // update config emptyFile := hclwrite.NewEmptyFile() + outputs := make([]types.Output, 0) + resourcesMap := make(map[string]types.AzureResource) for _, r := range resources { + if r.IsMigrated() { + outputs = append(outputs, r.Outputs()...) + } + resourcesMap[r.OldAddress(nil)] = r + } + + for _, addr := range addresses { + r := resourcesMap[addr] + if r == nil { + continue + } if !r.IsMigrated() { emptyFile.Body().AppendBlock(writeBlockMap[r.OldAddress(nil)]) emptyFile.Body().AppendNewline() @@ -270,6 +292,7 @@ func (c AztfMigrateCommand) Handle(ctx context.Context, arguments []json.RawMess emptyFile.Body().AppendNewline() } if migratedBlock := r.MigratedBlock(); migratedBlock != nil { + types.ReplaceOutputs(migratedBlock, outputs) emptyFile.Body().AppendBlock(migratedBlock) emptyFile.Body().AppendNewline() } @@ -289,8 +312,6 @@ func (c AztfMigrateCommand) Handle(ctx context.Context, arguments []json.RawMess }, }) - reportProgress(ctx, "Migration completed.", 100) - return nil, nil } diff --git a/vendor/github.com/Azure/aztfmigrate/types/azurerm_resource.go b/vendor/github.com/Azure/aztfmigrate/types/azurerm_resource.go index bc9c667f..90bd0914 100644 --- a/vendor/github.com/Azure/aztfmigrate/types/azurerm_resource.go +++ b/vendor/github.com/Azure/aztfmigrate/types/azurerm_resource.go @@ -65,6 +65,7 @@ func (r *AzurermResource) GenerateNewConfig(terraform *tf.Terraform) error { } else { log.Printf("[ERROR] %+v", err) } + r.Block = InjectReference(r.Block, r.References) } else { // import and build combined block log.Printf("[INFO] generating config...") diff --git a/vendor/github.com/Azure/aztfmigrate/types/hcl.go b/vendor/github.com/Azure/aztfmigrate/types/hcl.go index 3c824a57..87ce42fc 100644 --- a/vendor/github.com/Azure/aztfmigrate/types/hcl.go +++ b/vendor/github.com/Azure/aztfmigrate/types/hcl.go @@ -100,7 +100,7 @@ func ReplaceGenericOutputs(workingDirectory string, outputs []Output) error { continue } if block != nil { - replaceOutputs(block, outputs) + ReplaceOutputs(block, outputs) } } if err := os.WriteFile(filepath.Join(workingDirectory, file.Name()), hclwrite.Format(f.Bytes()), 0600); err != nil { @@ -110,7 +110,7 @@ func ReplaceGenericOutputs(workingDirectory string, outputs []Output) error { return nil } -func replaceOutputs(block *hclwrite.Block, outputs []Output) { +func ReplaceOutputs(block *hclwrite.Block, outputs []Output) { for attrName, attr := range block.Body().Attributes() { attrValue := string(attr.Expr().BuildTokens(nil).Bytes()) for _, output := range outputs { @@ -119,7 +119,7 @@ func replaceOutputs(block *hclwrite.Block, outputs []Output) { block.Body().SetAttributeRaw(attrName, helper.GetTokensForExpression(attrValue)) } for index := range block.Body().Blocks() { - replaceOutputs(block.Body().Blocks()[index], outputs) + ReplaceOutputs(block.Body().Blocks()[index], outputs) } } diff --git a/vendor/github.com/Azure/aztfmigrate/types/utils.go b/vendor/github.com/Azure/aztfmigrate/types/utils.go index 40ad1b9b..c6c7de58 100644 --- a/vendor/github.com/Azure/aztfmigrate/types/utils.go +++ b/vendor/github.com/Azure/aztfmigrate/types/utils.go @@ -53,7 +53,7 @@ func getReferencesForAddress(address string, p *tfjson.Plan, refValueMap map[str for _, ref := range res { // if it refers to some resource's id before, after migration, it will refer to its name now // TODO: use regex - if strings.HasPrefix(ref.Name, "azurerm_") && len(strings.Split(ref.Name, ".")) == 3 && strings.HasSuffix(ref.Name, "id") { + if len(strings.Split(ref.Name, ".")) == 3 && strings.HasSuffix(ref.Name, "id") { temp = append(temp, Reference{ Name: ref.Name[0:strings.LastIndex(ref.Name, ".")] + ".name", }) @@ -63,6 +63,11 @@ func getReferencesForAddress(address string, p *tfjson.Plan, refValueMap map[str }) } } + if len(strings.Split(ref.Name, ".")) == 3 && strings.HasSuffix(ref.Name, "name") { + temp = append(temp, Reference{ + Name: ref.Name[0:strings.LastIndex(ref.Name, ".")] + ".id", + }) + } } res = append(res, temp...) break diff --git a/vendor/modules.txt b/vendor/modules.txt index 0ea568db..21a56456 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,7 +1,7 @@ # dario.cat/mergo v1.0.1 ## explicit; go 1.13 dario.cat/mergo -# github.com/Azure/aztfmigrate v1.15.1-0.20241010052938-f55ac2833cd3 +# github.com/Azure/aztfmigrate v1.15.1-0.20241010081637-fcc6b9d0f7dc ## explicit; go 1.22.0 github.com/Azure/aztfmigrate/azurerm github.com/Azure/aztfmigrate/azurerm/coverage