From 60c180588a931e1b28920be7209aea66aeb5039d Mon Sep 17 00:00:00 2001 From: Felipe Bessa Coelho Date: Mon, 2 Sep 2024 18:12:57 +0100 Subject: [PATCH 1/4] add data source to fetch a zone's DCV Delegation identifier --- .changelog/3885.txt | 3 + docs/data-sources/dcv_delegation.md | 25 +++++++ internal/framework/provider/provider.go | 2 + .../service/dcv_delegation/data_source.go | 66 +++++++++++++++++++ .../dcv_delegation/data_source_test.go | 37 +++++++++++ .../framework/service/dcv_delegation/model.go | 9 +++ .../service/dcv_delegation/schema.go | 29 ++++++++ 7 files changed, 171 insertions(+) create mode 100644 .changelog/3885.txt create mode 100644 docs/data-sources/dcv_delegation.md create mode 100644 internal/framework/service/dcv_delegation/data_source.go create mode 100644 internal/framework/service/dcv_delegation/data_source_test.go create mode 100644 internal/framework/service/dcv_delegation/model.go create mode 100644 internal/framework/service/dcv_delegation/schema.go diff --git a/.changelog/3885.txt b/.changelog/3885.txt new file mode 100644 index 0000000000..dacc9a2e9c --- /dev/null +++ b/.changelog/3885.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +cloudflare_dcv_delegation +``` diff --git a/docs/data-sources/dcv_delegation.md b/docs/data-sources/dcv_delegation.md new file mode 100644 index 0000000000..7e5bef7c5a --- /dev/null +++ b/docs/data-sources/dcv_delegation.md @@ -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 + +### 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 + + diff --git a/internal/framework/provider/provider.go b/internal/framework/provider/provider.go index f7f49679c3..6226c8a90e 100644 --- a/internal/framework/provider/provider.go +++ b/internal/framework/provider/provider.go @@ -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" @@ -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, } } diff --git a/internal/framework/service/dcv_delegation/data_source.go b/internal/framework/service/dcv_delegation/data_source.go new file mode 100644 index 0000000000..ab9f07a295 --- /dev/null +++ b/internal/framework/service/dcv_delegation/data_source.go @@ -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 = &CloudflareDCVDelegationDataSource{} + +func NewDataSource() datasource.DataSource { + return &CloudflareDCVDelegationDataSource{} +} + +type CloudflareDCVDelegationDataSource struct { + client *muxclient.Client +} + +func (r *CloudflareDCVDelegationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_dcv_delegation" +} + +func (r *CloudflareDCVDelegationDataSource) 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 *CloudflareDCVDelegationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data CloudflareDCVDelegationModel + 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 = CloudflareDCVDelegationModel{ + 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)...) +} diff --git a/internal/framework/service/dcv_delegation/data_source_test.go b/internal/framework/service/dcv_delegation/data_source_test.go new file mode 100644 index 0000000000..9753a2b444 --- /dev/null +++ b/internal/framework/service/dcv_delegation/data_source_test.go @@ -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) +} diff --git a/internal/framework/service/dcv_delegation/model.go b/internal/framework/service/dcv_delegation/model.go new file mode 100644 index 0000000000..4cfa20b197 --- /dev/null +++ b/internal/framework/service/dcv_delegation/model.go @@ -0,0 +1,9 @@ +package dcv_delegation + +import "github.com/hashicorp/terraform-plugin-framework/types" + +type CloudflareDCVDelegationModel struct { + ID types.String `tfsdk:"id"` + ZoneID types.String `tfsdk:"zone_id"` + Hostname types.String `tfsdk:"hostname"` +} diff --git a/internal/framework/service/dcv_delegation/schema.go b/internal/framework/service/dcv_delegation/schema.go new file mode 100644 index 0000000000..792d967144 --- /dev/null +++ b/internal/framework/service/dcv_delegation/schema.go @@ -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 *CloudflareDCVDelegationDataSource) 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, + }, + }, + } +} From 03cc98fbf875ba09522651e1a4bf283aaed529ad Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 3 Sep 2024 08:53:09 +1000 Subject: [PATCH 2/4] remove `cloudflare` prefix --- .../service/dcv_delegation/data_source.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/framework/service/dcv_delegation/data_source.go b/internal/framework/service/dcv_delegation/data_source.go index ab9f07a295..f44d58fdcd 100644 --- a/internal/framework/service/dcv_delegation/data_source.go +++ b/internal/framework/service/dcv_delegation/data_source.go @@ -10,21 +10,21 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" ) -var _ datasource.DataSource = &CloudflareDCVDelegationDataSource{} +var _ datasource.DataSource = &DCVDelegationDataSource{} func NewDataSource() datasource.DataSource { - return &CloudflareDCVDelegationDataSource{} + return &DCVDelegationDataSource{} } -type CloudflareDCVDelegationDataSource struct { +type DCVDelegationDataSource struct { client *muxclient.Client } -func (r *CloudflareDCVDelegationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { +func (r *DCVDelegationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_dcv_delegation" } -func (r *CloudflareDCVDelegationDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { +func (r *DCVDelegationDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { if req.ProviderData == nil { return } @@ -43,8 +43,8 @@ func (r *CloudflareDCVDelegationDataSource) Configure(ctx context.Context, req d r.client = client } -func (r *CloudflareDCVDelegationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var data CloudflareDCVDelegationModel +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 @@ -56,7 +56,7 @@ func (r *CloudflareDCVDelegationDataSource) Read(ctx context.Context, req dataso return } - data = CloudflareDCVDelegationModel{ + data = DCVDelegationModel{ ZoneID: types.StringValue(data.ZoneID.ValueString()), ID: types.StringValue(dcv.UUID), Hostname: types.StringValue(fmt.Sprintf("%s.dcv.cloudflare.com", dcv.UUID)), From 023d24ecfedaa8268546cdd70f7c3239810420b0 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 3 Sep 2024 08:53:28 +1000 Subject: [PATCH 3/4] remove `cloudflare` prefix --- internal/framework/service/dcv_delegation/model.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/framework/service/dcv_delegation/model.go b/internal/framework/service/dcv_delegation/model.go index 4cfa20b197..85fb62a2dc 100644 --- a/internal/framework/service/dcv_delegation/model.go +++ b/internal/framework/service/dcv_delegation/model.go @@ -2,7 +2,7 @@ package dcv_delegation import "github.com/hashicorp/terraform-plugin-framework/types" -type CloudflareDCVDelegationModel struct { +type DCVDelegationModel struct { ID types.String `tfsdk:"id"` ZoneID types.String `tfsdk:"zone_id"` Hostname types.String `tfsdk:"hostname"` From 709ce0ba79927b482d875ef6265836c5ed53ab79 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 3 Sep 2024 08:53:45 +1000 Subject: [PATCH 4/4] remove `cloudflare` prefix --- internal/framework/service/dcv_delegation/schema.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/framework/service/dcv_delegation/schema.go b/internal/framework/service/dcv_delegation/schema.go index 792d967144..1840713aed 100644 --- a/internal/framework/service/dcv_delegation/schema.go +++ b/internal/framework/service/dcv_delegation/schema.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" ) -func (r *CloudflareDCVDelegationDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { +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{