|
| 1 | +package cluster |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/databricks/databricks-sdk-go/apierr" |
| 9 | + "github.com/databricks/databricks-sdk-go/service/compute" |
| 10 | + "github.com/databricks/terraform-provider-databricks/common" |
| 11 | + pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common" |
| 12 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/converters" |
| 13 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema" |
| 14 | + "github.com/databricks/terraform-provider-databricks/internal/service/compute_tf" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 19 | +) |
| 20 | + |
| 21 | +func DataSourceCluster() datasource.DataSource { |
| 22 | + return &ClusterDataSource{} |
| 23 | +} |
| 24 | + |
| 25 | +var _ datasource.DataSourceWithConfigure = &ClusterDataSource{} |
| 26 | + |
| 27 | +type ClusterDataSource struct { |
| 28 | + Client *common.DatabricksClient |
| 29 | +} |
| 30 | + |
| 31 | +type ClusterInfo struct { |
| 32 | + ClusterId types.String `tfsdk:"cluster_id" tf:"optional,computed"` |
| 33 | + Name types.String `tfsdk:"cluster_name" tf:"optional,computed"` |
| 34 | + ClusterInfo *compute_tf.ClusterDetails `tfsdk:"cluster_info" tf:"optional,computed"` |
| 35 | +} |
| 36 | + |
| 37 | +func (d *ClusterDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 38 | + resp.TypeName = "databricks_cluster_pluginframework" |
| 39 | +} |
| 40 | + |
| 41 | +func (d *ClusterDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 42 | + resp.Schema = schema.Schema{ |
| 43 | + Attributes: tfschema.DataSourceStructToSchemaMap(ClusterInfo{}, nil), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (d *ClusterDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 48 | + if d.Client == nil { |
| 49 | + d.Client = pluginfwcommon.ConfigureDataSource(req, resp) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func validateClustersList(ctx context.Context, clusters []compute_tf.ClusterDetails, clusterName string) diag.Diagnostics { |
| 54 | + if len(clusters) == 0 { |
| 55 | + return diag.Diagnostics{diag.NewErrorDiagnostic(fmt.Sprintf("there is no cluster with name '%s'", clusterName), "")} |
| 56 | + } |
| 57 | + if len(clusters) > 1 { |
| 58 | + clusterIDs := []string{} |
| 59 | + for _, cluster := range clusters { |
| 60 | + clusterIDs = append(clusterIDs, cluster.ClusterId.ValueString()) |
| 61 | + } |
| 62 | + return diag.Diagnostics{diag.NewErrorDiagnostic(fmt.Sprintf("there is more than one cluster with name '%s'", clusterName), fmt.Sprintf("The IDs of those clusters are: %s. When specifying a cluster name, the name must be unique. Alternatively, specify the cluster by ID using the cluster_id attribute.", strings.Join(clusterIDs, ", ")))} |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func (d *ClusterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 68 | + w, diags := d.Client.GetWorkspaceClient() |
| 69 | + resp.Diagnostics.Append(diags...) |
| 70 | + if resp.Diagnostics.HasError() { |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + var clusterInfo ClusterInfo |
| 75 | + resp.Diagnostics.Append(req.Config.Get(ctx, &clusterInfo)...) |
| 76 | + if resp.Diagnostics.HasError() { |
| 77 | + return |
| 78 | + } |
| 79 | + clusterName := clusterInfo.Name.ValueString() |
| 80 | + clusterId := clusterInfo.ClusterId.ValueString() |
| 81 | + if clusterName != "" { |
| 82 | + clustersGoSDk, err := w.Clusters.ListAll(ctx, compute.ListClustersRequest{}) |
| 83 | + if err != nil { |
| 84 | + resp.Diagnostics.AddError("failed to list clusters", err.Error()) |
| 85 | + return |
| 86 | + } |
| 87 | + var clustersTfSDK []compute_tf.ClusterDetails |
| 88 | + for _, cluster := range clustersGoSDk { |
| 89 | + var clusterDetails compute_tf.ClusterDetails |
| 90 | + resp.Diagnostics.Append(converters.GoSdkToTfSdkStruct(ctx, cluster, &clusterDetails)...) |
| 91 | + if resp.Diagnostics.HasError() { |
| 92 | + return |
| 93 | + } |
| 94 | + clustersTfSDK = append(clustersTfSDK, clusterDetails) |
| 95 | + } |
| 96 | + namedClusters := []compute_tf.ClusterDetails{} |
| 97 | + for _, cluster := range clustersTfSDK { |
| 98 | + if cluster.ClusterName == clusterInfo.Name { |
| 99 | + namedClusters = append(namedClusters, cluster) |
| 100 | + } |
| 101 | + } |
| 102 | + resp.Diagnostics.Append(validateClustersList(ctx, namedClusters, clusterName)...) |
| 103 | + if resp.Diagnostics.HasError() { |
| 104 | + return |
| 105 | + } |
| 106 | + clusterInfo.ClusterInfo = &namedClusters[0] |
| 107 | + } else if clusterId != "" { |
| 108 | + cluster, err := w.Clusters.GetByClusterId(ctx, clusterId) |
| 109 | + if err != nil { |
| 110 | + if apierr.IsMissing(err) { |
| 111 | + resp.State.RemoveResource(ctx) |
| 112 | + } |
| 113 | + resp.Diagnostics.AddError(fmt.Sprintf("failed to get cluster with cluster id: %s", clusterId), err.Error()) |
| 114 | + return |
| 115 | + } |
| 116 | + var clusterDetails compute_tf.ClusterDetails |
| 117 | + resp.Diagnostics.Append(converters.GoSdkToTfSdkStruct(ctx, cluster, &clusterDetails)...) |
| 118 | + if resp.Diagnostics.HasError() { |
| 119 | + return |
| 120 | + } |
| 121 | + clusterInfo.ClusterInfo = &clusterDetails |
| 122 | + } else { |
| 123 | + resp.Diagnostics.AddError("you need to specify either `cluster_name` or `cluster_id`", "") |
| 124 | + return |
| 125 | + } |
| 126 | + clusterInfo.ClusterId = clusterInfo.ClusterInfo.ClusterId |
| 127 | + clusterInfo.Name = clusterInfo.ClusterInfo.ClusterName |
| 128 | + resp.Diagnostics.Append(resp.State.Set(ctx, clusterInfo)...) |
| 129 | +} |
0 commit comments