Skip to content

Commit

Permalink
add data sources for Share and Shares
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchy committed Oct 29, 2024
1 parent 0975310 commit 2235d30
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 6 deletions.
2 changes: 2 additions & 0 deletions internal/providers/pluginfw/pluginfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (p *DatabricksProviderPluginFramework) DataSources(ctx context.Context) []f
volume.DataSourceVolumes,
registered_model.DataSourceRegisteredModel,
notificationdestinations.DataSourceNotificationDestinations,
sharing.DataSourceShare,
sharing.DataSourceShares,
}
}

Expand Down
74 changes: 74 additions & 0 deletions internal/providers/pluginfw/resources/sharing/data_share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package sharing

import (
"context"

"github.com/databricks/databricks-sdk-go/service/sharing"
"github.com/databricks/terraform-provider-databricks/common"
pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/converters"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema"
"github.com/databricks/terraform-provider-databricks/internal/service/sharing_tf"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)

func DataSourceShare() datasource.DataSource {
return &ShareDataSource{}
}

var _ datasource.DataSourceWithConfigure = &ShareDataSource{}

type ShareDataSource struct {
Client *common.DatabricksClient
}

func (d *ShareDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = pluginfwcommon.GetDatabricksStagingName("share")
}

func (d *ShareDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
attrs, blocks := tfschema.DataSourceStructToSchemaMap(sharing_tf.ShareInfo{}, nil)
resp.Schema = schema.Schema{
Attributes: attrs,
Blocks: blocks,
}
}

func (d *ShareDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if d.Client == nil {
d.Client = pluginfwcommon.ConfigureDataSource(req, resp)
}
}

func (d *ShareDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
w, diags := d.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

var config sharing_tf.ShareInfo
diags = req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

share, err := w.Shares.Get(ctx, sharing.GetShareRequest{
Name: config.Name.ValueString(),
IncludeSharedData: true,
})
if err != nil {
resp.Diagnostics.AddError("Failed to fetch share", err.Error())
return
}

var shareInfoTfSdk sharing_tf.ShareInfo
resp.Diagnostics.Append(converters.GoSdkToTfSdkStruct(ctx, share, &shareInfoTfSdk)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, shareInfoTfSdk)...)
}
67 changes: 67 additions & 0 deletions internal/providers/pluginfw/resources/sharing/data_shares.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package sharing

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/databricks/databricks-sdk-go/service/sharing"
"github.com/databricks/terraform-provider-databricks/common"
pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)

type SharesList struct {
Shares []types.String `tfsdk:"shares" tf:"computed,optional,slice_set"`
}

func DataSourceShares() datasource.DataSource {
return &SharesDataSource{}
}

var _ datasource.DataSourceWithConfigure = &SharesDataSource{}

type SharesDataSource struct {
Client *common.DatabricksClient
}

func (d *SharesDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = pluginfwcommon.GetDatabricksStagingName("shares")
}

func (d *SharesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
attrs, blocks := tfschema.DataSourceStructToSchemaMap(SharesList{}, nil)
resp.Schema = schema.Schema{
Attributes: attrs,
Blocks: blocks,
}
}

func (d *SharesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if d.Client == nil {
d.Client = pluginfwcommon.ConfigureDataSource(req, resp)
}
}

func (d *SharesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
w, diags := d.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

shares, err := w.Shares.ListAll(ctx, sharing.ListSharesRequest{})
if err != nil {
resp.Diagnostics.AddError("Failed to fetch shares", err.Error())
return
}

shareNames := make([]types.String, len(shares))
for i, share := range shares {
shareNames[i] = types.StringValue(share.Name)
}

resp.Diagnostics.Append(resp.State.Set(ctx, SharesList{Shares: shareNames})...)
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package acceptance
package sharing_test

import (
"strconv"
"testing"

"github.com/databricks/terraform-provider-databricks/internal/acceptance"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func checkSharesDataSourcePopulated(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
_, ok := s.Modules[0].Resources["data.databricks_shares.this"]
require.True(t, ok, "data.databricks_shares.this has to be there")
_, ok := s.Modules[0].Resources["data.databricks_shares_pluginframework.this"]
require.True(t, ok, "data.databricks_shares_pluginframework.this has to be there")
num_shares, _ := strconv.Atoi(s.Modules[0].Outputs["shares"].Value.(string))
assert.GreaterOrEqual(t, num_shares, 1)
return nil
}
}
func TestUcAccDataSourceShares(t *testing.T) {
UnityWorkspaceLevel(t, Step{
acceptance.UnityWorkspaceLevel(t, acceptance.Step{
Template: `
resource "databricks_catalog" "sandbox" {
name = "sandbox{var.RANDOM}"
Expand Down Expand Up @@ -85,11 +86,11 @@ func TestUcAccDataSourceShares(t *testing.T) {
}
}
data "databricks_shares" "this" {
data "databricks_shares_pluginframework" "this" {
depends_on = [databricks_share.myshare]
}
output "shares" {
value = length(data.databricks_shares.this.shares)
value = length(data.databricks_shares_pluginframework.this.shares)
}
`,
Check: checkSharesDataSourcePopulated(t),
Expand Down

0 comments on commit 2235d30

Please sign in to comment.