@@ -10,22 +10,23 @@ import (
1010 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1111 "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
1212 "github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
13- matlas "go.mongodb.org/atlas/mongodbatlas "
13+ "go.mongodb.org/atlas-sdk/v20231115005/admin "
1414)
1515
1616func Resource () * schema.Resource {
1717 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 ,
2222 Importer : & schema.ResourceImporter {
23- StateContext : resourceMongoDBAtlasOrgInvitationImportState ,
23+ StateContext : resourceImport ,
2424 },
2525 Schema : map [string ]* schema.Schema {
2626 "org_id" : {
2727 Type : schema .TypeString ,
2828 Required : true ,
29+ ForceNew : true ,
2930 },
3031 "username" : {
3132 Type : schema .TypeString ,
@@ -68,23 +69,50 @@ func Resource() *schema.Resource {
6869 }
6970}
7071
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
74106 ids := conversion .DecodeStateID (d .Id ())
75107 orgID := ids ["org_id" ]
76108 username := ids ["username" ]
77109 invitationID := ids ["invitation_id" ]
78110
79111 if orgID != invitationID {
80- orgInvitation , _ , err := conn . Organizations . Invitation (ctx , orgID , invitationID )
112+ orgInvitation , _ , err := connV2 . OrganizationsApi . GetOrganizationInvitation (ctx , orgID , invitationID ). Execute ( )
81113 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 ) {
88116 d .SetId ("" )
89117 return nil
90118 }
@@ -94,35 +122,35 @@ func resourceMongoDBAtlasOrgInvitationRead(ctx context.Context, d *schema.Resour
94122 return diag .Errorf ("error getting Organization Invitation information: %s" , err )
95123 }
96124
97- if err := d .Set ("username" , orgInvitation .Username ); err != nil {
125+ if err := d .Set ("username" , orgInvitation .GetUsername () ); err != nil {
98126 return diag .Errorf ("error getting `username` for Organization Invitation (%s): %s" , d .Id (), err )
99127 }
100128
101- if err := d .Set ("org_id" , orgInvitation .OrgID ); err != nil {
129+ if err := d .Set ("org_id" , orgInvitation .GetOrgId () ); err != nil {
102130 return diag .Errorf ("error getting `username` for Organization Invitation (%s): %s" , d .Id (), err )
103131 }
104132
105- if err := d .Set ("invitation_id" , orgInvitation .ID ); err != nil {
133+ if err := d .Set ("invitation_id" , orgInvitation .GetId () ); err != nil {
106134 return diag .Errorf ("error getting `invitation_id` for Organization Invitation (%s): %s" , d .Id (), err )
107135 }
108136
109- if err := d .Set ("expires_at" , orgInvitation .ExpiresAt ); err != nil {
137+ if err := d .Set ("expires_at" , conversion . TimePtrToStringPtr ( orgInvitation .ExpiresAt ) ); err != nil {
110138 return diag .Errorf ("error getting `expires_at` for Organization Invitation (%s): %s" , d .Id (), err )
111139 }
112140
113- if err := d .Set ("created_at" , orgInvitation .CreatedAt ); err != nil {
141+ if err := d .Set ("created_at" , conversion . TimePtrToStringPtr ( orgInvitation .CreatedAt ) ); err != nil {
114142 return diag .Errorf ("error getting `created_at` for Organization Invitation (%s): %s" , d .Id (), err )
115143 }
116144
117- if err := d .Set ("inviter_username" , orgInvitation .InviterUsername ); err != nil {
145+ if err := d .Set ("inviter_username" , orgInvitation .GetInviterUsername () ); err != nil {
118146 return diag .Errorf ("error getting `inviter_username` for Organization Invitation (%s): %s" , d .Id (), err )
119147 }
120148
121- if err := d .Set ("teams_ids" , orgInvitation .TeamIDs ); err != nil {
149+ if err := d .Set ("teams_ids" , orgInvitation .GetTeamIds () ); err != nil {
122150 return diag .Errorf ("error getting `teams_ids` for Organization Invitation (%s): %s" , d .Id (), err )
123151 }
124152
125- if err := d .Set ("roles" , orgInvitation .Roles ); err != nil {
153+ if err := d .Set ("roles" , orgInvitation .GetRoles () ); err != nil {
126154 return diag .Errorf ("error getting `roles` for Organization Invitation (%s): %s" , d .Id (), err )
127155 }
128156 }
@@ -135,117 +163,78 @@ func resourceMongoDBAtlasOrgInvitationRead(ctx context.Context, d *schema.Resour
135163 return nil
136164}
137165
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
173168 ids := conversion .DecodeStateID (d .Id ())
174169 orgID := ids ["org_id" ]
175170 username := ids ["username" ]
176171 invitationID := ids ["invitation_id" ]
177172
178- _ , _ , err := conn . Organizations . Invitation (ctx , orgID , invitationID )
173+ _ , _ , err := connV2 . OrganizationsApi . GetOrganizationInvitation (ctx , orgID , invitationID ). Execute ( )
179174 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 ) {
186177 d .SetId ("" )
187178 return nil
188179 }
189180 return nil
190181 }
191182 }
192- _ , err = conn . Organizations . DeleteInvitation (ctx , orgID , invitationID )
183+ _ , _ , err = connV2 . OrganizationsApi . DeleteOrganizationInvitation (ctx , orgID , invitationID ). Execute ( )
193184 if err != nil {
194185 return diag .Errorf ("error deleting Organization invitation for user %s: %s" , username , err )
195186 }
196187 d .SetId ("" )
197188 return nil
198189}
199190
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
202193 ids := conversion .DecodeStateID (d .Id ())
203194 orgID := ids ["org_id" ]
204195 username := ids ["username" ]
205196 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 ,
209200 }
210-
211- _ , _ , err := conn .Organizations .UpdateInvitationByID (ctx , orgID , invitationID , invitationReq )
201+ _ , _ , err := connV2 .OrganizationsApi .UpdateOrganizationInvitationById (ctx , orgID , invitationID , invitationReq ).Execute ()
212202 if err != nil {
213203 return diag .Errorf ("error updating Organization invitation for user %s: for %s" , username , err )
214204 }
215-
216- return resourceMongoDBAtlasOrgInvitationRead (ctx , d , meta )
205+ return resourceRead (ctx , d , meta )
217206}
218207
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
221210 orgID , username , err := splitOrgInvitationImportID (d .Id ())
222211 if err != nil {
223212 return nil , err
224213 }
225214
226- orgInvitations , _ , err := conn . Organizations . Invitations (ctx , orgID , nil )
215+ orgInvitations , _ , err := connV2 . OrganizationsApi . ListOrganizationInvitations (ctx , orgID ). Execute ( )
227216 if err != nil {
228217 return nil , fmt .Errorf ("couldn't import Organization invitations, error: %s" , err )
229218 }
230219
231220 for _ , orgInvitation := range orgInvitations {
232- if orgInvitation .Username != username {
221+ if conversion . SafeString ( orgInvitation .Username ) != username {
233222 continue
234223 }
235224
236- if err := d .Set ("username" , orgInvitation .Username ); err != nil {
225+ if err := d .Set ("username" , orgInvitation .GetUsername () ); err != nil {
237226 return nil , fmt .Errorf ("error getting `username` for Organization Invitation (%s): %s" , username , err )
238227 }
239- if err := d .Set ("org_id" , orgInvitation .GroupID ); err != nil {
228+ if err := d .Set ("org_id" , orgInvitation .GetOrgId () ); err != nil {
240229 return nil , fmt .Errorf ("error getting `org_id` for Organization Invitation (%s): %s" , username , err )
241230 }
242- if err := d .Set ("invitation_id" , orgInvitation .ID ); err != nil {
231+ if err := d .Set ("invitation_id" , orgInvitation .GetId () ); err != nil {
243232 return nil , fmt .Errorf ("error getting `invitation_id` for Organization Invitation (%s): %s" , username , err )
244233 }
245234 d .SetId (conversion .EncodeStateID (map [string ]string {
246235 "username" : username ,
247236 "org_id" : orgID ,
248- "invitation_id" : orgInvitation .ID ,
237+ "invitation_id" : orgInvitation .GetId () ,
249238 }))
250239 return []* schema.ResourceData {d }, nil
251240 }
@@ -268,17 +257,15 @@ func splitOrgInvitationImportID(id string) (orgID, username string, err error) {
268257 return
269258}
270259
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 ( )
273262 if err != nil {
274- return false , err
263+ return false
275264 }
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
280268 }
281269 }
282-
283- return false , nil
270+ return false
284271}
0 commit comments