Skip to content

Commit cb07ae7

Browse files
committed
Share workspace URL helpers between experimental and bundle open
Move shared workspace UI routes and base URL synthesis into libs/workspaceurls so experimental open and bundle resource URLs stay aligned, including vanity hosts that still require the ?o workspace suffix.
1 parent 17af4a8 commit cb07ae7

File tree

15 files changed

+192
-101
lines changed

15 files changed

+192
-101
lines changed

bundle/config/mutator/initialize_urls.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package mutator
22

33
import (
44
"context"
5-
"net/url"
6-
"strconv"
7-
"strings"
85

96
"github.com/databricks/cli/bundle"
107
"github.com/databricks/cli/libs/diag"
8+
"github.com/databricks/cli/libs/workspaceurls"
119
)
1210

1311
type initializeURLs struct{}
@@ -25,38 +23,24 @@ func (m *initializeURLs) Name() string {
2523
}
2624

2725
func (m *initializeURLs) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
28-
workspaceId, err := b.WorkspaceClient().CurrentWorkspaceID(ctx)
26+
workspaceID, err := b.WorkspaceClient().CurrentWorkspaceID(ctx)
2927
if err != nil {
3028
return diag.FromErr(err)
3129
}
32-
orgId := strconv.FormatInt(workspaceId, 10)
3330
host := b.WorkspaceClient().Config.CanonicalHostName()
34-
err = initializeForWorkspace(b, orgId, host)
31+
err = initializeForWorkspace(b, workspaceID, host)
3532
if err != nil {
3633
return diag.FromErr(err)
3734
}
3835
return nil
3936
}
4037

