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

Add databricks_connection resource to support Lakehouse Federation #2528

Merged
merged 24 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5ef3414
first draft
nkvuong Jul 28, 2023
a007f4a
add foreign catalog
nkvuong Jul 28, 2023
207ec02
update doc
nkvuong Jul 28, 2023
bacb45c
Fixed `databricks_job` resource to clear instance-specific attributes…
marekbrysa Jul 29, 2023
9bb853f
Added `full_refresh` attribute to the `pipeline_task` in `databricks_…
alexott Jul 31, 2023
1087e85
Configured merge queue for the provider (#2533)
mgyucht Jul 31, 2023
60b98d1
misc doc updates (#2516)
nkvuong Jul 31, 2023
5ce7ad6
Bump github.com/databricks/databricks-sdk-go from 0.13.0 to 0.14.1 (#…
dependabot[bot] Jul 31, 2023
0599851
Fix IP ACL read (#2515)
nkvuong Jul 31, 2023
345184c
Add support for `USE_MARKETPLACE_ASSETS` privilege to metastore (#2505)
bvdboom Jul 31, 2023
52af856
Add git job_source to job resource (#2538)
shreyas-goenka Aug 1, 2023
33a88d7
Allow search SQL Warehouses by name in `databricks_sql_warehouse` dat…
alexott Aug 1, 2023
86a2e5c
Late jobs support (aka health conditions) in `databricks_job` resourc…
alexott Aug 1, 2023
9761051
feedback
nkvuong Aug 2, 2023
f91d380
Merge branch 'master' into feature/uc-connection
nkvuong Aug 2, 2023
270a4c9
Merge branch 'master' into feature/uc-connection
nkvuong Aug 14, 2023
87c4c27
update struct
nkvuong Aug 14, 2023
4989866
add suppress diff
nkvuong Aug 14, 2023
9f9de75
fix suppress diff
nkvuong Aug 14, 2023
22b9e8a
fix acceptance tests
nkvuong Aug 14, 2023
de26141
test feedback
nkvuong Aug 15, 2023
6e82b4e
make id a pair
nkvuong Aug 15, 2023
baa3441
better sensitive options handling
nkvuong Aug 16, 2023
74acee9
reorder id pair
nkvuong Aug 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions catalog/resource_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ func ucDirectoryPathSlashAndEmptySuppressDiff(k, old, new string, d *schema.Reso
}

type CatalogInfo struct {
Name string `json:"name"`
Comment string `json:"comment,omitempty"`
StorageRoot string `json:"storage_root,omitempty" tf:"force_new"`
ProviderName string `json:"provider_name,omitempty" tf:"force_new,conflicts:storage_root"`
ShareName string `json:"share_name,omitempty" tf:"force_new,conflicts:storage_root"`
Properties map[string]string `json:"properties,omitempty"`
Owner string `json:"owner,omitempty" tf:"computed"`
IsolationMode string `json:"isolation_mode,omitempty" tf:"computed"`
MetastoreID string `json:"metastore_id,omitempty" tf:"computed"`
Name string `json:"name"`
Comment string `json:"comment,omitempty"`
StorageRoot string `json:"storage_root,omitempty" tf:"force_new"`
ProviderName string `json:"provider_name,omitempty" tf:"force_new,conflicts:storage_root"`
ShareName string `json:"share_name,omitempty" tf:"force_new,conflicts:storage_root"`
ConnectionName string `json:"connection_name,omitempty" tf:"force_new,conflicts:storage_root"`
Properties map[string]string `json:"properties,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

CatalogInfo also has options - our open api spec needs updating.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@nkvuong Can you address this one as well? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@andrewli81 how would the options be split between connections & catalogs? and would there be sensitive options to be handled here as well?
once this is fixed in OpenAPI, we just need to regenerate the SDK, so should not take long

Copy link
Contributor

Choose a reason for hiding this comment

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

@nkvuong

@andrewli81 how would the options be split between connections & catalogs? and would there be sensitive options to be handled here as well?

The options of catalogs are mainly used to identify databases. This is because database systems like postgresql uses 3 layer namespaces (same as UC), so customer will need to specify which database to import into a UC catalog.

