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 17 commits
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
2 changes: 2 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,7 @@ 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,
}
}

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"`
}

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)...)
}
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
@@ -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
10 changes: 3 additions & 7 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())
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