@@ -11,7 +11,7 @@ import (
1111 "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
1212 "github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1313 "github.com/spf13/cast"
14- matlas "go.mongodb.org/atlas/mongodbatlas "
14+ "go.mongodb.org/atlas-sdk/v20231115005/admin "
1515)
1616
1717const (
@@ -25,11 +25,11 @@ const (
2525
2626func Resource () * schema.Resource {
2727 return & schema.Resource {
28- CreateContext : resourceMongoDBAtlasX509AuthDBUserCreate ,
29- ReadContext : resourceMongoDBAtlasX509AuthDBUserRead ,
30- DeleteContext : resourceMongoDBAtlasX509AuthDBUserDelete ,
28+ CreateContext : resourceCreate ,
29+ ReadContext : resourceRead ,
30+ DeleteContext : resourceDelete ,
3131 Importer : & schema.ResourceImporter {
32- StateContext : resourceMongoDBAtlasX509AuthDBUserImportState ,
32+ StateContext : resourceImport ,
3333 },
3434 Schema : map [string ]* schema.Schema {
3535 "project_id" : {
@@ -98,27 +98,29 @@ func Resource() *schema.Resource {
9898 }
9999}
100100
101- func resourceMongoDBAtlasX509AuthDBUserCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
102- conn := meta .(* config.MongoDBClient ).Atlas
103-
101+ func resourceCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
102+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
104103 projectID := d .Get ("project_id" ).(string )
105104 username := d .Get ("username" ).(string )
106105
107- var serialNumber string
108-
109106 if expirationMonths , ok := d .GetOk ("months_until_expiration" ); ok {
110- res , _ , err := conn .X509AuthDBUsers .CreateUserCertificate (ctx , projectID , username , expirationMonths .(int ))
107+ months := expirationMonths .(int )
108+ params := & admin.UserCert {
109+ MonthsUntilExpiration : & months ,
110+ }
111+ certStr , _ , err := connV2 .X509AuthenticationApi .CreateDatabaseUserCertificate (ctx , projectID , username , params ).Execute ()
111112 if err != nil {
112113 return diag .FromErr (fmt .Errorf (errorX509AuthDBUsersCreate , username , projectID , err ))
113114 }
114-
115- serialNumber = cast .ToString (res .ID )
116- if err := d .Set ("current_certificate" , cast .ToString (res .Certificate )); err != nil {
115+ if err := d .Set ("current_certificate" , cast .ToString (certStr )); err != nil {
117116 return diag .FromErr (fmt .Errorf (errorX509AuthDBUsersSetting , "current_certificate" , username , err ))
118117 }
119118 } else {
120119 customerX509Cas := d .Get ("customer_x509_cas" ).(string )
121- _ , _ , err := conn .X509AuthDBUsers .SaveConfiguration (ctx , projectID , & matlas.CustomerX509 {Cas : customerX509Cas })
120+ userReq := & admin.UserSecurity {
121+ CustomerX509 : & admin.DBUserTLSX509Settings {Cas : & customerX509Cas },
122+ }
123+ _ , _ , err := connV2 .LDAPConfigurationApi .SaveLDAPConfiguration (ctx , projectID , userReq ).Execute ()
122124 if err != nil {
123125 return diag .FromErr (fmt .Errorf (errorCustomerX509AuthDBUsersCreate , projectID , err ))
124126 }
@@ -127,27 +129,24 @@ func resourceMongoDBAtlasX509AuthDBUserCreate(ctx context.Context, d *schema.Res
127129 d .SetId (conversion .EncodeStateID (map [string ]string {
128130 "project_id" : projectID ,
129131 "username" : username ,
130- "serial_number" : serialNumber ,
132+ "serial_number" : "" , // not returned in create API, got later in Read
131133 }))
132134
133- return resourceMongoDBAtlasX509AuthDBUserRead (ctx , d , meta )
135+ return resourceRead (ctx , d , meta )
134136}
135137
136- func resourceMongoDBAtlasX509AuthDBUserRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
137- conn := meta .(* config.MongoDBClient ).Atlas
138-
138+ func resourceRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
139+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
139140 ids := conversion .DecodeStateID (d .Id ())
140141 projectID := ids ["project_id" ]
141142 username := ids ["username" ]
142-
143143 var (
144- certificates []matlas.UserCertificate
145- err error
144+ certificates []admin.UserCert
146145 serialNumber string
147146 )
148147
149148 if username != "" {
150- certificates , _ , err = conn . X509AuthDBUsers . GetUserCertificates (ctx , projectID , username , nil )
149+ resp , _ , err := connV2 . X509AuthenticationApi . ListDatabaseUserCertificates (ctx , projectID , username ). Execute ( )
151150 if err != nil {
152151 // new resource missing
153152 reset := strings .Contains (err .Error (), "404" ) && ! d .IsNewResource ()
@@ -157,11 +156,13 @@ func resourceMongoDBAtlasX509AuthDBUserRead(ctx context.Context, d *schema.Resou
157156 }
158157 return diag .FromErr (fmt .Errorf (errorX509AuthDBUsersRead , username , projectID , err ))
159158 }
160- for _ , val := range certificates {
161- serialNumber = cast .ToString (val .ID )
159+ if resp != nil && resp .Results != nil {
160+ certificates = * resp .Results
161+ if len (certificates ) > 0 {
162+ serialNumber = cast .ToString (certificates [len (certificates )- 1 ].GetId ()) // Get SerialId from last user certificate
163+ }
162164 }
163165 }
164-
165166 if err := d .Set ("certificates" , flattenCertificates (certificates )); err != nil {
166167 return diag .FromErr (fmt .Errorf (errorX509AuthDBUsersSetting , "certificates" , username , err ))
167168 }
@@ -175,30 +176,27 @@ func resourceMongoDBAtlasX509AuthDBUserRead(ctx context.Context, d *schema.Resou
175176 return nil
176177}
177178
178- func resourceMongoDBAtlasX509AuthDBUserDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
179+ func resourceDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
179180 // We don't do anything because X.509 certificates can not be deleted or disassociated from a user.
180181 // More info: https://jira.mongodb.org/browse/HELP-53363
181182 d .SetId ("" )
182183 return nil
183184}
184185
185- func resourceMongoDBAtlasX509AuthDBUserImportState (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
186- conn := meta .(* config.MongoDBClient ).Atlas
187-
186+ func resourceImport (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
187+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
188188 parts := strings .SplitN (d .Id (), "-" , 2 )
189189 if len (parts ) != 1 && len (parts ) != 2 {
190190 return nil , errors .New ("import format error: to import a X509 Authentication, use the formats {project_id} or {project_id}-{username}" )
191191 }
192-
193192 var username string
194193 if len (parts ) == 2 {
195194 username = parts [1 ]
196195 }
197-
198196 projectID := parts [0 ]
199197
200198 if username != "" {
201- _ , _ , err := conn . X509AuthDBUsers . GetUserCertificates (ctx , projectID , username , nil )
199+ _ , _ , err := connV2 . X509AuthenticationApi . ListDatabaseUserCertificates (ctx , projectID , username ). Execute ( )
202200 if err != nil {
203201 return nil , fmt .Errorf (errorX509AuthDBUsersRead , username , projectID , err )
204202 }
@@ -208,12 +206,12 @@ func resourceMongoDBAtlasX509AuthDBUserImportState(ctx context.Context, d *schem
208206 }
209207 }
210208
211- customerX509 , _ , err := conn . X509AuthDBUsers . GetCurrentX509Conf (ctx , projectID )
209+ resp , _ , err := connV2 . LDAPConfigurationApi . GetLDAPConfiguration (ctx , projectID ). Execute ( )
212210 if err != nil {
213211 return nil , fmt .Errorf (errorCustomerX509AuthDBUsersRead , projectID , err )
214212 }
215-
216- if err := d .Set ("customer_x509_cas" , customerX509 .Cas ); err != nil {
213+ customerX509 := resp . GetCustomerX509 ()
214+ if err := d .Set ("customer_x509_cas" , customerX509 .GetCas () ); err != nil {
217215 return nil , fmt .Errorf (errorX509AuthDBUsersSetting , "certificates" , username , err )
218216 }
219217
@@ -230,17 +228,16 @@ func resourceMongoDBAtlasX509AuthDBUserImportState(ctx context.Context, d *schem
230228 return []* schema.ResourceData {d }, nil
231229}
232230
233- func flattenCertificates (userCertificates []matlas. UserCertificate ) []map [string ]any {
231+ func flattenCertificates (userCertificates []admin. UserCert ) []map [string ]any {
234232 certificates := make ([]map [string ]any , len (userCertificates ))
235233 for i , v := range userCertificates {
236234 certificates [i ] = map [string ]any {
237- "id" : v .ID ,
238- "created_at" : v .CreatedAt ,
239- "group_id" : v .GroupID ,
240- "not_after" : v .NotAfter ,
241- "subject" : v .Subject ,
235+ "id" : v .GetId () ,
236+ "created_at" : conversion . TimePtrToStringPtr ( v .CreatedAt ) ,
237+ "group_id" : v .GetGroupId () ,
238+ "not_after" : conversion . TimePtrToStringPtr ( v .NotAfter ) ,
239+ "subject" : v .GetSubject () ,
242240 }
243241 }
244-
245242 return certificates
246243}
0 commit comments