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_cluster data source to plugin framework #3988

Merged
merged 28 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
test passing
  • Loading branch information
tanmay-db committed Sep 4, 2024
commit a707ca1b05277967c54df7d4028c1fea560597c6
26 changes: 18 additions & 8 deletions internal/providers/pluginfw/resources/cluster/data_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"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"
Expand All @@ -24,10 +26,10 @@ type ClusterDataSource struct {
}

type ClusterInfo struct {
Id types.String `json:"id,omitempty" tf:"computed"`
ClusterId types.String `json:"cluster_id,omitempty" tf:"computed"`
Name types.String `json:"cluster_name,omitempty" tf:"computed"`
ClusterInfo *compute.ClusterDetails `json:"cluster_info,omitempty" tf:"computed"`
Id types.String `tfsdk:"id" tf:"optional"`
ClusterId types.String `tfsdk:"cluster_id" tf:"optional"`
Name types.String `tfsdk:"cluster_name" tf:"optional"`
ClusterInfo *compute_tf.ClusterDetails `tfsdk:"cluster_info" tf:"optional"`
}

func (d *ClusterDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand All @@ -36,7 +38,13 @@ func (d *ClusterDataSource) Metadata(ctx context.Context, req datasource.Metadat

func (d *ClusterDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: tfschema.DataSourceStructToSchemaMap(ClusterInfo{}, nil),
Attributes: tfschema.DataSourceStructToSchemaMap(ClusterInfo{}, func(c tfschema.CustomizableSchema) tfschema.CustomizableSchema {
c.SetComputed("id")
c.SetComputed("cluster_id")
c.SetComputed("cluster_name")
c.SetComputed("cluster_info")
return c
}),
}
}

Expand Down Expand Up @@ -66,10 +74,12 @@ func (d *ClusterDataSource) Read(ctx context.Context, req datasource.ReadRequest
resp.Diagnostics.AddError("failed to list clusters", err.Error())
return
}
namedClusters := []compute.ClusterDetails{}
for _, clst := range clusters {
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.ValueString() {
if cluster.ClusterName == clusterInfo.Name {
namedClusters = append(namedClusters, cluster)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestAccDataSourceClusterPluginFramework(t *testing.T) {
}

output "cluster_info" {
value = data.databricks_cluster.this.cluster_info
value = data.databricks_cluster_pluginframework.this.cluster_info
}`,
})
}
22 changes: 14 additions & 8 deletions internal/providers/pluginfw/resources/cluster/data_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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 {
Expand All @@ -23,9 +24,9 @@ type ClustersDataSource struct {
}

type ClustersInfo struct {
Id string `json:"id,omitempty" tf:"computed"`
Ids []string `json:"ids,omitempty" tf:"computed,slice_set"`
ClusterNameContains string `json:"cluster_name_contains,omitempty"`
Id types.String `tfsdk:"id" tf:"optional"`
Ids []types.String `tfsdk:"ids" tf:"optional"`
ClusterNameContains types.String `tfsdk:"cluster_name_contains" tf:"optional"`
}

func (d *ClustersDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand All @@ -34,7 +35,12 @@ func (d *ClustersDataSource) Metadata(ctx context.Context, req datasource.Metada

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

Expand Down Expand Up @@ -64,16 +70,16 @@ func (d *ClustersDataSource) Read(ctx context.Context, req datasource.ReadReques
return
}

ids := make([]string, 0, len(clusters))
name_contains := strings.ToLower(clustersInfo.ClusterNameContains)
ids := make([]types.String, 0, len(clusters))
name_contains := strings.ToLower(clustersInfo.ClusterNameContains.ValueString())
for _, v := range clusters {
match_name := strings.Contains(strings.ToLower(v.ClusterName), name_contains)
if name_contains != "" && !match_name {
continue
}
ids = append(ids, v.ClusterId)
ids = append(ids, types.StringValue(v.ClusterId))
}
clustersInfo.Ids = ids
clustersInfo.Id = "_"
clustersInfo.Id = types.StringValue("_")
resp.State.Set(ctx, clustersInfo)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAccDataSourceClustersNoFilterPluginFramework(t *testing.T) {
func TestAccDataSourceClustersWithFilterPluginFramework(t *testing.T) {
acceptance.WorkspaceLevel(t, acceptance.Step{
Template: `
data "databricks_clusters" "this" {
data "databricks_clusters_pluginframework" "this" {
cluster_name_contains = "Default"
}`,
})
Expand Down
Loading