Skip to content
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

Added support for Unity Catalog databricks_catalog_workspace_binding resource #2364

Merged
merged 19 commits into from
Jun 19, 2023

Conversation

ehab-eb
Copy link
Contributor

@ehab-eb ehab-eb commented Jun 1, 2023

Changes

Closes #2312

Tests

  • make test run locally
  • relevant change in docs/ folder
  • covered with integration tests in internal/acceptance
  • relevant acceptance tests are passing
  • using Go SDK

@ehab-eb ehab-eb changed the title Add ResourceCatalogWorkspaceBinding [WIP] Add ResourceCatalogWorkspaceBinding Jun 1, 2023
@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 1, 2023

@nkvuong It's far from done but I though I'd open a draft PR so you can see the direction I'm going in.

The main challenge so far is that the terraform schema doesn't match the API schema so I'm trying to figure out the best way to work around that.

Copy link
Contributor

@nkvuong nkvuong left a comment

Choose a reason for hiding this comment

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

Leave a couple of comments on how to proceed

catalog/resource_catalog_workspace_binding.go Outdated Show resolved Hide resolved
catalog/resource_catalog_workspace_binding.go Outdated Show resolved Hide resolved
catalog/resource_catalog_workspace_binding.go Outdated Show resolved Hide resolved
catalog/resource_catalog_workspace_binding.go Outdated Show resolved Hide resolved
catalog/resource_catalog_workspace_binding.go Outdated Show resolved Hide resolved
catalog/resource_catalog_workspace_binding.go Outdated Show resolved Hide resolved
catalog/resource_catalog_workspace_binding.go Outdated Show resolved Hide resolved
catalog/resource_catalog_workspace_binding_test.go Outdated Show resolved Hide resolved
catalog/resource_catalog_workspace_binding.go Outdated Show resolved Hide resolved
Copy link
Contributor

@nfx nfx left a comment

Choose a reason for hiding this comment

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

  1. use BindResource
  2. add acceptance test
  3. add documentation

catalog/resource_catalog_workspace_binding.go Outdated Show resolved Hide resolved
catalog/resource_catalog_workspace_binding.go Show resolved Hide resolved
@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 2, 2023

Thank you both for the feedback! I've addressed most of it but I'm running into issues here with the pair id.

The workspace id is defined as int64 and the Unpack function is expecting string. Since the unpacking infers the types from the schema I'm not entirely sure how to convert the type without affecting the terraform schema data types. Any advice?

@nfx
Copy link
Contributor

nfx commented Jun 5, 2023

@ehab-eb use BindResource and convert between int64/string within the methods.

@nkvuong nkvuong force-pushed the catalog-workspace-binding branch from 7282154 to a78168e Compare June 5, 2023 09:59
@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 5, 2023

@nfx @nkvuong I've updated the code to use BindResource and the tests are now passing! Feels like the tests aren't covering all bases though, so I'm happy to discuss what other cases to implement.

@nkvuong
Copy link
Contributor

nkvuong commented Jun 5, 2023

acceptance test is failing with cannot read catalog workspace binding: Catalog has no binding to this workspace. This is because the catalog isolation mode is not correctly set. That fix is in this PR #2357

@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 5, 2023

acceptance test is failing with cannot read catalog workspace binding: Catalog has no binding to this workspace. This is because the catalog isolation mode is not correctly set. That fix is in this PR #2357

Once your PR is merged should the isolation level be implicitly updated in this resource or should it rely on the catalog resource setting the correct isolation mode?

@ehab-eb ehab-eb mentioned this pull request Jun 5, 2023
5 tasks
@nkvuong
Copy link
Contributor

nkvuong commented Jun 5, 2023

should the isolation level be implicitly updated in this resource

this may cause drifts in the catalog resource, which is not ideal

should it rely on the catalog resource setting the correct isolation mode?

this is better, but then require the catalog to be created in Terraform, but I would prefer this approach. maybe worth checking that the catalog has the correct isolation mode during creation?

@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 5, 2023

this is better, but then require the catalog to be created in Terraform,

Or just that the user has toggled any of the workspaces attached to this catalog using the UI.

maybe worth checking that the catalog has the correct isolation mode during creation?

I was thinking the same thing but I'm wondering if it makes more sense to just let the API fail naturally?

Either way I'll add a note in the docs about the isolation mode.

@nkvuong nkvuong force-pushed the catalog-workspace-binding branch from 76a5b6d to e576830 Compare June 5, 2023 20:39
@nkvuong
Copy link
Contributor

nkvuong commented Jun 5, 2023

so we have a slight problem - creating catalog with isolation_mode = "ISOLATED" will result in GET API failing with the message cannot read catalog: Catalog 'dev{var.random}' is not accessible in current workspace and cannot read catalog: Catalog 'test{var.random}' is not accessible in current workspace

I'm going to check with our engineering team on how they expect it to work

@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 5, 2023

