Skip to content

Commit

Permalink
#3468: started migrating to the plugin framework, adding support for …
Browse files Browse the repository at this point in the history
…workspace-level and account-level provider.
  • Loading branch information
dgomez04 committed Oct 23, 2024
1 parent bffcf54 commit 7fa5823
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions internal/providers/pluginfw/resources/user/data_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package user

import (
"context"

"github.com/databricks/terraform-provider-databricks/common"
pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func DataSourceUsers() datasource.DataSource {
return &UsersDataSource{}
}

var _ datasource.DataSourceWithConfigure = &UsersDataSource{}

type UsersDataSource struct {
Client *common.DatabricksClient
}

type UserData struct {
Id types.String `tfsdk:"id,omitempty" tf:"computed"`
UserName types.String `tfsdk:"user_name,omitempty" tf:"computed"`
DisplayName types.String `tfsdk:"display_name,omitempty" tf:"computed"`
}

type UsersInfo struct {
DisplayNameContains string `json:"display_name_contains,omitempty" tf:"computed"`
UserNameContains string `json:"user_name_contains,omitempty" tf:"computed"`
Users []UserData `json:"users,omitempty" tf:"computed"` // TODO: use UserData[] or []iam_tf.ListAccountUserResponse / []iam_tf.ListUserResponse?
}

func (d *UsersDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "databricks_users"
}

func (d *UsersDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
attrs, blocks := tfschema.DataSourceStructToSchemaMap(UsersInfo{}, nil)
resp.Schema = schema.Schema{
Attributes: attrs,
Blocks: blocks,
}
}

func (d *UsersDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if d.Client == nil {
d.Client = pluginfwcommon.ConfigureDataSource(req, resp)
}
}

// AppendDiagAndCheckErrors is a helper function that simplifies error handling by combining the appending of diagnostics and the checking of errors in a single step.
// It is particularly useful in data source and resource read operations where you want to append diagnostics and immediately determine if an error has occurred.
func AppendDiagAndCheckErrors(resp *datasource.ReadResponse, diags diag.Diagnostics) bool {
resp.Diagnostics.Append(diags...)
return resp.Diagnostics.HasError()
}

func validateFilters(data *UsersInfo) diag.Diagnostics {
if data.DisplayNameContains != "" && data.UserNameContains != "" {
return diag.Diagnostics{diag.NewErrorDiagnostic("Invalid configuration", "Exactly one of display_name_contains or user_name_contains should be specified, not both.")}
}
return nil
}

func (d *UsersDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var userInfo UsersInfo

diags := req.Config.Get(ctx, &userInfo)
diags = append(diags, validateFilters(&userInfo)...)

if AppendDiagAndCheckErrors(resp, diags) {
return
}

if d.Client.Config.IsAccountClient() {
a, diags := d.Client.GetAccountClient()

Check failure on line 80 in internal/providers/pluginfw/resources/user/data_users.go

View workflow job for this annotation

GitHub Actions / tests

a declared and not used
if AppendDiagAndCheckErrors(resp, diags) {
return
}
// TODO: Add retrieval of iterator at the account level.
} else {
w, diags := d.Client.GetWorkspaceClient()

Check failure on line 86 in internal/providers/pluginfw/resources/user/data_users.go

View workflow job for this annotation

GitHub Actions / tests

w declared and not used (compile)
if AppendDiagAndCheckErrors(resp, diags) {
return
}
// TODO: Add retreival of iterator at the workspace level.
}
// TODO: Continue setting the datasource.
}

0 comments on commit 7fa5823

Please sign in to comment.