forked from DataDog/terraform-provider-datadog
/
data_source_datadog_permissions.go
58 lines (50 loc) · 1.71 KB
/
data_source_datadog_permissions.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
package datadog
import (
"context"
"github.com/Telemetron/terraform-provider-datadog/v3/datadog/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceDatadogPermissions() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to retrieve the list of Datadog permissions by name and their corresponding ID, for use in the role resource.",
ReadContext: dataSourceDatadogPermissionsRead,
Schema: map[string]*schema.Schema{
// Computed values
"permissions": {
Description: "Map of permissions names to their corresponding ID.",
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceDatadogPermissionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
datadogClientV2 := providerConf.DatadogClientV2
authV2 := providerConf.AuthV2
res, resp, err := datadogClientV2.RolesApi.ListPermissions(authV2)
if err != nil {
return utils.TranslateClientErrorDiag(err, resp, "error listing permissions")
}
if err := utils.CheckForUnparsed(res); err != nil {
return diag.FromErr(err)
}
perms := res.GetData()
permsNameToID := make(map[string]string, len(perms))
for _, perm := range perms {
// Don't list restricted permissions, as they cannot be granted/revoked to/from a role
if perm.Attributes.GetRestricted() {
continue
}
permsNameToID[perm.Attributes.GetName()] = perm.GetId()
}
if err := d.Set("permissions", permsNameToID); err != nil {
return diag.FromErr(err)
}
d.SetId("datadog-permissions")
return nil
}