Skip to content

Commit 01c0fa7

Browse files
authored
fix/sg-rule (#1970)
* fix/sg-rule * fix/sg-rule
1 parent e67f245 commit 01c0fa7

File tree

4 files changed

+231
-119
lines changed

4 files changed

+231
-119
lines changed

.changelog/1970.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_security_group_rule_set: Optimize `service_template_id` usage issue
3+
```

tencentcloud/resource_tc_security_group_rule_set.go

Lines changed: 162 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,86 @@ Provides a resource to create security group rule. This resource is similar with
66
Example Usage
77
88
```hcl
9-
resource "tencentcloud_security_group" "sglab_1" {
10-
name = "mysg_1"
11-
description = "favourite sg_1"
9+
resource "tencentcloud_security_group" "base" {
10+
name = "test-set-sg"
11+
description = "Testing Rule Set Security"
1212
}
1313
14-
resource "tencentcloud_security_group_rule_set" "sglab_1" {
15-
security_group_id = tencentcloud_security_group.sglab_1.id
14+
resource "tencentcloud_security_group" "relative" {
15+
name = "for-relative"
16+
description = "Used for attach security policy"
17+
}
18+
19+
resource "tencentcloud_address_template" "foo" {
20+
name = "test-set-aTemp"
21+
addresses = ["10.0.0.1", "10.0.1.0/24", "10.0.0.1-10.0.0.100"]
22+
}
23+
24+
resource "tencentcloud_address_template_group" "foo" {
25+
name = "test-set-atg"
26+
template_ids = [tencentcloud_address_template.foo.id]
27+
}
28+
29+
resource "tencentcloud_security_group_rule_set" "base" {
30+
security_group_id = tencentcloud_security_group.base.id
31+
32+
ingress {
33+
action = "ACCEPT"
34+
cidr_block = "10.0.0.0/22"
35+
protocol = "TCP"
36+
port = "80-90"
37+
description = "A:Allow Ips and 80-90"
38+
}
39+
40+
ingress {
41+
action = "ACCEPT"
42+
cidr_block = "10.0.2.1"
43+
protocol = "UDP"
44+
port = "8080"
45+
description = "B:Allow UDP 8080"
46+
}
47+
48+
ingress {
49+
action = "ACCEPT"
50+
cidr_block = "10.0.2.1"
51+
protocol = "UDP"
52+
port = "8080"
53+
description = "C:Allow UDP 8080"
54+
}
55+
1656
ingress {
17-
cidr_block = "10.0.0.0/16" # Accept IP or CIDR
18-
protocol = "TCP" # Default is ALL
19-
port = "80" # Accept port e.g. 80 or PortRange e.g. 8080-8089
2057
action = "ACCEPT"
21-
description = "favourite sg rule_1"
58+
cidr_block = "172.18.1.2"
59+
protocol = "ALL"
60+
port = "ALL"
61+
description = "D:Allow ALL"
2262
}
63+
2364
ingress {
65+
action = "DROP"
2466
protocol = "TCP"
2567
port = "80"
26-
action = "ACCEPT"
27-
source_security_id = tencentcloud_security_group.sglab_3.id
28-
description = "favourite sg rule_2"
68+
source_security_id = tencentcloud_security_group.relative.id
69+
description = "E:Block relative"
2970
}
3071
3172
egress {
32-
action = "ACCEPT"
33-
address_template_id = "ipm-xxxxxxxx" # Support address template (group)
34-
description = "Allow address template"
73+
action = "DROP"
74+
cidr_block = "10.0.0.0/16"
75+
protocol = "ICMP"
76+
description = "A:Block ping3"
3577
}
78+
3679
egress {
37-
action = "ACCEPT"
38-
service_template_group = "ppmg-xxxxxxxx" # Support protocol template (group)
39-
description = "Allow protocol template"
80+
action = "DROP"
81+
address_template_id = tencentcloud_address_template.foo.id
82+
description = "B:Allow template"
4083
}
84+
4185
egress {
42-
cidr_block = "10.0.0.0/16"
43-
protocol = "TCP"
44-
port = "80"
45-
action = "DROP"
46-
description = "favourite sg egress rule"
86+
action = "DROP"
87+
address_template_group = tencentcloud_address_template_group.foo.id
88+
description = "C:DROP template group"
4789
}
4890
}
4991
```
@@ -122,13 +164,13 @@ func resourceTencentCloudSecurityGroupRuleSet() *schema.Resource {
122164
"protocol": {
123165
Type: schema.TypeString,
124166
Optional: true,
125-
Default: "ALL",
167+
Computed: true,
126168
Description: "Type of IP protocol. Valid values: `TCP`, `UDP` and `ICMP`. Default to all types protocol, and conflicts with `service_template_*`.",
127169
},
128170
"port": {
129171
Type: schema.TypeString,
130172
Optional: true,
131-
Default: "ALL",
173+
Computed: true,
132174
Description: "Range of the port. The available value can be one, multiple or one segment. E.g. `80`, `80,90` and `80-90`. Default to all ports, and conflicts with `service_template_*`.",
133175
},
134176
}
@@ -151,17 +193,13 @@ func resourceTencentCloudSecurityGroupRuleSet() *schema.Resource {
151193
Type: schema.TypeList,
152194
Optional: true,
153195
Description: "List of ingress rule. NOTE: this block is ordered, the first rule has the highest priority.",
154-
Elem: &schema.Resource{
155-
Schema: ruleElem,
156-
},
196+
Elem: &schema.Resource{Schema: ruleElem},
157197
},
158198
"egress": {
159199
Type: schema.TypeList,
160200
Optional: true,
161201
Description: "List of egress rule. NOTE: this block is ordered, the first rule has the highest priority.",
162-
Elem: &schema.Resource{
163-
Schema: ruleElem,
164-
},
202+
Elem: &schema.Resource{Schema: ruleElem},
165203
},
166204
"version": {
167205
Type: schema.TypeString,
@@ -175,26 +213,33 @@ func resourceTencentCloudSecurityGroupRuleSet() *schema.Resource {
175213
func resourceTencentCloudSecurityGroupRuleSetCreate(d *schema.ResourceData, m interface{}) error {
176214
defer logElapsed("resource.tencentcloud_security_group_rule_set.create")()
177215

178-
logId := getLogId(contextNil)
179-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
180-
service := VpcService{client: m.(*TencentCloudClient).apiV3Conn}
216+
var (
217+
logId = getLogId(contextNil)
218+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
219+
service = VpcService{client: m.(*TencentCloudClient).apiV3Conn}
220+
request = vpc.NewModifySecurityGroupPoliciesRequest()
221+
securityGroupId string
222+
err error
223+
)
224+
225+
if v, ok := d.GetOk("security_group_id"); ok {
226+
request.SecurityGroupId = helper.String(v.(string))
227+
securityGroupId = v.(string)
228+
}
181229

182-
var err error
183-
id := d.Get("security_group_id").(string)
184-
request := vpc.NewModifySecurityGroupPoliciesRequest()
185-
request.SecurityGroupId = helper.String(id)
186230
request.SecurityGroupPolicySet = &vpc.SecurityGroupPolicySet{}
187231

188232
if v, ok := d.GetOk("ingress"); ok {
189-
rules := v.([]interface{})
190-
request.SecurityGroupPolicySet.Ingress, err = unmarshalSecurityPolicy(rules)
233+
ingressRules := v.([]interface{})
234+
request.SecurityGroupPolicySet.Ingress, err = unmarshalSecurityPolicy(ingressRules)
191235
if err != nil {
192236
return err
193237
}
194238
}
239+
195240
if v, ok := d.GetOk("egress"); ok {
196-
rules := v.([]interface{})
197-
request.SecurityGroupPolicySet.Egress, err = unmarshalSecurityPolicy(rules)
241+
egressRules := v.([]interface{})
242+
request.SecurityGroupPolicySet.Egress, err = unmarshalSecurityPolicy(egressRules)
198243
if err != nil {
199244
return err
200245
}
@@ -205,19 +250,21 @@ func resourceTencentCloudSecurityGroupRuleSetCreate(d *schema.ResourceData, m in
205250
return err
206251
}
207252

208-
d.SetId(id)
253+
d.SetId(securityGroupId)
209254
return resourceTencentCloudSecurityGroupRuleSetRead(d, m)
210255
}
211256

212257
func resourceTencentCloudSecurityGroupRuleSetRead(d *schema.ResourceData, m interface{}) error {
213258
defer logElapsed("resource.tencentcloud_security_group_rule_set.read")()
214259
defer inconsistentCheck(d, m)()
215260

216-
logId := getLogId(contextNil)
217-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
218-
service := VpcService{client: m.(*TencentCloudClient).apiV3Conn}
261+
var (
262+
logId = getLogId(contextNil)
263+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
264+
service = VpcService{client: m.(*TencentCloudClient).apiV3Conn}
265+
securityGroupId = d.Id()
266+
)
219267

220-
securityGroupId := d.Id()
221268
request := vpc.NewDescribeSecurityGroupPoliciesRequest()
222269
request.SecurityGroupId = &securityGroupId
223270

@@ -226,86 +273,105 @@ func resourceTencentCloudSecurityGroupRuleSetRead(d *schema.ResourceData, m inte
226273
return err
227274
}
228275

276+
if result == nil {
277+
d.SetId("")
278+
return fmt.Errorf("resource `tencentcloud_security_group_rule_set` %s does not exist", d.Id())
279+
}
280+
229281
_ = d.Set("security_group_id", securityGroupId)
230-
d.SetId(securityGroupId)
231-
_ = d.Set("version", result.Version)
232-
if len(result.Ingress) > 0 {
282+
283+
if result.Version != nil {
284+
_ = d.Set("version", result.Version)
285+
}
286+
287+
if result.Ingress != nil {
233288
_ = d.Set("ingress", marshalSecurityPolicy(result.Ingress))
234289
}
235-
if len(result.Egress) > 0 {
290+
291+
if result.Egress != nil {
236292
_ = d.Set("egress", marshalSecurityPolicy(result.Egress))
237293
}
294+
238295
return nil
239296
}
240297

241298
func resourceTencentCloudSecurityGroupRuleSetUpdate(d *schema.ResourceData, m interface{}) error {
242299
defer logElapsed("tencentcloud_security_group_rule_set.update")()
243-
logId := getLogId(contextNil)
244-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
245-
client := m.(*TencentCloudClient).apiV3Conn
246-
service := VpcService{client}
247-
248-
version := d.Get("version").(string)
249-
ver, vErr := strconv.ParseInt(version, 10, 64)
250-
nextVer := ""
251300

252-
request := vpc.NewModifySecurityGroupPoliciesRequest()
253-
request.SecurityGroupId = helper.String(d.Id())
254-
request.SecurityGroupPolicySet = &vpc.SecurityGroupPolicySet{}
255-
request.SortPolicys = helper.Bool(true)
256-
if vErr == nil {
257-
nextVer = fmt.Sprintf("%d", ver+1)
258-
request.SecurityGroupPolicySet.Version = &nextVer
301+
var (
302+
logId = getLogId(contextNil)
303+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
304+
service = VpcService{client: m.(*TencentCloudClient).apiV3Conn}
305+
request = vpc.NewModifySecurityGroupPoliciesRequest()
306+
securityGroupId = d.Id()
307+
nextVer string
308+
needChange bool
309+
err error
310+
)
311+
312+
mutableArgs := []string{"ingress", "egress"}
313+
314+
for _, v := range mutableArgs {
315+
if d.HasChange(v) {
316+
needChange = true
317+
break
318+
}
259319
}
260320

261-
var err error
262-
if d.HasChange("ingress") {
263-
rules := d.Get("ingress").([]interface{})
264-
request.SecurityGroupPolicySet.Ingress, err = unmarshalSecurityPolicy(rules)
265-
if err != nil {
266-
return err
321+
if needChange {
322+
version := d.Get("version").(string)
323+
ver, vErr := strconv.ParseInt(version, 10, 64)
324+
request.SecurityGroupId = helper.String(securityGroupId)
325+
request.SecurityGroupPolicySet = &vpc.SecurityGroupPolicySet{}
326+
request.SortPolicys = helper.Bool(true)
327+
if vErr == nil {
328+
nextVer = fmt.Sprintf("%d", ver+1)
329+
request.SecurityGroupPolicySet.Version = helper.String(nextVer)
267330
}
268-
}
269-
if d.HasChange("egress") {
270-
rules := d.Get("egress").([]interface{})
271-
request.SecurityGroupPolicySet.Egress, err = unmarshalSecurityPolicy(rules)
331+
332+
if d.HasChange("ingress") {
333+
ingressRules := d.Get("ingress").([]interface{})
334+
request.SecurityGroupPolicySet.Ingress, err = unmarshalSecurityPolicy(ingressRules)
335+
if err != nil {
336+
return err
337+
}
338+
}
339+
340+
if d.HasChange("egress") {
341+
egressRules := d.Get("egress").([]interface{})
342+
request.SecurityGroupPolicySet.Egress, err = unmarshalSecurityPolicy(egressRules)
343+
if err != nil {
344+
return err
345+
}
346+
}
347+
348+
err = service.ModifySecurityGroupPolicies(ctx, request)
272349
if err != nil {
273350
return err
274351
}
275352
}
276-
err = service.ModifySecurityGroupPolicies(ctx, request)
277-
if err != nil {
278-
return err
279-
}
280353

281354
return resourceTencentCloudSecurityGroupRuleSetRead(d, m)
282355
}
283356

284357
func resourceTencentCloudSecurityGroupRuleSetDelete(d *schema.ResourceData, m interface{}) error {
285358
defer logElapsed("resource.tencentcloud_security_group_rule_set.delete")()
286359

287-
logId := getLogId(contextNil)
288-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
289-
290-
service := VpcService{client: m.(*TencentCloudClient).apiV3Conn}
360+
var (
361+
logId = getLogId(contextNil)
362+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
363+
service = VpcService{client: m.(*TencentCloudClient).apiV3Conn}
364+
request = vpc.NewModifySecurityGroupPoliciesRequest()
365+
securityGroupId = d.Id()
366+
)
291367

292-
id := d.Id()
293-
294-
request := vpc.NewModifySecurityGroupPoliciesRequest()
295-
request.SecurityGroupId = &id
368+
request.SecurityGroupId = &securityGroupId
296369
request.SecurityGroupPolicySet = &vpc.SecurityGroupPolicySet{
297370
Version: helper.String("0"),
298371
Ingress: []*vpc.SecurityGroupPolicy{},
299372
Egress: []*vpc.SecurityGroupPolicy{},
300373
}
301-
//if v, ok := d.GetOk("ingress"); ok {
302-
// rules := v.([]interface{})
303-
// request.SecurityGroupPolicySet.Ingress, _ = unmarshalSecurityPolicy(rules)
304-
//}
305-
//if v, ok := d.GetOk("egress"); ok {
306-
// rules := v.([]interface{})
307-
// request.SecurityGroupPolicySet.Egress, _ = unmarshalSecurityPolicy(rules)
308-
//}
374+
309375
err := service.ModifySecurityGroupPolicies(ctx, request)
310376
if err != nil {
311377
log.Printf("[CRITAL]%s security group rule delete failed: %s\n ", logId, err.Error())

tencentcloud/resource_tc_security_group_rule_set_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1010
)
1111

12+
// go test -i; go test -test.run TestAccTencentCloudSecurityGroupRuleSetResource_basic -v
1213
func TestAccTencentCloudSecurityGroupRuleSetResource_basic(t *testing.T) {
1314
t.Parallel()
1415
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)