@@ -51,6 +51,7 @@ func resourceMongoDBAtlasProjectAPIKey() *schema.Resource {
51
51
Type : schema .TypeString ,
52
52
},
53
53
ConflictsWith : []string {"project_assignment" },
54
+ Deprecated : fmt .Sprintf (DeprecationMessageParameterToResource , "v1.12.0" , "project_assignment" ),
54
55
},
55
56
"project_assignment" : {
56
57
Type : schema .TypeSet ,
@@ -157,27 +158,6 @@ func resourceMongoDBAtlasProjectAPIKeyRead(ctx context.Context, d *schema.Resour
157
158
projectID := ids ["project_id" ]
158
159
apiKeyID := ids ["api_key_id" ]
159
160
160
- options := & matlas.ListOptions {}
161
-
162
- apiKeyOrgList , _ , err := conn .Root .List (ctx , options )
163
- if err != nil {
164
- return diag .FromErr (fmt .Errorf ("error getting api key information: %s" , err ))
165
- }
166
- projectAssignments , err := getAPIProjectAssignments (ctx , conn , apiKeyOrgList , apiKeyID )
167
- if err != nil {
168
- return diag .FromErr (fmt .Errorf ("error getting api key information: %s" , err ))
169
- }
170
-
171
- _ , roleOk := d .GetOk ("role_names" )
172
- if ! roleOk {
173
- if err := d .Set ("role_names" , nil ); err != nil {
174
- return diag .FromErr (fmt .Errorf ("error setting `roles`: %s" , err ))
175
- }
176
- if err := d .Set ("project_assignment" , FlattenProjectAssignment (projectAssignments )); err != nil {
177
- return diag .Errorf (errorProjectSetting , `created` , projectID , err )
178
- }
179
- }
180
-
181
161
projectAPIKeys , _ , err := conn .ProjectAPIKeys .List (ctx , projectID , nil )
182
162
if err != nil {
183
163
return diag .FromErr (fmt .Errorf ("error getting api key information: %s" , err ))
@@ -200,7 +180,18 @@ func resourceMongoDBAtlasProjectAPIKeyRead(ctx context.Context, d *schema.Resour
200
180
if err := d .Set ("public_key" , val .PublicKey ); err != nil {
201
181
return diag .FromErr (fmt .Errorf ("error setting `public_key`: %s" , err ))
202
182
}
203
- if roleOk {
183
+
184
+ _ , roleOk := d .GetOk ("role_names" )
185
+ if ! roleOk {
186
+ if err := d .Set ("role_names" , nil ); err != nil {
187
+ return diag .FromErr (fmt .Errorf ("error setting `roles`: %s" , err ))
188
+ }
189
+ if projectAssignments , err := newProjectAssignment (ctx , conn , apiKeyID ); err == nil {
190
+ if err := d .Set ("project_assignment" , projectAssignments ); err != nil {
191
+ return diag .Errorf (errorProjectSetting , `created` , projectID , err )
192
+ }
193
+ }
194
+ } else {
204
195
if err := d .Set ("role_names" , flattenProjectAPIKeyRoles (projectID , val .Roles )); err != nil {
205
196
return diag .FromErr (fmt .Errorf ("error setting `roles`: %s" , err ))
206
197
}
@@ -387,24 +378,6 @@ func resourceMongoDBAtlasProjectAPIKeyImportState(ctx context.Context, d *schema
387
378
return []* schema.ResourceData {d }, nil
388
379
}
389
380
390
- func flattenProjectAPIKeys (ctx context.Context , conn * matlas.Client , projectID string , apiKeys []matlas.APIKey ) []map [string ]interface {} {
391
- var results []map [string ]interface {}
392
-
393
- if len (apiKeys ) > 0 {
394
- results = make ([]map [string ]interface {}, len (apiKeys ))
395
- for k , apiKey := range apiKeys {
396
- results [k ] = map [string ]interface {}{
397
- "api_key_id" : apiKey .ID ,
398
- "description" : apiKey .Desc ,
399
- "public_key" : apiKey .PublicKey ,
400
- "private_key" : apiKey .PrivateKey ,
401
- "role_names" : flattenProjectAPIKeyRoles (projectID , apiKey .Roles ),
402
- }
403
- }
404
- }
405
- return results
406
- }
407
-
408
381
func flattenProjectAPIKeyRoles (projectID string , apiKeyRoles []matlas.AtlasRole ) []string {
409
382
if len (apiKeyRoles ) == 0 {
410
383
return nil
@@ -435,13 +408,23 @@ func ExpandProjectAssignmentSet(projectAssignments *schema.Set) []*APIProjectAss
435
408
return res
436
409
}
437
410
438
- func FlattenProjectAssignment (apiKeyAssignmentSet []APIProjectAssignmentKeyInput ) []map [string ]interface {} {
411
+ func newProjectAssignment (ctx context.Context , conn * matlas.Client , apiKeyID string ) ([]map [string ]interface {}, error ) {
412
+ apiKeyOrgList , _ , err := conn .Root .List (ctx , nil )
413
+ if err != nil {
414
+ return nil , fmt .Errorf ("error getting api key information: %s" , err )
415
+ }
416
+
417
+ projectAssignments , err := getAPIProjectAssignments (ctx , conn , apiKeyOrgList , apiKeyID )
418
+ if err != nil {
419
+ return nil , fmt .Errorf ("error getting api key information: %s" , err )
420
+ }
421
+
439
422
var results []map [string ]interface {}
440
423
var atlasRoles []matlas.AtlasRole
441
424
var atlasRole matlas.AtlasRole
442
- if len (apiKeyAssignmentSet ) > 0 {
443
- results = make ([]map [string ]interface {}, len (apiKeyAssignmentSet ))
444
- for k , apiKey := range apiKeyAssignmentSet {
425
+ if len (projectAssignments ) > 0 {
426
+ results = make ([]map [string ]interface {}, len (projectAssignments ))
427
+ for k , apiKey := range projectAssignments {
445
428
for _ , roleName := range apiKey .RoleNames {
446
429
atlasRole .GroupID = apiKey .ProjectID
447
430
atlasRole .RoleName = roleName
@@ -453,7 +436,7 @@ func FlattenProjectAssignment(apiKeyAssignmentSet []APIProjectAssignmentKeyInput
453
436
}
454
437
}
455
438
}
456
- return results
439
+ return results , nil
457
440
}
458
441
459
442
func getStateProjectAssignmentAPIKeys (d * schema.ResourceData ) (newAPIKeys , changedAPIKeys , removedAPIKeys []interface {}) {
0 commit comments