@@ -7,14 +7,14 @@ import (
7
7
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8
8
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
9
9
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10
+ "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
10
11
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
11
-
12
- matlas "go.mongodb.org/atlas/mongodbatlas"
12
+ "go.mongodb.org/atlas-sdk/v20231115005/admin"
13
13
)
14
14
15
15
func PluralDataSource () * schema.Resource {
16
16
return & schema.Resource {
17
- ReadContext : dataSourceMongoDBAtlasAPIKeysRead ,
17
+ ReadContext : dataSourcePluralRead ,
18
18
Schema : map [string ]* schema.Schema {
19
19
"org_id" : {
20
20
Type : schema .TypeString ,
@@ -59,26 +59,38 @@ func PluralDataSource() *schema.Resource {
59
59
}
60
60
}
61
61
62
- func dataSourceMongoDBAtlasAPIKeysRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
63
- // Get client connection.
64
- conn := meta .(* config.MongoDBClient ).Atlas
65
- options := & matlas.ListOptions {
66
- PageNum : d .Get ("page_num" ).(int ),
67
- ItemsPerPage : d .Get ("items_per_page" ).(int ),
62
+ func dataSourcePluralRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
63
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
64
+ orgID := d .Get ("org_id" ).(string )
65
+ params := & admin.ListApiKeysApiParams {
66
+ PageNum : conversion .IntPtr (d .Get ("page_num" ).(int )),
67
+ ItemsPerPage : conversion .IntPtr (d .Get ("items_per_page" ).(int )),
68
+ OrgId : orgID ,
68
69
}
69
70
70
- orgID := d . Get ( "org_id" ).( string )
71
+ apiKeys , _ , err := connV2 . ProgrammaticAPIKeysApi . ListApiKeysWithParams ( ctx , params ). Execute ( )
71
72
72
- apiKeys , _ , err := conn .APIKeys .List (ctx , orgID , options )
73
73
if err != nil {
74
74
return diag .FromErr (fmt .Errorf ("error getting api keys information: %s" , err ))
75
75
}
76
76
77
- if err := d .Set ("results" , flattenOrgAPIKeys (ctx , conn , orgID , apiKeys )); err != nil {
77
+ if err := d .Set ("results" , flattenOrgAPIKeys (ctx , orgID , apiKeys . GetResults () )); err != nil {
78
78
return diag .FromErr (fmt .Errorf ("error setting `results`: %s" , err ))
79
79
}
80
80
81
81
d .SetId (id .UniqueId ())
82
-
83
82
return nil
84
83
}
84
+
85
+ func flattenOrgAPIKeys (ctx context.Context , orgID string , apiKeys []admin.ApiKeyUserDetails ) []map [string ]any {
86
+ results := make ([]map [string ]any , len (apiKeys ))
87
+ for k , apiKey := range apiKeys {
88
+ results [k ] = map [string ]any {
89
+ "api_key_id" : apiKey .GetId (),
90
+ "description" : apiKey .GetDesc (),
91
+ "public_key" : apiKey .GetPublicKey (),
92
+ "role_names" : flattenOrgAPIKeyRoles (orgID , apiKey .GetRoles ()),
93
+ }
94
+ }
95
+ return results
96
+ }
0 commit comments