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

[Internal] Migrate databricks_clusters data source to plugin framework #3998

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions internal/providers/pluginfw/pluginfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/databricks/terraform-provider-databricks/commands"
"github.com/databricks/terraform-provider-databricks/common"
providercommon "github.com/databricks/terraform-provider-databricks/internal/providers/common"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/cluster"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/qualitymonitor"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/volume"

Expand Down Expand Up @@ -48,6 +49,8 @@ func (p *DatabricksProviderPluginFramework) Resources(ctx context.Context) []fun
func (p *DatabricksProviderPluginFramework) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
volume.DataSourceVolumes,
cluster.DataSourceCluster,
cluster.DataSourceClusters,
}
}

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

import (
"context"
"fmt"

"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/service/compute"
"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/compute_tf"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func DataSourceCluster() datasource.DataSource {
return &ClusterDataSource{}
}

var _ datasource.DataSourceWithConfigure = &ClusterDataSource{}

type ClusterDataSource struct {
Client *common.DatabricksClient
}

type ClusterInfo struct {
ClusterId types.String `tfsdk:"cluster_id" tf:"optional,computed"`
Name types.String `tfsdk:"cluster_name" tf:"optional,computed"`
ClusterInfo *compute_tf.ClusterDetails `tfsdk:"cluster_info" tf:"optional,computed"`
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 we should also try adding a proto level annotation for making a field computed in the tf configuration, so the generated tfsdk structs will have these

}

func (d *ClusterDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "databricks_cluster_pluginframework"
}

func (d *ClusterDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: tfschema.DataSourceStructToSchemaMap(ClusterInfo{}, nil),
}
}

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

func (d *ClusterDataSource) 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 clusterInfo ClusterInfo
resp.Diagnostics.Append(req.Config.Get(ctx, &clusterInfo)...)
if resp.Diagnostics.HasError() {
return
}
if clusterInfo.Name.ValueString() != "" {
clusters, err := w.Clusters.ListAll(ctx, compute.ListClustersRequest{})
if err != nil {
if apierr.IsMissing(err) {
resp.State.RemoveResource(ctx)
}
resp.Diagnostics.AddError("failed to list clusters", err.Error())
return
}
var clustersTfSDK []compute_tf.ClusterDetails
converters.GoSdkToTfSdkStruct(ctx, clusters, clustersTfSDK)
namedClusters := []compute_tf.ClusterDetails{}
for _, clst := range clustersTfSDK {
cluster := clst
if cluster.ClusterName == clusterInfo.Name {
namedClusters = append(namedClusters, cluster)
}
}
if len(namedClusters) == 0 {
resp.Diagnostics.AddError(fmt.Sprintf("there is no cluster with name '%s'", clusterInfo.Name.ValueString()), "")
return
}
if len(namedClusters) > 1 {
resp.Diagnostics.AddError(fmt.Sprintf("there is more than one cluster with name '%s'", clusterInfo.Name.ValueString()), "")
return
}
clusterInfo.ClusterInfo = &namedClusters[0]
} else if clusterInfo.ClusterId.ValueString() != "" {
cluster, err := w.Clusters.GetByClusterId(ctx, clusterInfo.ClusterId.ValueString())
if err != nil {
if apierr.IsMissing(err) {
resp.State.RemoveResource(ctx)
}
resp.Diagnostics.AddError(fmt.Sprintf("failed to get cluster with cluster id: %s", clusterInfo.ClusterId.ValueString()), err.Error())
return
}
converters.GoSdkToTfSdkStruct(ctx, cluster, clusterInfo.ClusterInfo)
} else {
resp.Diagnostics.AddError("you need to specify either `cluster_name` or `cluster_id`", "")
return
}
clusterInfo.ClusterId = clusterInfo.ClusterInfo.ClusterId
resp.Diagnostics.Append(resp.State.Set(ctx, clusterInfo)...)
}
81 changes: 81 additions & 0 deletions internal/providers/pluginfw/resources/cluster/data_clusters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cluster

import (
"context"
"strings"

"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/service/compute"
"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"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func DataSourceClusters() datasource.DataSource {
return &ClustersDataSource{}
}

var _ datasource.DataSourceWithConfigure = &ClustersDataSource{}

type ClustersDataSource struct {
Client *common.DatabricksClient
}

type ClustersInfo struct {
Ids []types.String `tfsdk:"ids" tf:"optional,computed"`
ClusterNameContains types.String `tfsdk:"cluster_name_contains" tf:"optional"`
}

func (d *ClustersDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "databricks_clusters_pluginframework"
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto here

}

func (d *ClustersDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: tfschema.DataSourceStructToSchemaMap(ClustersInfo{}, nil),
}
}

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

func (d *ClustersDataSource) 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 clustersInfo ClustersInfo
resp.Diagnostics.Append(req.Config.Get(ctx, &clustersInfo)...)
if resp.Diagnostics.HasError() {
return
}

clusters, err := w.Clusters.ListAll(ctx, compute.ListClustersRequest{})
if err != nil {
if apierr.IsMissing(err) {
resp.State.RemoveResource(ctx)
}
Comment on lines +63 to +65
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this happen? Is it an error if there are no clusters? (Probably not.)

resp.Diagnostics.AddError("failed to list clusters", err.Error())
return
}

ids := make([]types.String, 0, len(clusters))
name_contains := strings.ToLower(clustersInfo.ClusterNameContains.ValueString())
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use camelCase?

