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

Add filtering flag for managed headers API #1765

3 changes: 3 additions & 0 deletions .changelog/1765.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/cloudflare_managed_headers: Use API level filtering
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
```
29 changes: 6 additions & 23 deletions internal/provider/resource_cloudflare_managed_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,17 @@ func resourceCloudflareManagedHeadersCreate(ctx context.Context, d *schema.Resou
func resourceCloudflareManagedHeadersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)
headers, err := client.ListZoneManagedHeaders(ctx, cloudflare.ListManagedHeadersParams{
ZoneID: zoneID,
headers, err := client.ListZoneManagedHeaders(ctx, client.ZoneIdentifier(zoneID), cloudflare.ListManagedHeadersParams{
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
Status: "enabled",
})
if err != nil {
return diag.FromErr(fmt.Errorf("error reading managed headers: %w", err))
}

// Filter out headers that are not enabled. This will eventually move into
// the API endpoint or the SDK.
var enabledRequestHeaders []cloudflare.ManagedHeader
var enabledResponseHeaders []cloudflare.ManagedHeader

for _, header := range headers.ManagedRequestHeaders {
if header.Enabled {
enabledRequestHeaders = append(enabledRequestHeaders, header)
}
}

for _, header := range headers.ManagedResponseHeaders {
if header.Enabled {
enabledResponseHeaders = append(enabledResponseHeaders, header)
}
}

if err := d.Set("managed_request_headers", buildResourceFromManagedHeaders(enabledRequestHeaders)); err != nil {
if err := d.Set("managed_request_headers", buildResourceFromManagedHeaders(headers.ManagedRequestHeaders)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("managed_response_headers", buildResourceFromManagedHeaders(enabledResponseHeaders)); err != nil {
if err := d.Set("managed_response_headers", buildResourceFromManagedHeaders(headers.ManagedResponseHeaders)); err != nil {
return diag.FromErr(err)
}
return nil
Expand Down Expand Up @@ -151,8 +134,8 @@ func resourceCloudflareManagedHeadersDelete(ctx context.Context, d *schema.Resou
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)

headers, err := client.ListZoneManagedHeaders(ctx, cloudflare.ListManagedHeadersParams{
ZoneID: zoneID,
headers, err := client.ListZoneManagedHeaders(ctx, client.ZoneIdentifier(zoneID), cloudflare.ListManagedHeadersParams{
OnlyEnabled: true,
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
})
if err != nil {
return diag.FromErr(fmt.Errorf("error reading managed headers: %w", err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func testSweepCloudflareManagedHeaders(r string) error {
return errors.New("CLOUDFLARE_ZONE_ID must be set")
}

managedHeaders, err := client.ListZoneManagedHeaders(context.Background(), cloudflare.ListManagedHeadersParams{
ZoneID: zoneID,
managedHeaders, err := client.ListZoneManagedHeaders(context.Background(), client.ZoneIdentifier(zoneID), cloudflare.ListManagedHeadersParams{
OnlyEnabled: true,
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
})
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Failed to fetch Cloudflare Zone Managed Headers: %s", err))
Expand Down