|
1 | 1 | package bundle |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
4 | 8 | "github.com/databricks/cli/bundle" |
5 | 9 | "github.com/databricks/cli/bundle/render" |
6 | 10 | "github.com/databricks/cli/cmd/bundle/utils" |
7 | 11 | "github.com/databricks/cli/cmd/root" |
8 | 12 | "github.com/databricks/cli/libs/flags" |
9 | 13 | "github.com/databricks/cli/libs/logdiag" |
| 14 | + "github.com/databricks/cli/libs/tmpdms" |
10 | 15 | "github.com/spf13/cobra" |
11 | 16 | ) |
12 | 17 |
|
@@ -51,11 +56,60 @@ Useful after deployment to see what was created and where to find it.`, |
51 | 56 | } |
52 | 57 |
|
53 | 58 | func showSummary(cmd *cobra.Command, b *bundle.Bundle) error { |
54 | | - if root.OutputType(cmd) == flags.OutputText { |
55 | | - return render.RenderSummary(cmd.Context(), cmd.OutOrStdout(), b) |
| 59 | + ctx := cmd.Context() |
| 60 | + |
| 61 | + // When the deployment metadata service is in use, surface the bundle's |
| 62 | + // deployment_id and current version_id so callers (e.g. the workspace UI) |
| 63 | + // can link to the deployment metadata. Both are empty otherwise. |
| 64 | + versionID, err := currentDeploymentVersion(ctx, b) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + switch root.OutputType(cmd) { |
| 70 | + case flags.OutputText: |
| 71 | + return render.RenderSummary(ctx, cmd.OutOrStdout(), b, b.DeploymentID, versionID) |
| 72 | + case flags.OutputJSON: |
| 73 | + return renderSummaryJSON(cmd, b, versionID) |
| 74 | + } |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// currentDeploymentVersion returns the latest version_id recorded for the |
| 79 | +// bundle's deployment, or "" when the deployment metadata service is not in use. |
| 80 | +func currentDeploymentVersion(ctx context.Context, b *bundle.Bundle) (string, error) { |
| 81 | + if b.DeploymentID == "" { |
| 82 | + return "", nil |
| 83 | + } |
| 84 | + svc, err := tmpdms.NewDeploymentMetadataAPI(b.WorkspaceClient(ctx)) |
| 85 | + if err != nil { |
| 86 | + return "", fmt.Errorf("failed to create metadata service client: %w", err) |
| 87 | + } |
| 88 | + dep, err := svc.GetDeployment(ctx, tmpdms.GetDeploymentRequest{DeploymentID: b.DeploymentID}) |
| 89 | + if err != nil { |
| 90 | + return "", fmt.Errorf("failed to get deployment: %w", err) |
| 91 | + } |
| 92 | + return dep.LastVersionID, nil |
| 93 | +} |
| 94 | + |
| 95 | +// renderSummaryJSON marshals the bundle configuration and, when the deployment |
| 96 | +// metadata service is in use, adds a top-level "deployment" object carrying the |
| 97 | +// deployment_id and version_id. |
| 98 | +func renderSummaryJSON(cmd *cobra.Command, b *bundle.Bundle, versionID string) error { |
| 99 | + value := b.Config.Value().AsAny() |
| 100 | + if b.DeploymentID != "" { |
| 101 | + // The config root is always a mapping for a loaded bundle. |
| 102 | + value.(map[string]any)["deployment"] = map[string]string{ |
| 103 | + "deployment_id": b.DeploymentID, |
| 104 | + "version_id": versionID, |
| 105 | + } |
56 | 106 | } |
57 | | - if root.OutputType(cmd) == flags.OutputJSON { |
58 | | - return renderJsonOutput(cmd, b) |
| 107 | + buf, err := json.MarshalIndent(value, "", " ") |
| 108 | + if err != nil { |
| 109 | + return err |
59 | 110 | } |
| 111 | + out := cmd.OutOrStdout() |
| 112 | + _, _ = out.Write(buf) |
| 113 | + _, _ = out.Write([]byte{'\n'}) |
60 | 114 | return nil |
61 | 115 | } |
0 commit comments