Skip to content

Use the Go SDK for databricks_mws_workspaces #4633

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 18 commits into from
Apr 14, 2025

Conversation

mgyucht
Copy link
Contributor

@mgyucht mgyucht commented Apr 10, 2025

Changes

Currently, many resources in the mws package are implemented manually in the Terraform provider. Refactoring these to use the Go SDK reduces the amount of code that needs to be maintained in the TF provider and also ensures that new changes are released automatically. This PR should be nearly identical in terms of behavior, but there are some small variations because of subtle limitations in the Go SDK. For example, we may need to fetch the workspace an additional time using the Go SDK because WaitGetWorkspaceRunning does not return the fetched workspace in a terminal failure state.

Refactoring and Improvements:

  • Refactored databricks_mws_workspaces resource and datasource, as well as databricks_mws_ncc_binding, to use the Go SDK.

Caching Mechanisms:

  • Introduced caching for workspace clients in DatabricksClient to improve performance and reduce redundant API calls. Added cachedWorkspaceClient, cachedWorkspaceClients, and cachedAccountClient fields along with associated methods. [1] [2] [3]

Test Case Updates:

  • Updated TestDataSourceMwsWorkspaces to use the new Go SDK mocks for testing, replacing HTTP fixtures with mock client responses. [1] [2] [3]
  • Refactored TestMwsAccWorkspaces_TokenUpdate to include additional checks and new token update scenarios.
  • Modified TestResourceNccBindingCreate and TestResourceNccBindingUpdate to use mock clients for workspace updates. [1] [2]

Test Framework Enhancements:

  • Added support for multiple mock workspace clients in the ResourceFixture struct and validation logic. [1] [2] [3]

Tests

Tests run locally. Verifying with integration tests.

TODO

  • Onboard client.go changes to code generation.
  • Add back unit tests for token changes

@mgyucht mgyucht requested review from a team as code owners April 10, 2025 16:50
@mgyucht mgyucht requested review from tanmay-db and removed request for a team April 10, 2025 16:50
@mgyucht mgyucht temporarily deployed to test-trigger-is April 10, 2025 16:50 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 10, 2025 16:52 — with GitHub Actions Inactive
@alexott
Copy link
Contributor

alexott commented Apr 10, 2025

common/client.go is marked as // Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT. - so it will be overwritten?

@mgyucht mgyucht temporarily deployed to test-trigger-is April 10, 2025 19:33 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 10, 2025 19:34 — with GitHub Actions Inactive
@mgyucht
Copy link
Contributor Author

mgyucht commented Apr 10, 2025

@alexott good observation, that will have to move to universe.

@mgyucht mgyucht temporarily deployed to test-trigger-is April 10, 2025 19:36 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 10, 2025 20:18 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 10, 2025 20:20 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 11, 2025 05:37 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 11, 2025 05:38 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 11, 2025 07:56 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 11, 2025 07:57 — with GitHub Actions Inactive
@mgyucht mgyucht changed the title [WIP] Use the Go SDK for databricks_mws_workspaces Use the Go SDK for databricks_mws_workspaces Apr 11, 2025
@mgyucht mgyucht temporarily deployed to test-trigger-is April 11, 2025 09:15 — with GitHub Actions Inactive
@alexott alexott requested a review from Copilot April 11, 2025 10:54
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

@mgyucht mgyucht temporarily deployed to test-trigger-is April 11, 2025 12:00 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 11, 2025 12:02 — with GitHub Actions Inactive
Copy link
Contributor

@alexott alexott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. just few comments

