Skip to content

Commit 696a161

Browse files
authored
Merge pull request #1448 from tencentcloudstack/feat/gaap_sg_rule_support_change
fix when cidr is 1.1.1.1/32 and attr support change
2 parents 78d769d + 9b2fb47 commit 696a161

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

.changelog/1448.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_gaap_security_rule: keep consistency when cidr is 1.1.1.1/32 and attr support change
3+
```

tencentcloud/resource_tc_gaap_security_rule.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"errors"
4141
"fmt"
4242
"regexp"
43+
"strings"
4344

4445
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
4546
)
@@ -64,15 +65,13 @@ func resourceTencentCloudGaapSecurityRule() *schema.Resource {
6465
"cidr_ip": {
6566
Type: schema.TypeString,
6667
Required: true,
67-
ForceNew: true,
6868
ValidateFunc: validateCidrIp,
6969
Description: "A network address block of the request source.",
7070
},
7171
"action": {
7272
Type: schema.TypeString,
7373
Required: true,
7474
ValidateFunc: validateAllowedStringValue([]string{"ACCEPT", "DROP"}),
75-
ForceNew: true,
7675
Description: "Policy of the rule. Valid value: `ACCEPT` and `DROP`.",
7776
},
7877
"name": {
@@ -87,14 +86,12 @@ func resourceTencentCloudGaapSecurityRule() *schema.Resource {
8786
Optional: true,
8887
Default: "ALL",
8988
ValidateFunc: validateAllowedStringValue([]string{"ALL", "TCP", "UDP"}),
90-
ForceNew: true,
9189
Description: "Protocol of the security policy rule. Default value is `ALL`. Valid value: `TCP`, `UDP` and `ALL`.",
9290
},
9391
"port": {
9492
Type: schema.TypeString,
9593
Optional: true,
9694
Default: "ALL",
97-
ForceNew: true,
9895
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
9996
value := v.(string)
10097
if value == "ALL" {
@@ -162,7 +159,20 @@ func resourceTencentCloudGaapSecurityRuleRead(d *schema.ResourceData, m interfac
162159
}
163160

164161
_ = d.Set("policy_id", rule.PolicyId)
165-
_ = d.Set("cidr_ip", rule.SourceCidr)
162+
163+
cidrIp := *rule.SourceCidr
164+
// fix when cidr is "x.x.x.x/32", because return will remove /32
165+
if v, ok := d.GetOk("cidr_ip"); ok {
166+
getCidrIp := v.(string)
167+
splits := strings.Split(getCidrIp, "/")
168+
if len(splits) > 1 {
169+
if splits[1] == "32" && cidrIp == splits[0] {
170+
cidrIp = fmt.Sprintf("%s/32", cidrIp)
171+
}
172+
}
173+
}
174+
175+
_ = d.Set("cidr_ip", cidrIp)
166176
_ = d.Set("action", rule.Action)
167177
_ = d.Set("name", rule.AliasName)
168178
_ = d.Set("protocol", rule.Protocol)
@@ -181,15 +191,15 @@ func resourceTencentCloudGaapSecurityRuleUpdate(d *schema.ResourceData, m interf
181191

182192
id := d.Id()
183193
policyId := d.Get("policy_id").(string)
194+
cidrIp := d.Get("cidr_ip").(string)
195+
action := d.Get("action").(string)
196+
port := d.Get("port").(string)
197+
protocol := d.Get("protocol").(string)
184198
name := d.Get("name").(string)
185199

186-
if name == "" {
187-
return errors.New("new name can't be empty")
188-
}
189-
190200
service := GaapService{client: m.(*TencentCloudClient).apiV3Conn}
191201

192-
if err := service.ModifySecurityRuleName(ctx, policyId, id, name); err != nil {
202+
if err := service.ModifySecurityRule(ctx, policyId, id, cidrIp, action, port, protocol, name); err != nil {
193203
return err
194204
}
195205

tencentcloud/resource_tc_gaap_security_rule_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func init() {
4747
})
4848
}
4949

50-
func TestAccTencentCloudGaapSecurityRule_basic(t *testing.T) {
50+
func TestAccTencentCloudGaapSecurityRuleResource_basic(t *testing.T) {
5151
t.Parallel()
5252
id := new(string)
5353

@@ -75,7 +75,7 @@ func TestAccTencentCloudGaapSecurityRule_basic(t *testing.T) {
7575
})
7676
}
7777

78-
func TestAccTencentCloudGaapSecurityRule_drop(t *testing.T) {
78+
func TestAccTencentCloudGaapSecurityRuleResource_drop(t *testing.T) {
7979
t.Parallel()
8080
id := new(string)
8181

@@ -98,7 +98,7 @@ func TestAccTencentCloudGaapSecurityRule_drop(t *testing.T) {
9898
})
9999
}
100100

101-
func TestAccTencentCloudGaapSecurityRule_name(t *testing.T) {
101+
func TestAccTencentCloudGaapSecurityRuleResource_name(t *testing.T) {
102102
t.Parallel()
103103
id := new(string)
104104

@@ -129,7 +129,7 @@ func TestAccTencentCloudGaapSecurityRule_name(t *testing.T) {
129129
})
130130
}
131131

132-
func TestAccTencentCloudGaapSecurityRule_ipSubnet(t *testing.T) {
132+
func TestAccTencentCloudGaapSecurityRuleResource_ipSubnet(t *testing.T) {
133133
t.Parallel()
134134
id := new(string)
135135

@@ -152,7 +152,7 @@ func TestAccTencentCloudGaapSecurityRule_ipSubnet(t *testing.T) {
152152
})
153153
}
154154

155-
func TestAccTencentCloudGaapSecurityRule_allProtocols(t *testing.T) {
155+
func TestAccTencentCloudGaapSecurityRuleResource_allProtocols(t *testing.T) {
156156
t.Parallel()
157157
id := new(string)
158158

@@ -175,7 +175,7 @@ func TestAccTencentCloudGaapSecurityRule_allProtocols(t *testing.T) {
175175
})
176176
}
177177

178-
func TestAccTencentCloudGaapSecurityRule_AllPorts(t *testing.T) {
178+
func TestAccTencentCloudGaapSecurityRuleResource_AllPorts(t *testing.T) {
179179
t.Parallel()
180180
id := new(string)
181181

tencentcloud/service_tencentcloud_gaap.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1644,12 +1644,17 @@ func (me *GaapService) DescribeSecurityRule(ctx context.Context, id string) (sec
16441644
return
16451645
}
16461646

1647-
func (me *GaapService) ModifySecurityRuleName(ctx context.Context, policyId, ruleId, name string) error {
1647+
func (me *GaapService) ModifySecurityRule(ctx context.Context, policyId, ruleId, cidrIp, action, port, protocol, name string) error {
16481648
logId := getLogId(ctx)
16491649

16501650
request := gaap.NewModifySecurityRuleRequest()
16511651
request.PolicyId = &policyId
16521652
request.RuleId = &ruleId
1653+
request.SourceCidr = &cidrIp
1654+
request.RuleAction = &action
1655+
request.DestPortRange = &port
1656+
request.Protocol = &protocol
1657+
16531658
request.AliasName = &name
16541659

16551660
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {

website/docs/r/gaap_security_rule.html.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ resource "tencentcloud_gaap_security_rule" "foo" {
3939

4040
The following arguments are supported:
4141

42-
* `action` - (Required, String, ForceNew) Policy of the rule. Valid value: `ACCEPT` and `DROP`.
43-
* `cidr_ip` - (Required, String, ForceNew) A network address block of the request source.
42+
* `action` - (Required, String) Policy of the rule. Valid value: `ACCEPT` and `DROP`.
43+
* `cidr_ip` - (Required, String) A network address block of the request source.
4444
* `policy_id` - (Required, String, ForceNew) ID of the security policy.
4545
* `name` - (Optional, String) Name of the security policy rule. Maximum length is 30.
46-
* `port` - (Optional, String, ForceNew) Target port. Default value is `ALL`. Valid examples: `80`, `80,443` and `3306-20000`.
47-
* `protocol` - (Optional, String, ForceNew) Protocol of the security policy rule. Default value is `ALL`. Valid value: `TCP`, `UDP` and `ALL`.
46+
* `port` - (Optional, String) Target port. Default value is `ALL`. Valid examples: `80`, `80,443` and `3306-20000`.
47+
* `protocol` - (Optional, String) Protocol of the security policy rule. Default value is `ALL`. Valid value: `TCP`, `UDP` and `ALL`.
4848

4949
## Attributes Reference
5050

0 commit comments

Comments
 (0)