|
| 1 | +package registered_model |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/databricks/databricks-sdk-go/apierr" |
| 8 | + "github.com/databricks/databricks-sdk-go/service/catalog" |
| 9 | + "github.com/databricks/terraform-provider-databricks/common" |
| 10 | + pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common" |
| 11 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/converters" |
| 12 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema" |
| 13 | + "github.com/databricks/terraform-provider-databricks/internal/service/catalog_tf" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | +) |
| 18 | + |
| 19 | +func DataSourceRegisteredModel() datasource.DataSource { |
| 20 | + return &RegisteredModelDataSource{} |
| 21 | +} |
| 22 | + |
| 23 | +var _ datasource.DataSourceWithConfigure = &RegisteredModelDataSource{} |
| 24 | + |
| 25 | +type RegisteredModelDataSource struct { |
| 26 | + Client *common.DatabricksClient |
| 27 | +} |
| 28 | + |
| 29 | +type RegisteredModelData struct { |
| 30 | + FullName types.String `tfsdk:"full_name"` |
| 31 | + IncludeAliases types.Bool `tfsdk:"include_aliases" tf:"optional"` |
| 32 | + IncludeBrowse types.Bool `tfsdk:"include_browse" tf:"optional"` |
| 33 | + ModelInfo []catalog_tf.RegisteredModelInfo `tfsdk:"model_info" tf:"optional,computed"` |
| 34 | +} |
| 35 | + |
| 36 | +func (d *RegisteredModelDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 37 | + resp.TypeName = "databricks_registered_model" |
| 38 | +} |
| 39 | + |
| 40 | +func (d *RegisteredModelDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 41 | + attrs, blocks := tfschema.DataSourceStructToSchemaMap(RegisteredModelData{}, nil) |
| 42 | + resp.Schema = schema.Schema{ |
| 43 | + Attributes: attrs, |
| 44 | + Blocks: blocks, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (d *RegisteredModelDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 49 | + if d.Client == nil { |
| 50 | + d.Client = pluginfwcommon.ConfigureDataSource(req, resp) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (d *RegisteredModelDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 55 | + w, diags := d.Client.GetWorkspaceClient() |
| 56 | + resp.Diagnostics.Append(diags...) |
| 57 | + if resp.Diagnostics.HasError() { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + var registeredModel RegisteredModelData |
| 62 | + diags = req.Config.Get(ctx, ®isteredModel) |
| 63 | + resp.Diagnostics.Append(diags...) |
| 64 | + if resp.Diagnostics.HasError() { |
| 65 | + return |
| 66 | + } |
| 67 | + modelFullName := registeredModel.FullName.ValueString() |
| 68 | + modelInfoSdk, err := w.RegisteredModels.Get(ctx, catalog.GetRegisteredModelRequest{ |
| 69 | + FullName: modelFullName, |
| 70 | + IncludeAliases: registeredModel.IncludeAliases.ValueBool(), |
| 71 | + IncludeBrowse: registeredModel.IncludeBrowse.ValueBool(), |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + if apierr.IsMissing(err) { |
| 75 | + resp.State.RemoveResource(ctx) |
| 76 | + } |
| 77 | + resp.Diagnostics.AddError(fmt.Sprintf("failed to get registered model %s", modelFullName), err.Error()) |
| 78 | + return |
| 79 | + } |
| 80 | + var modelInfo catalog_tf.RegisteredModelInfo |
| 81 | + resp.Diagnostics.Append(converters.GoSdkToTfSdkStruct(ctx, modelInfoSdk, &modelInfo)...) |
| 82 | + if resp.Diagnostics.HasError() { |
| 83 | + return |
| 84 | + } |
| 85 | + if modelInfo.Aliases == nil { |
| 86 | + modelInfo.Aliases = []catalog_tf.RegisteredModelAlias{} |
| 87 | + } |
| 88 | + registeredModel.ModelInfo = append(registeredModel.ModelInfo, modelInfo) |
| 89 | + resp.Diagnostics.Append(resp.State.Set(ctx, registeredModel)...) |
| 90 | +} |
0 commit comments