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

Adds databricks_volume as data source #3211

Merged
merged 17 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
46 changes: 46 additions & 0 deletions catalog/data_volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package catalog

import (
"context"
"fmt"

"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/terraform-provider-databricks/common"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type volumeDataParams struct {
FullName string `json:"full_name,omitempty"`
CatalogName string `json:"catalog_name,omitempty"`
SchemaName string `json:"schema_name,omitempty"`
Name string `json:"name,omitempty"`
}

func customizeVolumeParams(s map[string]*schema.Schema) map[string]*schema.Schema {
common.CustomizeSchemaPath(s, "full_name").SetExactlyOneOf([]string{"catalog_name"}).SetComputed()
common.CustomizeSchemaPath(s, "catalog_name").SetRequiredWith([]string{"schema_name", "name"}).SetComputed()
common.CustomizeSchemaPath(s, "schema_name").SetComputed()
common.CustomizeSchemaPath(s, "name").SetComputed()
return s
}

func volumeDataRead(ctx context.Context, data volumeDataParams, w *databricks.WorkspaceClient) (*catalog.VolumeInfo, error) {
volumeRequest := catalog.ReadVolumeRequest{}
if data.FullName != "" {
volumeRequest.FullNameArg = data.FullName
} else {
volumeRequest.FullNameArg = fmt.Sprintf("%s.%s.%s", data.CatalogName, data.SchemaName, data.Name)
}

volume, err := w.Volumes.Read(ctx, volumeRequest)
if err != nil {
return nil, err
}
return volume, nil
}

func DataSourceVolume() common.Resource {
return common.WorkspaceDataWithCustomParams(volumeDataRead, customizeVolumeParams)
}
57 changes: 52 additions & 5 deletions common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func DataResource(sc any, read func(context.Context, any, *DatabricksClient) err
func WorkspaceData[T any](read func(context.Context, *T, *databricks.WorkspaceClient) error) Resource {
return genericDatabricksData((*DatabricksClient).WorkspaceClient, func(ctx context.Context, s struct{}, t *T, wc *databricks.WorkspaceClient) error {
return read(ctx, t, wc)
}, false)
}, NoCustomize, false)
}

// WorkspaceDataWithParams defines a data source that can be used to read data from the workspace API.
Expand Down Expand Up @@ -293,7 +293,52 @@ func WorkspaceDataWithParams[T, P any](read func(context.Context, P, *databricks
}
*s = *res
return nil
}, true)
}, NoCustomize, true)
}

// WorkspaceDataWithCustomParams defines a data source that can be used to read data from the workspace API.
// It differs from WorkspaceDataParams in that it accepts a function that customizes user-supplied parameters as
// a second argument to the function.
//
//
// The first argument is a function that will be called to read the data from the workspace API, returning the
// value of the resource type. The function should return an error if the data cannot be read or the resource cannot
// be found.
//
// The second argument is a function that will be used to customize user-supplied parameters to take advantage of
// terraform go SDK schema customization. This can be leveraged for user-supplied parameters validation.

// Example usage:
//
// type SqlWarehouse struct { ... }
//
// type SqlWarehouseDataParams struct {
// Id string `json:"id" tf:"computed,optional"`
// Name string `json:"name" tf:"computed,optional"`
// }
//
// WorkspaceDataWithCustomParams(
// func(ctx context.Context, data SqlWarehouseDataParams, w *databricks.WorkspaceClient) (*SqlWarehouse, error) {
// // User-provided attributes are present in the `data` parameter.
// // The resource should be returned.
// ...
// },
// func(s map[string]*schema.Schema) map[string]*schema.Schema {
// common.CustomizeSchemaPath(s, "name").SetExacltyOne([]string{"id"})
// ...
// return s
// })

func WorkspaceDataWithCustomParams[T, P any](read func(context.Context, P, *databricks.WorkspaceClient) (*T, error),
customize func(map[string]*schema.Schema) map[string]*schema.Schema) Resource {
return genericDatabricksData((*DatabricksClient).WorkspaceClient, func(ctx context.Context, o P, s *T, w *databricks.WorkspaceClient) error {
res, err := read(ctx, o, w)
if err != nil {
return err
}
*s = *res
return nil
}, customize, true)
}

// AccountData is a generic way to define account data resources in Terraform provider.
Expand All @@ -310,7 +355,7 @@ func WorkspaceDataWithParams[T, P any](read func(context.Context, P, *databricks
func AccountData[T any](read func(context.Context, *T, *databricks.AccountClient) error) Resource {
return genericDatabricksData((*DatabricksClient).AccountClient, func(ctx context.Context, s struct{}, t *T, ac *databricks.AccountClient) error {
return read(ctx, t, ac)
}, false)
}, NoCustomize, false)
}

// AccountDataWithParams defines a data source that can be used to read data from the workspace API.
Expand Down Expand Up @@ -352,21 +397,23 @@ func AccountDataWithParams[T, P any](read func(context.Context, P, *databricks.A
}
*s = *res
return nil
}, true)
}, NoCustomize, true)
}

// genericDatabricksData is generic and common way to define both account and workspace data and calls their respective clients.
//
// If hasOther is true, all of the fields of SdkType will be marked as computed in the final schema, and the fields
// from OtherFields will be overlaid on top of the schema generated by SdkType. Otherwise, the schema generated by
// SdkType will be used directly.
// customizeOther allows you to customize OtherFields with customization. If no customization is required pass NoCustomize.
func genericDatabricksData[T, P, C any](
getClient func(*DatabricksClient) (C, error),
read func(context.Context, P, *T, C) error,
customizeOther func(map[string]*schema.Schema) map[string]*schema.Schema,
hasOther bool) Resource {
var dummy T
var other P
otherFields := StructToSchema(other, NoCustomize)
otherFields := StructToSchema(other, customizeOther)
s := StructToSchema(dummy, func(m map[string]*schema.Schema) map[string]*schema.Schema {
// For WorkspaceData and AccountData, a single data type is used to represent all of the fields of
// the resource, so its configuration is correct. For the *WithParams methods, the SdkType parameter
Expand Down
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func DatabricksProvider() *schema.Provider {
"databricks_sql_warehouses": sql.DataSourceWarehouses().ToResource(),
"databricks_tables": catalog.DataSourceTables().ToResource(),
"databricks_views": catalog.DataSourceViews().ToResource(),
"databricks_volume": catalog.DataSourceVolume().ToResource(),
"databricks_volumes": catalog.DataSourceVolumes().ToResource(),
"databricks_user": scim.DataSourceUser().ToResource(),
"databricks_zones": clusters.DataSourceClusterZones().ToResource(),
Expand Down
Loading