Skip to content

Commit

Permalink
DLP-1223: Add DLP dataset support to terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh Bector committed Mar 1, 2024
1 parent c58feb6 commit adb1211
Show file tree
Hide file tree
Showing 17 changed files with 258 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3135.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
cloudflare_dlp_datasets
```
12 changes: 12 additions & 0 deletions .changelog/3136.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2` from 1.25.0 to 1.25.1
```
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2/config` from 1.27.1 to 1.27.2
```
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2/credentials` from 1.17.1 to 1.17.2
```
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2/service/s3` from 1.50.2 to 1.50.3
```
9 changes: 9 additions & 0 deletions .changelog/3138.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2/config` from 1.27.2 to 1.27.3
```
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2/credentials` from 1.17.2 to 1.17.3
```
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2/service/s3` from 1.50.3 to 1.51.0
```
3 changes: 3 additions & 0 deletions .changelog/3139.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:dependency
provider: bump github.com/hashicorp/terraform-plugin-go from 0.21.0 to 0.22.0
```
3 changes: 3 additions & 0 deletions .changelog/3140.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_access_application: Fix issue with sending allow_authenticate_via_warp on updates when it is not provided
```
12 changes: 12 additions & 0 deletions .changelog/3141.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2` from 1.25.1 to 1.25.2
```
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2/config` from 1.27.3 to 1.27.4
```
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2/credentials` from 1.17.3 to 1.17.4
```
```release-note:dependency
provider: bump `github.com/aws/aws-sdk-go-v2/service/s3` from 1.51.0 to 1.51.1
```
3 changes: 3 additions & 0 deletions .changelog/3142.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:dependency
provider: bump github.com/hashicorp/terraform-plugin-sdk/v2 from 2.32.0 to 2.33.0
```
3 changes: 3 additions & 0 deletions .changelog/3147.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:dependency
provider: bump github.com/hashicorp/terraform-plugin-sdk/v2 from 2.32.0 to 2.33.0
```
3 changes: 3 additions & 0 deletions .changelog/3148.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:dependency
provider: bump github.com/cloudflare/cloudflare-go from 0.88.0 to 0.89.0
```
3 changes: 3 additions & 0 deletions .changelog/3149.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:dependency
provider: bump github.com/hashicorp/terraform-plugin-mux from 0.14.0 to 0.15.0
```
41 changes: 41 additions & 0 deletions docs/data-sources/dlp_datasets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
page_title: "cloudflare_dlp_datasets Data Source - Cloudflare"
subcategory: ""
description: |-
Use this data source to retrieve all DLP datasets for an account.
---

# cloudflare_dlp_datasets (Data Source)

Use this data source to retrieve all DLP datasets for an account.

## Example Usage

```terraform
data "cloudflare_dlp_datasets" "example" {
account_id = "f037e56e89293a057740de681ac9abbe"
}
```
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `account_id` (String) The account ID to fetch DLP Datasets from.

### Read-Only

- `datasets` (List of Object) A list of DLP Datasets. (see [below for nested schema](#nestedatt--datasets))

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

Read-Only:

- `description` (String)
- `id` (String)
- `name` (String)
- `secret` (Boolean)
- `status` (String)


3 changes: 3 additions & 0 deletions examples/data-sources/cloudflare_dlp_datasets/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "cloudflare_dlp_datasets" "example" {
account_id = "f037e56e89293a057740de681ac9abbe"
}
2 changes: 2 additions & 0 deletions internal/framework/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/api_token_permissions_groups"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/d1"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/dlp_datasets"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/email_routing_address"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/email_routing_rule"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/list_item"
Expand Down Expand Up @@ -345,6 +346,7 @@ func (p *CloudflareProvider) DataSources(ctx context.Context) []func() datasourc
api_token_permissions_groups.NewDataSource,
origin_ca_certificate.NewDataSource,
user.NewDataSource,
dlp_datasets.NewDataSource,
}
}

Expand Down
70 changes: 70 additions & 0 deletions internal/framework/service/dlp_datasets/data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dlp_datasets

import (
"context"
"fmt"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ datasource.DataSource = &CloudflareDlpDatasetsDataSource{}

func NewDataSource() datasource.DataSource {
return &CloudflareDlpDatasetsDataSource{}
}

type CloudflareDlpDatasetsDataSource struct {
client *cloudflare.API
}

func (d *CloudflareDlpDatasetsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_dlp_datasets"
}

func (d *CloudflareDlpDatasetsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*cloudflare.API)
if !ok {
resp.Diagnostics.AddError(
"Unexpected resource configure type",
fmt.Sprintf("Expected *cloudflare.API, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

d.client = client
}

func (d *CloudflareDlpDatasetsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data DlpDatasetsModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

accountDatasets, err := d.client.ListDLPDatasets(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()), cloudflare.ListDLPDatasetsParams{})
if err != nil {
resp.Diagnostics.AddError("failed to fetch DLP Datasets: %w", err.Error())
return
}

var datasets []*DlpDatasetModel
for _, dataset := range accountDatasets {
datasets = append(datasets, &DlpDatasetModel{
ID: types.StringValue(dataset.ID),
Name: types.StringValue(dataset.Name),
Description: types.StringValue(dataset.Description),
Status: types.StringValue(dataset.Status),
Secret: types.BoolValue(*dataset.Secret),
})
}

data.Datasets = datasets
resp.Diagnostics.Append(resp.State.Set(ctx, datasets)...)
if resp.Diagnostics.HasError() {
return
}
}
35 changes: 35 additions & 0 deletions internal/framework/service/dlp_datasets/data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dlp_datasets_test

import (
"fmt"
"os"
"testing"

"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareDlpDatasetsDataSourceConfig(accountID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.cloudflare_dlp_datasets.my_datasets", "account_id"),
),
},
},
})
}

func testAccCheckCloudflareDlpDatasetsDataSourceConfig(accountID string) string {
return fmt.Sprintf(`
data "cloudflare_dlp_datasets" "my_datasets" {
account_id = "%s"
}
`, accountID)
}
18 changes: 18 additions & 0 deletions internal/framework/service/dlp_datasets/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dlp_datasets

import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

type DlpDatasetsModel struct {
AccountID types.String `tfsdk:"account_id"`
Datasets []*DlpDatasetModel `tfsdk:"datasets"`
}

type DlpDatasetModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Status types.String `tfsdk:"status"`
Secret types.Bool `tfsdk:"secret"`
}
35 changes: 35 additions & 0 deletions internal/framework/service/dlp_datasets/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dlp_datasets

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func (d *CloudflareDlpDatasetsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Use this data source to retrieve all DLP datasets for an account.",
Attributes: map[string]schema.Attribute{
"account_id": schema.StringAttribute{
Required: true,
Description: "The account ID to fetch DLP Datasets from.",
},
"datasets": schema.ListAttribute{
Computed: true,
ElementType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"id": types.StringType,
"name": types.StringType,
"description": types.StringType,
"status": types.StringType,
"secret": types.BoolType,
},
},
Description: "A list of DLP Datasets.",
},
},
}
}

0 comments on commit adb1211

Please sign in to comment.