|
| 1 | +package migrate |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "maps" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/databricks/cli/bundle/config" |
| 10 | + "github.com/databricks/cli/bundle/config/resources" |
| 11 | + "github.com/databricks/cli/bundle/deploy/terraform" |
| 12 | + "github.com/databricks/cli/bundle/direct" |
| 13 | + "github.com/databricks/cli/bundle/direct/dresources" |
| 14 | + "github.com/databricks/cli/bundle/direct/dstate" |
| 15 | + "github.com/databricks/cli/libs/dyn" |
| 16 | + "github.com/databricks/cli/libs/log" |
| 17 | + "github.com/databricks/cli/libs/structs/structaccess" |
| 18 | + "github.com/databricks/cli/libs/structs/structpath" |
| 19 | + "github.com/databricks/cli/libs/structs/structvar" |
| 20 | +) |
| 21 | + |
| 22 | +// BuildStateFromTF iterates over bundle resources, resolves cross-resource |
| 23 | +// references using TF state attributes, and writes each resource's state entry. |
| 24 | +// configRoot should be an un-interpolated config (with ${resources.*} references). |
| 25 | +func BuildStateFromTF( |
| 26 | + ctx context.Context, |
| 27 | + configRoot *config.Root, |
| 28 | + adapters map[string]*dresources.Adapter, |
| 29 | + stateDB *dstate.DeploymentState, |
| 30 | + tfAttrs TFStateAttrs, |
| 31 | + tfIDs terraform.ExportedResourcesMap, |
| 32 | + etags map[string]string, |
| 33 | +) error { |
| 34 | + // Collect all resource nodes (same patterns as makePlan). |
| 35 | + var nodes []string |
| 36 | + patterns := []dyn.Pattern{ |
| 37 | + dyn.NewPattern(dyn.Key("resources"), dyn.AnyKey(), dyn.AnyKey()), |
| 38 | + dyn.NewPattern(dyn.Key("resources"), dyn.AnyKey(), dyn.AnyKey(), dyn.Key("permissions")), |
| 39 | + dyn.NewPattern(dyn.Key("resources"), dyn.AnyKey(), dyn.AnyKey(), dyn.Key("grants")), |
| 40 | + } |
| 41 | + for _, pat := range patterns { |
| 42 | + _, err := dyn.MapByPattern( |
| 43 | + configRoot.Value(), |
| 44 | + pat, |
| 45 | + func(p dyn.Path, v dyn.Value) (dyn.Value, error) { |
| 46 | + nodes = append(nodes, p.String()) |
| 47 | + return dyn.InvalidValue, nil |
| 48 | + }, |
| 49 | + ) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + for _, node := range nodes { |
| 56 | + idEntry, ok := tfIDs[node] |
| 57 | + if !ok { |
| 58 | + // Resource is in config but not in TF state (new resource); skip. |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + group := config.GetResourceTypeFromKey(node) |
| 63 | + if group == "" { |
| 64 | + return fmt.Errorf("cannot determine resource type for %q", node) |
| 65 | + } |
| 66 | + |
| 67 | + adapter, ok := adapters[group] |
| 68 | + if !ok { |
| 69 | + log.Warnf(ctx, "unsupported resource type %q for %s, skipping", group, node) |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + inputConfig, err := configRoot.GetResourceConfig(node) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("%s: getting config: %w", node, err) |
| 76 | + } |
| 77 | + |
| 78 | + baseRefs := map[string]string{} |
| 79 | + |
| 80 | + switch { |
| 81 | + case strings.HasSuffix(node, ".permissions"): |
| 82 | + var sv *structvar.StructVar |
| 83 | + if strings.HasPrefix(node, "resources.secret_scopes.") { |
| 84 | + typedConfig, ok := inputConfig.(*[]resources.SecretScopePermission) |
| 85 | + if !ok { |
| 86 | + return fmt.Errorf("%s: expected *[]resources.SecretScopePermission, got %T", node, inputConfig) |
| 87 | + } |
| 88 | + sv, err = dresources.PrepareSecretScopeAclsInputConfig(*typedConfig, node) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("%s: preparing secret scope ACLs config: %w", node, err) |
| 91 | + } |
| 92 | + } else { |
| 93 | + sv, err = dresources.PreparePermissionsInputConfig(inputConfig, node) |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("%s: preparing permissions config: %w", node, err) |
| 96 | + } |
| 97 | + } |
| 98 | + inputConfig = sv.Value |
| 99 | + baseRefs = sv.Refs |
| 100 | + |
| 101 | + case strings.HasSuffix(node, ".grants"): |
| 102 | + sv, err := dresources.PrepareGrantsInputConfig(inputConfig, node) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("%s: preparing grants config: %w", node, err) |
| 105 | + } |
| 106 | + inputConfig = sv.Value |
| 107 | + baseRefs = sv.Refs |
| 108 | + } |
| 109 | + |
| 110 | + newStateValue, err := adapter.PrepareState(inputConfig) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("%s: PrepareState: %w", node, err) |
| 113 | + } |
| 114 | + |
| 115 | + refs, err := direct.ExtractReferences(configRoot.Value(), node) |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("%s: extracting references: %w", node, err) |
| 118 | + } |
| 119 | + maps.Copy(refs, baseRefs) |
| 120 | + |
| 121 | + sv := structvar.NewStructVar(newStateValue, refs) |
| 122 | + |
| 123 | + // Resolve each reference using TF state. |
| 124 | + // node format: "resources.<group>.<name>" or "resources.<group>.<name>.permissions" |
| 125 | + parts := strings.SplitN(node, ".", 4) |
| 126 | + var srcGroup, srcName string |
| 127 | + if len(parts) >= 3 { |
| 128 | + srcGroup = parts[1] |
| 129 | + srcName = parts[2] |
| 130 | + } |
| 131 | + |
| 132 | + // Collect all field paths that need resolution (avoid modifying map during iteration). |
| 133 | + type refEntry struct { |
| 134 | + fieldPathStr string |
| 135 | + refTemplate string |
| 136 | + } |
| 137 | + var pendingRefs []refEntry |
| 138 | + for fieldPathStr, refTemplate := range sv.Refs { |
| 139 | + pendingRefs = append(pendingRefs, refEntry{fieldPathStr, refTemplate}) |
| 140 | + } |
| 141 | + |
| 142 | + for _, pending := range pendingRefs { |
| 143 | + fieldPath, err := structpath.ParsePath(pending.fieldPathStr) |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("%s: parsing field path %q: %w", node, pending.fieldPathStr, err) |
| 146 | + } |
| 147 | + |
| 148 | + // ResolveFieldRef returns the fully resolved value for this field, |
| 149 | + // using either Method A (TF state lookup) or Method B (template evaluation). |
| 150 | + value, err := ResolveFieldRef(ctx, tfAttrs, srcGroup, srcName, fieldPath, pending.refTemplate) |
| 151 | + if err != nil { |
| 152 | + return fmt.Errorf("%s: cannot resolve field %q (template %q): %w", node, pending.fieldPathStr, pending.refTemplate, err) |
| 153 | + } |
| 154 | + |
| 155 | + // Set the resolved value directly and remove the ref entry. |
| 156 | + if err := structaccess.Set(sv.Value, fieldPath, value); err != nil { |
| 157 | + return fmt.Errorf("%s: cannot set resolved value for field %q: %w", node, pending.fieldPathStr, err) |
| 158 | + } |
| 159 | + delete(sv.Refs, pending.fieldPathStr) |
| 160 | + } |
| 161 | + |
| 162 | + if len(sv.Refs) > 0 { |
| 163 | + return fmt.Errorf("%s: unresolved references: %v", node, sv.Refs) |
| 164 | + } |
| 165 | + |
| 166 | + // Handle etag for dashboards. |
| 167 | + if etag := etags[node]; etag != "" { |
| 168 | + if err := structaccess.Set(sv.Value, structpath.NewStringKey(nil, "etag"), etag); err != nil { |
| 169 | + return fmt.Errorf("%s: cannot set etag: %w", node, err) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if err := stateDB.SaveState(node, idEntry.ID, sv.Value, nil); err != nil { |
| 174 | + return fmt.Errorf("%s: SaveState: %w", node, err) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return nil |
| 179 | +} |
0 commit comments