-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathdata_source_mongodbatlas_api_keys.go
83 lines (73 loc) · 1.94 KB
/
data_source_mongodbatlas_api_keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package mongodbatlas
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
matlas "go.mongodb.org/atlas/mongodbatlas"
)
func dataSourceMongoDBAtlasAPIKeys() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasAPIKeysRead,
Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Required: true,
},
"page_num": {
Type: schema.TypeInt,
Optional: true,
},
"items_per_page": {
Type: schema.TypeInt,
Optional: true,
},
"results": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Computed: true,
},
"api_key_id": {
Type: schema.TypeString,
Computed: true,
},
"public_key": {
Type: schema.TypeString,
Computed: true,
},
"role_names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
func dataSourceMongoDBAtlasAPIKeysRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Get client connection.
conn := meta.(*MongoDBClient).Atlas
options := &matlas.ListOptions{
PageNum: d.Get("page_num").(int),
ItemsPerPage: d.Get("items_per_page").(int),
}
orgID := d.Get("org_id").(string)
apiKeys, _, err := conn.APIKeys.List(ctx, orgID, options)
if err != nil {
return diag.FromErr(fmt.Errorf("error getting api keys information: %s", err))
}
if err := d.Set("results", flattenOrgAPIKeys(ctx, conn, orgID, apiKeys)); err != nil {
return diag.FromErr(fmt.Errorf("error setting `results`: %s", err))
}
d.SetId(resource.UniqueId())
return nil
}