-
Notifications
You must be signed in to change notification settings - Fork 208
feat: Adds new mongodbatlas_cloud_user_team_assignment
resource
#3502
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
Changes from 21 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
91192c2
Extracted users attribute common schema to dsschema/users_schema.go
83bef3a
Added attribute users to team data source
b486498
Modified tests
ac113ed
Modified doc
ae5fe56
Added changelog
25b4b63
Created ressource, schema and models
138c010
WIP - Implementing Create method
9c29d84
Merge branch 'CLOUDP-320243-dev-2.0.0' into CLOUDP-329984
776b107
WIP - Added markdown description for attributes in schema and fixed m…
4c518ef
Fixed variables names
0865818
WIP - Implement Read, Update, Delete, Import
6329b76
Fixed model attributes names.
13ad925
Minor change
78464a5
Added tests
bb0aaae
Fix
11b6f56
Add github
005b0c8
Fix
dd638e6
Merge branch 'CLOUDP-320243-dev-2.0.0' into CLOUDP-329984
a810a32
Fix
10a3c1f
Changelog
a54e77c
Fix
389c42a
Fixed function naming
d1dd176
Merge branch 'CLOUDP-320243-dev-2.0.0' into CLOUDP-329984
7c3c11f
Changed test
7ebd8d2
Changed `project_role_assignments` from List to Set
3d467d0
Changed the attribute name `project_roles_assignments` to project_rol…
73b3bfa
Merge branch 'CLOUDP-320243-dev-2.0.0' into CLOUDP-329984
eba734d
minor changes
9519315
Fix
adf5af5
Fix
bfd3817
Fix
057f72c
Changed `project_role_assignments` from List to Set
57c73b3
Removed redundant check
503bbfc
Code improvements
a36d888
feat: Adds new singular data source `mongodbatlas_cloud_user_team_ass…
csanx 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_team_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 clouduserteamassignment_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,109 @@ | ||
package clouduserteamassignment | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"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/v20250312005/admin" | ||
) | ||
|
||
func NewTFUserTeamAssignmentModel(ctx context.Context, apiResp *admin.OrgUserResponse) (*TFUserTeamAssignmentModel, diag.Diagnostics) { | ||
diags := diag.Diagnostics{} | ||
var rolesObj types.Object | ||
var rolesDiags diag.Diagnostics | ||
|
||
if apiResp == nil { | ||
return nil, diags | ||
} | ||
|
||
rolesObj, rolesDiags = NewTFRolesModel(ctx, &apiResp.Roles) | ||
AgustinBettati marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
diags.Append(rolesDiags...) | ||
|
||
userTeamAssignment := TFUserTeamAssignmentModel{ | ||
UserId: types.StringValue(apiResp.GetId()), | ||
Username: types.StringValue(apiResp.GetUsername()), | ||
OrgMembershipStatus: types.StringValue(apiResp.GetOrgMembershipStatus()), | ||
Roles: rolesObj, | ||
InvitationCreatedAt: types.StringPointerValue(conversion.TimePtrToStringPtr(apiResp.InvitationCreatedAt)), | ||
InvitationExpiresAt: types.StringPointerValue(conversion.TimePtrToStringPtr(apiResp.InvitationExpiresAt)), | ||
InviterUsername: types.StringPointerValue(apiResp.InviterUsername), | ||
Country: types.StringPointerValue(apiResp.Country), | ||
FirstName: types.StringPointerValue(apiResp.FirstName), | ||
LastName: types.StringPointerValue(apiResp.LastName), | ||
CreatedAt: types.StringPointerValue(conversion.TimePtrToStringPtr(apiResp.CreatedAt)), | ||
LastAuth: types.StringPointerValue(conversion.TimePtrToStringPtr(apiResp.LastAuth)), | ||
MobileNumber: types.StringPointerValue(apiResp.MobileNumber), | ||
} | ||
|
||
userTeamAssignment.TeamIDs = types.SetNull(types.StringType) | ||
if apiResp.TeamIds != nil { | ||
userTeamAssignment.TeamIDs, diags = types.SetValueFrom(ctx, types.StringType, apiResp.TeamIds) | ||
if diags.HasError() { | ||
return nil, diags | ||
} | ||
} | ||
|
||
return &userTeamAssignment, nil | ||
} | ||
|
||
func NewTFRolesModel(ctx context.Context, roles *admin.OrgUserRolesResponse) (types.Object, diag.Diagnostics) { | ||
diags := diag.Diagnostics{} | ||
|
||
if roles == nil { | ||
return types.ObjectNull(RolesObjectAttrTypes), diags | ||
} | ||
|
||
var orgRoles types.Set | ||
if roles.OrgRoles == nil || len(*roles.OrgRoles) == 0 { | ||
orgRoles = types.SetNull(types.StringType) | ||
} else { | ||
orgRoles, _ = types.SetValueFrom(ctx, types.StringType, *roles.OrgRoles) | ||
} | ||
|
||
projectRoleAssignmentsList := NewTFProjectRoleAssignments(ctx, roles.GroupRoleAssignments) | ||
|
||
rolesObj, _ := types.ObjectValue( | ||
RolesObjectAttrTypes, | ||
map[string]attr.Value{ | ||
"project_role_assignments": projectRoleAssignmentsList, | ||
"org_roles": orgRoles, | ||
}, | ||
) | ||
|
||
return rolesObj, diags | ||
} | ||
|
||
func NewTFProjectRoleAssignments(ctx context.Context, groupRoleAssignments *[]admin.GroupRoleAssignment) types.List { | ||
if groupRoleAssignments == nil { | ||
return types.ListNull(ProjectRoleAssignmentsAttrType) | ||
} | ||
|
||
var projectRoleAssignments []TFProjectRoleAssignmentsModel | ||
|
||
for _, pra := range *groupRoleAssignments { | ||
projectID := types.StringPointerValue(pra.GroupId) | ||
var projectRoles types.Set | ||
if pra.GroupRoles == nil || len(*pra.GroupRoles) == 0 { | ||
projectRoles = types.SetNull(types.StringType) | ||
} else { | ||
projectRoles, _ = types.SetValueFrom(ctx, types.StringType, pra.GroupRoles) | ||
} | ||
projectRoleAssignments = append(projectRoleAssignments, TFProjectRoleAssignmentsModel{ | ||
ProjectId: projectID, | ||
ProjectRoles: projectRoles, | ||
}) | ||
} | ||
|
||
praList, _ := types.ListValueFrom(ctx, ProjectRoleAssignmentsAttrType.ElemType.(types.ObjectType), projectRoleAssignments) | ||
return praList | ||
} | ||
|
||
func NewUserTeamAssignmentReq(ctx context.Context, plan *TFUserTeamAssignmentModel) (*admin.AddOrRemoveUserFromTeam, diag.Diagnostics) { | ||
addOrRemoveUserFromTeam := admin.AddOrRemoveUserFromTeam{ | ||
Id: plan.UserId.ValueString(), | ||
} | ||
return &addOrRemoveUserFromTeam, nil | ||
} |
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,186 @@ | ||
package clouduserteamassignment_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/clouduserteamassignment" | ||
"github.com/stretchr/testify/assert" | ||
"go.mongodb.org/atlas-sdk/v20250312005/admin" | ||
) | ||
|
||
const ( | ||
testUserID = "user-123" | ||
testUsername = "jdoe" | ||
testFirstName = "John" | ||
testLastName = "Doe" | ||
testCountry = "CA" | ||
testMobile = "+1555123456" | ||
testInviter = "admin" | ||
testOrgMembershipStatus = "ACTIVE" | ||
testInviterUsername = "" | ||
|
||
testOrgRoleOwner = "ORG_OWNER" | ||
testOrgRoleMember = "ORG_MEMBER" | ||
testProjectRoleOwner = "PROJECT_OWNER" | ||
testProjectRoleRead = "PROJECT_READ_ONLY" | ||
testProjectRoleMember = "PROJECT_MEMBER" | ||
|
||
testTeamID1 = "team1" | ||
testTeamID2 = "team2" | ||
testProjectID1 = "project-123" | ||
testOrgID = "org-123" | ||
) | ||
|
||
var ( | ||
when = time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC) | ||
testCreatedAt = when.Format(time.RFC3339) | ||
testInvitationCreatedAt = when.Add(-24 * time.Hour).Format(time.RFC3339) | ||
testInvitationExpiresAt = when.Add(24 * time.Hour).Format(time.RFC3339) | ||
testLastAuth = when.Add(-2 * time.Hour).Format(time.RFC3339) | ||
|
||
testTeamIDs = []string{"team1", "team2"} | ||
testOrgRoles = []string{"owner", "readWrite"} | ||
) | ||
|
||
type sdkToTFModelTestCase struct { | ||
SDKResp *admin.OrgUserResponse | ||
expectedTFModel *clouduserteamassignment.TFUserTeamAssignmentModel | ||
} | ||
|
||
func TestUserTeamAssignmentSDKToTFModel(t *testing.T) { | ||
ctx := t.Context() | ||
|
||
fullResp := &admin.OrgUserResponse{ | ||
Id: testUserID, | ||
Username: testUsername, | ||
FirstName: admin.PtrString(testFirstName), | ||
LastName: admin.PtrString(testLastName), | ||
Country: admin.PtrString(testCountry), | ||
MobileNumber: admin.PtrString(testMobile), | ||
OrgMembershipStatus: testOrgMembershipStatus, | ||
CreatedAt: admin.PtrTime(when), | ||
LastAuth: admin.PtrTime(when.Add(-2 * time.Hour)), | ||
InvitationCreatedAt: admin.PtrTime(when.Add(-24 * time.Hour)), | ||
InvitationExpiresAt: admin.PtrTime(when.Add(24 * time.Hour)), | ||
InviterUsername: admin.PtrString(testInviterUsername), | ||
TeamIds: &testTeamIDs, | ||
Roles: admin.OrgUserRolesResponse{ | ||
OrgRoles: &testOrgRoles, | ||
}, | ||
} | ||
|
||
orgRolesSet, _ := types.SetValueFrom(ctx, types.StringType, testOrgRoles) | ||
expectedRoles, _ := types.ObjectValue(clouduserteamassignment.RolesObjectAttrTypes, map[string]attr.Value{ | ||
"org_roles": orgRolesSet, | ||
"project_role_assignments": types.ListNull(clouduserteamassignment.ProjectRoleAssignmentsAttrType), | ||
}) | ||
expectedTeams, _ := types.SetValueFrom(ctx, types.StringType, testTeamIDs) | ||
expectedFullModel := &clouduserteamassignment.TFUserTeamAssignmentModel{ | ||
UserId: types.StringValue(testUserID), | ||
Username: types.StringValue(testUsername), | ||
FirstName: types.StringValue(testFirstName), | ||
LastName: types.StringValue(testLastName), | ||
Country: types.StringValue(testCountry), | ||
MobileNumber: types.StringValue(testMobile), | ||
OrgMembershipStatus: types.StringValue(testOrgMembershipStatus), | ||
CreatedAt: types.StringValue(testCreatedAt), | ||
LastAuth: types.StringValue(testLastAuth), | ||
InvitationCreatedAt: types.StringValue(testInvitationCreatedAt), | ||
InvitationExpiresAt: types.StringValue(testInvitationExpiresAt), | ||
InviterUsername: types.StringValue(testInviterUsername), | ||
OrgId: types.StringNull(), | ||
TeamId: types.StringNull(), | ||
Roles: expectedRoles, | ||
TeamIDs: expectedTeams, | ||
} | ||
|
||
testCases := map[string]sdkToTFModelTestCase{ | ||
"nil SDK response": { | ||
SDKResp: nil, | ||
expectedTFModel: nil, | ||
}, | ||
"Complete SDK response": { | ||
SDKResp: fullResp, | ||
expectedTFModel: expectedFullModel, | ||
}, | ||
} | ||
|
||
for testName, tc := range testCases { | ||
t.Run(testName, func(t *testing.T) { | ||
resultModel, diags := clouduserteamassignment.NewTFUserTeamAssignmentModel(t.Context(), tc.SDKResp) | ||
assert.False(t, diags.HasError(), "expected no diagnostics") | ||
assert.Equal(t, tc.expectedTFModel, resultModel, "created terraform model did not match expected output") | ||
}) | ||
} | ||
} | ||
|
||
func createRolesObject(ctx context.Context, orgRoles []string, projectAssignments []clouduserteamassignment.TFProjectRoleAssignmentsModel) types.Object { | ||
orgRolesSet, _ := types.SetValueFrom(ctx, types.StringType, orgRoles) | ||
|
||
var projectRoleAssignmentsList types.List | ||
if len(projectAssignments) == 0 { | ||
projectRoleAssignmentsList = types.ListNull(clouduserteamassignment.ProjectRoleAssignmentsAttrType.ElemType.(types.ObjectType)) | ||
} else { | ||
projectRoleAssignmentsList, _ = types.ListValueFrom(ctx, clouduserteamassignment.ProjectRoleAssignmentsAttrType.ElemType.(types.ObjectType), projectAssignments) | ||
} | ||
|
||
obj, _ := types.ObjectValue( | ||
clouduserteamassignment.RolesObjectAttrTypes, | ||
map[string]attr.Value{ | ||
"org_roles": orgRolesSet, | ||
"project_role_assignments": projectRoleAssignmentsList, | ||
}, | ||
) | ||
return obj | ||
} | ||
|
||
func TestNewUserTeamAssignmentReq(t *testing.T) { | ||
ctx := t.Context() | ||
projectAssignment := clouduserteamassignment.TFProjectRoleAssignmentsModel{ | ||
ProjectId: types.StringValue(testProjectID1), | ||
ProjectRoles: types.SetValueMust(types.StringType, []attr.Value{types.StringValue(testProjectRoleOwner)}), | ||
} | ||
teams, _ := types.SetValueFrom(ctx, types.StringType, testTeamIDs) | ||
testCases := map[string]struct { | ||
plan *clouduserteamassignment.TFUserTeamAssignmentModel | ||
expected *admin.AddOrRemoveUserFromTeam | ||
}{ | ||
"Complete model": { | ||
plan: &clouduserteamassignment.TFUserTeamAssignmentModel{ | ||
OrgId: types.StringValue(testOrgID), | ||
TeamId: types.StringValue(testTeamID1), | ||
UserId: types.StringValue(testUserID), | ||
Username: types.StringValue(testUsername), | ||
OrgMembershipStatus: types.StringValue(testOrgMembershipStatus), | ||
Roles: createRolesObject(ctx, testOrgRoles, []clouduserteamassignment.TFProjectRoleAssignmentsModel{ | ||
projectAssignment, | ||
}), | ||
TeamIDs: teams, | ||
InvitationCreatedAt: types.StringValue(testInvitationCreatedAt), | ||
InvitationExpiresAt: types.StringValue(testInvitationExpiresAt), | ||
InviterUsername: types.StringValue(testInviterUsername), | ||
Country: types.StringValue(testCountry), | ||
FirstName: types.StringValue(testFirstName), | ||
LastName: types.StringValue(testLastName), | ||
CreatedAt: types.StringValue(testCreatedAt), | ||
LastAuth: types.StringValue(testLastAuth), | ||
MobileNumber: types.StringValue(testMobile), | ||
}, | ||
expected: &admin.AddOrRemoveUserFromTeam{ | ||
Id: testUserID, | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
req, diags := clouduserteamassignment.NewUserTeamAssignmentReq(ctx, tc.plan) | ||
assert.False(t, diags.HasError(), "expected no diagnostics") | ||
assert.Equal(t, tc.expected, req) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.