Address reviewer comments

This commit is contained in:
Lennart Kats 2024-10-04 20:43:00 -07:00
parent 1325fd867a
commit 189d40b1fa
No known key found for this signature in database
GPG Key ID: 1EB8B57673197023
2 changed files with 11 additions and 19 deletions

View File

@ -10,7 +10,7 @@ import (
"github.com/databricks/cli/libs/diag"
)
type initializeUrls struct {
type initializeURLs struct {
name string
}
@ -19,30 +19,25 @@ type initializeUrls struct {
// 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{}
return &initializeURLs{}
}
func (m *initializeUrls) Name() string {
return fmt.Sprintf("ConfigureURLs(%s)", m.name)
func (m *initializeURLs) Name() string {
return fmt.Sprintf("InitializeURLs(%s)", m.name)
}
func (m *initializeUrls) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
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)
urlPrefix := b.WorkspaceClient().Config.CanonicalHostName() + "/"
initializeForWorkspace(b, orgId, urlPrefix)
return nil
}
func configureForOrgId(b *bundle.Bundle, orgId string) {
urlPrefix := b.Config.Workspace.Host
if !strings.HasSuffix(urlPrefix, "/") {
urlPrefix += "/"
}
urlSuffix := ""
func initializeForWorkspace(b *bundle.Bundle, orgId string, urlPrefix string) {
// 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
@ -50,6 +45,7 @@ func configureForOrgId(b *bundle.Bundle, orgId string) {
//
// See https://docs.databricks.com/en/workspace/workspace-details.html for
// further reading about the '?o=' suffix.
urlSuffix := ""
if !strings.Contains(urlPrefix, orgId) {
urlSuffix = "?o=" + orgId
}

View File

@ -91,7 +91,7 @@ func TestInitializeURLs(t *testing.T) {
"schema1": "https://mycompany.databricks.com/explore/data/catalog/schema?o=123456",
}
configureForOrgId(b, "123456")
initializeForWorkspace(b, "123456", "https://mycompany.databricks.com/")
for _, rs := range b.Config.Resources.AllResources() {
for key, r := range rs {
@ -103,10 +103,6 @@ func TestInitializeURLs(t *testing.T) {
func URLs_NoOrgId(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Workspace: config.Workspace{
// Hostname with org id in URL and no trailing /
Host: "https://adb-123456.azuredatabricks.net",
},
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"job1": {
@ -118,7 +114,7 @@ func URLs_NoOrgId(t *testing.T) {
},
}
configureForOrgId(b, "123456")
initializeForWorkspace(b, "123456", "https://adb-123456.azuredatabricks.net/")
require.Equal(t, "https://adb-123456.azuredatabricks.net/jobs/1", b.Config.Resources.Jobs["job1"].URL)
}