-
Notifications
You must be signed in to change notification settings - Fork 205
feat: Add new resource mongodbatlas_cloud_user_project_assignment #3568
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
Open
csanx
wants to merge
30
commits into
CLOUDP-320243-dev-2.0.0
Choose a base branch
from
CLOUDP-333176
base: CLOUDP-320243-dev-2.0.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,085
−0
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
84513be
WIP- Created resource
cb5b312
Added model tests
b92ebf9
Added tests
2f6bfee
Fixed import
bc7fe7b
Typo
8d3db26
Merge branch 'CLOUDP-320243-dev-2.0.0' into CLOUDP-333176
7271d8a
Merge branch 'CLOUDP-320243-dev-2.0.0' into CLOUDP-333176
9601faa
Preview new SDK version
fdd1ded
WIP - Migration test
b42949c
WIP - migration test
f8d2d0a
Updated SDK
ece5d8d
Revert "Updated SDK"
c7029fc
Merge branch 'CLOUDP-320243-dev-2.0.0' of github.com:mongodb/terrafor…
ece229a
Merge branch 'CLOUDP-320243-dev-2.0.0' into CLOUDP-333176
8690f0e
Updated SDK
fa00932
Fixed migration test
21b815a
Changelog
4e3f64a
Merge branch 'CLOUDP-320243-dev-2.0.0' of github.com:mongodb/terrafor…
0e54c53
Merge branch 'CLOUDP-320243-dev-2.0.0' into CLOUDP-333176
072a4b2
Merge branch 'CLOUDP-320243-dev-2.0.0' into CLOUDP-333176
b396420
CheckDestroy
aa22dc6
Refactor
d2d1136
typo
542fa5e
Fix
c48e857
Added TestNewAtlasUpdateReq
59950ba
Empty/nil test cases
e011813
Fix
12bbfa7
Added active user to test
866306d
Added MONGODB_ATLAS_USERNAME_2
a2ea83a
Changed Update to read from API
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
resource/mongodbatlas_cloud_user_project_assignment | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package clouduserprojectassignment_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
cleanup := acc.SetupSharedResources() | ||
exitCode := m.Run() | ||
cleanup() | ||
os.Exit(exitCode) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package clouduserprojectassignment | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" | ||
|
||
"go.mongodb.org/atlas-sdk/v20250312006/admin" | ||
) | ||
|
||
func NewTFModel(ctx context.Context, projectID string, apiResp *admin.GroupUserResponse) (*TFModel, diag.Diagnostics) { | ||
diags := diag.Diagnostics{} | ||
|
||
if apiResp == nil { | ||
return nil, diags | ||
} | ||
|
||
roles := conversion.TFSetValueOrNull(ctx, &apiResp.Roles, types.StringType) | ||
|
||
return &TFModel{ | ||
Country: types.StringPointerValue(apiResp.Country), | ||
CreatedAt: types.StringPointerValue(conversion.TimePtrToStringPtr(apiResp.CreatedAt)), | ||
FirstName: types.StringPointerValue(apiResp.FirstName), | ||
ProjectId: types.StringValue(projectID), | ||
UserId: types.StringValue(apiResp.GetId()), | ||
InvitationCreatedAt: types.StringPointerValue(conversion.TimePtrToStringPtr(apiResp.InvitationCreatedAt)), | ||
InvitationExpiresAt: types.StringPointerValue(conversion.TimePtrToStringPtr(apiResp.InvitationExpiresAt)), | ||
InviterUsername: types.StringPointerValue(apiResp.InviterUsername), | ||
LastAuth: types.StringPointerValue(conversion.TimePtrToStringPtr(apiResp.LastAuth)), | ||
LastName: types.StringPointerValue(apiResp.LastName), | ||
MobileNumber: types.StringPointerValue(apiResp.MobileNumber), | ||
OrgMembershipStatus: types.StringValue(apiResp.GetOrgMembershipStatus()), | ||
Roles: roles, | ||
Username: types.StringValue(apiResp.GetUsername()), | ||
}, diags | ||
} | ||
|
||
func NewProjectUserReq(ctx context.Context, plan *TFModel) (*admin.GroupUserRequest, diag.Diagnostics) { | ||
roleNames := []string{} | ||
if !plan.Roles.IsNull() && !plan.Roles.IsUnknown() { | ||
roleNames = conversion.TypesSetToString(ctx, plan.Roles) | ||
} | ||
|
||
addProjectUserReq := admin.GroupUserRequest{ | ||
Username: plan.Username.ValueString(), | ||
Roles: roleNames, | ||
} | ||
return &addProjectUserReq, nil | ||
} | ||
|
||
func NewAtlasUpdateReq(ctx context.Context, plan *TFModel, currentRoles []string) (addRequests, removeRequests []*admin.AddOrRemoveGroupRole, diags diag.Diagnostics) { | ||
var desiredRoles []string | ||
if !plan.Roles.IsNull() && !plan.Roles.IsUnknown() { | ||
desiredRoles = conversion.TypesSetToString(ctx, plan.Roles) | ||
} | ||
|
||
rolesToAdd, rolesToRemove := diffRoles(currentRoles, desiredRoles) | ||
|
||
addRequests = make([]*admin.AddOrRemoveGroupRole, 0, len(rolesToAdd)) | ||
for _, role := range rolesToAdd { | ||
addRequests = append(addRequests, &admin.AddOrRemoveGroupRole{ | ||
GroupRole: role, | ||
}) | ||
} | ||
|
||
removeRequests = make([]*admin.AddOrRemoveGroupRole, 0, len(rolesToRemove)) | ||
for _, role := range rolesToRemove { | ||
removeRequests = append(removeRequests, &admin.AddOrRemoveGroupRole{ | ||
GroupRole: role, | ||
}) | ||
} | ||
|
||
return addRequests, removeRequests, nil | ||
} | ||
|
||
func diffRoles(oldRoles, newRoles []string) (toAdd, toRemove []string) { | ||
oldRolesMap := make(map[string]bool, len(oldRoles)) | ||
|
||
for _, role := range oldRoles { | ||
oldRolesMap[role] = true | ||
} | ||
|
||
for _, role := range newRoles { | ||
if oldRolesMap[role] { | ||
delete(oldRolesMap, role) | ||
} else { | ||
toAdd = append(toAdd, role) | ||
} | ||
} | ||
|
||
for role := range oldRolesMap { | ||
toRemove = append(toRemove, role) | ||
} | ||
|
||
return toAdd, toRemove | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not urgent, can be done in a follow-up ticket - but this method should have its unit test, or you can have a test for
NewAtlasUpdateReq
that checks it always generates the right request based on diffThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test for
NewAtlasUpdateReq