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 resource and databricks_clusters data source to plugin framework #4220

Draft
wants to merge 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

const dataSourceName = "cluster"
const dataSourceNameCluster = "cluster"

func DataSourceCluster() datasource.DataSource {
return &ClusterDataSource{}
Expand All @@ -38,7 +38,7 @@ type ClusterInfo struct {
}

func (d *ClusterDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = pluginfwcommon.GetDatabricksStagingName(dataSourceName)
resp.TypeName = pluginfwcommon.GetDatabricksStagingName(dataSourceNameCluster)
}

func (d *ClusterDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
Expand Down Expand Up @@ -70,7 +70,7 @@ func validateClustersList(ctx context.Context, clusters []compute_tf.ClusterDeta
}

func (d *ClusterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
ctx = pluginfwcontext.SetUserAgentInDataSourceContext(ctx, dataSourceName)
ctx = pluginfwcontext.SetUserAgentInDataSourceContext(ctx, dataSourceNameCluster)
w, diags := d.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
80 changes: 80 additions & 0 deletions internal/providers/pluginfw/resources/cluster/data_clusters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cluster

import (
"context"
"strings"

"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"
)

const dataSourceNameClusters = "clusters"

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 = pluginfwcommon.GetDatabricksStagingName(dataSourceNameClusters)
}

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

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 {
resp.Diagnostics.AddError("failed to list clusters", err.Error())
return
}

ids := make([]types.String, 0, len(clusters))
nameContains := strings.ToLower(clustersInfo.ClusterNameContains.ValueString())
for _, v := range clusters {
matchName := strings.Contains(strings.ToLower(v.ClusterName), nameContains)
if matchName {
ids = append(ids, types.StringValue(v.ClusterId))
}
}
clustersInfo.Ids = ids
resp.Diagnostics.Append(resp.State.Set(ctx, clustersInfo)...)
}
Loading
Loading