@@ -16,7 +16,10 @@ import (
16
16
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
17
17
)
18
18
19
- const resourceName = "cloud_user_project_assignment"
19
+ const (
20
+ resourceName = "cloud_user_project_assignment"
21
+ errorReadingUser = "Error retrieving project users"
22
+ )
20
23
21
24
var _ resource.ResourceWithConfigure = & rs {}
22
25
var _ resource.ResourceWithImportState = & rs {}
@@ -67,44 +70,62 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou
67
70
resp .Diagnostics .Append (resp .State .Set (ctx , newCloudUserProjectAssignmentModel )... )
68
71
}
69
72
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 ) {
79
74
var userResp * admin.GroupUserResponse
80
75
var httpResp * http.Response
81
76
var err error
82
-
83
- if ! state .UserId .IsNull () && state .UserId .ValueString () != "" {
84
- userID := state .UserId .ValueString ()
77
+ if userID != "" {
85
78
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
89
84
}
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
92
87
params := & admin.ListProjectUsersApiParams {
93
88
GroupId : projectID ,
94
89
Username : & username ,
95
90
}
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
101
95
}
102
- userResp = & usersResp .GetResults ()[0 ]
96
+ return nil , err
97
+ }
98
+ if userListResp == nil || len (userListResp .GetResults ()) == 0 {
99
+ return nil , nil
103
100
}
101
+ userResp = & userListResp .GetResults ()[0 ]
104
102
}
105
103
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 )
106
123
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 )
108
129
return
109
130
}
110
131
0 commit comments