Skip to content

Commit

Permalink
Use TypeSet instead of TypeList for filters attributes
Browse files Browse the repository at this point in the history
Cause filters and its child attributes don't care about ordering of
values.
  • Loading branch information
0gajun committed Mar 22, 2022
1 parent c9dc2e4 commit fe50c84
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
27 changes: 24 additions & 3 deletions cloudflare/resource_cloudflare_notification_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func resourceCloudflareNotificationPolicyRead(d *schema.ResourceData, meta inter
d.Set("created", policy.Result.Created.Format(time.RFC3339))
d.Set("modified", policy.Result.Modified.Format(time.RFC3339))

if err := d.Set("filters", policy.Result.Filters); err != nil {
if err := d.Set("filters", setNotificationFilters(policy.Result.Filters)); err != nil {
return fmt.Errorf("failed to set filters: %s", err)
}

Expand Down Expand Up @@ -129,7 +129,6 @@ func buildNotificationPolicy(d *schema.ResourceData) cloudflare.NotificationPoli
notificationPolicy := cloudflare.NotificationPolicy{}
notificationPolicy.Mechanisms = make(map[string]cloudflare.NotificationMechanismIntegrations)
notificationPolicy.Conditions = make(map[string]interface{})
notificationPolicy.Filters = make(map[string][]string)

if name, ok := d.GetOk("name"); ok {
notificationPolicy.Name = name.(string)
Expand Down Expand Up @@ -160,7 +159,7 @@ func buildNotificationPolicy(d *schema.ResourceData) cloudflare.NotificationPoli
}

if filters, ok := d.GetOk("filters"); ok {
notificationPolicy.Filters = filters.([]interface{})[0].(map[string][]string)
notificationPolicy.Filters = getNotificationFilters(filters.(*schema.Set))
}

if conditions, ok := d.GetOk("conditions"); ok {
Expand Down Expand Up @@ -197,3 +196,25 @@ func setNotificationMechanisms(md []cloudflare.NotificationMechanismData) *schem

return schema.NewSet(schema.HashResource(mechanismData), mechanisms)
}

func getNotificationFilters(s *schema.Set) map[string][]string {
filters := make(map[string][]string)

for _, v := range s.List() {
for key, set := range v.(map[string]interface{}) {
filters[key] = expandInterfaceToStringList(set.(*schema.Set).List())
}
}

return filters
}

func setNotificationFilters(m map[string][]string) []interface{} {
filters := map[string]*schema.Set{}

for k, v := range m {
filters[k] = expandStringListToSet(v)
}

return []interface{}{filters}
}
6 changes: 3 additions & 3 deletions cloudflare/schema_cloudflare_notification_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ func resourceCloudflareNotificationPolicySchema() map[string]*schema.Schema {
Required: true,
},
"filters": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"zones": {
Type: schema.TypeList,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
"services": {
Type: schema.TypeList,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Expand Down

0 comments on commit fe50c84

Please sign in to comment.