|
| 1 | +package library |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/databricks/databricks-sdk-go/service/compute" |
| 9 | + "github.com/databricks/terraform-provider-databricks/clusters" |
| 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/databricks/terraform-provider-databricks/libraries" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 20 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier" |
| 21 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 22 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 23 | + |
| 24 | + "github.com/databricks/databricks-sdk-go" |
| 25 | +) |
| 26 | + |
| 27 | +const libraryDefaultInstallationTimeout = 15 * time.Minute |
| 28 | + |
| 29 | +var _ resource.ResourceWithConfigure = &LibraryResource{} |
| 30 | + |
| 31 | +func ResourceLibrary() resource.Resource { |
| 32 | + return &LibraryResource{} |
| 33 | +} |
| 34 | + |
| 35 | +func readLibrary(ctx context.Context, w *databricks.WorkspaceClient, waitParams compute.Wait, libraryRep string, libraryExtended *LibraryExtended) diag.Diagnostics { |
| 36 | + res, err := libraries.WaitForLibrariesInstalledSdk(ctx, w, waitParams, libraryDefaultInstallationTimeout) |
| 37 | + if err != nil { |
| 38 | + return diag.Diagnostics{diag.NewErrorDiagnostic("failed to wait for library installation", err.Error())} |
| 39 | + } |
| 40 | + |
| 41 | + for _, v := range res.LibraryStatuses { |
| 42 | + thisRep := v.Library.String() |
| 43 | + if thisRep == libraryRep { |
| 44 | + // This is not entirely necessary as we can directly write the fields in the config into the state, because there's no computed field. |
| 45 | + diags := converters.GoSdkToTfSdkStruct(ctx, v.Library, libraryExtended) |
| 46 | + |
| 47 | + if diags.HasError() { |
| 48 | + return diags |
| 49 | + } |
| 50 | + |
| 51 | + libraryExtended.ClusterId = types.StringValue(waitParams.ClusterID) |
| 52 | + |
| 53 | + return nil |
| 54 | + } |
| 55 | + } |
| 56 | + return diag.Diagnostics{diag.NewErrorDiagnostic("failed to find the installed library", fmt.Sprintf("failed to find %s on %s", libraryRep, waitParams.ClusterID))} |
| 57 | +} |
| 58 | + |
| 59 | +type LibraryExtended struct { |
| 60 | + compute_tf.Library |
| 61 | + ClusterId types.String `tfsdk:"cluster_id"` |
| 62 | +} |
| 63 | + |
| 64 | +type LibraryResource struct { |
| 65 | + Client *common.DatabricksClient |
| 66 | +} |
| 67 | + |
| 68 | +func (r *LibraryResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 69 | + resp.TypeName = "databricks_library_pluginframework" |
| 70 | +} |
| 71 | + |
| 72 | +func (r *LibraryResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 73 | + resp.Schema = schema.Schema{ |
| 74 | + Description: "Terraform schema for Databricks Library", |
| 75 | + Attributes: tfschema.ResourceStructToSchemaMap(LibraryExtended{}, func(c tfschema.CustomizableSchema) tfschema.CustomizableSchema { |
| 76 | + // c.AddPlanModifier(stringplanmodifier.RequiresReplace(), "cluster_id") |
| 77 | + // c.AddPlanModifier(objectplanmodifier.RequiresReplace(), "cran") |
| 78 | + // c.AddPlanModifier(stringplanmodifier.RequiresReplace(), "egg") |
| 79 | + // c.AddPlanModifier(stringplanmodifier.RequiresReplace(), "jar") |
| 80 | + // c.AddPlanModifier(objectplanmodifier.RequiresReplace(), "maven") |
| 81 | + // c.AddPlanModifier(objectplanmodifier.RequiresReplace(), "pypi") |
| 82 | + // c.AddPlanModifier(stringplanmodifier.RequiresReplace(), "requirements") |
| 83 | + // c.AddPlanModifier(stringplanmodifier.RequiresReplace(), "whl") |
| 84 | + for field, attribute := range c.ToAttributeMap() { |
| 85 | + switch attribute.(type) { |
| 86 | + case tfschema.StringAttributeBuilder: |
| 87 | + c.AddPlanModifier(stringplanmodifier.RequiresReplace(), field) |
| 88 | + case tfschema.SingleNestedAttributeBuilder: |
| 89 | + c.AddPlanModifier(objectplanmodifier.RequiresReplace(), field) |
| 90 | + } |
| 91 | + } |
| 92 | + return c |
| 93 | + }), |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func (r *LibraryResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 98 | + if r.Client == nil { |
| 99 | + r.Client = pluginfwcommon.ConfigureResource(req, resp) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func (r *LibraryResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 104 | + w, diags := r.Client.GetWorkspaceClient() |
| 105 | + resp.Diagnostics.Append(diags...) |
| 106 | + if resp.Diagnostics.HasError() { |
| 107 | + return |
| 108 | + } |
| 109 | + var libraryTfSDK LibraryExtended |
| 110 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &libraryTfSDK)...) |
| 111 | + if resp.Diagnostics.HasError() { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + var libGoSDK compute.Library |
| 116 | + resp.Diagnostics.Append(converters.TfSdkToGoSdkStruct(ctx, libraryTfSDK, &libGoSDK)...) |
| 117 | + if resp.Diagnostics.HasError() { |
| 118 | + return |
| 119 | + } |
| 120 | + installLib := compute.InstallLibraries{ |
| 121 | + Libraries: []compute.Library{libGoSDK}, |
| 122 | + } |
| 123 | + req.Plan.GetAttribute(ctx, path.Root("cluster_id"), &installLib.ClusterId) |
| 124 | + err := w.Libraries.Install(ctx, installLib) |
| 125 | + if err != nil { |
| 126 | + resp.Diagnostics.AddError("failed to install library", err.Error()) |
| 127 | + return |
| 128 | + } |
| 129 | + waitParams := compute.Wait{ |
| 130 | + ClusterID: installLib.ClusterId, |
| 131 | + IsRunning: true, |
| 132 | + } |
| 133 | + libraryRep := libGoSDK.String() |
| 134 | + installedLib := LibraryExtended{} |
| 135 | + |
| 136 | + resp.Diagnostics.Append(readLibrary(ctx, w, waitParams, libraryRep, &installedLib)...) |
| 137 | + |
| 138 | + if resp.Diagnostics.HasError() { |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + resp.Diagnostics.Append(resp.State.Set(ctx, installedLib)...) |
| 143 | +} |
| 144 | + |
| 145 | +func (r *LibraryResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 146 | + w, diags := r.Client.GetWorkspaceClient() |
| 147 | + resp.Diagnostics.Append(diags...) |
| 148 | + if resp.Diagnostics.HasError() { |
| 149 | + return |
| 150 | + } |
| 151 | + var libraryTfSDK LibraryExtended |
| 152 | + resp.Diagnostics.Append(req.State.Get(ctx, &libraryTfSDK)...) |
| 153 | + if resp.Diagnostics.HasError() { |
| 154 | + return |
| 155 | + } |
| 156 | + var libGoSDK compute.Library |
| 157 | + resp.Diagnostics.Append(converters.TfSdkToGoSdkStruct(ctx, libraryTfSDK, &libGoSDK)...) |
| 158 | + if resp.Diagnostics.HasError() { |
| 159 | + return |
| 160 | + } |
| 161 | + clusterId := libraryTfSDK.ClusterId.ValueString() |
| 162 | + libraryRep := libGoSDK.String() |
| 163 | + installedLib := LibraryExtended{} |
| 164 | + waitParams := compute.Wait{ |
| 165 | + ClusterID: clusterId, |
| 166 | + IsRefresh: true, |
| 167 | + } |
| 168 | + |
| 169 | + resp.Diagnostics.Append(readLibrary(ctx, w, waitParams, libraryRep, &installedLib)...) |
| 170 | + |
| 171 | + if resp.Diagnostics.HasError() { |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + resp.Diagnostics.Append(resp.State.Set(ctx, installedLib)...) |
| 176 | +} |
| 177 | + |
| 178 | +func (r *LibraryResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 179 | + resp.Diagnostics.AddError("failed to update library", "updating library is not supported") |
| 180 | +} |
| 181 | + |
| 182 | +func (r *LibraryResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 183 | + w, diags := r.Client.GetWorkspaceClient() |
| 184 | + resp.Diagnostics.Append(diags...) |
| 185 | + if resp.Diagnostics.HasError() { |
| 186 | + return |
| 187 | + } |
| 188 | + var libraryTfSDK LibraryExtended |
| 189 | + resp.Diagnostics.Append(req.State.Get(ctx, &libraryTfSDK)...) |
| 190 | + if resp.Diagnostics.HasError() { |
| 191 | + return |
| 192 | + } |
| 193 | + clusterID := libraryTfSDK.ClusterId.ValueString() |
| 194 | + var libGoSDK compute.Library |
| 195 | + resp.Diagnostics.Append(converters.TfSdkToGoSdkStruct(ctx, libraryTfSDK, &libGoSDK)...) |
| 196 | + if resp.Diagnostics.HasError() { |
| 197 | + return |
| 198 | + } |
| 199 | + libraryRep := libGoSDK.String() |
| 200 | + _, err := clusters.StartClusterAndGetInfo(ctx, w, clusterID) |
| 201 | + if err != nil { |
| 202 | + resp.Diagnostics.AddError("failed to start and get cluster", err.Error()) |
| 203 | + return |
| 204 | + } |
| 205 | + cll, err := w.Libraries.ClusterStatusByClusterId(ctx, clusterID) |
| 206 | + if err != nil { |
| 207 | + resp.Diagnostics.AddError("failed to get libraries", err.Error()) |
| 208 | + return |
| 209 | + } |
| 210 | + for _, v := range cll.LibraryStatuses { |
| 211 | + if v.Library.String() != libraryRep { |
| 212 | + continue |
| 213 | + } |
| 214 | + err := w.Libraries.Uninstall(ctx, compute.UninstallLibraries{ |
| 215 | + ClusterId: clusterID, |
| 216 | + Libraries: []compute.Library{*v.Library}, |
| 217 | + }) |
| 218 | + if err != nil { |
| 219 | + resp.Diagnostics.AddError("failed to uninstall library", err.Error()) |
| 220 | + } |
| 221 | + return |
| 222 | + } |
| 223 | + // Keeping the implementation to be consistent with the sdk-v2 implementation. Eventually we should update this to be not |
| 224 | + // an error, for cases such as the library being manually uninstalled. |
| 225 | + resp.Diagnostics.AddError("failed to uninstall library", fmt.Sprintf("failed to find %s on %s", libraryRep, clusterID)) |
| 226 | +} |
0 commit comments