@@ -10,22 +10,23 @@ import (
10
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
11
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
12
12
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
13
- matlas "go.mongodb.org/atlas/mongodbatlas "
13
+ "go.mongodb.org/atlas-sdk/v20231115005/admin "
14
14
)
15
15
16
16
func Resource () * schema.Resource {
17
17
return & schema.Resource {
18
- CreateContext : resourceMongoDBAtlasOrgInvitationCreate ,
19
- ReadContext : resourceMongoDBAtlasOrgInvitationRead ,
20
- DeleteContext : resourceMongoDBAtlasOrgInvitationDelete ,
21
- UpdateContext : resourceMongoDBAtlasOrgInvitationUpdate ,
18
+ CreateContext : resourceCreate ,
19
+ ReadContext : resourceRead ,
20
+ DeleteContext : resourceDelete ,
21
+ UpdateContext : resourceUpdate ,
22
22
Importer : & schema.ResourceImporter {
23
- StateContext : resourceMongoDBAtlasOrgInvitationImportState ,
23
+ StateContext : resourceImport ,
24
24
},
25
25
Schema : map [string ]* schema.Schema {
26
26
"org_id" : {
27
27
Type : schema .TypeString ,
28
28
Required : true ,
29
+ ForceNew : true ,
29
30
},
30
31
"username" : {
31
32
Type : schema .TypeString ,
@@ -68,23 +69,50 @@ func Resource() *schema.Resource {
68
69
}
69
70
}
70
71
71
- func resourceMongoDBAtlasOrgInvitationRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
72
- // Get client connection.
73
- conn := meta .(* config.MongoDBClient ).Atlas
72
+ func resourceCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
73
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
74
+ orgID := d .Get ("org_id" ).(string )
75
+ roles := conversion .ExpandStringListFromSetSchema (d .Get ("roles" ).(* schema.Set ))
76
+ teamIDs := conversion .ExpandStringListFromSetSchema (d .Get ("teams_ids" ).(* schema.Set ))
77
+ invitationReq := & admin.OrganizationInvitationRequest {
78
+ Roles : & roles ,
79
+ TeamIds : & teamIDs ,
80
+ Username : conversion .StringPtr (d .Get ("username" ).(string )),
81
+ }
82
+
83
+ if validateOrgInvitationAlreadyAccepted (ctx , connV2 , invitationReq .GetUsername (), orgID ) {
84
+ d .SetId (conversion .EncodeStateID (map [string ]string {
85
+ "username" : invitationReq .GetUsername (),
86
+ "org_id" : orgID ,
87
+ "invitation_id" : orgID ,
88
+ }))
89
+ } else {
90
+ invitationRes , _ , err := connV2 .OrganizationsApi .CreateOrganizationInvitation (ctx , orgID , invitationReq ).Execute ()
91
+ if err != nil {
92
+ return diag .Errorf ("error creating Organization invitation for user %s: %s" , d .Get ("username" ).(string ), err )
93
+ }
94
+
95
+ d .SetId (conversion .EncodeStateID (map [string ]string {
96
+ "username" : invitationRes .GetUsername (),
97
+ "org_id" : invitationRes .GetOrgId (),
98
+ "invitation_id" : invitationRes .GetId (),
99
+ }))
100
+ }
101
+ return resourceRead (ctx , d , meta )
102
+ }
103
+
104
+ func resourceRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
105
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
74
106
ids := conversion .DecodeStateID (d .Id ())
75
107
orgID := ids ["org_id" ]
76
108
username := ids ["username" ]
77
109
invitationID := ids ["invitation_id" ]
78
110
79
111
if orgID != invitationID {
80
- orgInvitation , _ , err := conn . Organizations . Invitation (ctx , orgID , invitationID )
112
+ orgInvitation , _ , err := connV2 . OrganizationsApi . GetOrganizationInvitation (ctx , orgID , invitationID ). Execute ( )
81
113
if err != nil {
82
- // case 404
83
- // deleted in the backend case
84
-
85
- if strings .Contains (err .Error (), "404" ) {
86
- accepted , _ := validateOrgInvitationAlreadyAccepted (ctx , meta .(* config.MongoDBClient ), username , orgID )
87
- if accepted {
114
+ if strings .Contains (err .Error (), "404" ) { // case 404: deleted in the backend case
115
+ if validateOrgInvitationAlreadyAccepted (ctx , connV2 , username , orgID ) {
88
116
d .SetId ("" )
89
117
return nil
90
118
}
@@ -94,35 +122,35 @@ func resourceMongoDBAtlasOrgInvitationRead(ctx context.Context, d *schema.Resour
94
122
return diag .Errorf ("error getting Organization Invitation information: %s" , err )
95
123
}
96
124
97
- if err := d .Set ("username" , orgInvitation .Username ); err != nil {
125
+ if err := d .Set ("username" , orgInvitation .GetUsername () ); err != nil {
98
126
return diag .Errorf ("error getting `username` for Organization Invitation (%s): %s" , d .Id (), err )
99
127
}
100
128
101
- if err := d .Set ("org_id" , orgInvitation .OrgID ); err != nil {
129
+ if err := d .Set ("org_id" , orgInvitation .GetOrgId () ); err != nil {
102
130
return diag .Errorf ("error getting `username` for Organization Invitation (%s): %s" , d .Id (), err )
103
131
}
104
132
105
- if err := d .Set ("invitation_id" , orgInvitation .ID ); err != nil {
133
+ if err := d .Set ("invitation_id" , orgInvitation .GetId () ); err != nil {
106
134
return diag .Errorf ("error getting `invitation_id` for Organization Invitation (%s): %s" , d .Id (), err )
107
135
}
108
136
109
- if err := d .Set ("expires_at" , orgInvitation .ExpiresAt ); err != nil {
137
+ if err := d .Set ("expires_at" , conversion . TimePtrToStringPtr ( orgInvitation .ExpiresAt ) ); err != nil {
110
138
return diag .Errorf ("error getting `expires_at` for Organization Invitation (%s): %s" , d .Id (), err )
111
139
}
112
140
113
- if err := d .Set ("created_at" , orgInvitation .CreatedAt ); err != nil {
141
+ if err := d .Set ("created_at" , conversion . TimePtrToStringPtr ( orgInvitation .CreatedAt ) ); err != nil {
114
142
return diag .Errorf ("error getting `created_at` for Organization Invitation (%s): %s" , d .Id (), err )
115
143
}
116
144
117
- if err := d .Set ("inviter_username" , orgInvitation .InviterUsername ); err != nil {
145
+ if err := d .Set ("inviter_username" , orgInvitation .GetInviterUsername () ); err != nil {
118
146
return diag .Errorf ("error getting `inviter_username` for Organization Invitation (%s): %s" , d .Id (), err )
119
147
}
120
148
121
- if err := d .Set ("teams_ids" , orgInvitation .TeamIDs ); err != nil {
149
+ if err := d .Set ("teams_ids" , orgInvitation .GetTeamIds () ); err != nil {
122
150
return diag .Errorf ("error getting `teams_ids` for Organization Invitation (%s): %s" , d .Id (), err )
123
151
}
124
152
125
- if err := d .Set ("roles" , orgInvitation .Roles ); err != nil {
153
+ if err := d .Set ("roles" , orgInvitation .GetRoles () ); err != nil {
126
154
return diag .Errorf ("error getting `roles` for Organization Invitation (%s): %s" , d .Id (), err )
127
155
}
128
156
}
@@ -135,117 +163,78 @@ func resourceMongoDBAtlasOrgInvitationRead(ctx context.Context, d *schema.Resour
135
163
return nil
136
164
}
137
165
138
- func resourceMongoDBAtlasOrgInvitationCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
139
- // Get client connection.
140
- conn := meta .(* config.MongoDBClient ).Atlas
141
- orgID := d .Get ("org_id" ).(string )
142
-
143
- invitationReq := & matlas.Invitation {
144
- Roles : conversion .ExpandStringListFromSetSchema (d .Get ("roles" ).(* schema.Set )),
145
- TeamIDs : conversion .ExpandStringListFromSetSchema (d .Get ("teams_ids" ).(* schema.Set )),
146
- Username : d .Get ("username" ).(string ),
147
- }
148
-
149
- accepted , _ := validateOrgInvitationAlreadyAccepted (ctx , meta .(* config.MongoDBClient ), invitationReq .Username , orgID )
150
- if accepted {
151
- d .SetId (conversion .EncodeStateID (map [string ]string {
152
- "username" : invitationReq .Username ,
153
- "org_id" : orgID ,
154
- "invitation_id" : orgID ,
155
- }))
156
- } else {
157
- invitationRes , _ , err := conn .Organizations .InviteUser (ctx , orgID , invitationReq )
158
- if err != nil {
159
- return diag .Errorf ("error creating Organization invitation for user %s: %s" , d .Get ("username" ).(string ), err )
160
- }
161
-
162
- d .SetId (conversion .EncodeStateID (map [string ]string {
163
- "username" : invitationRes .Username ,
164
- "org_id" : invitationRes .OrgID ,
165
- "invitation_id" : invitationRes .ID ,
166
- }))
167
- }
168
- return resourceMongoDBAtlasOrgInvitationRead (ctx , d , meta )
169
- }
170
-
171
- func resourceMongoDBAtlasOrgInvitationDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
172
- conn := meta .(* config.MongoDBClient ).Atlas
166
+ func resourceDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
167
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
173
168
ids := conversion .DecodeStateID (d .Id ())
174
169
orgID := ids ["org_id" ]
175
170
username := ids ["username" ]
176
171
invitationID := ids ["invitation_id" ]
177
172
178
- _ , _ , err := conn . Organizations . Invitation (ctx , orgID , invitationID )
173
+ _ , _ , err := connV2 . OrganizationsApi . GetOrganizationInvitation (ctx , orgID , invitationID ). Execute ( )
179
174
if err != nil {
180
- // case 404
181
- // deleted in the backend case
182
-
183
- if strings .Contains (err .Error (), "404" ) {
184
- accepted , _ := validateOrgInvitationAlreadyAccepted (ctx , meta .(* config.MongoDBClient ), username , orgID )
185
- if accepted {
175
+ if strings .Contains (err .Error (), "404" ) { // case 404: deleted in the backend case
176
+ if validateOrgInvitationAlreadyAccepted (ctx , connV2 , username , orgID ) {
186
177
d .SetId ("" )
187
178
return nil
188
179
}
189
180
return nil
190
181
}
191
182
}
192
- _ , err = conn . Organizations . DeleteInvitation (ctx , orgID , invitationID )
183
+ _ , _ , err = connV2 . OrganizationsApi . DeleteOrganizationInvitation (ctx , orgID , invitationID ). Execute ( )
193
184
if err != nil {
194
185
return diag .Errorf ("error deleting Organization invitation for user %s: %s" , username , err )
195
186
}
196
187
d .SetId ("" )
197
188
return nil
198
189
}
199
190
200
- func resourceMongoDBAtlasOrgInvitationUpdate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
201
- conn := meta .(* config.MongoDBClient ).Atlas
191
+ func resourceUpdate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
192
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
202
193
ids := conversion .DecodeStateID (d .Id ())
203
194
orgID := ids ["org_id" ]
204
195
username := ids ["username" ]
205
196
invitationID := ids ["invitation_id" ]
206
-
207
- invitationReq := & matlas. Invitation {
208
- Roles : conversion . ExpandStringListFromSetSchema ( d . Get ( " roles" ).( * schema. Set )) ,
197
+ roles := conversion . ExpandStringListFromSetSchema ( d . Get ( "roles" ).( * schema. Set ))
198
+ invitationReq := & admin. OrganizationInvitationUpdateRequest {
199
+ Roles : & roles ,
209
200
}
210
-
211
- _ , _ , err := conn .Organizations .UpdateInvitationByID (ctx , orgID , invitationID , invitationReq )
201
+ _ , _ , err := connV2 .OrganizationsApi .UpdateOrganizationInvitationById (ctx , orgID , invitationID , invitationReq ).Execute ()
212
202
if err != nil {
213
203
return diag .Errorf ("error updating Organization invitation for user %s: for %s" , username , err )
214
204
}
215
-
216
- return resourceMongoDBAtlasOrgInvitationRead (ctx , d , meta )
205
+ return resourceRead (ctx , d , meta )
217
206
}
218
207
219
- func resourceMongoDBAtlasOrgInvitationImportState (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
220
- conn := meta .(* config.MongoDBClient ).Atlas
208
+ func resourceImport (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
209
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
221
210
orgID , username , err := splitOrgInvitationImportID (d .Id ())
222
211
if err != nil {
223
212
return nil , err
224
213
}
225
214
226
- orgInvitations , _ , err := conn . Organizations . Invitations (ctx , orgID , nil )
215
+ orgInvitations , _ , err := connV2 . OrganizationsApi . ListOrganizationInvitations (ctx , orgID ). Execute ( )
227
216
if err != nil {
228
217
return nil , fmt .Errorf ("couldn't import Organization invitations, error: %s" , err )
229
218
}
230
219
231
220
for _ , orgInvitation := range orgInvitations {
232
- if orgInvitation .Username != username {
221
+ if conversion . SafeString ( orgInvitation .Username ) != username {
233
222
continue
234
223
}
235
224
236
- if err := d .Set ("username" , orgInvitation .Username ); err != nil {
225
+ if err := d .Set ("username" , orgInvitation .GetUsername () ); err != nil {
237
226
return nil , fmt .Errorf ("error getting `username` for Organization Invitation (%s): %s" , username , err )
238
227
}
239
- if err := d .Set ("org_id" , orgInvitation .GroupID ); err != nil {
228
+ if err := d .Set ("org_id" , orgInvitation .GetOrgId () ); err != nil {
240
229
return nil , fmt .Errorf ("error getting `org_id` for Organization Invitation (%s): %s" , username , err )
241
230
}
242
- if err := d .Set ("invitation_id" , orgInvitation .ID ); err != nil {
231
+ if err := d .Set ("invitation_id" , orgInvitation .GetId () ); err != nil {
243
232
return nil , fmt .Errorf ("error getting `invitation_id` for Organization Invitation (%s): %s" , username , err )
244
233
}
245
234
d .SetId (conversion .EncodeStateID (map [string ]string {
246
235
"username" : username ,
247
236
"org_id" : orgID ,
248
- "invitation_id" : orgInvitation .ID ,
237
+ "invitation_id" : orgInvitation .GetId () ,
249
238
}))
250
239
return []* schema.ResourceData {d }, nil
251
240
}
@@ -268,17 +257,15 @@ func splitOrgInvitationImportID(id string) (orgID, username string, err error) {
268
257
return
269
258
}
270
259
271
- func validateOrgInvitationAlreadyAccepted (ctx context.Context , conn * config. MongoDBClient , username , orgID string ) ( bool , error ) {
272
- user , _ , err := conn . Atlas . AtlasUsers . GetByName (ctx , username )
260
+ func validateOrgInvitationAlreadyAccepted (ctx context.Context , connV2 * admin. APIClient , username , orgID string ) bool {
261
+ user , _ , err := connV2 . MongoDBCloudUsersApi . GetUserByUsername (ctx , username ). Execute ( )
273
262
if err != nil {
274
- return false , err
263
+ return false
275
264
}
276
-
277
- for _ , role := range user .Roles {
278
- if role .OrgID == orgID {
279
- return true , nil
265
+ for _ , role := range user .GetRoles () {
266
+ if role .GetOrgId () == orgID {
267
+ return true
280
268
}
281
269
}
282
-
283
- return false , nil
270
+ return false
284
271
}
0 commit comments