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

datasource cloudflare_list and cloudflare_lists #2296

Merged
merged 16 commits into from
Mar 20, 2023
Merged
7 changes: 7 additions & 0 deletions .changelog/2296.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-data-source
cloudflare_list
```

```release-note:new-data-source
cloudflare_lists
```
36 changes: 36 additions & 0 deletions docs/data-sources/list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
page_title: "cloudflare_list Data Source - Cloudflare"
subcategory: ""
description: |-
Use this data source to lookup a List https://developers.cloudflare.com/api/operations/lists-get-lists.
---

# cloudflare_list (Data Source)

Use this data source to lookup a [List](https://developers.cloudflare.com/api/operations/lists-get-lists).

## Example Usage

```terraform
data "cloudflare_list" "example" {
account_id = "f037e56e89293a057740de681ac9abbe"
name = "list_name"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `account_id` (String) The account identifier to target for the resource.
- `name` (String) The list name to target for the resource.

### Read-Only

- `description` (String) List description.
- `id` (String) The ID of this resource.
- `kind` (String) List kind.
- `numitems` (Number) Number of items in list.


43 changes: 43 additions & 0 deletions docs/data-sources/lists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
page_title: "cloudflare_lists Data Source - Cloudflare"
subcategory: ""
description: |-
Use this data source to lookup Lists https://developers.cloudflare.com/api/operations/lists-get-lists.
---

# cloudflare_lists (Data Source)

Use this data source to lookup [Lists](https://developers.cloudflare.com/api/operations/lists-get-lists).

## Example Usage

```terraform
data "cloudflare_lists" "example" {
account_id = "f037e56e89293a057740de681ac9abbe"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `account_id` (String) The account identifier to target for the resource.

### Read-Only

- `id` (String) The ID of this resource.
- `lists` (List of Object) (see [below for nested schema](#nestedatt--lists))

<a id="nestedatt--lists"></a>
### Nested Schema for `lists`

Read-Only:

- `description` (String)
- `id` (String)
- `kind` (String)
- `name` (String)
- `numitems` (Number)


4 changes: 4 additions & 0 deletions examples/data-sources/cloudflare_list/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "cloudflare_list" "example" {
account_id = "f037e56e89293a057740de681ac9abbe"
name = "list_name"
}
3 changes: 3 additions & 0 deletions examples/data-sources/cloudflare_lists/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "cloudflare_lists" "example" {
account_id = "f037e56e89293a057740de681ac9abbe"
}
74 changes: 74 additions & 0 deletions internal/sdkv2provider/data_source_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package sdkv2provider

import (
"context"
"fmt"

"github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceCloudflareList() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudflareListRead,

Schema: map[string]*schema.Schema{
consts.AccountIDSchemaKey: {
Description: "The account identifier to target for the resource.",
Type: schema.TypeString,
Required: true,
},
"name": {
Description: "The list name to target for the resource.",
Type: schema.TypeString,
Required: true,
},
"description": {
Description: "List description.",
Type: schema.TypeString,
Computed: true,
},
"kind": {
Description: "List kind.",
Type: schema.TypeString,
Computed: true,
},
"numitems": {
Description: "Number of items in list.",
Type: schema.TypeInt,
Computed: true,
},
},
Description: "Use this data source to lookup a [List](https://developers.cloudflare.com/api/operations/lists-get-lists).",
}
}

func dataSourceCloudflareListRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accountID := d.Get(consts.AccountIDSchemaKey).(string)
name := d.Get("name").(string)

tflog.Debug(ctx, "reading list")

lists, err := client.ListLists(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.ListListsParams{})
if err != nil {
return diag.FromErr(fmt.Errorf("failed to fetch Cloudflare lists: %w", err))
}

for _, l := range lists {
if l.Name == name {
d.SetId(l.ID)
d.Set("name", l.Name)
d.Set("description", l.Description)
d.Set("kind", l.Kind)
d.Set("numitems", l.NumItems)

return nil
}
}

return diag.Errorf("unable to find list named %s", name)
}
62 changes: 62 additions & 0 deletions internal/sdkv2provider/data_source_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package sdkv2provider

import (
"fmt"
"os"
"testing"

"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccCloudflareListDataSource(t *testing.T) {
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
rnd := generateRandomResourceName()

name := "data.cloudflare_list." + rnd
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAccount(t)
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareListCreate(accountID, rnd),
},
{
Config: testAccCheckCloudflareListDataSource(accountID, rnd),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, consts.AccountIDSchemaKey, accountID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "kind", "ip"),
resource.TestCheckResourceAttr(name, "numitems", "0"),
),
},
},
})
}

func testAccCheckCloudflareListCreate(accountID, name string) string {
return fmt.Sprintf(`
resource "cloudflare_list" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s"
kind = "ip"
}
`, name, accountID)
}

func testAccCheckCloudflareListDataSource(accountID, name string) string {
return fmt.Sprintf(`
resource "cloudflare_list" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s"
kind = "ip"
}

data "cloudflare_list" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s"
}`, name, accountID)
}
95 changes: 95 additions & 0 deletions internal/sdkv2provider/data_source_lists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package sdkv2provider

import (
"context"
"fmt"

"github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceCloudflareLists() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudflareListsRead,

Schema: map[string]*schema.Schema{
consts.AccountIDSchemaKey: {
Description: "The account identifier to target for the resource.",
Type: schema.TypeString,
Required: true,
},
"lists": {
rwhelan marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Description: "List identifier.",
Type: schema.TypeString,
Optional: true,
},
"name": {
Description: "The list name to target for the resource.",
Type: schema.TypeString,
Optional: true,
},
"description": {
Description: "List description.",
Type: schema.TypeString,
Optional: true,
},
"kind": {
Description: "List kind.",
Type: schema.TypeString,
Optional: true,
},
"numitems": {
Description: "Number of items in list.",
Type: schema.TypeInt,
Optional: true,
},
},
},
},
},
Description: "Use this data source to lookup [Lists](https://developers.cloudflare.com/api/operations/lists-get-lists).",
}
}

func dataSourceCloudflareListsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accountID := d.Get(consts.AccountIDSchemaKey).(string)

tflog.Debug(ctx, "reading list")

lists, err := client.ListLists(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.ListListsParams{})
if err != nil {
return diag.FromErr(fmt.Errorf("failed to fetch Cloudflare lists: %w", err))
}

listIds := make([]string, 0)
listDetails := make([]interface{}, 0)

for _, l := range lists {
listDetails = append(listDetails, map[string]interface{}{
"id": l.ID,
"name": l.Name,
"description": l.Description,
"kind": l.Kind,
"numitems": l.NumItems,
})

listIds = append(listIds, l.ID)
}

err = d.Set("lists", listDetails)
if err != nil {
return diag.FromErr(fmt.Errorf("error setting lists: %w", err))
}

d.SetId(stringListChecksum(listIds))
return nil
}
Loading