Skip to content

Commit 3356a97

Browse files
authored
Move terraform.Load() to statemgmt.Load() (#3161)
## Changes Move terraform.Load to statemgmt.Load Move terraform.TerraformToBundle to statemgmt.StateToBundle. Move related unit tests. Pure move, no functional changes. Stacked on top of #3134 ## Why Preparation for making it work with direct deployment.
1 parent f154d35 commit 3356a97

File tree

10 files changed

+706
-681
lines changed

10 files changed

+706
-681
lines changed

bundle/deploy/terraform/convert.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/databricks/cli/bundle/config"
87
"github.com/databricks/cli/bundle/config/resources"
98
"github.com/databricks/cli/bundle/deploy/terraform/tfdyn"
109
"github.com/databricks/cli/bundle/internal/tf/schema"
@@ -77,65 +76,3 @@ func BundleToTerraformWithDynValue(ctx context.Context, root dyn.Value) (*schema
7776

7877
return tfroot, nil
7978
}
80-
81-
func ensureMap(v dyn.Value, path dyn.Path) (dyn.Value, error) {
82-
item, _ := dyn.GetByPath(v, path)
83-
if !item.IsValid() {
84-
var err error
85-
v, err = dyn.SetByPath(v, path, dyn.V(dyn.NewMapping()))
86-
if err != nil {
87-
return dyn.InvalidValue, fmt.Errorf("internal error: failed to create %s: %s", path, err)
88-
}
89-
}
90-
return v, nil
91-
}
92-
93-
func TerraformToBundle(ctx context.Context, state ExportedResourcesMap, config *config.Root) error {
94-
return config.Mutate(func(v dyn.Value) (dyn.Value, error) {
95-
var err error
96-
v, err = ensureMap(v, dyn.Path{dyn.Key("resources")})
97-
if err != nil {
98-
return v, err
99-
}
100-
101-
for groupName, group := range state {
102-
var err error
103-
v, err = ensureMap(v, dyn.Path{dyn.Key("resources"), dyn.Key(groupName)})
104-
if err != nil {
105-
return v, err
106-
}
107-
108-
for resourceName, attrs := range group {
109-
path := dyn.Path{dyn.Key("resources"), dyn.Key(groupName), dyn.Key(resourceName)}
110-
resource, err := dyn.GetByPath(v, path)
111-
if !resource.IsValid() {
112-
m := dyn.NewMapping()
113-
m.SetLoc("id", nil, dyn.V(attrs.ID))
114-
m.SetLoc("modified_status", nil, dyn.V(resources.ModifiedStatusDeleted))
115-
v, err = dyn.SetByPath(v, path, dyn.V(m))
116-
if err != nil {
117-
return dyn.InvalidValue, err
118-
}
119-
} else if err != nil {
120-
return dyn.InvalidValue, err
121-
} else {
122-
v, err = dyn.SetByPath(v, dyn.Path{dyn.Key("resources"), dyn.Key(groupName), dyn.Key(resourceName), dyn.Key("id")}, dyn.V(attrs.ID))
123-
if err != nil {
124-
return dyn.InvalidValue, err
125-
}
126-
}
127-
}
128-
}
129-
130-
return dyn.MapByPattern(v, dyn.Pattern{dyn.Key("resources"), dyn.AnyKey(), dyn.AnyKey()}, func(p dyn.Path, inner dyn.Value) (dyn.Value, error) {
131-
idPath := dyn.Path{dyn.Key("id")}
132-
statusPath := dyn.Path{dyn.Key("modified_status")}
133-
id, _ := dyn.GetByPath(inner, idPath)
134-
status, _ := dyn.GetByPath(inner, statusPath)
135-
if !id.IsValid() && !status.IsValid() {
136-
return dyn.SetByPath(inner, statusPath, dyn.V(resources.ModifiedStatusCreated))
137-
}
138-
return inner, nil
139-
})
140-
})
141-
}

0 commit comments

Comments
 (0)