Skip to content

Commit e46ad2b

Browse files
alexottmgyucht
andauthored
[Feature] Add databricks_registered_model data source (#4033)
## Changes <!-- Summary of your changes that are easy to understand --> ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [x] relevant change in `docs/` folder - [x] covered with integration tests in `internal/acceptance` - [x] relevant acceptance tests are passing - [x] using Go SDK --------- Co-authored-by: Miles Yucht <[email protected]>
1 parent 4a70e64 commit e46ad2b

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
subcategory: "Unity Catalog"
3+
---
4+
# databricks_registered_model Data Source
5+
6+
-> This resource can only be used with a workspace-level provider!
7+
8+
This resource allows you to get information about [Model in Unity Catalog](https://docs.databricks.com/en/mlflow/models-in-uc.html) in Databricks.
9+
10+
## Example Usage
11+
12+
```hcl
13+
data "databricks_registered_model" "this" {
14+
full_name = "main.default.my_model"
15+
}
16+
```
17+
18+
## Argument Reference
19+
20+
The following arguments are supported:
21+
22+
* `full_name` - (Required, String) The fully-qualified name of the registered model (`catalog_name.schema_name.name`).
23+
* `include_aliases` - (Optional, Boolean) flag to specify if list of aliases should be included into output.
24+
* `include_browse` - (Optional, Boolean) flag to specify if include registered models in the response for which the principal can only access selective metadata for.
25+
26+
## Attribute Reference
27+
28+
The following attributes are exported:
29+
30+
* `model_info` - block with information about the model in Unity Catalog:
31+
* `aliases` - the list of aliases associated with this model. Each item is object consisting of following attributes:
32+
* `alias_name` - string with the name of alias
33+
* `version_num` - associated model version
34+
* `catalog_name` - The name of the catalog where the schema and the registered model reside.
35+
* `comment` - The comment attached to the registered model.
36+
* `created_at` - the Unix timestamp at the model's creation
37+
* `created_by` - the identifier of the user who created the model
38+
* `full_name` - The fully-qualified name of the registered model (`catalog_name.schema_name.name`).
39+
* `metastore_id` - the unique identifier of the metastore
40+
* `name` - The name of the registered model.
41+
* `owner` - Name of the registered model owner.
42+
* `schema_name` - The name of the schema where the registered model resides.
43+
* `storage_location` - The storage location under which model version data files are stored.
44+
* `updated_at` - the timestamp of the last time changes were made to the model
45+
* `updated_by` - the identifier of the user who updated the model last time
46+
47+
## Related Resources
48+
49+
The following resources are often used in the same context:
50+
51+
* [databricks_registered_model](../resources/schema.md) resource to manage models within Unity Catalog.
52+
* [databricks_model_serving](../resources/model_serving.md) to serve this model on a Databricks serving endpoint.
53+
* [databricks_mlflow_experiment](../resources/mlflow_experiment.md) to manage [MLflow experiments](https://docs.databricks.com/data/data-sources/mlflow-experiment.html) in Databricks.

internal/acceptance/registered_model_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ func TestUcAccRegisteredModel(t *testing.T) {
4343
owner = "account users"
4444
comment = "new comment"
4545
}
46+
data "databricks_registered_model" "model" {
47+
full_name = databricks_registered_model.model.id
48+
include_model_versions = true
49+
}
50+
output "model" {
51+
value = data.databricks_registered_model.model
52+
}
4653
`,
4754
},
4855
)

internal/providers/pluginfw/pluginfw.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/cluster"
2020
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/library"
2121
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/qualitymonitor"
22+
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/registered_model"
2223
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/volume"
2324

2425
"github.com/hashicorp/terraform-plugin-framework/datasource"
@@ -52,6 +53,7 @@ func (p *DatabricksProviderPluginFramework) DataSources(ctx context.Context) []f
5253
return []func() datasource.DataSource{
5354
cluster.DataSourceCluster,
5455
volume.DataSourceVolumes,
56+
registered_model.DataSourceRegisteredModel,
5557
}
5658
}
5759

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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, &registeredModel)
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

Comments
 (0)