зеркало из https://github.com/Azure/azapi-lsp.git
update aztfmigrate (#110)
This commit is contained in:
Родитель
ef6ea14bab
Коммит
c44e76aa0b
2
go.mod
2
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
|
||||
|
|
4
go.sum
4
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=
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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...")
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче