databricks-cli/bundle/config/mutator/initialize_urls.go

63 lines
1.7 KiB
Go

package mutator
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/diag"
)
type initializeUrls struct {
name string
}
// 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 {
return &initializeUrls{}
}
func (m *initializeUrls) Name() string {
return fmt.Sprintf("ConfigureURLs(%s)", m.name)
}
func (m *initializeUrls) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
workspaceId, err := b.WorkspaceClient().CurrentWorkspaceID(ctx)
orgId := strconv.FormatInt(workspaceId, 10)
if err != nil {
return diag.FromErr(err)
}
configureForOrgId(b, orgId)
return nil
}
func configureForOrgId(b *bundle.Bundle, orgId string) {
urlPrefix := b.Config.Workspace.Host
if !strings.HasSuffix(urlPrefix, "/") {
urlPrefix += "/"
}
urlSuffix := ""
// 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.
if !strings.Contains(urlPrefix, orgId) {
urlSuffix = "?o=" + orgId
}
for _, rs := range b.Config.Resources.AllResources() {
for _, r := range rs {
r.InitializeURL(urlPrefix, urlSuffix)
}
}
}