Skip to content

Commit aa22dc6

Browse files
author
Cristina Sánchez Sánchez
committed
Refactor
1 parent b396420 commit aa22dc6

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

internal/service/clouduserprojectassignment/resource.go

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import (
1616
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1717
)
1818

19-
const resourceName = "cloud_user_project_assignment"
19+
const (
20+
resourceName = "cloud_user_project_assignment"
21+
errorReadingUser = "Error retrieving project users"
22+
)
2023

2124
var _ resource.ResourceWithConfigure = &rs{}
2225
var _ resource.ResourceWithImportState = &rs{}
@@ -67,44 +70,62 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou
6770
resp.Diagnostics.Append(resp.State.Set(ctx, newCloudUserProjectAssignmentModel)...)
6871
}
6972

70-
func (r *rs) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
71-
var state TFModel
72-
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
73-
if resp.Diagnostics.HasError() {
74-
return
75-
}
76-
77-
connV2 := r.Client.AtlasV2
78-
projectID := state.ProjectId.ValueString()
73+
func fetchTeamUser(ctx context.Context, connV2 *admin.APIClient, projectID, userID, username string) (*admin.GroupUserResponse, error) {
7974
var userResp *admin.GroupUserResponse
8075
var httpResp *http.Response
8176
var err error
82-
83-
if !state.UserId.IsNull() && state.UserId.ValueString() != "" {
84-
userID := state.UserId.ValueString()
77+
if userID != "" {
8578
userResp, httpResp, err = connV2.MongoDBCloudUsersApi.GetProjectUser(ctx, projectID, userID).Execute()
86-
if validate.StatusNotFound(httpResp) {
87-
resp.State.RemoveResource(ctx)
88-
return
79+
if err != nil {
80+
if validate.StatusNotFound(httpResp) {
81+
return nil, nil
82+
}
83+
return nil, err
8984
}
90-
} else if !state.Username.IsNull() && state.Username.ValueString() != "" { // required for import
91-
username := state.Username.ValueString()
85+
} else if username != "" {
86+
var userListResp *admin.PaginatedGroupUser
9287
params := &admin.ListProjectUsersApiParams{
9388
GroupId: projectID,
9489
Username: &username,
9590
}
96-
usersResp, _, err := connV2.MongoDBCloudUsersApi.ListProjectUsersWithParams(ctx, params).Execute()
97-
if err == nil && usersResp != nil {
98-
if len(usersResp.GetResults()) == 0 {
99-
resp.State.RemoveResource(ctx)
100-
return
91+
userListResp, httpResp, err = connV2.MongoDBCloudUsersApi.ListProjectUsersWithParams(ctx, params).Execute()
92+
if err != nil {
93+
if validate.StatusNotFound(httpResp) {
94+
return nil, nil
10195
}
102-
userResp = &usersResp.GetResults()[0]
96+
return nil, err
97+
}
98+
if userListResp == nil || len(userListResp.GetResults()) == 0 {
99+
return nil, nil
103100
}
101+
userResp = &userListResp.GetResults()[0]
104102
}
105103

104+
return userResp, nil
105+
}
106+
107+
func (r *rs) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
108+
var state TFModel
109+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
110+
if resp.Diagnostics.HasError() {
111+
return
112+
}
113+
114+
connV2 := r.Client.AtlasV2
115+
projectID := state.ProjectId.ValueString()
116+
var userResp *admin.GroupUserResponse
117+
var err error
118+
119+
userID := state.UserId.ValueString()
120+
username := state.Username.ValueString()
121+
122+
userResp, err = fetchTeamUser(ctx, connV2, projectID, userID, username)
106123
if err != nil {
107-
resp.Diagnostics.AddError(fmt.Sprintf("error fetching user(%s) from ProjectID(%s):", userResp.Username, projectID), err.Error())
124+
resp.Diagnostics.AddError(errorReadingUser, err.Error())
125+
return
126+
}
127+
if userResp == nil {
128+
resp.State.RemoveResource(ctx)
108129
return
109130
}
110131

0 commit comments

Comments
 (0)