Skip to content

Commit

Permalink
Add filtering flag for managed headers API
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Davydov committed Jul 12, 2022
1 parent c3988d1 commit 409df6a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
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{
OnlyEnabled: true,
})
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,
})
if err != nil {
return diag.FromErr(fmt.Errorf("error reading managed headers: %w", err))
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/resource_cloudflare_managed_headers_test.go
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,
})
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Failed to fetch Cloudflare Zone Managed Headers: %s", err))
Expand Down

0 comments on commit 409df6a

Please sign in to comment.