@@ -2,14 +2,13 @@ package mongodbatlas
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"fmt"
7
6
"regexp"
7
+ "strings"
8
8
9
9
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11
-
12
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13
12
matlas "go.mongodb.org/atlas/mongodbatlas"
14
13
)
15
14
@@ -85,45 +84,48 @@ func resourceMongoDBAtlasOrgInvitationRead(ctx context.Context, d *schema.Resour
85
84
if err != nil {
86
85
// case 404
87
86
// deleted in the backend case
88
- var target * matlas.ErrorResponse
89
- if errors .As (err , & target ) && target .ErrorCode == "NOT_FOUND" {
90
- d .SetId ("" )
87
+
88
+ if strings .Contains (err .Error (), "404" ) {
89
+ accepted , _ := validateOrgInvitationAlreadyAccepted (ctx , meta .(* MongoDBClient ), username , orgID )
90
+ if ! accepted {
91
+ d .SetId ("" )
92
+ }
91
93
return nil
92
94
}
93
95
94
- return diag .FromErr ( fmt . Errorf ("error getting Organization Invitation information: %w " , err ) )
96
+ return diag .Errorf ("error getting Organization Invitation information: %s " , err )
95
97
}
96
98
97
99
if err := d .Set ("username" , orgInvitation .Username ); err != nil {
98
- return diag .FromErr ( fmt . Errorf ("error getting `username` for Organization Invitation (%s): %w " , d .Id (), err ) )
100
+ return diag .Errorf ("error getting `username` for Organization Invitation (%s): %s " , d .Id (), err )
99
101
}
100
102
101
103
if err := d .Set ("org_id" , orgInvitation .OrgID ); err != nil {
102
- return diag .FromErr ( fmt . Errorf ("error getting `username` for Organization Invitation (%s): %w " , d .Id (), err ) )
104
+ return diag .Errorf ("error getting `username` for Organization Invitation (%s): %s " , d .Id (), err )
103
105
}
104
106
105
107
if err := d .Set ("invitation_id" , orgInvitation .ID ); err != nil {
106
- return diag .FromErr ( fmt . Errorf ("error getting `invitation_id` for Organization Invitation (%s): %w " , d .Id (), err ) )
108
+ return diag .Errorf ("error getting `invitation_id` for Organization Invitation (%s): %s " , d .Id (), err )
107
109
}
108
110
109
111
if err := d .Set ("expires_at" , orgInvitation .ExpiresAt ); err != nil {
110
- return diag .FromErr ( fmt . Errorf ("error getting `expires_at` for Organization Invitation (%s): %w " , d .Id (), err ) )
112
+ return diag .Errorf ("error getting `expires_at` for Organization Invitation (%s): %s " , d .Id (), err )
111
113
}
112
114
113
115
if err := d .Set ("created_at" , orgInvitation .CreatedAt ); err != nil {
114
- return diag .FromErr ( fmt . Errorf ("error getting `created_at` for Organization Invitation (%s): %w " , d .Id (), err ) )
116
+ return diag .Errorf ("error getting `created_at` for Organization Invitation (%s): %s " , d .Id (), err )
115
117
}
116
118
117
119
if err := d .Set ("inviter_username" , orgInvitation .InviterUsername ); err != nil {
118
- return diag .FromErr ( fmt . Errorf ("error getting `inviter_username` for Organization Invitation (%s): %w " , d .Id (), err ) )
120
+ return diag .Errorf ("error getting `inviter_username` for Organization Invitation (%s): %s " , d .Id (), err )
119
121
}
120
122
121
123
if err := d .Set ("teams_ids" , orgInvitation .TeamIDs ); err != nil {
122
- return diag .FromErr ( fmt . Errorf ("error getting `teams_ids` for Organization Invitation (%s): %w " , d .Id (), err ) )
124
+ return diag .Errorf ("error getting `teams_ids` for Organization Invitation (%s): %s " , d .Id (), err )
123
125
}
124
126
125
127
if err := d .Set ("roles" , orgInvitation .Roles ); err != nil {
126
- return diag .FromErr ( fmt . Errorf ("error getting `roles` for Organization Invitation (%s): %w " , d .Id (), err ) )
128
+ return diag .Errorf ("error getting `roles` for Organization Invitation (%s): %s " , d .Id (), err )
127
129
}
128
130
129
131
d .SetId (encodeStateID (map [string ]string {
@@ -148,7 +150,7 @@ func resourceMongoDBAtlasOrgInvitationCreate(ctx context.Context, d *schema.Reso
148
150
149
151
invitationRes , _ , err := conn .Organizations .InviteUser (ctx , orgID , invitationReq )
150
152
if err != nil {
151
- return diag .FromErr ( fmt . Errorf ("error creating Organization invitation for user %s: %w " , d .Get ("username" ).(string ), err ) )
153
+ return diag .Errorf ("error creating Organization invitation for user %s: %s " , d .Get ("username" ).(string ), err )
152
154
}
153
155
154
156
d .SetId (encodeStateID (map [string ]string {
@@ -169,7 +171,7 @@ func resourceMongoDBAtlasOrgInvitationDelete(ctx context.Context, d *schema.Reso
169
171
170
172
_ , err := conn .Organizations .DeleteInvitation (ctx , orgID , invitationID )
171
173
if err != nil {
172
- return diag .FromErr ( fmt . Errorf ("error deleting Organization invitation for user %s: %w " , username , err ) )
174
+ return diag .Errorf ("error deleting Organization invitation for user %s: %s " , username , err )
173
175
}
174
176
175
177
return nil
@@ -188,7 +190,7 @@ func resourceMongoDBAtlasOrgInvitationUpdate(ctx context.Context, d *schema.Reso
188
190
189
191
_ , _ , err := conn .Organizations .UpdateInvitationByID (ctx , orgID , invitationID , invitationReq )
190
192
if err != nil {
191
- return diag .FromErr ( fmt . Errorf ("error updating Organization invitation for user %s: for %w " , username , err ) )
193
+ return diag .Errorf ("error updating Organization invitation for user %s: for %s " , username , err )
192
194
}
193
195
194
196
return resourceMongoDBAtlasOrgInvitationRead (ctx , d , meta )
@@ -203,7 +205,7 @@ func resourceMongoDBAtlasOrgInvitationImportState(ctx context.Context, d *schema
203
205
204
206
orgInvitations , _ , err := conn .Organizations .Invitations (ctx , orgID , nil )
205
207
if err != nil {
206
- return nil , fmt .Errorf ("couldn't import Organization invitations, error: %w " , err )
208
+ return nil , fmt .Errorf ("couldn't import Organization invitations, error: %s " , err )
207
209
}
208
210
209
211
for _ , orgInvitation := range orgInvitations {
@@ -212,13 +214,13 @@ func resourceMongoDBAtlasOrgInvitationImportState(ctx context.Context, d *schema
212
214
}
213
215
214
216
if err := d .Set ("username" , orgInvitation .Username ); err != nil {
215
- return nil , fmt .Errorf ("error getting `username` for Organization Invitation (%s): %w " , username , err )
217
+ return nil , fmt .Errorf ("error getting `username` for Organization Invitation (%s): %s " , username , err )
216
218
}
217
219
if err := d .Set ("org_id" , orgInvitation .GroupID ); err != nil {
218
- return nil , fmt .Errorf ("error getting `org_id` for Organization Invitation (%s): %w " , username , err )
220
+ return nil , fmt .Errorf ("error getting `org_id` for Organization Invitation (%s): %s " , username , err )
219
221
}
220
222
if err := d .Set ("invitation_id" , orgInvitation .ID ); err != nil {
221
- return nil , fmt .Errorf ("error getting `invitation_id` for Organization Invitation (%s): %w " , username , err )
223
+ return nil , fmt .Errorf ("error getting `invitation_id` for Organization Invitation (%s): %s " , username , err )
222
224
}
223
225
d .SetId (encodeStateID (map [string ]string {
224
226
"username" : username ,
@@ -245,3 +247,18 @@ func splitOrgInvitationImportID(id string) (orgID, username string, err error) {
245
247
246
248
return
247
249
}
250
+
251
+ func validateOrgInvitationAlreadyAccepted (ctx context.Context , conn * MongoDBClient , username , orgID string ) (bool , error ) {
252
+ user , _ , err := conn .Atlas .AtlasUsers .GetByName (ctx , username )
253
+ if err != nil {
254
+ return false , err
255
+ }
256
+
257
+ for _ , role := range user .Roles {
258
+ if role .OrgID == orgID {
259
+ return true , nil
260
+ }
261
+ }
262
+
263
+ return false , nil
264
+ }
0 commit comments