@@ -11,17 +11,17 @@ import (
11
11
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
12
12
13
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14
- matlas "go.mongodb.org/atlas/mongodbatlas "
14
+ "go.mongodb.org/atlas-sdk/v20231115005/admin "
15
15
)
16
16
17
17
func Resource () * schema.Resource {
18
18
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 ,
23
23
Importer : & schema.ResourceImporter {
24
- StateContext : resourceMongoDBAtlasProjectInvitationImportState ,
24
+ StateContext : resourceImport ,
25
25
},
26
26
Schema : map [string ]* schema.Schema {
27
27
"project_id" : {
@@ -61,51 +61,72 @@ func Resource() *schema.Resource {
61
61
}
62
62
}
63
63
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
67
90
ids := conversion .DecodeStateID (d .Id ())
68
91
projectID := ids ["project_id" ]
69
92
username := ids ["username" ]
70
93
invitationID := ids ["invitation_id" ]
71
94
72
- projectInvitation , resp , err := conn . Projects . Invitation (ctx , projectID , invitationID )
95
+ projectInvitation , resp , err := connV2 . ProjectsApi . GetProjectInvitation (ctx , projectID , invitationID ). Execute ( )
73
96
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
77
98
d .SetId ("" )
78
99
return nil
79
100
}
80
101
81
102
return diag .FromErr (fmt .Errorf ("error getting Project Invitation information: %w" , err ))
82
103
}
83
104
84
- if err := d .Set ("username" , projectInvitation .Username ); err != nil {
105
+ if err := d .Set ("username" , projectInvitation .GetUsername () ); err != nil {
85
106
return diag .FromErr (fmt .Errorf ("error getting `username` for Project Invitation (%s): %w" , d .Id (), err ))
86
107
}
87
108
88
- if err := d .Set ("project_id" , projectInvitation .GroupID ); err != nil {
109
+ if err := d .Set ("project_id" , projectInvitation .GetGroupId () ); err != nil {
89
110
return diag .FromErr (fmt .Errorf ("error getting `project_id` for Project Invitation (%s): %w" , d .Id (), err ))
90
111
}
91
112
92
- if err := d .Set ("invitation_id" , projectInvitation .ID ); err != nil {
113
+ if err := d .Set ("invitation_id" , projectInvitation .GetId () ); err != nil {
93
114
return diag .FromErr (fmt .Errorf ("error getting `invitation_id` for Project Invitation (%s): %w" , d .Id (), err ))
94
115
}
95
116
96
- if err := d .Set ("expires_at" , projectInvitation .ExpiresAt ); err != nil {
117
+ if err := d .Set ("expires_at" , conversion . TimePtrToStringPtr ( projectInvitation .ExpiresAt ) ); err != nil {
97
118
return diag .FromErr (fmt .Errorf ("error getting `expires_at` for Project Invitation (%s): %w" , d .Id (), err ))
98
119
}
99
120
100
- if err := d .Set ("created_at" , projectInvitation .CreatedAt ); err != nil {
121
+ if err := d .Set ("created_at" , conversion . TimePtrToStringPtr ( projectInvitation .CreatedAt ) ); err != nil {
101
122
return diag .FromErr (fmt .Errorf ("error getting `created_at` for Project Invitation (%s): %w" , d .Id (), err ))
102
123
}
103
124
104
- if err := d .Set ("inviter_username" , projectInvitation .InviterUsername ); err != nil {
125
+ if err := d .Set ("inviter_username" , projectInvitation .GetInviterUsername () ); err != nil {
105
126
return diag .FromErr (fmt .Errorf ("error getting `inviter_username` for Project Invitation (%s): %w" , d .Id (), err ))
106
127
}
107
128
108
- if err := d .Set ("roles" , projectInvitation .Roles ); err != nil {
129
+ if err := d .Set ("roles" , projectInvitation .GetRoles () ); err != nil {
109
130
return diag .FromErr (fmt .Errorf ("error getting `roles` for Project Invitation (%s): %w" , d .Id (), err ))
110
131
}
111
132
@@ -118,94 +139,67 @@ func resourceMongoDBAtlasProjectInvitationRead(ctx context.Context, d *schema.Re
118
139
return nil
119
140
}
120
141
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
147
144
ids := conversion .DecodeStateID (d .Id ())
148
145
projectID := ids ["project_id" ]
149
146
username := ids ["username" ]
150
147
invitationID := ids ["invitation_id" ]
151
148
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 ()
153
154
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 ))
155
156
}
156
-
157
- return nil
157
+ return resourceRead (ctx , d , meta )
158
158
}
159
159
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
162
162
ids := conversion .DecodeStateID (d .Id ())
163
163
projectID := ids ["project_id" ]
164
164
username := ids ["username" ]
165
165
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 ()
172
167
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 ))
174
169
}
175
-
176
- return resourceMongoDBAtlasProjectInvitationRead (ctx , d , meta )
170
+ return nil
177
171
}
178
172
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
181
175
projectID , username , err := splitProjectInvitationImportID (d .Id ())
182
176
if err != nil {
183
177
return nil , err
184
178
}
185
179
186
- projectInvitations , _ , err := conn . Projects . Invitations (ctx , projectID , nil )
180
+ projectInvitations , _ , err := connV2 . ProjectsApi . ListProjectInvitations (ctx , projectID ). Execute ( )
187
181
if err != nil {
188
182
return nil , fmt .Errorf ("couldn't import Project invitations, error: %s" , err )
189
183
}
190
184
191
185
for _ , projectInvitation := range projectInvitations {
192
- if projectInvitation .Username != username {
186
+ if conversion . SafeString ( projectInvitation .Username ) != username {
193
187
continue
194
188
}
195
189
196
- if err := d .Set ("username" , projectInvitation .Username ); err != nil {
190
+ if err := d .Set ("username" , projectInvitation .GetUsername () ); err != nil {
197
191
return nil , fmt .Errorf ("error getting `username` for Project Invitation (%s): %w" , username , err )
198
192
}
199
- if err := d .Set ("project_id" , projectInvitation .GroupID ); err != nil {
193
+ if err := d .Set ("project_id" , projectInvitation .GetGroupId () ); err != nil {
200
194
return nil , fmt .Errorf ("error getting `project_id` for Project Invitation (%s): %w" , username , err )
201
195
}
202
- if err := d .Set ("invitation_id" , projectInvitation .ID ); err != nil {
196
+ if err := d .Set ("invitation_id" , projectInvitation .GetId () ); err != nil {
203
197
return nil , fmt .Errorf ("error getting `invitation_id` for Project Invitation (%s): %w" , username , err )
204
198
}
205
199
d .SetId (conversion .EncodeStateID (map [string ]string {
206
200
"username" : username ,
207
201
"project_id" : projectID ,
208
- "invitation_id" : projectInvitation .ID ,
202
+ "invitation_id" : projectInvitation .GetId () ,
209
203
}))
210
204
return []* schema.ResourceData {d }, nil
211
205
}
0 commit comments