Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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 NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### New Features and Improvements

* Add `provider_config` support for manual plugin framework resources and data sources([#5115](https://github.com/databricks/terraform-provider-databricks/pull/5115))

### Bug Fixes

* Fix crash when error happens during reading `databricks_job` ([#5110](https://github.com/databricks/terraform-provider-databricks/pull/5110))
Expand Down
71 changes: 40 additions & 31 deletions common/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 31 additions & 9 deletions internal/providers/pluginfw/products/app/data_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema"
"github.com/databricks/terraform-provider-databricks/internal/service/apps_tf"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

func DataSourceApp() datasource.DataSource {
Expand All @@ -26,18 +26,20 @@ type dataSourceApp struct {
type dataApp struct {
Name types.String `tfsdk:"name"`
App types.Object `tfsdk:"app"`
tfschema.Namespace
}

func (dataApp) ApplySchemaCustomizations(attrs map[string]tfschema.AttributeBuilder) map[string]tfschema.AttributeBuilder {
attrs["name"] = attrs["name"].SetRequired()
attrs["app"] = attrs["app"].SetComputed()

attrs["provider_config"] = attrs["provider_config"].SetOptional()
return attrs
}

func (dataApp) GetComplexFieldTypes(context.Context) map[string]reflect.Type {
return map[string]reflect.Type{
"app": reflect.TypeOf(apps_tf.App{}),
"app": reflect.TypeOf(apps_tf.App{}),
"provider_config": reflect.TypeOf(tfschema.ProviderConfigData{}),
}
}

Expand All @@ -59,19 +61,33 @@ func (a *dataSourceApp) Configure(ctx context.Context, req datasource.ConfigureR

func (a *dataSourceApp) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
ctx = pluginfwcontext.SetUserAgentInDataSourceContext(ctx, resourceName)
w, diags := a.client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)

var config dataApp
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}

var name types.String
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("name"), &name)...)
var workspaceID string
if !config.ProviderConfig.IsNull() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, let's refactor this out to a separate method

var namespace tfschema.ProviderConfigData
resp.Diagnostics.Append(config.ProviderConfig.As(ctx, &namespace, basetypes.ObjectAsOptions{
UnhandledNullAsEmpty: true,
UnhandledUnknownAsEmpty: true,
})...)
if resp.Diagnostics.HasError() {
return
}
workspaceID = namespace.WorkspaceID.ValueString()
}

w, diags := a.client.GetWorkspaceClientForUnifiedProviderWithDiagnostics(ctx, workspaceID)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

appGoSdk, err := w.Apps.GetByName(ctx, name.ValueString())
appGoSdk, err := w.Apps.GetByName(ctx, config.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError("failed to read app", err.Error())
return
Expand All @@ -82,7 +98,13 @@ func (a *dataSourceApp) Read(ctx context.Context, req datasource.ReadRequest, re
if resp.Diagnostics.HasError() {
return
}
dataApp := dataApp{Name: name, App: newApp.ToObjectValue(ctx)}
dataApp := dataApp{
Name: config.Name,
App: newApp.ToObjectValue(ctx),
Namespace: tfschema.Namespace{
ProviderConfig: config.ProviderConfig,
},
}
resp.Diagnostics.Append(resp.State.Set(ctx, dataApp)...)
if resp.Diagnostics.HasError() {
return
Expand Down
36 changes: 32 additions & 4 deletions internal/providers/pluginfw/products/app/data_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

func DataSourceApps() datasource.DataSource {
Expand All @@ -26,17 +27,19 @@ type dataSourceApps struct {

type dataApps struct {
Apps types.List `tfsdk:"app"`
tfschema.Namespace
}

func (dataApps) ApplySchemaCustomizations(attrs map[string]tfschema.AttributeBuilder) map[string]tfschema.AttributeBuilder {
attrs["app"] = attrs["app"].SetComputed()

attrs["provider_config"] = attrs["provider_config"].SetOptional()
return attrs
}

func (dataApps) GetComplexFieldTypes(context.Context) map[string]reflect.Type {
return map[string]reflect.Type{
"app": reflect.TypeOf(apps_tf.App{}),
"app": reflect.TypeOf(apps_tf.App{}),
"provider_config": reflect.TypeOf(tfschema.ProviderConfigData{}),
}
}

Expand All @@ -58,7 +61,27 @@ func (a *dataSourceApps) Configure(ctx context.Context, req datasource.Configure

func (a *dataSourceApps) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
ctx = pluginfwcontext.SetUserAgentInDataSourceContext(ctx, resourceName)
w, diags := a.client.GetWorkspaceClient()

var config dataApps
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}

var workspaceID string
if !config.ProviderConfig.IsNull() {
var namespace tfschema.ProviderConfigData
resp.Diagnostics.Append(config.ProviderConfig.As(ctx, &namespace, basetypes.ObjectAsOptions{
UnhandledNullAsEmpty: true,
UnhandledUnknownAsEmpty: true,
})...)
if resp.Diagnostics.HasError() {
return
}
workspaceID = namespace.WorkspaceID.ValueString()
}

w, diags := a.client.GetWorkspaceClientForUnifiedProviderWithDiagnostics(ctx, workspaceID)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -79,7 +102,12 @@ func (a *dataSourceApps) Read(ctx context.Context, req datasource.ReadRequest, r
}
apps = append(apps, app.ToObjectValue(ctx))
}
dataApps := dataApps{Apps: types.ListValueMust(apps_tf.App{}.Type(ctx), apps)}
dataApps := dataApps{
Apps: types.ListValueMust(apps_tf.App{}.Type(ctx), apps),
Namespace: tfschema.Namespace{
ProviderConfig: config.ProviderConfig,
},
}
resp.Diagnostics.Append(resp.State.Set(ctx, dataApps)...)
if resp.Diagnostics.HasError() {
return
Expand Down
Loading
Loading