@@ -450,7 +450,7 @@ func ResourceIBMISNetworkACLValidator() *validate.ResourceValidator {
450450 MaxValueLength : 128 })
451451 validateSchema = append (validateSchema ,
452452 validate.ValidateSchema {
453- Identifier : isSecurityGroupRuleProtocol ,
453+ Identifier : isNetworkACLRuleProtocol ,
454454 ValidateFunctionIdentifier : validate .ValidateAllowedStringValue ,
455455 Type : validate .TypeString ,
456456 AllowedValues : protocol })
@@ -504,7 +504,7 @@ func nwaclCreate(context context.Context, d *schema.ResourceData, meta interface
504504 if rls , ok := d .GetOk (isNetworkACLRules ); ok {
505505 rules = rls .([]interface {})
506506 }
507- err = validateInlineRules (rules )
507+ err = validateInlineRules (d , rules )
508508 if err != nil {
509509 return flex .DiscriminatedTerraformErrorf (err , err .Error (), "ibm_is_network_acl" , "create" , "validate-inline-rules" ).GetDiag ()
510510 }
@@ -531,7 +531,7 @@ func nwaclCreate(context context.Context, d *schema.ResourceData, meta interface
531531 return tfErr .GetDiag ()
532532 }
533533
534- err = createInlineRules (sess , nwaclid , rules )
534+ err = createInlineRules (d , sess , nwaclid , rules )
535535 if err != nil {
536536 tfErr := flex .TerraformErrorf (err , fmt .Sprintf ("createInlineRules failed: %s" , err .Error ()), "ibm_is_network_acl" , "create" )
537537 log .Printf ("[DEBUG]\n %s" , tfErr .GetDebugMessage ())
@@ -865,7 +865,7 @@ func nwaclUpdate(context context.Context, d *schema.ResourceData, meta interface
865865 }
866866 }
867867 if d .HasChange (isNetworkACLRules ) {
868- err := validateInlineRules (rules )
868+ err := validateInlineRules (d , rules )
869869 if err != nil {
870870 tfErr := flex .TerraformErrorf (err , fmt .Sprintf ("validateInlineRules failed: %s" , err .Error ()), "ibm_is_network_acl" , "update" )
871871 log .Printf ("[DEBUG]\n %s" , tfErr .GetDebugMessage ())
@@ -879,7 +879,7 @@ func nwaclUpdate(context context.Context, d *schema.ResourceData, meta interface
879879 return tfErr .GetDiag ()
880880 }
881881 //Create the rules as per the def
882- err = createInlineRules (sess , id , rules )
882+ err = createInlineRules (d , sess , id , rules )
883883 if err != nil {
884884 tfErr := flex .TerraformErrorf (err , fmt .Sprintf ("createInlineRules failed: %s" , err .Error ()), "ibm_is_network_acl" , "update" )
885885 log .Printf ("[DEBUG]\n %s" , tfErr .GetDebugMessage ())
@@ -1022,8 +1022,8 @@ func clearRules(nwaclC *vpcv1.VpcV1, nwaclid string) error {
10221022 return nil
10231023}
10241024
1025- func validateInlineRules (rules []interface {}) error {
1026- for _ , rule := range rules {
1025+ func validateInlineRules (d * schema. ResourceData , rules []interface {}) error {
1026+ for i , rule := range rules {
10271027 rulex := rule .(map [string ]interface {})
10281028 action := rulex [isNetworkACLRuleAction ].(string )
10291029 if (action != "allow" ) && (action != "deny" ) {
@@ -1042,41 +1042,42 @@ func validateInlineRules(rules []interface{}) error {
10421042 }
10431043
10441044 protocol := rulex [isNetworkACLRuleProtocol ]
1045- icmpType := rulex [ isNetworkACLRuleICMPType ]
1046- icmpCode := rulex [ isNetworkACLRuleICMPCode ]
1047- portMin := rulex [ isNetworkACLRulePortMin ]
1048- portMax := rulex [ isNetworkACLRulePortMax ]
1049- srcPortMin := rulex [ isNetworkACLRuleSourcePortMin ]
1050- srcPortMax := rulex [ isNetworkACLRuleSourcePortMax ]
1051-
1045+ icmpType := fmt . Sprintf ( "rules.%d.type" , i )
1046+ icmpCode := fmt . Sprintf ( "rules.%d.code" , i )
1047+ portMin := fmt . Sprintf ( "rules.%d.port_min" , i )
1048+ portMax := fmt . Sprintf ( "rules.%d.port_max" , i )
1049+ srcPortMin := fmt . Sprintf ( "rules.%d.source_port_min" , i )
1050+ srcPortMax := fmt . Sprintf ( "rules.%d.source_port_max" , i )
1051+ var okIcmpType , okIcmpCode bool
10521052 if protocol != "icmp" && protocol != "" {
1053- if icmpType != nil && icmpType != 0 {
1053+ if _ , ok := d . GetOk ( icmpType ); ok {
10541054 return fmt .Errorf ("attribute 'type' conflicts with protocol %q; 'type' is only valid for icmp protocol" , protocol )
10551055 }
1056- if icmpCode != nil && icmpCode != 0 {
1056+ if _ , ok := d . GetOk ( icmpCode ); ok {
10571057 return fmt .Errorf ("attribute 'code' conflicts with protocol %q; 'code' is only valid for icmp protocol" , protocol )
10581058 }
10591059 }
10601060
10611061 if protocol == "icmp" {
1062- if (icmpType != nil && icmpCode == nil ) || (icmpType == nil && icmpCode != nil ) {
1062+ _ , okIcmpType = d .GetOk (icmpType )
1063+ _ , okIcmpCode = d .GetOk (icmpCode )
1064+ if (okIcmpType && ! okIcmpCode ) || (! okIcmpType && okIcmpCode ) {
10631065 return fmt .Errorf ("'code' and 'type' must both be specified together for icmp protocol" )
10641066 }
10651067 }
10661068
10671069 if protocol != "tcp" && protocol != "udp" && protocol != "" {
1068- fmt .Println ("Inside Print the protocol value " , protocol )
1069- if portMin != nil && portMin != 0 {
1070+ if _ , ok := d .GetOk (portMin ); ok {
10701071 return fmt .Errorf ("attribute 'port_min' conflicts with protocol %s; ports apply only to tcp/udp protocol" , protocol )
10711072 }
1072- if portMax != nil && portMax != 0 {
1073+ if _ , ok := d . GetOk ( portMax ); ok {
10731074 return fmt .Errorf ("attribute 'port_max' conflicts with protocol %s; ports apply only to tcp/udp protocol" , protocol )
10741075 }
10751076
1076- if srcPortMin != nil && srcPortMin != 0 {
1077+ if _ , ok := d . GetOk ( srcPortMin ); ok {
10771078 return fmt .Errorf ("attribute 'source_port_min' conflicts with protocol %s; ports apply only to tcp/udp protocol" , protocol )
10781079 }
1079- if srcPortMax != nil && srcPortMax != 0 {
1080+ if _ , ok := d . GetOk ( srcPortMax ); ok {
10801081 return fmt .Errorf ("attribute 'source_port_max' conflicts with protocol %s; ports apply only to tcp/udp protocol" , protocol )
10811082 }
10821083 }
@@ -1085,7 +1086,7 @@ func validateInlineRules(rules []interface{}) error {
10851086 return nil
10861087}
10871088
1088- func createInlineRules (nwaclC * vpcv1.VpcV1 , nwaclid string , rules []interface {}) error {
1089+ func createInlineRules (d * schema. ResourceData , nwaclC * vpcv1.VpcV1 , nwaclid string , rules []interface {}) error {
10891090 before := ""
10901091
10911092 for i := 0 ; i <= len (rules )- 1 ; i ++ {
@@ -1143,12 +1144,14 @@ func createInlineRules(nwaclC *vpcv1.VpcV1, nwaclid string, rules []interface{})
11431144 }
11441145 }
11451146 } else if protocol == "icmp" {
1147+ icmpType := fmt .Sprintf ("rules.%d.type" , i )
1148+ icmpCode := fmt .Sprintf ("rules.%d.code" , i )
11461149 ruleTemplate .Protocol = & protocol
1147- if val , ok := rulex [ "type" ] ; ok {
1150+ if val , ok := d . GetOk ( icmpType ) ; ok {
11481151 icmptype = int64 (val .(int ))
11491152 ruleTemplate .Type = & icmptype
11501153 }
1151- if val , ok := rulex [ "code" ] ; ok {
1154+ if val , ok := d . GetOk ( icmpCode ) ; ok {
11521155 icmpcode = int64 (val .(int ))
11531156 ruleTemplate .Code = & icmpcode
11541157 }
0 commit comments