https://docs.databricks.com/en/query-federation/postgresql.html#language-SQL:~:text=Run%20the%20following%20SQL%20command%20in%20a%20notebook%20or%20Databricks%20SQL%20editor.%20Items%20in%20brackets%20are%20optional.%20Replace%20the%20placeholder%20values%3A

The open API spec has been fixed: https://docs.databricks.com/api/workspace/connections/create

Can we also fix it here?

Owner string `json:"owner,omitempty" tf:"computed"`
IsolationMode string `json:"isolation_mode,omitempty" tf:"computed"`
MetastoreID string `json:"metastore_id,omitempty" tf:"computed"`
}

func ResourceCatalog() *schema.Resource {
Expand Down
121 changes: 121 additions & 0 deletions catalog/resource_connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package catalog

import (
"context"

"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/exp/slices"
)

// This structure contains the fields of catalog.UpdateConnection and catalog.CreateConnection
Copy link
Contributor

Choose a reason for hiding this comment

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

We need better utilities for converting Go SDK structs to TF schemas so we don't need to inline stuff like this.

// We need to create this because we need Owner, FullNameArg, SchemaName and CatalogName which aren't present in a single of them.
// We also need to annotate tf:"computed" for the Owner field.
type ConnectionInfo struct {
// User-provided free-form text description.
Comment string `json:"comment,omitempty" tf:"force_new"`
// The type of connection.
ConnectionType string `json:"connection_type" tf:"force_new"`
// Unique identifier of parent metastore.
MetastoreId string `json:"metastore_id,omitempty" tf:"computed"`
// Name of the connection.
Name string `json:"name"`
// Name of the connection.
NameArg string `json:"-" url:"-"`
// A map of key-value properties attached to the securable.
Options map[string]string `json:"options" tf:"sensitive"`
// Username of current owner of the connection.
Owner string `json:"owner,omitempty" tf:"force_new,suppress_diff"`
// An object containing map of key-value properties attached to the
// connection.
Properties map[string]string `json:"properties,omitempty" tf:"force_new"`
// If the connection is read only.
ReadOnly bool `json:"read_only,omitempty" tf:"force_new,computed"`
}

var sensitiveOptions = []string{"user", "password", "personalAccessToken", "access_token", "client_secret", "OAuthPvtKey"}

func ResourceConnection() *schema.Resource {
s := common.StructToSchema(ConnectionInfo{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
return m
})
Comment on lines +41 to +43
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that we have multiple places like this, so it makes sense to move this to the common package

pi := common.NewPairID("metastore_id", "name").Schema(
func(m map[string]*schema.Schema) map[string]*schema.Schema {
return s
})
return common.Resource{
Schema: s,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
var createConnectionRequest catalog.CreateConnection
common.DataToStructPointer(d, s, &createConnectionRequest)
conn, err := w.Connections.Create(ctx, createConnectionRequest)
if err != nil {
return err
}
d.Set("metastore_id", conn.MetastoreId)
pi.Pack(d)
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
_, connName, err := pi.Unpack(d)
if err != nil {
return err
}
conn, err := w.Connections.GetByNameArg(ctx, connName)
if err != nil {
return err
}
// We need to preserve original sensitive options as API doesn't return them
var cOrig catalog.CreateConnection
common.DataToStructPointer(d, s, &cOrig)
for key, element := range cOrig.Options {
if slices.Contains(sensitiveOptions, key) {
conn.Options[key] = element
}
}
return common.StructToData(conn, s, d)
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
var updateConnectionRequest catalog.UpdateConnection
common.DataToStructPointer(d, s, &updateConnectionRequest)
_, connName, err := pi.Unpack(d)
updateConnectionRequest.NameArg = connName
if err != nil {
return err
}
conn, err := w.Connections.Update(ctx, updateConnectionRequest)
if err != nil {
return err
}
// We need to repack the Id as the name may have changed
d.Set("name", conn.Name)
pi.Pack(d)
return nil
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
_, connName, err := pi.Unpack(d)
if err != nil {
return err
}
return w.Connections.DeleteByNameArg(ctx, connName)
},
}.ToResource()
}
Loading
Loading