Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloudflare_account_roles data source #1238

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1238.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
cloudflare_account_roles
```
75 changes: 75 additions & 0 deletions cloudflare/data_source_account_roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cloudflare

import (
"context"
"fmt"
"log"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceCloudflareAccountRoles() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudflareAccountRolesRead,

Schema: map[string]*schema.Schema{
"account_id": {
Type: schema.TypeString,
Required: true,
},

"roles": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
}

func dataSourceCloudflareAccountRolesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)
accountID := d.Get("account_id").(string)

log.Printf("[DEBUG] Reading Account Roles")
roles, err := client.AccountRoles(context.Background(), accountID)
if err != nil {
return fmt.Errorf("error listing Account Roles: %s", err)
}

roleIds := make([]string, 0)
roleDetails := make([]interface{}, 0)

for _, v := range roles {
roleDetails = append(roleDetails, map[string]interface{}{
"id": v.ID,
"name": v.Name,
"description": v.Description,
})
roleIds = append(roleIds, v.ID)
}

err = d.Set("roles", roleDetails)
if err != nil {
return fmt.Errorf("error setting roles: %s", err)
}

d.SetId(stringListChecksum(roleIds))
return nil
}
52 changes: 52 additions & 0 deletions cloudflare/data_source_account_roles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cloudflare
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccCloudflareAccountRoles(t *testing.T) {
rnd := generateRandomResourceName()
name := fmt.Sprintf("data.cloudflare_account_roles.%s", rnd)
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCloudflareAccountRolesConfig(rnd, accountID),
Check: resource.ComposeTestCheckFunc(
testAccCloudflareAccountRolesDataSourceId(name),
resource.TestCheckResourceAttr(name, "roles.#", "20"),
),
},
},
})
}

func testAccCloudflareAccountRolesDataSourceId(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
all := s.RootModule().Resources
rs, ok := all[n]

if !ok {
return fmt.Errorf("can't find Account Roles data source: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Snapshot Account Roles source ID not set")
}
return nil
}
}

func testAccCloudflareAccountRolesConfig(name string, accountID string) string {
return fmt.Sprintf(`data "cloudflare_account_roles" "%[1]s" {
account_id = "%[2]s"
}`, name, accountID)
}
1 change: 1 addition & 0 deletions cloudflare/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func Provider() *schema.Provider {
},

DataSourcesMap: map[string]*schema.Resource{
"cloudflare_account_roles": dataSourceCloudflareAccountRoles(),
"cloudflare_api_token_permission_groups": dataSourceCloudflareApiTokenPermissionGroups(),
"cloudflare_ip_ranges": dataSourceCloudflareIPRanges(),
"cloudflare_origin_ca_root_certificate": dataSourceCloudflareOriginCARootCertificate(),
Expand Down
3 changes: 3 additions & 0 deletions website/cloudflare.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<li<%= sidebar_current("docs-cloudflare-datasource") %>>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-cloudflare-datasource-account-roles") %>>
<a href="/docs/providers/cloudflare/d/account_roles.html">cloudflare_account_roles</a>
</li>
<li<%= sidebar_current("docs-cloudflare-api-token-permission-groups") %>>
<a href="/docs/providers/cloudflare/d/api_token_permission_groups.html">cloudflare_api_token_permission_groups</a>
</li>
Expand Down
49 changes: 49 additions & 0 deletions website/docs/d/account_roles.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
layout: "cloudflare"
page_title: "Cloudflare: cloudflare_account_roles"
sidebar_current: "docs-cloudflare-datasource-account-roles"
description: |-
Get information on a Cloudflare Account Roles.
---

# cloudflare_account_roles

Use this data source to lookup [Account Roles][1].

## Example usage

```hcl
data "cloudflare_account_roles" "account_roles" {
account_id = var.cloudflare_account_id
}

locals {
roles_by_name = {
for role in data.cloudflare_account_roles.account_roles.roles :
role.name => role
}
}

resource "cloudflare_account_member" "member" {
...
role_ids = [
local.roles_by_name["Administrator"].id
]
}
```

## Argument Reference

* `account_id` - (Required) The account for which to list the roles.

## Attributes Reference

- `roles` - A list of roles object. See below for nested attributes.

**roles**

- `id` - Role identifier tag
- `name` - Role Name
- `description` - Description of role's permissions

[1]: https://api.cloudflare.com/#account-roles-properties