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: swap filtering to use API instead of custom logic
```
32 changes: 11 additions & 21 deletions internal/provider/resource_cloudflare_managed_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,19 @@ 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.ZoneIdentifier(zoneID), cloudflare.ListManagedHeadersParams{})
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)
}
}
headers, err := client.ListZoneManagedHeaders(ctx, cloudflare.ZoneIdentifier(zoneID), cloudflare.ListManagedHeadersParams{
Status: "enabled",
})

for _, header := range headers.ManagedResponseHeaders {
if header.Enabled {
enabledResponseHeaders = append(enabledResponseHeaders, header)
}
if err != nil {
return diag.FromErr(fmt.Errorf("error reading managed headers: %w", err))
}

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 @@ -148,7 +135,10 @@ func resourceCloudflareManagedHeadersDelete(ctx context.Context, d *schema.Resou
client := meta.(*cloudflare.API)
zoneID := d.Get("zone_id").(string)

headers, err := client.ListZoneManagedHeaders(ctx, cloudflare.ZoneIdentifier(zoneID), cloudflare.ListManagedHeadersParams{})
headers, err := client.ListZoneManagedHeaders(ctx, cloudflare.ZoneIdentifier(zoneID), cloudflare.ListManagedHeadersParams{
Status: "enabled",
})

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,7 +31,9 @@ func testSweepCloudflareManagedHeaders(r string) error {
return errors.New("CLOUDFLARE_ZONE_ID must be set")
}

managedHeaders, err := client.ListZoneManagedHeaders(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListManagedHeadersParams{})
managedHeaders, err := client.ListZoneManagedHeaders(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListManagedHeadersParams{
Status: "enabled",
})
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Failed to fetch Cloudflare Zone Managed Headers: %s", err))
}
Expand Down