@@ -51,6 +51,7 @@ func resourceMongoDBAtlasProjectAPIKey() *schema.Resource {
5151 Type : schema .TypeString ,
5252 },
5353 ConflictsWith : []string {"project_assignment" },
54+ Deprecated : fmt .Sprintf (DeprecationMessageParameterToResource , "v1.12.0" , "project_assignment" ),
5455 },
5556 "project_assignment" : {
5657 Type : schema .TypeSet ,
@@ -157,27 +158,6 @@ func resourceMongoDBAtlasProjectAPIKeyRead(ctx context.Context, d *schema.Resour
157158 projectID := ids ["project_id" ]
158159 apiKeyID := ids ["api_key_id" ]
159160
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-
181161 projectAPIKeys , _ , err := conn .ProjectAPIKeys .List (ctx , projectID , nil )
182162 if err != nil {
183163 return diag .FromErr (fmt .Errorf ("error getting api key information: %s" , err ))
@@ -200,7 +180,18 @@ func resourceMongoDBAtlasProjectAPIKeyRead(ctx context.Context, d *schema.Resour
200180 if err := d .Set ("public_key" , val .PublicKey ); err != nil {
201181 return diag .FromErr (fmt .Errorf ("error setting `public_key`: %s" , err ))
202182 }
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 {
204195 if err := d .Set ("role_names" , flattenProjectAPIKeyRoles (projectID , val .Roles )); err != nil {
205196 return diag .FromErr (fmt .Errorf ("error setting `roles`: %s" , err ))
206197 }
@@ -387,24 +378,6 @@ func resourceMongoDBAtlasProjectAPIKeyImportState(ctx context.Context, d *schema
387378 return []* schema.ResourceData {d }, nil
388379}
389380
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-
408381func flattenProjectAPIKeyRoles (projectID string , apiKeyRoles []matlas.AtlasRole ) []string {
409382 if len (apiKeyRoles ) == 0 {
410383 return nil
@@ -435,13 +408,23 @@ func ExpandProjectAssignmentSet(projectAssignments *schema.Set) []*APIProjectAss
435408 return res
436409}
437410
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+
439422 var results []map [string ]interface {}
440423 var atlasRoles []matlas.AtlasRole
441424 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 {
445428 for _ , roleName := range apiKey .RoleNames {
446429 atlasRole .GroupID = apiKey .ProjectID
447430 atlasRole .RoleName = roleName
@@ -453,7 +436,7 @@ func FlattenProjectAssignment(apiKeyAssignmentSet []APIProjectAssignmentKeyInput
453436 }
454437 }
455438 }
456- return results
439+ return results , nil
457440}
458441
459442func getStateProjectAssignmentAPIKeys (d * schema.ResourceData ) (newAPIKeys , changedAPIKeys , removedAPIKeys []interface {}) {
0 commit comments