Skip to content

Commit 396ba4e

Browse files
committed
migrate: add ETagFor method on TFStateAttrs; simplify etag handling
Replace the verbose LookupTFField call for etag with a direct JSON read via TFStateAttrs.ETagFor(group, name). Co-authored-by: Isaac
1 parent 28e4fc8 commit 396ba4e

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

bundle/migrate/build_state.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,12 @@ func BuildStateFromTF(
204204
return fmt.Errorf("%s: unresolved references: %v", node, sv.Refs)
205205
}
206206

207-
// Handle etag for dashboards: look it up directly from TF state attributes.
207+
// Handle etag for dashboards: read it directly from TF state attributes.
208208
// The "etag" field is a computed TF attribute not present in the bundle config,
209209
// so it does not flow through PrepareState/ExtractReferences.
210-
if etag, err := LookupTFField(tfAttrs, group, srcName, structpath.NewStringKey(nil, "etag")); err == nil && etag != nil {
211-
if etagStr, ok := etag.(string); ok && etagStr != "" {
212-
if err := structaccess.Set(sv.Value, structpath.NewStringKey(nil, "etag"), etagStr); err != nil {
213-
return fmt.Errorf("%s: cannot set etag: %w", node, err)
214-
}
210+
if etag := tfAttrs.ETagFor(group, srcName); etag != "" {
211+
if err := structaccess.Set(sv.Value, structpath.NewStringKey(nil, "etag"), etag); err != nil {
212+
return fmt.Errorf("%s: cannot set etag: %w", node, err)
215213
}
216214
}
217215

bundle/migrate/tf_state.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ var tfStateFieldAliases = map[string]map[string]string{
2525
// TFStateAttrs maps (tfResourceType → resourceName → raw JSON attributes).
2626
type TFStateAttrs map[string]map[string]json.RawMessage
2727

28+
// ETagFor returns the "etag" attribute for a bundle resource, or "" if absent.
29+
// Reads directly from the raw JSON without full path translation.
30+
func (a TFStateAttrs) ETagFor(group, name string) string {
31+
tfType, ok := terraform.GroupToTerraformName[group]
32+
if !ok {
33+
return ""
34+
}
35+
raw, ok := a[tfType][name]
36+
if !ok {
37+
return ""
38+
}
39+
var v struct {
40+
Etag string `json:"etag,omitempty"`
41+
}
42+
if err := json.Unmarshal(raw, &v); err != nil {
43+
return ""
44+
}
45+
return v.Etag
46+
}
47+
2848
// TFState holds everything parsed from a single terraform state file read.
2949
type TFState struct {
3050
// Attrs maps (tfResourceType → resourceName → raw JSON attributes).

0 commit comments

Comments
 (0)