@@ -93,18 +93,13 @@ func resourceTencentCloudVpcACLCreate(d *schema.ResourceData, meta interface{})
9393 logId = getLogId (contextNil )
9494 ctx = context .WithValue (context .TODO (), logIdKey , logId )
9595 vpcService = VpcService {client : meta .(* TencentCloudClient ).apiV3Conn }
96- vpcID string
97- name string
98- ingress []VpcACLRule
99- egress []VpcACLRule
96+
97+ ingress []VpcACLRule
98+ egress []VpcACLRule
99+ vpcID = d .Get ("vpc_id" ).(string )
100+ name = d .Get ("name" ).(string )
100101 )
101102
102- if temp , ok := d .GetOk ("vpc_id" ); ok {
103- vpcID = temp .(string )
104- }
105- if temp , ok := d .GetOk ("name" ); ok {
106- name = temp .(string )
107- }
108103 if temp , ok := d .GetOk ("ingress" ); ok {
109104 ingressStrs := helper .InterfacesStrings (temp .([]interface {}))
110105 for _ , ingressStr := range ingressStrs {
@@ -131,13 +126,12 @@ func resourceTencentCloudVpcACLCreate(d *schema.ResourceData, meta interface{})
131126 return err
132127 }
133128
129+ d .SetId (aclID )
134130 err = vpcService .AttachRulesToACL (ctx , aclID , ingress , egress )
135131 if err != nil {
136132 return err
137133 }
138134
139- d .SetId (aclID )
140-
141135 return resourceTencentCloudVpcACLRead (d , meta )
142136}
143137
@@ -152,7 +146,7 @@ func resourceTencentCloudVpcACLRead(d *schema.ResourceData, meta interface{}) er
152146 id = d .Id ()
153147 )
154148
155- vpcID , createTime , has , err := service .DescribeNetWorkByACLID (ctx , id )
149+ info , has , err := service .DescribeNetWorkByACLID (ctx , id )
156150 if err != nil {
157151 return err
158152 }
@@ -167,128 +161,111 @@ func resourceTencentCloudVpcACLRead(d *schema.ResourceData, meta interface{}) er
167161 return errRet
168162 }
169163
170- _ = d .Set ("vpc_id" , vpcID )
171- _ = d .Set ("create_time" , createTime )
164+ _ = d .Set ("vpc_id" , info .VpcId )
165+ _ = d .Set ("create_time" , info .CreatedTime )
166+ _ = d .Set ("name" , info .NetworkAclName )
167+ egressList := make ([]map [string ]interface {}, 0 , len (info .EgressEntries ))
168+ for i := range info .EgressEntries {
169+ result := map [string ]interface {}{
170+ "protocol" : info .EgressEntries [i ].Protocol ,
171+ "port" : info .EgressEntries [i ].Port ,
172+ "cidr_ip" : info .EgressEntries [i ].CidrBlock ,
173+ "policy" : info .EgressEntries [i ].Action ,
174+ }
175+ egressList = append (egressList , result )
176+ }
177+
178+ ingressList := make ([]map [string ]interface {}, 0 , len (info .IngressEntries ))
179+ for i := range info .IngressEntries {
180+ result := map [string ]interface {}{
181+ "protocol" : info .IngressEntries [i ].Protocol ,
182+ "port" : info .IngressEntries [i ].Port ,
183+ "cidr_ip" : info .IngressEntries [i ].CidrBlock ,
184+ "policy" : info .IngressEntries [i ].Action ,
185+ }
186+ ingressList = append (ingressList , result )
187+ }
188+ _ = d .Set ("egress" , egressList )
189+ _ = d .Set ("ingress" , ingressList )
190+
172191 return nil
173192}
174193
175194func resourceTencentCloudVpcACLUpdate (d * schema.ResourceData , meta interface {}) error {
176195 defer logElapsed ("resource.tencentcloud_vpc_acl.update" )()
177196
178- const (
179- DeleteAll int8 = iota
180- DeleteIngress
181- DeleteEgress
182- )
183-
184197 var (
185198 logId = getLogId (contextNil )
186199 ctx = context .WithValue (context .TODO (), logIdKey , logId )
187200 service = VpcService {client : meta .(* TencentCloudClient ).apiV3Conn }
188201 id = d .Id ()
189202
190- name * string
191- ingress []VpcACLRule
192- egress []VpcACLRule
193- deleteIngress bool
194- deleteEgress bool
203+ name * string
204+ ingress []VpcACLRule
205+ egress []VpcACLRule
195206 )
196207
208+ d .Partial (true )
209+
197210 if d .HasChange ("name" ) {
198211 name = helper .String (d .Get ("name" ).(string ))
199212 err := service .ModifyVpcNetworkAcl (ctx , & id , name )
200213 if err != nil {
201214 return nil
202215 }
216+
217+ d .SetPartial ("name" )
203218 }
219+
204220 if d .HasChange ("ingress" ) {
205- if raw , ok := d .GetOk ("ingress" ); ok {
206- oldIngress := helper .InterfacesStrings (raw .([]interface {}))
207- for _ , ingressStr := range oldIngress {
208- liteRule , err := parseACLRule (ingressStr )
209- if err != nil {
210- return err
211- }
212- ingress = append (ingress , liteRule )
221+ _ , new := d .GetChange ("ingress" )
222+ if len (new .([]interface {})) == 0 {
223+ //del ingress
224+ ingress = []VpcACLRule {
225+ {
226+ protocol : "all" ,
227+ cidrIp : "0.0.0.0/0" ,
228+ action : "DROP" ,
229+ },
213230 }
214231 } else {
215- old , _ := d .GetChange ("ingress" )
216- oldIngress := helper .InterfacesStrings (old .([]interface {}))
217- for _ , ingressStr := range oldIngress {
232+ newIngress := helper .InterfacesStrings (new .([]interface {}))
233+ for _ , ingressStr := range newIngress {
218234 liteRule , err := parseACLRule (ingressStr )
219235 if err != nil {
220236 return err
221237 }
222238 ingress = append (ingress , liteRule )
223239 }
224-
225- deleteIngress = true
226240 }
227241 }
228242
229243 if d .HasChange ("egress" ) {
230- if raw , ok := d .GetOk ("egress" ); ok {
231- oldEgress := helper .InterfacesStrings (raw .([]interface {}))
232- for _ , egressStr := range oldEgress {
233- liteRule , err := parseACLRule (egressStr )
234- if err != nil {
235- return err
236- }
237- egress = append (egress , liteRule )
244+ _ , new := d .GetChange ("egress" )
245+ if len (new .([]interface {})) == 0 {
246+ //del ingress
247+ egress = []VpcACLRule {
248+ {
249+ protocol : "all" ,
250+ cidrIp : "0.0.0.0/0" ,
251+ action : "DROP" ,
252+ },
238253 }
239254 } else {
240- old , _ := d .GetChange ("egress" )
241- oldEgress := helper .InterfacesStrings (old .([]interface {}))
242- for _ , egressStr := range oldEgress {
255+ newIngress := helper .InterfacesStrings (new .([]interface {}))
256+ for _ , egressStr := range newIngress {
243257 liteRule , err := parseACLRule (egressStr )
244258 if err != nil {
245259 return err
246260 }
247261 egress = append (egress , liteRule )
248262 }
249-
250- deleteEgress = true
251263 }
252264 }
253265
254- d .Partial (true )
255-
256- if deleteIngress && deleteEgress {
257- if err := service .DeleteACLRulesByChoose (ctx , id , nil , nil , DeleteAll ); err != nil {
258- return err
259- }
260-
261- d .Partial (false )
262-
263- return resourceTencentCloudVpcACLRead (d , meta )
264- }
265-
266- if deleteIngress {
267- if err := service .DeleteACLRulesByChoose (ctx , id , ingress , nil , DeleteIngress ); err != nil {
268- return err
269- }
270-
271- d .SetPartial ("ingress" )
272-
273- ingress = nil
274- }
275-
276- if deleteEgress {
277- if err := service .DeleteACLRulesByChoose (ctx , id , nil , egress , DeleteEgress ); err != nil {
278- return err
279- }
280-
281- d .SetPartial ("egress" )
282-
283- egress = nil
284- }
285-
286- if len (ingress ) > 0 || len (egress ) > 0 {
287- if err := service .ModifyNetWorkAclRules (ctx , id , ingress , egress ); err != nil {
288- return err
289- }
266+ if err := service .ModifyNetWorkAclRules (ctx , id , ingress , egress ); err != nil {
267+ return err
290268 }
291-
292269 d .Partial (false )
293270
294271 return resourceTencentCloudVpcACLRead (d , meta )
@@ -309,7 +286,7 @@ func resourceTencentCloudVpcACLDelete(d *schema.ResourceData, meta interface{})
309286 return err
310287 }
311288
312- _ , _ , has , err := service .DescribeNetWorkByACLID (ctx , id )
289+ _ , has , err := service .DescribeNetWorkByACLID (ctx , id )
313290
314291 if err != nil {
315292 return err
0 commit comments