1
1
/*
2
2
Provide a resource to create security group some lite rules quickly.
3
3
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.
5
5
6
6
Example Usage
7
7
@@ -113,7 +113,7 @@ func resourceTencentCloudSecurityGroupLiteRuleCreate(d *schema.ResourceData, m i
113
113
}
114
114
}
115
115
116
- if err := service .AttachLiteRulesToSecurityGroup (ctx , sgId , ingress , egress , updateLiteRule , updateLiteRule ); err != nil {
116
+ if err := service .AttachLiteRulesToSecurityGroup (ctx , sgId , ingress , egress ); err != nil {
117
117
return err
118
118
}
119
119
@@ -170,16 +170,14 @@ func resourceTencentCloudSecurityGroupLiteRuleUpdate(d *schema.ResourceData, m i
170
170
service := VpcService {client : m .(* TencentCloudClient ).apiV3Conn }
171
171
172
172
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
177
177
)
178
178
179
179
if d .HasChange ("ingress" ) {
180
180
if raw , ok := d .GetOk ("ingress" ); ok {
181
- updateIngressKind = updateLiteRule
182
-
183
181
ingressStrs := helper .InterfacesStrings (raw .([]interface {}))
184
182
for _ , ingressStr := range ingressStrs {
185
183
liteRule , err := parseRule (ingressStr )
@@ -189,8 +187,6 @@ func resourceTencentCloudSecurityGroupLiteRuleUpdate(d *schema.ResourceData, m i
189
187
ingress = append (ingress , liteRule )
190
188
}
191
189
} else {
192
- updateIngressKind = deleteLiteRule
193
-
194
190
old , _ := d .GetChange ("ingress" )
195
191
ingressStrs := helper .InterfacesStrings (old .([]interface {}))
196
192
for _ , ingressStr := range ingressStrs {
@@ -200,13 +196,13 @@ func resourceTencentCloudSecurityGroupLiteRuleUpdate(d *schema.ResourceData, m i
200
196
}
201
197
ingress = append (ingress , liteRule )
202
198
}
199
+
200
+ deleteIngress = true
203
201
}
204
202
}
205
203
206
204
if d .HasChange ("egress" ) {
207
205
if raw , ok := d .GetOk ("egress" ); ok {
208
- updateEgressKind = updateLiteRule
209
-
210
206
egressStrs := helper .InterfacesStrings (raw .([]interface {}))
211
207
for _ , egressStr := range egressStrs {
212
208
liteRule , err := parseRule (egressStr )
@@ -216,8 +212,6 @@ func resourceTencentCloudSecurityGroupLiteRuleUpdate(d *schema.ResourceData, m i
216
212
egress = append (egress , liteRule )
217
213
}
218
214
} else {
219
- updateEgressKind = deleteLiteRule
220
-
221
215
old , _ := d .GetChange ("egress" )
222
216
egressStrs := helper .InterfacesStrings (old .([]interface {}))
223
217
for _ , egressStr := range egressStrs {
@@ -227,19 +221,52 @@ func resourceTencentCloudSecurityGroupLiteRuleUpdate(d *schema.ResourceData, m i
227
221
}
228
222
egress = append (egress , liteRule )
229
223
}
224
+
225
+ deleteEgress = true
230
226
}
231
227
}
232
228
233
- if updateIngressKind == deleteLiteRule && updateEgressKind == deleteLiteRule {
229
+ d .Partial (true )
230
+
231
+ if deleteIngress && deleteEgress {
234
232
if err := service .DetachAllLiteRulesFromSecurityGroup (ctx , id ); err != nil {
235
233
return err
236
234
}
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 {
239
264
return err
240
265
}
241
266
}
242
267
268
+ d .Partial (false )
269
+
243
270
return resourceTencentCloudSecurityGroupLiteRuleRead (d , m )
244
271
}
245
272
0 commit comments