Skip to content

Commit

Permalink
Add permissions datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
zippolyte committed Nov 17, 2020
1 parent 1dec8cd commit 04f6082
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
40 changes: 40 additions & 0 deletions datadog/data_source_datadog_permissions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package datadog

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceDatadogPermissions() *schema.Resource {
return &schema.Resource{
Read: dataSourceDatadogPermissionsRead,

Schema: map[string]*schema.Schema{
// Computed values
"permissions": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func dataSourceDatadogPermissionsRead(d *schema.ResourceData, meta interface{}) error {
providerConf := meta.(*ProviderConfiguration)
datadogClientV2 := providerConf.DatadogClientV2
authV2 := providerConf.AuthV2

res, _, err := datadogClientV2.RolesApi.ListPermissions(authV2).Execute()
if err != nil {
return translateClientError(err, "error querying roles")
}
perms := res.GetData()
permsNameToID := make(map[string]string, len(perms))
for _, perm := range perms {
permsNameToID[perm.Attributes.GetName()] = perm.GetId()
}
d.Set("permissions", permsNameToID)
return nil
}
1 change: 1 addition & 0 deletions datadog/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func Provider() terraform.ResourceProvider {
"datadog_dashboard_list": dataSourceDatadogDashboardList(),
"datadog_ip_ranges": dataSourceDatadogIpRanges(),
"datadog_monitor": dataSourceDatadogMonitor(),
"datadog_permissions": dataSourceDatadogPermissions(),
"datadog_role": dataSourceDatadogRole(),
"datadog_synthetics_locations": dataSourceDatadogSyntheticsLocations(),
},
Expand Down
17 changes: 17 additions & 0 deletions docs/data-sources/permissions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
page_title: "datadog_role"
---

# datadog_permissions Data Source

Use this data source to retrieve the list of Datadog permissions by name and their corresponding ID, for use in the role resource.

## Example Usage

```
data "datadog_permissions" "permissions" {}
```

## Attributes Reference

- `permissions`: Map of permissions names to their corresponding ID.

0 comments on commit 04f6082

Please sign in to comment.