for _, v := range clusters {
match_name := strings.Contains(strings.ToLower(v.ClusterName), name_contains)
if name_contains != "" && !match_name {
continue
}
ids = append(ids, types.StringValue(v.ClusterId))
Comment on lines +74 to +77
Copy link
Contributor

Choose a reason for hiding this comment

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

strings.Contains(any string, "") should always be true.

Suggested change
if name_contains != "" && !match_name {
continue
}
ids = append(ids, types.StringValue(v.ClusterId))
if match_name {
ids = append(ids, types.StringValue(v.ClusterId))
}

}
clustersInfo.Ids = ids
resp.Diagnostics.Append(resp.State.Set(ctx, clustersInfo)...)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tests

import (
"testing"

"github.com/databricks/terraform-provider-databricks/internal/acceptance"
)

func TestAccDataSourceCluster(t *testing.T) {
acceptance.WorkspaceLevel(t, acceptance.Step{
Template: `
data "databricks_cluster_pluginframework" "this" {
cluster_id = "{env.TEST_DEFAULT_CLUSTER_ID}"
}

output "cluster_info" {
value = data.databricks_cluster_pluginframework.this.cluster_info
}`,
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tests

import (
"testing"

"github.com/databricks/terraform-provider-databricks/internal/acceptance"
)

func TestAccDataSourceClustersNoFilter(t *testing.T) {
acceptance.WorkspaceLevel(t, acceptance.Step{
Template: `
data "databricks_clusters_pluginframework" "this" {
} `,
})
}

func TestAccDataSourceClustersWithFilter(t *testing.T) {
acceptance.WorkspaceLevel(t, acceptance.Step{
Template: `
data "databricks_clusters_pluginframework" "this" {
cluster_name_contains = "Default"
}`,
})
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package qualitymonitor_test
package tests

import (
"os"
Expand Down Expand Up @@ -48,7 +48,7 @@ resource "databricks_sql_table" "myInferenceTable" {

`

func TestUcAccQualityMonitorPluginFramework(t *testing.T) {
func TestUcAccQualityMonitor(t *testing.T) {
if os.Getenv("GOOGLE_CREDENTIALS") != "" {
t.Skipf("databricks_quality_monitor resource is not available on GCP")
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestUcAccQualityMonitorPluginFramework(t *testing.T) {
})
}

func TestUcAccUpdateQualityMonitorPluginFramework(t *testing.T) {
func TestUcAccUpdateQualityMonitor(t *testing.T) {
if os.Getenv("GOOGLE_CREDENTIALS") != "" {
t.Skipf("databricks_quality_monitor resource is not available on GCP")
}
Expand Down
12 changes: 4 additions & 8 deletions internal/providers/pluginfw/resources/volume/data_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type VolumesDataSource struct {
type VolumesList struct {
CatalogName types.String `tfsdk:"catalog_name"`
SchemaName types.String `tfsdk:"schema_name"`
Ids []types.String `tfsdk:"ids" tf:"optional"`
Ids []types.String `tfsdk:"ids" tf:"optional,computed"`
}

func (d *VolumesDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand All @@ -37,10 +37,7 @@ func (d *VolumesDataSource) Metadata(ctx context.Context, req datasource.Metadat

func (d *VolumesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: tfschema.DataSourceStructToSchemaMap(VolumesList{}, func(c tfschema.CustomizableSchema) tfschema.CustomizableSchema {
c.SetComputed("ids")
return c
}),
Attributes: tfschema.DataSourceStructToSchemaMap(VolumesList{}, nil),
}
}

Expand Down Expand Up @@ -69,13 +66,12 @@ func (d *VolumesDataSource) Read(ctx context.Context, req datasource.ReadRequest
if err != nil {
if apierr.IsMissing(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(fmt.Sprintf("Failed to get volumes for the catalog:%s and schema%s", listVolumesRequest.CatalogName, listVolumesRequest.SchemaName), err.Error())
resp.Diagnostics.AddError(fmt.Sprintf("failed to get volumes for the catalog:%s and schema%s", listVolumesRequest.CatalogName, listVolumesRequest.SchemaName), err.Error())
return
}
for _, v := range volumes {
volumesList.Ids = append(volumesList.Ids, types.StringValue(v.FullName))
}
resp.State.Set(ctx, volumesList)
resp.Diagnostics.Append(resp.State.Set(ctx, volumesList)...)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package volume_test
package tests

import (
"strconv"
Expand All @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require"
)

func checkDataSourceVolumesPluginFrameworkPopulated(t *testing.T) func(s *terraform.State) error {
func checkDataSourceVolumesPopulated(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
_, ok := s.Modules[0].Resources["data.databricks_volumes_pluginframework.this"]
require.True(t, ok, "data.databricks_volumes_pluginframework.this has to be there")
Expand All @@ -20,7 +20,7 @@ func checkDataSourceVolumesPluginFrameworkPopulated(t *testing.T) func(s *terraf
}
}

func TestUcAccDataSourceVolumesPluginFramework(t *testing.T) {
func TestUcAccDataSourceVolumes(t *testing.T) {
acceptance.UnityWorkspaceLevel(t, acceptance.Step{
Template: `
resource "databricks_catalog" "sandbox" {
Expand Down Expand Up @@ -54,6 +54,6 @@ func TestUcAccDataSourceVolumesPluginFramework(t *testing.T) {
value = length(data.databricks_volumes_pluginframework.this.ids)
}
`,
Check: checkDataSourceVolumesPluginFrameworkPopulated(t),
Check: checkDataSourceVolumesPopulated(t),
})
}
Loading
Loading