2024-01-25 11:32:47 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
2024-10-18 06:45:47 +00:00
|
|
|
"github.com/databricks/cli/bundle/config/mutator"
|
2024-01-25 11:32:47 +00:00
|
|
|
"github.com/databricks/cli/bundle/deploy/terraform"
|
|
|
|
"github.com/databricks/cli/bundle/phases"
|
2024-10-18 06:45:47 +00:00
|
|
|
"github.com/databricks/cli/bundle/render"
|
2024-02-14 18:04:45 +00:00
|
|
|
"github.com/databricks/cli/cmd/bundle/utils"
|
2024-01-25 11:32:47 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
|
|
|
"github.com/databricks/cli/libs/flags"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newSummaryCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2024-03-28 10:32:34 +00:00
|
|
|
Use: "summary",
|
2024-10-18 06:45:47 +00:00
|
|
|
Short: "Summarize resources deployed by this bundle",
|
2024-03-28 10:32:34 +00:00
|
|
|
Args: root.NoArgs,
|
2024-01-25 11:32:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-03-28 10:32:34 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
b, diags := utils.ConfigureBundleWithVariables(cmd)
|
|
|
|
if err := diags.Error(); err != nil {
|
|
|
|
return diags.Error()
|
|
|
|
}
|
2024-01-25 11:32:47 +00:00
|
|
|
|
2024-03-28 10:32:34 +00:00
|
|
|
diags = bundle.Apply(ctx, b, phases.Initialize())
|
2024-03-25 14:18:47 +00:00
|
|
|
if err := diags.Error(); err != nil {
|
2024-01-25 11:32:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:32:34 +00:00
|
|
|
cacheDir, err := terraform.Dir(ctx, b)
|
2024-01-25 11:32:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-01 08:25:12 +00:00
|
|
|
_, stateFileErr := os.Stat(filepath.Join(cacheDir, terraform.TerraformStateFileName))
|
|
|
|
_, configFileErr := os.Stat(filepath.Join(cacheDir, terraform.TerraformConfigFileName))
|
|
|
|
noCache := errors.Is(stateFileErr, os.ErrNotExist) || errors.Is(configFileErr, os.ErrNotExist)
|
2024-01-25 11:32:47 +00:00
|
|
|
|
|
|
|
if forcePull || noCache {
|
2024-03-28 10:32:34 +00:00
|
|
|
diags = bundle.Apply(ctx, b, bundle.Seq(
|
2024-03-01 08:25:12 +00:00
|
|
|
terraform.StatePull(),
|
|
|
|
terraform.Interpolate(),
|
|
|
|
terraform.Write(),
|
|
|
|
))
|
2024-03-25 14:18:47 +00:00
|
|
|
if err := diags.Error(); err != nil {
|
2024-01-25 11:32:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-18 06:45:47 +00:00
|
|
|
diags = bundle.Apply(ctx, b,
|
|
|
|
bundle.Seq(terraform.Load(), mutator.InitializeURLs()))
|
2024-03-25 14:18:47 +00:00
|
|
|
if err := diags.Error(); err != nil {
|
2024-01-25 11:32:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch root.OutputType(cmd) {
|
|
|
|
case flags.OutputText:
|
2024-10-18 06:45:47 +00:00
|
|
|
return render.RenderSummary(ctx, cmd.OutOrStdout(), b)
|
2024-01-25 11:32:47 +00:00
|
|
|
case flags.OutputJSON:
|
|
|
|
buf, err := json.MarshalIndent(b.Config, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.OutOrStdout().Write(buf)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown output type %s", root.OutputType(cmd))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|