That's kind of strange since I'm not specifying an isolation mode for the prod catalog. My understanding is that it should default to OPEN and be accessible from all workspaces.

@nkvuong
Copy link
Contributor

nkvuong commented Jun 6, 2023

@ehab-eb sorry, copied the wrong error message, as I was trying to test locally as well.

I checked in the UI, and the API actually returns a 403 as well and the UI just hides the error. However, workspace binding APIs and patching the catalog still works, which means we need to handle this 403 somehow

@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 6, 2023

@nkvuong so you're saying that when a catalog is created in isolated mode and then we try to attach it to the current workspace the bindings API returns a 403 AND succeeds in making the binding?

@nkvuong
Copy link
Contributor

nkvuong commented Jun 6, 2023

@ehab-eb this is the behaviour I am seeing:

  • POST a catalog in isolated mode - 200
  • GET a catalog in isolated mode - 403 (unless workspace is bound) <--- this is the problem for Terraform, as it needs to refresh the state by calling GET
  • GET catalog workspace bindings - 200
  • PATCH catalog isolation mode - 200

@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 6, 2023

@nkvuong Ah okay that makes sense.

The options I see for this are either co-locating the the bindings and isolation mode calls by combining the resources or attaching the current workspace by default when a catalog is set to ISOLATED.

What do you think?

@nkvuong
Copy link
Contributor

nkvuong commented Jun 6, 2023

@ehab-eb we won't have an ideal solution either way - I prefer the 2nd option (and we add it to the doc). A 3rd option is to handle the 403 response in the ReadContext for catalog, but that would cause issues with drift detection.

@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 6, 2023

Yep I agree. Option 2 makes the most sense. Will make the changes

@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 6, 2023

@nkvuong I'm scratching my head a little bit trying to find a way to get the current workspace id. I can find the host url but not sure how to connect that to an actual id. Any tips on where to look?

I considered implicitly calling mws_workspaces data source but that's an account level auth and I imagine it needs a different set of permissions than creating a catalog.

@nkvuong
Copy link
Contributor

nkvuong commented Jun 6, 2023

within go sdk, there is this method that should return the workspace_id - https://github.com/databricks/databricks-sdk-go/blob/main/service/catalog/api.go#LL783C25-L783C32

@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 6, 2023

@nkvuong Thanks! That did it :)

@ehab-eb ehab-eb requested a review from nfx June 9, 2023 07:46
@ehab-eb
Copy link
Contributor Author

ehab-eb commented Jun 13, 2023

@ehab-eb re-running acceptance tests, but there is an issue with catalog.NewMetastores(c.DatabricksClient).Current(ctx) where the struct has the wrong type, leading to cannot unmarshal number into Go struct field MetastoreAssignment.workspace_id of type string

I'll ask the engineering team to fix this

@nkvuong I've made a change to how I identify the current metastore. I believe it's more or less the same call underneath but I'm not sure what the root cause was for the error you got so it might be worth running the acceptance tests again?

@ehab-eb ehab-eb changed the title [WIP] Add ResourceCatalogWorkspaceBinding Added support for Unity Catalog databricks_catalog_workspace_binding resource Jun 13, 2023
@ehab-eb ehab-eb marked this pull request as ready for review June 13, 2023 10:02
@nkvuong nkvuong force-pushed the catalog-workspace-binding branch from 56c3c9f to 9b2c8d1 Compare June 13, 2023 12:33
@nkvuong
Copy link
Contributor

nkvuong commented Jun 13, 2023

@ehab-eb acceptance tests are still failing with

 Error: cannot create catalog: json: cannot unmarshal number into Go struct field MetastoreAssignment.workspace_id of type string
    
      with databricks_catalog.dev,
      on terraform_plugin_test.tf line 1, in resource "databricks_catalog" "dev":
       1: resource "databricks_catalog" "dev" {
    
    
    Error: cannot create catalog: json: cannot unmarshal number into Go struct field MetastoreAssignment.workspace_id of type string
    
      with databricks_catalog.test,
      on terraform_plugin_test.tf line 5, in resource "databricks_catalog" "test":
       5: resource "databricks_catalog" "test" {

@@ -75,6 +76,26 @@ func ResourceCatalog() *schema.Resource {
return err
}

if d.Get("isolation_mode") != "ISOLATED" {
Copy link
Contributor

Choose a reason for hiding this comment

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

try to use stronger typed APIs

Suggested change
if d.Get("isolation_mode") != "ISOLATED" {
if ci.IsolationMode != "ISOLATED" {

@nfx nfx merged commit b16338a into databricks:master Jun 19, 2023
@thaiphv
Copy link
Contributor

thaiphv commented Jun 27, 2023

Thanks for implementing this resource. @nfx when do you plan to release this?

@Hong-Kit
Copy link

@nfx Thanks this is awesome work! Would like to know about the release schedule too 🙏

@mgyucht mgyucht mentioned this pull request Jun 29, 2023
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.

[FEATURE] Catalog-Workspace Bindings
6 participants