41-
func initializeForWorkspace(b *bundle.Bundle, orgId, host string) error {
42-
baseURL, err := url.Parse(host)
38+
func initializeForWorkspace(b *bundle.Bundle, workspaceID int64, host string) error {
39+
baseURL, err := workspaceurls.WorkspaceBaseURL(host, workspaceID)
4340
if err != nil {
4441
return err
4542
}
4643

47-
// Add ?o=<workspace id> only if <workspace id> wasn't in the subdomain already.
48-
// The ?o= is needed when vanity URLs / legacy workspace URLs are used.
49-
// If it's not needed we prefer to leave it out since these URLs are rather
50-
// long for most terminals.
51-
//
52-
// See https://docs.databricks.com/en/workspace/workspace-details.html for
53-
// further reading about the '?o=' suffix.
54-
if !strings.Contains(baseURL.Hostname(), orgId) {
55-
values := baseURL.Query()
56-
values.Add("o", orgId)
57-
baseURL.RawQuery = values.Encode()
58-
}
59-
6044
for _, group := range b.Config.Resources.AllResources() {
6145
for _, r := range group.Resources {
6246
r.InitializeURL(*baseURL)

bundle/config/mutator/initialize_urls_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestInitializeURLs(t *testing.T) {
109109
"dashboard1": "https://mycompany.databricks.com/dashboardsv3/01ef8d56871e1d50ae30ce7375e42478/published?o=123456",
110110
}
111111

112-
err := initializeForWorkspace(b, "123456", "https://mycompany.databricks.com/")
112+
err := initializeForWorkspace(b, 123456, "https://mycompany.databricks.com/")
113113
require.NoError(t, err)
114114

115115
for _, group := range b.Config.Resources.AllResources() {
@@ -133,8 +133,28 @@ func TestInitializeURLsWithoutOrgId(t *testing.T) {
133133
},
134134
}
135135

136-
err := initializeForWorkspace(b, "123456", "https://adb-123456.azuredatabricks.net/")
136+
err := initializeForWorkspace(b, 123456, "https://adb-123456.azuredatabricks.net/")
137137
require.NoError(t, err)
138138

139139
require.Equal(t, "https://adb-123456.azuredatabricks.net/jobs/1", b.Config.Resources.Jobs["job1"].URL)
140140
}
141+
142+
func TestInitializeURLsWithWorkspaceIDInVanityHostname(t *testing.T) {
143+
b := &bundle.Bundle{
144+
Config: config.Root{
145+
Resources: config.Resources{
146+
Jobs: map[string]*resources.Job{
147+
"job1": {
148+
BaseResource: resources.BaseResource{ID: "1"},
149+
JobSettings: jobs.JobSettings{Name: "job1"},
150+
},
151+
},
152+
},
153+
},
154+
}
155+
156+
err := initializeForWorkspace(b, 123456, "https://workspace-123456.example.com/")
157+
require.NoError(t, err)
158+
159+
require.Equal(t, "https://workspace-123456.example.com/jobs/1?o=123456", b.Config.Resources.Jobs["job1"].URL)
160+
}

bundle/config/resources/alerts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66

77
"github.com/databricks/cli/libs/log"
8+
"github.com/databricks/cli/libs/workspaceurls"
89
"github.com/databricks/databricks-sdk-go"
910
"github.com/databricks/databricks-sdk-go/marshal"
1011
"github.com/databricks/databricks-sdk-go/service/sql"
@@ -52,8 +53,7 @@ func (a *Alert) InitializeURL(baseURL url.URL) {
5253
if a.ID == "" {
5354
return
5455
}
55-
baseURL.Path = "sql/alerts-v2/" + a.ID
56-
a.URL = baseURL.String()
56+
a.URL = workspaceurls.ResourceURL(baseURL, workspaceurls.AlertPattern, a.ID)
5757
}
5858

5959
func (a *Alert) GetName() string {

bundle/config/resources/apps.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66

77
"github.com/databricks/cli/libs/log"
8+
"github.com/databricks/cli/libs/workspaceurls"
89
"github.com/databricks/databricks-sdk-go"
910
"github.com/databricks/databricks-sdk-go/marshal"
1011
"github.com/databricks/databricks-sdk-go/service/apps"
@@ -83,8 +84,7 @@ func (a *App) InitializeURL(baseURL url.URL) {
8384
if a.ModifiedStatus == "" || a.ModifiedStatus == ModifiedStatusCreated {
8485
return
8586
}
86-
baseURL.Path = "apps/" + a.GetName()
87-
a.URL = baseURL.String()
87+
a.URL = workspaceurls.ResourceURL(baseURL, workspaceurls.AppPattern, a.GetName())
8888
}
8989

9090
func (a *App) GetName() string {

bundle/config/resources/clusters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66

77
"github.com/databricks/cli/libs/log"
8+
"github.com/databricks/cli/libs/workspaceurls"
89
"github.com/databricks/databricks-sdk-go"
910
"github.com/databricks/databricks-sdk-go/marshal"
1011
"github.com/databricks/databricks-sdk-go/service/compute"
@@ -47,8 +48,7 @@ func (s *Cluster) InitializeURL(baseURL url.URL) {
4748
if s.ID == "" {
4849
return
4950
}
50-
baseURL.Path = "compute/clusters/" + s.ID
51-
s.URL = baseURL.String()
51+
s.URL = workspaceurls.ResourceURL(baseURL, workspaceurls.ClusterPattern, s.ID)
5252
}
5353

5454
func (s *Cluster) GetName() string {

bundle/config/resources/dashboard.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package resources
22

33
import (
44
"context"
5-
"fmt"
65
"net/url"
76

87
"github.com/databricks/cli/libs/log"
8+
"github.com/databricks/cli/libs/workspaceurls"
99
"github.com/databricks/databricks-sdk-go"
1010
"github.com/databricks/databricks-sdk-go/marshal"
1111
"github.com/databricks/databricks-sdk-go/service/dashboards"
@@ -114,8 +114,7 @@ func (r *Dashboard) InitializeURL(baseURL url.URL) {
114114
return
115115
}
116116

117-
baseURL.Path = fmt.Sprintf("dashboardsv3/%s/published", r.ID)
118-
r.URL = baseURL.String()
117+
r.URL = workspaceurls.ResourceURL(baseURL, workspaceurls.DashboardPattern, r.ID)
119118
}
120119

121120
func (r *Dashboard) GetName() string {

bundle/config/resources/job.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77

88
"github.com/databricks/cli/libs/log"
9+
"github.com/databricks/cli/libs/workspaceurls"
910
"github.com/databricks/databricks-sdk-go"
1011
"github.com/databricks/databricks-sdk-go/marshal"
1112
"github.com/databricks/databricks-sdk-go/service/jobs"
@@ -54,8 +55,7 @@ func (j *Job) InitializeURL(baseURL url.URL) {
5455
if j.ID == "" {
5556
return
5657
}
57-
baseURL.Path = "jobs/" + j.ID
58-
j.URL = baseURL.String()
58+
j.URL = workspaceurls.ResourceURL(baseURL, workspaceurls.JobPattern, j.ID)
5959
}
6060

6161
func (j *Job) GetName() string {

bundle/config/resources/mlflow_experiment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66

77
"github.com/databricks/cli/libs/log"
8+
"github.com/databricks/cli/libs/workspaceurls"
89
"github.com/databricks/databricks-sdk-go"
910
"github.com/databricks/databricks-sdk-go/marshal"
1011
"github.com/databricks/databricks-sdk-go/service/ml"
@@ -49,8 +50,7 @@ func (s *MlflowExperiment) InitializeURL(baseURL url.URL) {
4950
if s.ID == "" {
5051
return
5152
}
52-
baseURL.Path = "ml/experiments/" + s.ID
53-
s.URL = baseURL.String()
53+
s.URL = workspaceurls.ResourceURL(baseURL, workspaceurls.ExperimentPattern, s.ID)
5454
}
5555

5656
func (s *MlflowExperiment) GetName() string {

bundle/config/resources/mlflow_model.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66

77
"github.com/databricks/cli/libs/log"
8+
"github.com/databricks/cli/libs/workspaceurls"
89
"github.com/databricks/databricks-sdk-go"
910
"github.com/databricks/databricks-sdk-go/marshal"
1011
"github.com/databricks/databricks-sdk-go/service/ml"
@@ -49,8 +50,7 @@ func (s *MlflowModel) InitializeURL(baseURL url.URL) {
4950
if s.ID == "" {
5051
return
5152
}
52-
baseURL.Path = "ml/models/" + s.ID
53-
s.URL = baseURL.String()
53+
s.URL = workspaceurls.ResourceURL(baseURL, workspaceurls.ModelPattern, s.ID)
5454
}
5555

5656
func (s *MlflowModel) GetName() string {

bundle/config/resources/model_serving_endpoint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66

77
"github.com/databricks/cli/libs/log"
8+
"github.com/databricks/cli/libs/workspaceurls"
89
"github.com/databricks/databricks-sdk-go"
910
"github.com/databricks/databricks-sdk-go/marshal"
1011
"github.com/databricks/databricks-sdk-go/service/serving"
@@ -54,8 +55,7 @@ func (s *ModelServingEndpoint) InitializeURL(baseURL url.URL) {
5455
if s.ID == "" {
5556
return
5657
}
57-
baseURL.Path = "ml/endpoints/" + s.ID
58-
s.URL = baseURL.String()
58+
s.URL = workspaceurls.ResourceURL(baseURL, workspaceurls.ModelServingEndpointPattern, s.ID)
5959
}
6060

6161
func (s *ModelServingEndpoint) GetName() string {

0 commit comments

Comments
 (0)