Skip to content

Commit 58fbeb1

Browse files
authored
fix/vpc (#2047)
* fix/vpc * fix/vpc-bug
1 parent 0f894b3 commit 58fbeb1

File tree

6 files changed

+213
-58
lines changed

6 files changed

+213
-58
lines changed

.changelog/2047.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_eni: Update `security_groups` field
3+
```

tencentcloud/resource_tc_eni.go

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,53 @@ Provides a resource to create an ENI.
44
Example Usage
55
66
```hcl
7-
resource "tencentcloud_vpc" "foo" {
8-
name = "ci-test-eni-vpc"
7+
data "tencentcloud_availability_zones_by_product" "zones" {
8+
product = "vpc"
9+
}
10+
11+
resource "tencentcloud_vpc" "vpc" {
12+
name = "vpc-example"
913
cidr_block = "10.0.0.0/16"
1014
}
1115
12-
resource "tencentcloud_subnet" "foo" {
13-
availability_zone = "ap-guangzhou-3"
14-
name = "ci-test-eni-subnet"
15-
vpc_id = tencentcloud_vpc.foo.id
16+
resource "tencentcloud_subnet" "subnet" {
17+
availability_zone = data.tencentcloud_availability_zones_by_product.zones.zones.0.name
18+
name = "subnet-example"
19+
vpc_id = tencentcloud_vpc.vpc.id
1620
cidr_block = "10.0.0.0/16"
1721
is_multicast = false
1822
}
1923
20-
resource "tencentcloud_eni" "foo" {
21-
name = "ci-test-eni"
22-
vpc_id = tencentcloud_vpc.foo.id
23-
subnet_id = tencentcloud_subnet.foo.id
24-
description = "eni desc"
25-
ipv4_count = 1
24+
resource "tencentcloud_security_group" "example1" {
25+
name = "tf-example-sg1"
26+
description = "sg desc."
27+
project_id = 0
28+
29+
tags = {
30+
"example" = "test"
31+
}
32+
}
33+
34+
resource "tencentcloud_security_group" "example2" {
35+
name = "tf-example-sg2"
36+
description = "sg desc."
37+
project_id = 0
38+
39+
tags = {
40+
"example" = "test"
41+
}
42+
}
43+
44+
resource "tencentcloud_eni" "example" {
45+
name = "tf-example-eni"
46+
vpc_id = tencentcloud_vpc.vpc.id
47+
subnet_id = tencentcloud_subnet.subnet.id
48+
description = "eni desc."
49+
ipv4_count = 1
50+
security_groups = [
51+
tencentcloud_security_group.example1.id,
52+
tencentcloud_security_group.example2.id
53+
]
2654
}
2755
```
2856
@@ -108,6 +136,7 @@ func resourceTencentCloudEni() *schema.Resource {
108136
"security_groups": {
109137
Type: schema.TypeSet,
110138
Optional: true,
139+
Computed: true,
111140
Elem: &schema.Schema{Type: schema.TypeString},
112141
Set: schema.HashString,
113142
Description: "A set of security group IDs.",

tencentcloud/resource_tc_eni_sg_attachment.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,57 @@ Provides a resource to create a eni_sg_attachment
44
Example Usage
55
66
```hcl
7+
data "tencentcloud_availability_zones_by_product" "zones" {
8+
product = "vpc"
9+
}
10+
11+
resource "tencentcloud_vpc" "vpc" {
12+
name = "vpc-example"
13+
cidr_block = "10.0.0.0/16"
14+
}
15+
16+
resource "tencentcloud_subnet" "subnet" {
17+
availability_zone = data.tencentcloud_availability_zones_by_product.zones.zones.0.name
18+
name = "subnet-example"
19+
vpc_id = tencentcloud_vpc.vpc.id
20+
cidr_block = "10.0.0.0/16"
21+
is_multicast = false
22+
}
23+
24+
resource "tencentcloud_security_group" "example1" {
25+
name = "tf-example-sg1"
26+
description = "sg desc."
27+
project_id = 0
28+
29+
tags = {
30+
"example" = "test"
31+
}
32+
}
33+
34+
resource "tencentcloud_security_group" "example2" {
35+
name = "tf-example-sg2"
36+
description = "sg desc."
37+
project_id = 0
38+
39+
tags = {
40+
"example" = "test"
41+
}
42+
}
43+
44+
resource "tencentcloud_eni" "example" {
45+
name = "tf-example-eni"
46+
vpc_id = tencentcloud_vpc.vpc.id
47+
subnet_id = tencentcloud_subnet.subnet.id
48+
description = "eni desc."
49+
ipv4_count = 1
50+
}
51+
752
resource "tencentcloud_eni_sg_attachment" "eni_sg_attachment" {
8-
network_interface_ids = ["eni-p0hkgx8p"]
9-
security_group_ids = ["sg-902tl7t7", "sg-edmur627"]
53+
network_interface_ids = [tencentcloud_eni.example.id]
54+
security_group_ids = [
55+
tencentcloud_security_group.example1.id,
56+
tencentcloud_security_group.example2.id
57+
]
1058
}
1159
```
1260

tencentcloud/resource_tc_security_group_rule_set.go

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -331,69 +331,68 @@ func resourceTencentCloudSecurityGroupRuleSetUpdate(d *schema.ResourceData, m in
331331
request.SecurityGroupPolicySet = &vpc.SecurityGroupPolicySet{}
332332
request.SortPolicys = helper.Bool(true)
333333

334-
ingressRules := d.Get("ingress").([]interface{})
335-
egressRules := d.Get("egress").([]interface{})
336-
if len(ingressRules) == 0 && len(egressRules) == 0 {
334+
oldIngress, newIngress := d.GetChange("ingress")
335+
oldEgress, newEgress := d.GetChange("egress")
336+
337+
oldIngressList := oldIngress.([]interface{})
338+
newIngressList := newIngress.([]interface{})
339+
oldEgressList := oldEgress.([]interface{})
340+
newEgressList := newEgress.([]interface{})
341+
342+
if len(newIngressList) == 0 && len(newEgressList) == 0 {
337343
ver = 0
338-
} else if len(ingressRules) != 0 && len(egressRules) == 0 {
339-
request.SecurityGroupPolicySet.Ingress, err = unmarshalSecurityPolicy(ingressRules)
344+
345+
} else if len(newIngressList) != 0 && len(newEgressList) == 0 {
346+
request.SecurityGroupPolicySet.Ingress, err = unmarshalSecurityPolicy(newIngressList)
340347
if err != nil {
341348
return err
342349
}
343350

344-
result, e := service.DescribeSecurityGroupPolicies(ctx, securityGroupId)
345-
if e != nil {
346-
return e
347-
}
348-
349-
if len(result.Egress) > 0 {
350-
tmpList := []*int64{}
351-
egressRulesList := marshalSecurityPolicy(result.Egress)
352-
for _, v := range egressRulesList {
351+
if len(oldEgressList) > 0 {
352+
tmpList := make([]*int64, 0)
353+
for _, v := range oldEgressList {
353354
item := v.(map[string]interface{})
354-
tmpList = append(tmpList, item["policy_index"].(*int64))
355+
policyIndex := int64(item["policy_index"].(int))
356+
tmpList = append(tmpList, &policyIndex)
355357
}
356358

357-
e = service.DeleteSecurityGroupPolicyByPolicyIndexList(ctx, securityGroupId, tmpList, "egress")
359+
e := service.DeleteSecurityGroupPolicyByPolicyIndexList(ctx, securityGroupId, tmpList, "egress")
358360
if e != nil {
359361
return e
360362
}
361363

362364
ver += 1
363365
}
364366

365-
} else if len(ingressRules) == 0 && len(egressRules) != 0 {
366-
request.SecurityGroupPolicySet.Egress, err = unmarshalSecurityPolicy(egressRules)
367+
} else if len(newIngressList) == 0 && len(newEgressList) != 0 {
368+
request.SecurityGroupPolicySet.Egress, err = unmarshalSecurityPolicy(newEgressList)
367369
if err != nil {
368370
return err
369371
}
370372

371-
result, e := service.DescribeSecurityGroupPolicies(ctx, securityGroupId)
372-
if e != nil {
373-
return e
374-
}
375-
376-
if len(result.Ingress) > 0 {
377-
tmpList := []*int64{}
378-
ingressRulesList := marshalSecurityPolicy(result.Ingress)
379-
for _, v := range ingressRulesList {
373+
if len(oldIngressList) > 0 {
374+
tmpList := make([]*int64, 0)
375+
for _, v := range oldIngressList {
380376
item := v.(map[string]interface{})
381-
tmpList = append(tmpList, item["policy_index"].(*int64))
377+
policyIndex := int64(item["policy_index"].(int))
378+
tmpList = append(tmpList, &policyIndex)
382379
}
383-
e = service.DeleteSecurityGroupPolicyByPolicyIndexList(ctx, securityGroupId, tmpList, "ingress")
380+
381+
e := service.DeleteSecurityGroupPolicyByPolicyIndexList(ctx, securityGroupId, tmpList, "ingress")
384382
if e != nil {
385383
return e
386384
}
387385

388386
ver += 1
389387
}
388+
390389
} else {
391-
request.SecurityGroupPolicySet.Ingress, err = unmarshalSecurityPolicy(ingressRules)
390+
request.SecurityGroupPolicySet.Ingress, err = unmarshalSecurityPolicy(newIngressList)
392391
if err != nil {
393392
return err
394393
}
395394

396-
request.SecurityGroupPolicySet.Egress, err = unmarshalSecurityPolicy(egressRules)
395+
request.SecurityGroupPolicySet.Egress, err = unmarshalSecurityPolicy(newEgressList)
397396
if err != nil {
398397
return err
399398
}

website/docs/r/eni.html.markdown

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,53 @@ Provides a resource to create an ENI.
1414
## Example Usage
1515

1616
```hcl
17-
resource "tencentcloud_vpc" "foo" {
18-
name = "ci-test-eni-vpc"
17+
data "tencentcloud_availability_zones_by_product" "zones" {
18+
product = "vpc"
19+
}
20+
21+
resource "tencentcloud_vpc" "vpc" {
22+
name = "vpc-example"
1923
cidr_block = "10.0.0.0/16"
2024
}
2125
22-
resource "tencentcloud_subnet" "foo" {
23-
availability_zone = "ap-guangzhou-3"
24-
name = "ci-test-eni-subnet"
25-
vpc_id = tencentcloud_vpc.foo.id
26+
resource "tencentcloud_subnet" "subnet" {
27+
availability_zone = data.tencentcloud_availability_zones_by_product.zones.zones.0.name
28+
name = "subnet-example"
29+
vpc_id = tencentcloud_vpc.vpc.id
2630
cidr_block = "10.0.0.0/16"
2731
is_multicast = false
2832
}
2933
30-
resource "tencentcloud_eni" "foo" {
31-
name = "ci-test-eni"
32-
vpc_id = tencentcloud_vpc.foo.id
33-
subnet_id = tencentcloud_subnet.foo.id
34-
description = "eni desc"
34+
resource "tencentcloud_security_group" "example1" {
35+
name = "tf-example-sg1"
36+
description = "sg desc."
37+
project_id = 0
38+
39+
tags = {
40+
"example" = "test"
41+
}
42+
}
43+
44+
resource "tencentcloud_security_group" "example2" {
45+
name = "tf-example-sg2"
46+
description = "sg desc."
47+
project_id = 0
48+
49+
tags = {
50+
"example" = "test"
51+
}
52+
}
53+
54+
resource "tencentcloud_eni" "example" {
55+
name = "tf-example-eni"
56+
vpc_id = tencentcloud_vpc.vpc.id
57+
subnet_id = tencentcloud_subnet.subnet.id
58+
description = "eni desc."
3559
ipv4_count = 1
60+
security_groups = [
61+
tencentcloud_security_group.example1.id,
62+
tencentcloud_security_group.example2.id
63+
]
3664
}
3765
```
3866

website/docs/r/eni_sg_attachment.html.markdown

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,57 @@ Provides a resource to create a eni_sg_attachment
1414
## Example Usage
1515

1616
```hcl
17+
data "tencentcloud_availability_zones_by_product" "zones" {
18+
product = "vpc"
19+
}
20+
21+
resource "tencentcloud_vpc" "vpc" {
22+
name = "vpc-example"
23+
cidr_block = "10.0.0.0/16"
24+
}
25+
26+
resource "tencentcloud_subnet" "subnet" {
27+
availability_zone = data.tencentcloud_availability_zones_by_product.zones.zones.0.name
28+
name = "subnet-example"
29+
vpc_id = tencentcloud_vpc.vpc.id
30+
cidr_block = "10.0.0.0/16"
31+
is_multicast = false
32+
}
33+
34+
resource "tencentcloud_security_group" "example1" {
35+
name = "tf-example-sg1"
36+
description = "sg desc."
37+
project_id = 0
38+
39+
tags = {
40+
"example" = "test"
41+
}
42+
}
43+
44+
resource "tencentcloud_security_group" "example2" {
45+
name = "tf-example-sg2"
46+
description = "sg desc."
47+
project_id = 0
48+
49+
tags = {
50+
"example" = "test"
51+
}
52+
}
53+
54+
resource "tencentcloud_eni" "example" {
55+
name = "tf-example-eni"
56+
vpc_id = tencentcloud_vpc.vpc.id
57+
subnet_id = tencentcloud_subnet.subnet.id
58+
description = "eni desc."
59+
ipv4_count = 1
60+
}
61+
1762
resource "tencentcloud_eni_sg_attachment" "eni_sg_attachment" {
18-
network_interface_ids = ["eni-p0hkgx8p"]
19-
security_group_ids = ["sg-902tl7t7", "sg-edmur627"]
63+
network_interface_ids = [tencentcloud_eni.example.id]
64+
security_group_ids = [
65+
tencentcloud_security_group.example1.id,
66+
tencentcloud_security_group.example2.id
67+
]
2068
}
2169
```
2270

0 commit comments

Comments
 (0)