Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions internal/meta/append_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package meta

import (
"fmt"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
)

func hclBlockAppendLifecycle(body *hclwrite.Body, ignoreChanges []string) error {
srcs := map[string][]byte{}
if len(ignoreChanges) > 0 {
for i := range ignoreChanges {
ignoreChanges[i] = ignoreChanges[i] + ","
}
srcs["ignore_changes"] = []byte("ignore_changes = [\n" + strings.Join(ignoreChanges, "\n") + "\n]\n")
}

if len(srcs) == 0 {
return nil
}

b := hclwrite.NewBlock("lifecycle", nil)
for name, src := range srcs {
expr, diags := hclwrite.ParseConfig(src, "f", hcl.InitialPos)
if diags.HasErrors() {
return fmt.Errorf(`building "lifecycle.%s" attribute: %s`, name, diags.Error())
}
b.Body().SetAttributeRaw(name, expr.Body().GetAttribute(name).Expr().BuildTokens(nil))
}
body.AppendBlock(b)
return nil
}
17 changes: 8 additions & 9 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,11 +1092,10 @@ func (meta baseMeta) stateToConfig(ctx context.Context, list ImportList) (Config
}
out = append(out, ConfigInfo{
ImportItem: importedList[i],
hcl: f,
dependencies: Dependencies{
refDeps: make(map[string]Dependency),
parentChildDeps: make(map[Dependency]bool),
ambiguousRefDeps: make(map[string][]Dependency),
HCL: f,
Dependencies: Dependencies{
ByIdRef: make(map[string]Dependency),
ByIdRefAmbiguous: make(map[string][]Dependency),
},
})
}
Expand Down Expand Up @@ -1148,7 +1147,7 @@ func (meta baseMeta) lifecycleAddon(configs ConfigInfos) (ConfigInfos, error) {
for i, cfg := range configs {
switch cfg.TFAddr.Type {
case "azurerm_application_insights_web_test":
if err := hclBlockAppendLifecycle(cfg.hcl.Body().Blocks()[0].Body(), []string{"tags"}); err != nil {
if err := hclBlockAppendLifecycle(cfg.HCL.Body().Blocks()[0].Body(), []string{"tags"}); err != nil {
return nil, fmt.Errorf("azurerm_application_insights_web_test: %v", err)
}
}
Expand All @@ -1158,12 +1157,12 @@ func (meta baseMeta) lifecycleAddon(configs ConfigInfos) (ConfigInfos, error) {
}

func (meta baseMeta) addDependency(configs ConfigInfos) (ConfigInfos, error) {
if err := configs.PopulateReferenceDependencies(); err != nil {
if err := configs.PopulateReferenceDeps(); err != nil {
return nil, fmt.Errorf("populating reference dependencies: %v", err)
}
configs.populateParentChildDependency()
configs.PopulateRelationDeps()

if err := configs.applyDependenciesToHclBlock(); err != nil {
if err := configs.ApplyDepsToHCL(); err != nil {
return nil, fmt.Errorf("applying dependencies to HCL blocks: %v", err)
}

Expand Down
Loading
Loading