Skip to content

Commit

Permalink
Merge pull request #3885 from fcoelho/master
Browse files Browse the repository at this point in the history
add data source to fetch a zone's DCV Delegation identifier
  • Loading branch information
jacobbednarz authored Sep 2, 2024
2 parents 18139e0 + 709ce0b commit c0931fc
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3885.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
cloudflare_dcv_delegation
```
25 changes: 25 additions & 0 deletions docs/data-sources/dcv_delegation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
page_title: "cloudflare_dcv_delegation Data Source - Cloudflare"
subcategory: ""
description: |-
Use this data source to retrieve the DCV Delegation unique identifier for a zone.
---

# cloudflare_dcv_delegation (Data Source)

Use this data source to retrieve the DCV Delegation unique identifier for a zone.


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

### Required

- `zone_id` (String) The zone identifier to target for the resource.

### Read-Only

- `hostname` (String) The DCV Delegation hostname
- `id` (String) The DCV Delegation unique identifier


2 changes: 2 additions & 0 deletions internal/framework/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/api_token_permissions_groups"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/cloud_connector_rules"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/d1"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/dcv_delegation"
"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"
Expand Down Expand Up @@ -391,6 +392,7 @@ func (p *CloudflareProvider) DataSources(ctx context.Context) []func() datasourc
dlp_datasets.NewDataSource,
gateway_categories.NewDataSource,
gateway_app_types.NewDataSource,
dcv_delegation.NewDataSource,
}
}

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

import (
"context"
"fmt"

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

var _ datasource.DataSource = &DCVDelegationDataSource{}

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

type DCVDelegationDataSource struct {
client *muxclient.Client
}

func (r *DCVDelegationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_dcv_delegation"
}

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

client, ok := req.ProviderData.(*muxclient.Client)

if !ok {
resp.Diagnostics.AddError(
"unexpected resource configure type",
fmt.Sprintf("Expected *muxclient.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

r.client = client
}

func (r *DCVDelegationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data DCVDelegationModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

dcv, _, err := r.client.V1.GetDCVDelegation(ctx, cloudflare.ZoneIdentifier(data.ZoneID.ValueString()), cloudflare.GetDCVDelegationParams{})
if err != nil {
resp.Diagnostics.AddError("failed to fetch DCV Delegation", err.Error())
return
}

data = DCVDelegationModel{
ZoneID: types.StringValue(data.ZoneID.ValueString()),
ID: types.StringValue(dcv.UUID),
Hostname: types.StringValue(fmt.Sprintf("%s.dcv.cloudflare.com", dcv.UUID)),
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
37 changes: 37 additions & 0 deletions internal/framework/service/dcv_delegation/data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dcv_delegation_test

import (
"fmt"
"os"
"testing"

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

func TestAccCloudflareDCVDelegationDataSource(t *testing.T) {
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareDCVDelegationDataSourceConfig(zoneID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.cloudflare_dcv_delegation.test", "id"),
resource.TestCheckResourceAttrSet("data.cloudflare_dcv_delegation.test", "hostname"),
resource.TestCheckResourceAttrSet("data.cloudflare_dcv_delegation.test", "zone_id"),
),
},
},
})
}

func testAccCheckCloudflareDCVDelegationDataSourceConfig(zoneID string) string {
return fmt.Sprintf(`
data "cloudflare_dcv_delegation" "test" {
zone_id = "%s"
}
`, zoneID)
}
9 changes: 9 additions & 0 deletions internal/framework/service/dcv_delegation/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dcv_delegation

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

type DCVDelegationModel struct {
ID types.String `tfsdk:"id"`
ZoneID types.String `tfsdk:"zone_id"`
Hostname types.String `tfsdk:"hostname"`
}
29 changes: 29 additions & 0 deletions internal/framework/service/dcv_delegation/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dcv_delegation

import (
"context"

"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)

func (r *DCVDelegationDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Use this data source to retrieve the DCV Delegation unique identifier for a zone.",
Attributes: map[string]schema.Attribute{
consts.ZoneIDSchemaKey: schema.StringAttribute{
MarkdownDescription: consts.ZoneIDSchemaDescription,
Required: true,
},
"id": schema.StringAttribute{
Description: "The DCV Delegation unique identifier",
Computed: true,
},
"hostname": schema.StringAttribute{
Description: "The DCV Delegation hostname",
Computed: true,
},
},
}
}

0 comments on commit c0931fc

Please sign in to comment.