Skip to content

Commit 6c27f1c

Browse files
committed
refactor: rewrite rules update
1 parent 5ff0ba7 commit 6c27f1c

File tree

3 files changed

+111
-195
lines changed

3 files changed

+111
-195
lines changed

tencentcloud/resource_tc_security_group_lite_rule.go

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Provide a resource to create security group some lite rules quickly.
33
4-
-> **NOTE:** It can't be used with tencentcloud_security_group_rule, and don't create multi tencentcloud_security_group_rule resources, otherwise it may cause problems.
4+
-> **NOTE:** It can't be used with tencentcloud_security_group_rule, and don't create multiple tencentcloud_security_group_rule resources, otherwise it may cause problems.
55
66
Example Usage
77
@@ -113,7 +113,7 @@ func resourceTencentCloudSecurityGroupLiteRuleCreate(d *schema.ResourceData, m i
113113
}
114114
}
115115

116-
if err := service.AttachLiteRulesToSecurityGroup(ctx, sgId, ingress, egress, updateLiteRule, updateLiteRule); err != nil {
116+
if err := service.AttachLiteRulesToSecurityGroup(ctx, sgId, ingress, egress); err != nil {
117117
return err
118118
}
119119

@@ -170,16 +170,14 @@ func resourceTencentCloudSecurityGroupLiteRuleUpdate(d *schema.ResourceData, m i
170170
service := VpcService{client: m.(*TencentCloudClient).apiV3Conn}
171171

172172
var (
173-
ingress []VpcSecurityGroupLiteRule
174-
egress []VpcSecurityGroupLiteRule
175-
updateIngressKind = noModifyLiteRule
176-
updateEgressKind = noModifyLiteRule
173+
ingress []VpcSecurityGroupLiteRule
174+
egress []VpcSecurityGroupLiteRule
175+
deleteIngress bool
176+
deleteEgress bool
177177
)
178178

179179
if d.HasChange("ingress") {
180180
if raw, ok := d.GetOk("ingress"); ok {
181-
updateIngressKind = updateLiteRule
182-
183181
ingressStrs := helper.InterfacesStrings(raw.([]interface{}))
184182
for _, ingressStr := range ingressStrs {
185183
liteRule, err := parseRule(ingressStr)
@@ -189,8 +187,6 @@ func resourceTencentCloudSecurityGroupLiteRuleUpdate(d *schema.ResourceData, m i
189187
ingress = append(ingress, liteRule)
190188
}
191189
} else {
192-
updateIngressKind = deleteLiteRule
193-
194190
old, _ := d.GetChange("ingress")
195191
ingressStrs := helper.InterfacesStrings(old.([]interface{}))
196192
for _, ingressStr := range ingressStrs {
@@ -200,13 +196,13 @@ func resourceTencentCloudSecurityGroupLiteRuleUpdate(d *schema.ResourceData, m i
200196
}
201197
ingress = append(ingress, liteRule)
202198
}
199+
200+
deleteIngress = true
203201
}
204202
}
205203

206204
if d.HasChange("egress") {
207205
if raw, ok := d.GetOk("egress"); ok {
208-
updateEgressKind = updateLiteRule
209-
210206
egressStrs := helper.InterfacesStrings(raw.([]interface{}))
211207
for _, egressStr := range egressStrs {
212208
liteRule, err := parseRule(egressStr)
@@ -216,8 +212,6 @@ func resourceTencentCloudSecurityGroupLiteRuleUpdate(d *schema.ResourceData, m i
216212
egress = append(egress, liteRule)
217213
}
218214
} else {
219-
updateEgressKind = deleteLiteRule
220-
221215
old, _ := d.GetChange("egress")
222216
egressStrs := helper.InterfacesStrings(old.([]interface{}))
223217
for _, egressStr := range egressStrs {
@@ -227,19 +221,52 @@ func resourceTencentCloudSecurityGroupLiteRuleUpdate(d *schema.ResourceData, m i
227221
}
228222
egress = append(egress, liteRule)
229223
}
224+
225+
deleteEgress = true
230226
}
231227
}
232228

233-
if updateIngressKind == deleteLiteRule && updateEgressKind == deleteLiteRule {
229+
d.Partial(true)
230+
231+
if deleteIngress && deleteEgress {
234232
if err := service.DetachAllLiteRulesFromSecurityGroup(ctx, id); err != nil {
235233
return err
236234
}
237-
} else {
238-
if err := service.modifyLiteRulesInSecurityGroup(ctx, id, ingress, egress, updateIngressKind, updateEgressKind); err != nil {
235+
236+
d.Partial(false)
237+
238+
return resourceTencentCloudSecurityGroupLiteRuleRead(d, m)
239+
}
240+
241+
if deleteIngress {
242+
if err := service.DeleteLiteRules(ctx, id, ingress, true); err != nil {
243+
return err
244+
}
245+
246+
d.SetPartial("ingress")
247+
248+
ingress = nil
249+
}
250+
251+
if deleteEgress {
252+
if err := service.DeleteLiteRules(ctx, id, egress, false); err != nil {
253+
return err
254+
}
255+
256+
d.SetPartial("egress")
257+
258+
egress = nil
259+
}
260+
261+
// if both len == 0, means both rules are deleted
262+
if len(ingress) > 0 || len(egress) > 0 {
263+
if err := service.modifyLiteRulesInSecurityGroup(ctx, id, ingress, egress); err != nil {
239264
return err
240265
}
241266
}
242267

268+
d.Partial(false)
269+
243270
return resourceTencentCloudSecurityGroupLiteRuleRead(d, m)
244271
}
245272

0 commit comments

Comments
 (0)