@@ -11,17 +11,17 @@ import (
1111 "github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1212
1313 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14- matlas "go.mongodb.org/atlas/mongodbatlas "
14+ "go.mongodb.org/atlas-sdk/v20231115005/admin "
1515)
1616
1717func Resource () * schema.Resource {
1818 return & schema.Resource {
19- CreateContext : resourceMongoDBAtlasProjectInvitationCreate ,
20- ReadContext : resourceMongoDBAtlasProjectInvitationRead ,
21- DeleteContext : resourceMongoDBAtlasProjectInvitationDelete ,
22- UpdateContext : resourceMongoDBAtlasProjectInvitationUpdate ,
19+ CreateContext : resourceCreate ,
20+ ReadContext : resourceRead ,
21+ UpdateContext : resourceUpdate ,
22+ DeleteContext : resourceDelete ,
2323 Importer : & schema.ResourceImporter {
24- StateContext : resourceMongoDBAtlasProjectInvitationImportState ,
24+ StateContext : resourceImport ,
2525 },
2626 Schema : map [string ]* schema.Schema {
2727 "project_id" : {
@@ -61,51 +61,72 @@ func Resource() *schema.Resource {
6161 }
6262}
6363
64- func resourceMongoDBAtlasProjectInvitationRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
65- // Get client connection.
66- conn := meta .(* config.MongoDBClient ).Atlas
64+ func resourceCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
65+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
66+ projectID := d .Get ("project_id" ).(string )
67+
68+ roles := createProjectStringListFromSetSchema (d .Get ("roles" ).(* schema.Set ))
69+ invitationReq := & admin.GroupInvitationRequest {
70+ Roles : & roles ,
71+ Username : conversion .StringPtr (d .Get ("username" ).(string )),
72+ }
73+
74+ invitationRes , _ , err := connV2 .ProjectsApi .CreateProjectInvitation (ctx , projectID , invitationReq ).Execute ()
75+ if err != nil {
76+ return diag .FromErr (fmt .Errorf ("error creating Project invitation for user %s: %w" , d .Get ("username" ).(string ), err ))
77+ }
78+
79+ d .SetId (conversion .EncodeStateID (map [string ]string {
80+ "username" : invitationRes .GetUsername (),
81+ "project_id" : invitationRes .GetGroupId (),
82+ "invitation_id" : invitationRes .GetId (),
83+ }))
84+
85+ return resourceRead (ctx , d , meta )
86+ }
87+
88+ func resourceRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
89+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
6790 ids := conversion .DecodeStateID (d .Id ())
6891 projectID := ids ["project_id" ]
6992 username := ids ["username" ]
7093 invitationID := ids ["invitation_id" ]
7194
72- projectInvitation , resp , err := conn . Projects . Invitation (ctx , projectID , invitationID )
95+ projectInvitation , resp , err := connV2 . ProjectsApi . GetProjectInvitation (ctx , projectID , invitationID ). Execute ( )
7396 if err != nil {
74- // case 404
75- // deleted in the backend case
76- if resp != nil && resp .StatusCode == http .StatusNotFound {
97+ if resp != nil && resp .StatusCode == http .StatusNotFound { // case 404: deleted in the backend case
7798 d .SetId ("" )
7899 return nil
79100 }
80101
81102 return diag .FromErr (fmt .Errorf ("error getting Project Invitation information: %w" , err ))
82103 }
83104
84- if err := d .Set ("username" , projectInvitation .Username ); err != nil {
105+ if err := d .Set ("username" , projectInvitation .GetUsername () ); err != nil {
85106 return diag .FromErr (fmt .Errorf ("error getting `username` for Project Invitation (%s): %w" , d .Id (), err ))
86107 }
87108
88- if err := d .Set ("project_id" , projectInvitation .GroupID ); err != nil {
109+ if err := d .Set ("project_id" , projectInvitation .GetGroupId () ); err != nil {
89110 return diag .FromErr (fmt .Errorf ("error getting `project_id` for Project Invitation (%s): %w" , d .Id (), err ))
90111 }
91112
92- if err := d .Set ("invitation_id" , projectInvitation .ID ); err != nil {
113+ if err := d .Set ("invitation_id" , projectInvitation .GetId () ); err != nil {
93114 return diag .FromErr (fmt .Errorf ("error getting `invitation_id` for Project Invitation (%s): %w" , d .Id (), err ))
94115 }
95116
96- if err := d .Set ("expires_at" , projectInvitation .ExpiresAt ); err != nil {
117+ if err := d .Set ("expires_at" , conversion . TimePtrToStringPtr ( projectInvitation .ExpiresAt ) ); err != nil {
97118 return diag .FromErr (fmt .Errorf ("error getting `expires_at` for Project Invitation (%s): %w" , d .Id (), err ))
98119 }
99120
100- if err := d .Set ("created_at" , projectInvitation .CreatedAt ); err != nil {
121+ if err := d .Set ("created_at" , conversion . TimePtrToStringPtr ( projectInvitation .CreatedAt ) ); err != nil {
101122 return diag .FromErr (fmt .Errorf ("error getting `created_at` for Project Invitation (%s): %w" , d .Id (), err ))
102123 }
103124
104- if err := d .Set ("inviter_username" , projectInvitation .InviterUsername ); err != nil {
125+ if err := d .Set ("inviter_username" , projectInvitation .GetInviterUsername () ); err != nil {
105126 return diag .FromErr (fmt .Errorf ("error getting `inviter_username` for Project Invitation (%s): %w" , d .Id (), err ))
106127 }
107128
108- if err := d .Set ("roles" , projectInvitation .Roles ); err != nil {
129+ if err := d .Set ("roles" , projectInvitation .GetRoles () ); err != nil {
109130 return diag .FromErr (fmt .Errorf ("error getting `roles` for Project Invitation (%s): %w" , d .Id (), err ))
110131 }
111132
@@ -118,94 +139,67 @@ func resourceMongoDBAtlasProjectInvitationRead(ctx context.Context, d *schema.Re
118139 return nil
119140}
120141
121- func resourceMongoDBAtlasProjectInvitationCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
122- // Get client connection.
123- conn := meta .(* config.MongoDBClient ).Atlas
124- projectID := d .Get ("project_id" ).(string )
125-
126- invitationReq := & matlas.Invitation {
127- Roles : createProjectStringListFromSetSchema (d .Get ("roles" ).(* schema.Set )),
128- Username : d .Get ("username" ).(string ),
129- }
130-
131- invitationRes , _ , err := conn .Projects .InviteUser (ctx , projectID , invitationReq )
132- if err != nil {
133- return diag .FromErr (fmt .Errorf ("error creating Project invitation for user %s: %w" , d .Get ("username" ).(string ), err ))
134- }
135-
136- d .SetId (conversion .EncodeStateID (map [string ]string {
137- "username" : invitationRes .Username ,
138- "project_id" : invitationRes .GroupID ,
139- "invitation_id" : invitationRes .ID ,
140- }))
141-
142- return resourceMongoDBAtlasProjectInvitationRead (ctx , d , meta )
143- }
144-
145- func resourceMongoDBAtlasProjectInvitationDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
146- conn := meta .(* config.MongoDBClient ).Atlas
142+ func resourceUpdate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
143+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
147144 ids := conversion .DecodeStateID (d .Id ())
148145 projectID := ids ["project_id" ]
149146 username := ids ["username" ]
150147 invitationID := ids ["invitation_id" ]
151148
152- _ , err := conn .Projects .DeleteInvitation (ctx , projectID , invitationID )
149+ roles := conversion .ExpandStringListFromSetSchema (d .Get ("roles" ).(* schema.Set ))
150+ invitationReq := & admin.GroupInvitationUpdateRequest {
151+ Roles : & roles ,
152+ }
153+ _ , _ , err := connV2 .ProjectsApi .UpdateProjectInvitationById (ctx , projectID , invitationID , invitationReq ).Execute ()
153154 if err != nil {
154- return diag .FromErr (fmt .Errorf ("error deleting Project invitation for user %s: %w" , username , err ))
155+ return diag .FromErr (fmt .Errorf ("error updating Project invitation for user %s: %w" , username , err ))
155156 }
156-
157- return nil
157+ return resourceRead (ctx , d , meta )
158158}
159159
160- func resourceMongoDBAtlasProjectInvitationUpdate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
161- conn := meta .(* config.MongoDBClient ).Atlas
160+ func resourceDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
161+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
162162 ids := conversion .DecodeStateID (d .Id ())
163163 projectID := ids ["project_id" ]
164164 username := ids ["username" ]
165165 invitationID := ids ["invitation_id" ]
166-
167- invitationReq := & matlas.Invitation {
168- Roles : conversion .ExpandStringListFromSetSchema (d .Get ("roles" ).(* schema.Set )),
169- }
170-
171- _ , _ , err := conn .Projects .UpdateInvitationByID (ctx , projectID , invitationID , invitationReq )
166+ _ , _ , err := connV2 .ProjectsApi .DeleteProjectInvitation (ctx , projectID , invitationID ).Execute ()
172167 if err != nil {
173- return diag .FromErr (fmt .Errorf ("error updating Project invitation for user %s: %w" , username , err ))
168+ return diag .FromErr (fmt .Errorf ("error deleting Project invitation for user %s: %w" , username , err ))
174169 }
175-
176- return resourceMongoDBAtlasProjectInvitationRead (ctx , d , meta )
170+ return nil
177171}
178172
179- func resourceMongoDBAtlasProjectInvitationImportState (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
180- conn := meta .(* config.MongoDBClient ).Atlas
173+ func resourceImport (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
174+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
181175 projectID , username , err := splitProjectInvitationImportID (d .Id ())
182176 if err != nil {
183177 return nil , err
184178 }
185179
186- projectInvitations , _ , err := conn . Projects . Invitations (ctx , projectID , nil )
180+ projectInvitations , _ , err := connV2 . ProjectsApi . ListProjectInvitations (ctx , projectID ). Execute ( )
187181 if err != nil {
188182 return nil , fmt .Errorf ("couldn't import Project invitations, error: %s" , err )
189183 }
190184
191185 for _ , projectInvitation := range projectInvitations {
192- if projectInvitation .Username != username {
186+ if conversion . SafeString ( projectInvitation .Username ) != username {
193187 continue
194188 }
195189
196- if err := d .Set ("username" , projectInvitation .Username ); err != nil {
190+ if err := d .Set ("username" , projectInvitation .GetUsername () ); err != nil {
197191 return nil , fmt .Errorf ("error getting `username` for Project Invitation (%s): %w" , username , err )
198192 }
199- if err := d .Set ("project_id" , projectInvitation .GroupID ); err != nil {
193+ if err := d .Set ("project_id" , projectInvitation .GetGroupId () ); err != nil {
200194 return nil , fmt .Errorf ("error getting `project_id` for Project Invitation (%s): %w" , username , err )
201195 }
202- if err := d .Set ("invitation_id" , projectInvitation .ID ); err != nil {
196+ if err := d .Set ("invitation_id" , projectInvitation .GetId () ); err != nil {
203197 return nil , fmt .Errorf ("error getting `invitation_id` for Project Invitation (%s): %w" , username , err )
204198 }
205199 d .SetId (conversion .EncodeStateID (map [string ]string {
206200 "username" : username ,
207201 "project_id" : projectID ,
208- "invitation_id" : projectInvitation .ID ,
202+ "invitation_id" : projectInvitation .GetId () ,
209203 }))
210204 return []* schema.ResourceData {d }, nil
211205 }
0 commit comments