Skip to content

feat: Adds description field to mongodbatlas_database_user #3280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 21, 2025

Conversation

EspenAlbert
Copy link
Collaborator

@EspenAlbert EspenAlbert commented Apr 14, 2025

Description

Adds description field to mongodbatlas_database_user

Link to any related issue(s): CLOUDP-288713

Considerations

  • Choosing optional only attribute
    • Implies extra logic to align config value with API value since null != "" in TPF. (Similar to recent resource_policy implementation)
    • Also, there is no way of setting the description to null in the SDK. Once it is set, it will always be "" since we cannot dump null (to be discussed based on this example.)
  • Will handle test refactoring in a follow-up issue: https://jira.mongodb.org/browse/CLOUDP-312885
  • Choosing to keep model_database_user_test.go simple, all paths are covered in acceptance tests.
    • state null plan null
    • state some value plan null
    • state null plan not-null
    • state set and plan set

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR. A migration guide must be created or updated if the new feature will go in a major version.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR. A migration guide must be created or updated.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contributing guides
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals I have added appropriate changelog entries.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

Further comments

@EspenAlbert EspenAlbert marked this pull request as ready for review April 16, 2025 09:41
@EspenAlbert EspenAlbert requested review from a team as code owners April 16, 2025 09:41
Copy link
Contributor

APIx bot: a message has been sent to Docs Slack channel

result.Password = dbUserModel.Password.ValueStringPointer()
result.Password = plan.Password.ValueStringPointer()
}
if plan.Description.IsNull() && !stateDescriptionValue.Equal(plan.Description) {
Copy link
Member

Choose a reason for hiding this comment

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

just to confirm, what happens if we don't have this condition?

wouldn't result.Description be "" in this case because of previous: Description: plan.Description.ValueStringPointer(), ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, ValueStringPointer would be nil and therefore not dumped.
Cannot use ValueString() since we don't want to set it to "" when it is not defined.

Copy link
Member

Choose a reason for hiding this comment

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

thanks

@@ -204,28 +208,28 @@ func (r *databaseUserRS) Schema(ctx context.Context, req resource.SchemaRequest,
}

func (r *databaseUserRS) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: maybe in a follow-up PR we want to unify RS and DS tests in the same test file and tests

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See PR description

Copy link
Member

Choose a reason for hiding this comment

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

thanks

resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

dbUserReq, d := NewMongoDBDatabaseUser(ctx, types.StringNull(), databaseUserPlan)
dbUserReq, d := NewMongoDBDatabaseUser(ctx, types.StringNull(), types.StringNull(), plan)
Copy link
Member

Choose a reason for hiding this comment

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

nit: use diags or localDiags as in other places

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks. Changed in 27168d9


// Use the ID only with the IMPORT operation
if databaseUserState.ID.ValueString() != "" && (username == "" || projectID == "" || authDatabaseName == "") {
projectID, username, authDatabaseName, err = SplitDatabaseUserImportID(databaseUserState.ID.ValueString())
if state.ID.ValueString() != "" && (username == "" || projectID == "" || authDatabaseName == "") {
Copy link
Member

Choose a reason for hiding this comment

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

I understand this change is not related to this PR, but wondering why we have a different Read logic when importing, instead of Import setting the attributes we need in Read so we don't need to do a special case in Read for import

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Wow, yeah you are right. Moved to Import function in e860fa9

@@ -65,6 +65,7 @@ var (
GroupId: projectID,
DatabaseName: authDatabaseName,
Username: username,
Description: conversion.Pointer(""),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would add a var emptyDescription = "" and then here use &emptyDescription

Copy link
Collaborator Author

@EspenAlbert EspenAlbert Apr 16, 2025

Choose a reason for hiding this comment

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

Not sure, I feel this is more readable. With an intermediate var you would need to check that var. Here it is all in one line

Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO it's more readable with the var but I don't have a strong opinion

resource.TestCheckResourceAttr(resourceName, "password", "test-acc-password"),
resource.TestCheckResourceAttr(resourceName, "auth_database_name", "admin"),
resource.TestCheckResourceAttr(resourceName, "labels.#", "2"),
),
},
{
Config: acc.ConfigDatabaseUserWithLabels(projectID, username, "read",
Config: acc.ConfigDatabaseUserWithLabels(projectID, username, "read", "",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice test!

Copy link
Collaborator

@oarbusi oarbusi left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@AgustinBettati AgustinBettati left a comment

Choose a reason for hiding this comment

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

one doubt otherwise LGTM

Copy link
Member

@AgustinBettati AgustinBettati left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 101 to 105
if inModel != nil && outModel.Description.Equal(types.StringValue("")) && inModel.Description.IsNull() {
// null != "" in TPF: Error: Provider produced inconsistent result after apply. .description: was null, but now cty.StringVal("")
databaseUserModel.Description = model.Description
outModel.Description = types.StringNull()
}

return databaseUserModel, nil
return outModel, nil
Copy link
Member

Choose a reason for hiding this comment

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

nice way of keeping it readable 👍

Copy link
Contributor

@kanchana-mongodb kanchana-mongodb left a comment

Choose a reason for hiding this comment

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

LGTM

@EspenAlbert EspenAlbert merged commit f7d100e into master Apr 21, 2025
40 checks passed
@EspenAlbert EspenAlbert deleted the CLOUDP-288713_db_user_description branch April 21, 2025 14:36
svc-apix-Bot added a commit that referenced this pull request Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants