2024-08-24 18:48:36 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-17 15:30:48 +00:00
|
|
|
"net/url"
|
2024-08-24 18:48:36 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/libs/diag"
|
|
|
|
)
|
|
|
|
|
2024-10-05 03:43:00 +00:00
|
|
|
type initializeURLs struct {
|
2024-08-24 18:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitializeURLs makes sure the URL field of each resource is configured.
|
|
|
|
// NOTE: since this depends on an extra API call, this mutator adds some extra
|
|
|
|
// latency. As such, it should only be used when needed.
|
|
|
|
// This URL field is used for the output of the 'bundle summary' CLI command.
|
|
|
|
func InitializeURLs() bundle.Mutator {
|
2024-10-05 03:43:00 +00:00
|
|
|
return &initializeURLs{}
|
2024-08-24 18:48:36 +00:00
|
|
|
}
|
|
|
|
|
2024-10-05 03:43:00 +00:00
|
|
|
func (m *initializeURLs) Name() string {
|
2024-10-17 13:41:24 +00:00
|
|
|
return "InitializeURLs"
|
2024-08-24 18:48:36 +00:00
|
|
|
}
|
|
|
|
|
2024-10-05 03:43:00 +00:00
|
|
|
func (m *initializeURLs) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2024-08-24 18:48:36 +00:00
|
|
|
workspaceId, err := b.WorkspaceClient().CurrentWorkspaceID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return diag.FromErr(err)
|
|
|
|
}
|
2024-10-17 08:22:16 +00:00
|
|
|
orgId := strconv.FormatInt(workspaceId, 10)
|
2024-10-17 15:30:48 +00:00
|
|
|
host := b.WorkspaceClient().Config.CanonicalHostName()
|
|
|
|
initializeForWorkspace(b, orgId, host)
|
2024-08-24 18:48:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-17 15:30:48 +00:00
|
|
|
func initializeForWorkspace(b *bundle.Bundle, orgId string, host string) error {
|
|
|
|
baseURL, err := url.Parse(host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-24 18:48:36 +00:00
|
|
|
// Add ?o=<workspace id> only if <workspace id> wasn't in the subdomain already.
|
|
|
|
// The ?o= is needed when vanity URLs / legacy workspace URLs are used.
|
|
|
|
// If it's not needed we prefer to leave it out since these URLs are rather
|
|
|
|
// long for most terminals.
|
|
|
|
//
|
|
|
|
// See https://docs.databricks.com/en/workspace/workspace-details.html for
|
|
|
|
// further reading about the '?o=' suffix.
|
2024-10-17 15:30:48 +00:00
|
|
|
if !strings.Contains(baseURL.Hostname(), orgId) {
|
|
|
|
values := baseURL.Query()
|
|
|
|
values.Add("o", orgId)
|
|
|
|
baseURL.RawQuery = values.Encode()
|
2024-08-24 18:48:36 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 14:39:17 +00:00
|
|
|
for _, group := range b.Config.Resources.AllResources() {
|
|
|
|
for _, r := range group.Resources {
|
2024-10-17 15:30:48 +00:00
|
|
|
r.InitializeURL(*baseURL)
|
2024-08-24 18:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-17 15:30:48 +00:00
|
|
|
|
|
|
|
return nil
|
2024-08-24 18:48:36 +00:00
|
|
|
}
|