-
Notifications
You must be signed in to change notification settings - Fork 66
Support creating a new workspace client from an account client #792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fcb231e
Support creating a new workspace client from an account client
mgyucht 9c97c58
add tests
mgyucht 9238834
reflectively copy exported fields
mgyucht 9fa8391
add test
mgyucht 2782548
move example to internal
mgyucht 7bd1f47
test
mgyucht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package databricks | ||
|
|
||
| import "github.com/databricks/databricks-sdk-go/service/provisioning" | ||
|
|
||
| // GetWorkspaceClient returns a WorkspaceClient for the given workspace. The | ||
| // workspace can be fetched by calling w.Workspaces.Get() or w.Workspaces.List(). | ||
| // | ||
| // The config used for the workspace is identical to that used for the account, | ||
| // except that the host is set to the workspace host, and the account ID is | ||
| // not set. | ||
| // | ||
| // Example: | ||
| // | ||
| // a, err := databricks.NewAccountClient() | ||
| // if err != nil { | ||
| // panic(err) | ||
| // } | ||
| // ctx := context.Background() | ||
| // workspaces, err := a.Workspaces.List(ctx) | ||
| // if err != nil { | ||
| // panic(err) | ||
| // } | ||
| // w, err := a.GetWorkspaceClient(workspaces[0]) | ||
| // if err != nil { | ||
| // panic(err) | ||
| // } | ||
| // me, err := w.CurrentUser.Me(ctx) | ||
| func (c *AccountClient) GetWorkspaceClient(w provisioning.Workspace) (*WorkspaceClient, error) { | ||
| host := c.Config.Environment().DeploymentURL(w.DeploymentName) | ||
| cfg, err := c.Config.NewWithWorkspaceHost(host) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| cfg.AzureResourceID = w.AzureResourceId() | ||
| return NewWorkspaceClient((*Config)(cfg)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestMwsAccAccountClient_GetWorkspaceClient_NoTranspile(t *testing.T) { | ||
| ctx, a := accountTest(t) | ||
| wss, err := a.Workspaces.List(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| if len(wss) == 0 { | ||
| t.Skip("No workspaces found") | ||
| } | ||
| w, err := a.GetWorkspaceClient(wss[0]) | ||
| assert.NoError(t, err) | ||
| me, err := w.CurrentUser.Me(ctx) | ||
| assert.NoError(t, err) | ||
| assert.True(t, me.Active) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package provisioning | ||
|
|
||
| import "fmt" | ||
|
|
||
| const resourceIdFormat = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Databricks/workspaces/%s" | ||
|
|
||
| // Return the AzureResourceID for the workspace, if it is an Azure workspace. | ||
| func (w Workspace) AzureResourceId() string { | ||
| if w.AzureWorkspaceInfo == nil { | ||
| return "" | ||
| } | ||
| return fmt.Sprintf(resourceIdFormat, w.AzureWorkspaceInfo.SubscriptionId, w.AzureWorkspaceInfo.ResourceGroup, w.WorkspaceName) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package provisioning | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestAzureResourceId_AzureWorkspace(t *testing.T) { | ||
| w := Workspace{ | ||
| WorkspaceName: "test", | ||
| AzureWorkspaceInfo: &AzureWorkspaceInfo{ | ||
| SubscriptionId: "sub", | ||
| ResourceGroup: "rg", | ||
| }, | ||
| } | ||
| assert.Equal(t, "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/test", w.AzureResourceId()) | ||
| } | ||
|
|
||
| func TestAzureResourceId_NonAzureWorkspace(t *testing.T) { | ||
| w := Workspace{ | ||
| WorkspaceName: "test", | ||
| } | ||
| assert.Equal(t, "", w.AzureResourceId()) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you comment when we have a
DeploymentNameand when we have to use theAzureResourceID?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the deployment name is always present when fetching a workspace, we don't actually have any hard dependency on the AzureResourceID field being set for the core functionality of the Config. In fact, because we are copying the
authfield, we don't have to preserve any of the fields that are used for configuration. I've simply implemented this because we can compute the AzureResourceId from the Workspace.