diff --git a/.changelog/1238.txt b/.changelog/1238.txt new file mode 100644 index 0000000000..20966945b2 --- /dev/null +++ b/.changelog/1238.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +cloudflare_account_roles +``` diff --git a/cloudflare/data_source_account_roles.go b/cloudflare/data_source_account_roles.go new file mode 100644 index 0000000000..d9a70f01c9 --- /dev/null +++ b/cloudflare/data_source_account_roles.go @@ -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 +} diff --git a/cloudflare/data_source_account_roles_test.go b/cloudflare/data_source_account_roles_test.go new file mode 100644 index 0000000000..ca2e06a279 --- /dev/null +++ b/cloudflare/data_source_account_roles_test.go @@ -0,0 +1,52 @@ +package cloudflare + +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) +} diff --git a/cloudflare/provider.go b/cloudflare/provider.go index f4d8ab44e5..5f773fd310 100644 --- a/cloudflare/provider.go +++ b/cloudflare/provider.go @@ -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(), diff --git a/website/cloudflare.erb b/website/cloudflare.erb index 7f3e1d9c42..807470431f 100644 --- a/website/cloudflare.erb +++ b/website/cloudflare.erb @@ -25,6 +25,9 @@ > Data Sources