Skip to content

Commit

Permalink
Add filtering flag for managed headers API (#1765)
Browse files Browse the repository at this point in the history
Add filtering flag for managed headers API
  • Loading branch information
WowVeryLogin authored Jul 26, 2022
1 parent 00ee985 commit dbd8d9c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
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

0 comments on commit dbd8d9c

Please sign in to comment.