-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3885 from fcoelho/master
add data source to fetch a zone's DCV Delegation identifier
- Loading branch information
Showing
7 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
cloudflare_dcv_delegation | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
internal/framework/service/dcv_delegation/data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
} | ||
} |