Skip to content

Commit

Permalink
Merge pull request #869 from pecigonzalo/feature/add-sha-ids
Browse files Browse the repository at this point in the history
Replace ids with checksum of the resource ids
  • Loading branch information
jacobbednarz authored Nov 20, 2020
2 parents 01129ae + 65bf2a1 commit 264d0c1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
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)
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)
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)
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

0 comments on commit 264d0c1

Please sign in to comment.