Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions bundle/deploy/terraform/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,36 +234,54 @@ func TerraformToBundle(state *tfjson.State, config *config.Root) error {
case "databricks_job":
var tmp schema.ResourceJob
conv(resource.AttributeValues, &tmp)
if config.Resources.Jobs == nil {
Comment thread
ilia-db marked this conversation as resolved.
Outdated
config.Resources.Jobs = make(map[string]*resources.Job)
}
cur := config.Resources.Jobs[resource.Name]
conv(tmp, &cur)
config.Resources.Jobs[resource.Name] = cur
case "databricks_pipeline":
var tmp schema.ResourcePipeline
conv(resource.AttributeValues, &tmp)
if config.Resources.Pipelines == nil {
config.Resources.Pipelines = make(map[string]*resources.Pipeline)
}
cur := config.Resources.Pipelines[resource.Name]
conv(tmp, &cur)
config.Resources.Pipelines[resource.Name] = cur
case "databricks_mlflow_model":
var tmp schema.ResourceMlflowModel
conv(resource.AttributeValues, &tmp)
if config.Resources.Models == nil {
config.Resources.Models = make(map[string]*resources.MlflowModel)
}
cur := config.Resources.Models[resource.Name]
conv(tmp, &cur)
config.Resources.Models[resource.Name] = cur
case "databricks_mlflow_experiment":
var tmp schema.ResourceMlflowExperiment
conv(resource.AttributeValues, &tmp)
if config.Resources.Experiments == nil {
config.Resources.Experiments = make(map[string]*resources.MlflowExperiment)
}
cur := config.Resources.Experiments[resource.Name]
conv(tmp, &cur)
config.Resources.Experiments[resource.Name] = cur
case "databricks_model_serving":
var tmp schema.ResourceModelServing
conv(resource.AttributeValues, &tmp)
if config.Resources.ModelServingEndpoints == nil {
config.Resources.ModelServingEndpoints = make(map[string]*resources.ModelServingEndpoint)
}
cur := config.Resources.ModelServingEndpoints[resource.Name]
conv(tmp, &cur)
config.Resources.ModelServingEndpoints[resource.Name] = cur
case "databricks_registered_model":
var tmp schema.ResourceRegisteredModel
conv(resource.AttributeValues, &tmp)
if config.Resources.RegisteredModels == nil {
config.Resources.RegisteredModels = make(map[string]*resources.RegisteredModel)
}
cur := config.Resources.RegisteredModels[resource.Name]
conv(tmp, &cur)
config.Resources.RegisteredModels[resource.Name] = cur
Expand Down
1 change: 1 addition & 0 deletions cmd/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ func New() *cobra.Command {
cmd.AddCommand(newTestCommand())
cmd.AddCommand(newValidateCommand())
cmd.AddCommand(newInitCommand())
cmd.AddCommand(newRemoteStateCommand())
return cmd
}
73 changes: 73 additions & 0 deletions cmd/bundle/remote_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package bundle

import (
"encoding/json"
"errors"
"os"
"path/filepath"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/deploy/terraform"
"github.com/databricks/cli/cmd/root"

"github.com/databricks/cli/bundle/phases"
"github.com/spf13/cobra"
)

func newRemoteStateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "remote-state",
Short: "Pull and print deployed state of the bundle",
Comment thread
andrewnester marked this conversation as resolved.
Outdated

PreRunE: root.MustWorkspaceClient,

// This command is currently intended only for the Databricks VSCode extension
Hidden: true,
}

var forcePull bool
cmd.Flags().BoolVar(&forcePull, "force-pull", false, "Skip local cache and load the state from the remote workspace")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
Comment thread
ilia-db marked this conversation as resolved.
b := bundle.Get(cmd.Context())

err := bundle.Apply(cmd.Context(), b, phases.Initialize())
if err != nil {
return err
}

cacheDir, err := terraform.Dir(cmd.Context(), b)
if err != nil {
return err
}
_, err = os.Stat(filepath.Join(cacheDir, terraform.TerraformStateFileName))
noCache := errors.Is(err, os.ErrNotExist)

if forcePull || noCache {
err = bundle.Apply(cmd.Context(), b, terraform.StatePull())
if err != nil {
return err
}
}

// After the initialization phase the local bundle configuration is no longer necessary.
// We want to populate the config with the remote state only.
b.Config.Resources = config.Resources{}

err = bundle.Apply(cmd.Context(), b, terraform.Load())
if err != nil {
return err
}

buf, err := json.MarshalIndent(b.Config, "", " ")
Comment thread
ilia-db marked this conversation as resolved.
Outdated
if err != nil {
return err
}

cmd.OutOrStdout().Write(buf)
Comment thread
ilia-db marked this conversation as resolved.
Outdated
return nil
}

return cmd
}