// wait for DNS caches to refresh, as sometimes we cannot make
// API calls to new workspaces immediately after it's created
func verifyWorkspaceReachable(ctx context.Context, w *databricks.WorkspaceClient) error {
return retries.New[struct{}](retries.WithRetryFunc(func(err error) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we need to close this old PR? #2957

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, nice catch!

}
}
s["account_id"].Sensitive = true
common.CustomizeSchemaPath(s, "account_id").SetRequired()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we get it from account-level provider?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just preserving the existing behavior & limiting the number of changes in this PR, but I think this can/should be removed in a follow-up PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created #4638 to track it

cachedWorkspaceClient *databricks.WorkspaceClient
cachedAccountClient *databricks.AccountClient
mu sync.Mutex
// cachedWorkspaceClients is a map of workspace clients for each workspace ID
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to have comments

}
log.Printf("[ERROR] Deleting failed workspace: %s", err)
if derr := a.Workspaces.Delete(ctx, provisioning.DeleteWorkspaceRequest{WorkspaceId: wait.Response.WorkspaceId}); derr != nil {
return fmt.Errorf("workspace creation failed: %w; failed workspace cleanup failed: %w", err, derr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failed workspace cleanup failed

should this be workspace cleanup failed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the existing message, but I think the point here is that the workspace creation failed, resulting in a failed workspace, and the cleanup of that failed workspace also failed.

}
return json.Marshal(workspaceCreationRequest)
provisioning.Workspace
CustomerManagedKeyID string `json:"customer_managed_key_id,omitempty"` // just for compatibility, will be removed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding removal, should we mark it deprecated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is marked as deprecated, see line 201.

}
return false
})).Wait(ctx, func(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to abstract out this timeout on top so it easy to read where the timeouts are defined when navigating code.

@alexott
Copy link
Contributor

alexott commented Apr 11, 2025

It also would be useful to update documentation - new fields and computed attributes

@@ -521,23 +200,15 @@ func ResourceMwsWorkspaces() common.Resource {
s["is_no_public_ip_enabled"].DiffSuppressFunc = func(k, old, new string, d *schema.ResourceData) bool {
return old != ""
}
s["is_no_public_ip_enabled"].Default = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should handle a case of is_no_public_ip_enabled = false. It's marked as omitempty, so it won't be set in the payload

@mgyucht mgyucht temporarily deployed to test-trigger-is April 14, 2025 09:55 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 14, 2025 09:56 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 14, 2025 11:58 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 14, 2025 11:58 — with GitHub Actions Inactive
@mgyucht mgyucht temporarily deployed to test-trigger-is April 14, 2025 12:34 — with GitHub Actions Inactive
Copy link

If integration tests don't run automatically, an authorized user can run them manually by following the instructions below:

Trigger:
go/deco-tests-run/terraform

Inputs:

  • PR number: 4633
  • Commit SHA: e0d9a7444815c5753f78c04397af3daaf000aea9

Checks will be approved automatically on success.

@mgyucht mgyucht temporarily deployed to test-trigger-is April 14, 2025 12:36 — with GitHub Actions Inactive
@mgyucht mgyucht added this pull request to the merge queue Apr 14, 2025
Merged via the queue into main with commit f6cd4a5 Apr 14, 2025
12 checks passed
@mgyucht mgyucht deleted the fix/use-go-sdk-for-databricks_mws_workspaces branch April 14, 2025 13:23
deco-sdk-tagging bot added a commit that referenced this pull request Apr 15, 2025
## Release v1.73.0

### New Features and Improvements

 * Add a new settings resource `databricks_disable_legacy_dbfs_setting` ([#4605](#4605))
 * Support for `lifetime` attribute in `databricks_service_principal_secret` resource ([#4633](#4633)).
 * Mark `databricks_sql_dashaboard` as deprecated ([#4641](#4641))

### Bug Fixes

 * Increase the default HTTP timeout to 65 seconds.

### Documentation

 * Document `user_api_scopes` in `databricks_app` resource and data sources ([#4614](#4614))
 * Document new fields in `databricks_model_serving` resource ([#4615](#4615))

### Exporter

 * Add export of `databricks_mws_network_connectivity_config` and `databricks_mws_ncc_private_endpoint_rule` ([#4613](#4613))

### Internal Changes

* Refactor `databricks_mws_workspaces` resource and datasource, as well as `databricks_mws_ncc_binding`, to use the Go SDK ([#4633](#4633)).
* Add `TestMwsAccGcpWorkspaces` and `TestMwsAccGcpByovpcWorkspaces` to flaky test ([#4624](#4624)).
* Bump Go SDK to latest release ([#4645](#4645))
github-merge-queue bot pushed a commit that referenced this pull request Apr 16, 2025
…igration of resource to Go SDK (#4652)

## Changes
<!-- Summary of your changes that are easy to understand -->
Reverts change in
#4633
and set computed for workspace_status and vpc_status until they are
fixed internally.
## Tests
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant tests
-->
N/A
- [ ] `make test` run locally
- [ ] relevant change in `docs/` folder
- [ ] covered with integration tests in `internal/acceptance`
- [ ] using Go SDK
- [ ] using TF Plugin Framework
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants