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

Replace ids with checksum of the resource ids #869

Merged
merged 5 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cloudflare/data_source_waf_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"regexp"
"time"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -110,6 +109,7 @@ func dataSourceCloudflareWAFGroupsRead(d *schema.ResourceData, meta interface{})
}

log.Printf("[DEBUG] Reading WAF Groups")
groupIds := make([]string, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see where we are appending to this slice. Am I missing it?

groupDetails := make([]interface{}, 0)
for _, pkg := range pkgList {
groupList, err := client.ListWAFGroups(zoneID, pkg.ID)
Expand All @@ -135,6 +135,7 @@ func dataSourceCloudflareWAFGroupsRead(d *schema.ResourceData, meta interface{})
"modified_rules_count": group.ModifiedRulesCount,
"package_id": pkg.ID,
})
groupIds = append(groupIds, group.ID)
}
}

Expand All @@ -143,7 +144,7 @@ func dataSourceCloudflareWAFGroupsRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error setting WAF groups: %s", err)
}

d.SetId("WAFGroups " + time.Now().UTC().String())
d.SetId(stringListChecksum(groupIds))
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions cloudflare/data_source_waf_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"regexp"
"time"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -96,6 +95,7 @@ func dataSourceCloudflareWAFPackagesRead(d *schema.ResourceData, meta interface{
}

log.Printf("[DEBUG] Reading WAF Packages")
packageIds := make([]string, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packageDetails := make([]interface{}, 0)
pkgList, err := client.ListWAFPackages(zoneID)
if err != nil {
Expand Down Expand Up @@ -127,14 +127,15 @@ func dataSourceCloudflareWAFPackagesRead(d *schema.ResourceData, meta interface{
"sensitivity": pkg.Sensitivity,
"action_mode": pkg.ActionMode,
})
packageIds = append(packageIds, pkg.ID)
}

err = d.Set("packages", packageDetails)
if err != nil {
return fmt.Errorf("Error setting WAF packages: %s", err)
}

d.SetId("WAFPackages " + time.Now().UTC().String())
d.SetId(stringListChecksum(packageIds))
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions cloudflare/data_source_waf_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"regexp"
"time"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -119,6 +118,7 @@ func dataSourceCloudflareWAFRulesRead(d *schema.ResourceData, meta interface{})
}

log.Printf("[DEBUG] Reading WAF Rules")
ruleIds := make([]string, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruleDetails := make([]interface{}, 0)
for _, pkg := range pkgList {
ruleList, err := client.ListWAFRules(zoneID, pkg.ID)
Expand Down Expand Up @@ -155,6 +155,7 @@ func dataSourceCloudflareWAFRulesRead(d *schema.ResourceData, meta interface{})
"package_id": pkg.ID,
"allowed_modes": rule.AllowedModes,
})
ruleIds = append(ruleIds, rule.ID)
}

if foundGroup {
Expand All @@ -170,7 +171,7 @@ func dataSourceCloudflareWAFRulesRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error setting WAF rules: %s", err)
}

d.SetId(fmt.Sprintf("WAFRules_%s", time.Now().UTC().String()))
d.SetId(stringListChecksum(ruleIds))
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions cloudflare/data_source_zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"regexp"
"time"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -93,6 +92,7 @@ func dataSourceCloudflareZonesRead(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("error listing Zone: %s", err)
}

zoneIds := make([]string, 0)
zoneDetails := make([]interface{}, 0)
for _, v := range zones.Result {
if filter.regexValue != nil {
Expand All @@ -109,15 +109,15 @@ func dataSourceCloudflareZonesRead(d *schema.ResourceData, meta interface{}) err
"id": v.ID,
"name": v.Name,
})
zoneIds = append(zoneIds, v.ID)
}

err = d.Set("zones", zoneDetails)
if err != nil {
return fmt.Errorf("Error setting zones: %s", err)
}

d.SetId(time.Now().UTC().String())

d.SetId(stringListChecksum(zoneIds))
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions cloudflare/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"regexp"
"sort"
"strings"

cloudflare "github.com/cloudflare/cloudflare-go"
Expand Down Expand Up @@ -66,6 +67,11 @@ func stringChecksum(s string) string {
return fmt.Sprintf("%x", bs)
}

func stringListChecksum(s []string) string {
sort.Strings(s)
return stringChecksum(strings.Join(s, ""))
}

// Returns true if string value exists in string slice
func contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
Expand Down