Skip to content

Commit

Permalink
update aztfmigrate
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Oct 10, 2024
1 parent ef6ea14 commit c8b71dc
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
29 changes: 25 additions & 4 deletions internal/langserver/handlers/command/aztfmigrate_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -100,21 +101,29 @@ 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{}

body, ok := syntaxDoc.Body.(*hclsyntax.Body)
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
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -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()
}
Expand All @@ -289,8 +312,6 @@ func (c AztfMigrateCommand) Handle(ctx context.Context, arguments []json.RawMess
},
})

reportProgress(ctx, "Migration completed.", 100)

return nil, nil
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/github.com/Azure/aztfmigrate/types/hcl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion vendor/github.com/Azure/aztfmigrate/types/utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit c8b71dc

Please sign in to comment.