Skip to content

Conversation

@gastonfournier
Copy link
Collaborator

@gastonfournier gastonfournier commented Oct 21, 2025

About the changes

Adds the ability to search for a user by email when using the user data source. This simplifies referring to a user when their ID is unknown.

Fixes #253

@gastonfournier gastonfournier self-assigned this Oct 21, 2025
@FredrikOseberg FredrikOseberg moved this from New to In Progress in Issues and PRs Oct 22, 2025
Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

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

I think it's okay. Could benefit from a little clean up before merging but seems correct at glance

return
}
createAPITokenRequest.ExpiresAt = &expire
createAPITokenRequest.SetExpiresAt(expire)
Copy link
Member

Choose a reason for hiding this comment

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

What happened here? Did our API change? Is this now optional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

v0.6.0 of the generated API client provides setter helpers for optional fields like expiresAt, and using the helper preserves the behaviour of the previous pointer assignment: Unleash/unleash-server-api-go@87392b0#diff-4f4e55d02e361836eb5485af7002e491e934b9742cfea3df8bcce513bf3c81d9R87-R89

}
user = apiUser

if emailProvided {
Copy link
Member

Choose a reason for hiding this comment

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

Struggling a bit with the nesting here. Not sure but I think flattening this if statement may help to make this monster a bit more readble. Tempted to say the conditions for the if statement can just be a single well named function because I also can't spot at a glance what this case represents

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

f9f3dee should flatten this a bit :D


// Read refreshes the Terraform state with the latest data.
func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Debug(ctx, "Preparing to read user data source")
Copy link
Member

Choose a reason for hiding this comment

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

This could really benefit from being broken up into smaller functions, this beast is over a hundred lines long now. At a quick glance I see 3 responsibilities here:

  1. An initial quick check of the config to check for an early exit
  2. Fetching a user by whatever property
  3. Converting that data into a terraform response

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agree: 435f130 should make it more readable

idProvided := !config.Id.IsNull() && config.Id.ValueString() != ""
emailProvided := !config.Email.IsNull() && config.Email.ValueString() != ""

if !idProvided && !emailProvided {
Copy link
Member

Choose a reason for hiding this comment

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

It's a shame to do this check at runtime, a config validator might let us push this to plan time. Something like this?

func (d *userDataSource) ConfigValidators(_ context.Context) []datasource.ConfigValidator {
	return []datasource.ConfigValidator{
		datasourcevalidator.AtLeastOneOf(
			path.MatchRoot("id"),
			path.MatchRoot("email"),
		),
	}
}

No idea if this will work on our version of tf, it's based off a quick search

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Interesting! Let me dig into it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, this requires terraform-plugin-framework-validators dependency... I'm validating the approach

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@sighphyre wdyt of this: #265

Copy link
Member

Choose a reason for hiding this comment

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

Yaaas queen

@github-project-automation github-project-automation bot moved this from In Progress to Approved PRs in Issues and PRs Oct 22, 2025
@gastonfournier gastonfournier merged commit 38fa522 into main Oct 23, 2025
27 checks passed
@gastonfournier gastonfournier deleted the fetch-user-by-email branch October 23, 2025 09:05
@github-project-automation github-project-automation bot moved this from Approved PRs to Done in Issues and PRs Oct 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Feat: load unleash_user datasource by email

2 participants