// Given a set of SG rules (ingress/egress blocks), this function
// will group the rules by from_port/to_port/protocol/description
// tuples. This is inverse operation of
// resourceAwsSecurityGroupExpandRules()
//
// For more detail, see comments for
// resourceAwsSecurityGroupExpandRules()
Given a set of SG rules (ingress/egress blocks), this function
will group the rules by from_port/to_port/protocol/description
tuples. This is inverse operation of
resourceAwsSecurityGroupExpandRules()

For more detail, see comments for
resourceAwsSecurityGroupExpandRules()
// Given a set of SG rules ( ingress / egress blocks ) this function // will group the rules by from_port / to_port / protocol / description // tuples . This is inverse operation of // resourceAwsSecurityGroupExpandRules () // // For more detail see comments for // resourceAwsSecurityGroupExpandRules ()
func resourceAwsSecurityGroupCollapseRules(ruleset string, rules []interface{}) []interface{} {
	var keys_to_collapse = []string{"cidr_blocks", "ipv6_cidr_blocks", "prefix_list_ids", "security_groups"}
	collapsed := make(map[string]map[string]interface{})
	for _, rule := range rules {
		r := rule.(map[string]interface{})
		ruleHash := idCollapseHash(ruleset, r["protocol"].(string), int64(r["to_port"].(int)), int64(r["from_port"].(int)), r["description"].(string))
		if _, ok := collapsed[ruleHash]; ok {
			if v, ok := r["self"]; ok && v.(bool) {
				collapsed[ruleHash]["self"] = r["self"]
			}
		} else {
			collapsed[ruleHash] = r
			continue
		}
		for _, key := range keys_to_collapse {
			if _, ok := r[key]; ok {
				if _, ok := collapsed[ruleHash][key]; ok {
					if key == "security_groups" {
						collapsed[ruleHash][key] = collapsed[ruleHash][key].(*schema.Set).Union(r[key].(*schema.Set))
					} else {
						collapsed[ruleHash][key] = append(collapsed[ruleHash][key].([]interface{}), r[key].([]interface{})...)
					}
				} else {
					collapsed[ruleHash][key] = r[key]
				}
			}
		}
	}
	values := make([]interface{}, 0, len(collapsed))
	for _, val := range collapsed {
		values = append(values, val)
	}
	return values
}
func resourceAwsSecurityGroupCollapseRules ( ruleset string , rules [ ] interface { } ) [ ] interface { } { var keys_to_collapse = [ ] string { " " , " " , " " , " " } collapsed := make ( map [ string ] map [ string ] interface { } ) for _ , rule := range rules { r := rule . ( map [ string ] interface { } ) ruleHash := idCollapseHash ( ruleset , r [ " " ] . ( string ) , int64 ( r [ " " ] . ( int ) ) , int64 ( r [ " " ] . ( int ) ) , r [ " " ] . ( string ) ) if _ , ok := collapsed [ ruleHash ] ; ok { if v , ok := r [ " " ] ; ok && v . ( bool ) { collapsed [ ruleHash ] [ " " ] = r [ " " ] } } else { collapsed [ ruleHash ] = r continue } for _ , key := range keys_to_collapse { if _ , ok := r [ key ] ; ok { if _ , ok := collapsed [ ruleHash ] [ key ] ; ok { if key == " " { collapsed [ ruleHash ] [ key ] = collapsed [ ruleHash ] [ key ] . ( * schema . Set ) . Union ( r [ key ] . ( * schema . Set ) ) } else { collapsed [ ruleHash ] [ key ] = append ( collapsed [ ruleHash ] [ key ] . ( [ ] interface { } ) , r [ key ] . ( [ ] interface { } ) ... ) } } else { collapsed [ ruleHash ] [ key ] = r [ key ] } } } } values := make ( [ ] interface { } , 0 , len ( collapsed ) ) for _ , val := range collapsed { values = append ( values , val ) } return values }
Given a set of SG rules (ingress/egress blocks), this function
will group the rules by from_port/to_port/protocol/description
tuples.
Given a set of SG rules ( ingress / egress blocks ) this function will group the rules by from_port / to_port / protocol / description tuples .