Skip to content

Commit c1c5e00

Browse files
tongyimingmikatong
andauthored
Feat/lighthouse firewall (#1624)
* support tencentcloud_lighthouse_firewall_rule * support tencentcloud_lighthouse_firewall_rules_template * add changelog * update --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent ddcf7cc commit c1c5e00

10 files changed

+783
-1
lines changed

.changelog/1624.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-resource
2+
tencentcloud_lighthouse_firewall_rule
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_lighthouse_firewall_rules_template
7+
```
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
Use this data source to query detailed information of lighthouse firewall_rules_template
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_lighthouse_firewall_rules_template" "firewall_rules_template" {
8+
}
9+
```
10+
*/
11+
package tencentcloud
12+
13+
import (
14+
"context"
15+
"encoding/json"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
19+
lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324"
20+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
21+
)
22+
23+
func dataSourceTencentCloudLighthouseFirewallRulesTemplate() *schema.Resource {
24+
return &schema.Resource{
25+
Read: dataSourceTencentCloudLighthouseFirewallRulesTemplateRead,
26+
Schema: map[string]*schema.Schema{
27+
"firewall_rule_set": {
28+
Computed: true,
29+
Type: schema.TypeList,
30+
Description: "Firewall rule details list.",
31+
Elem: &schema.Resource{
32+
Schema: map[string]*schema.Schema{
33+
"app_type": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
Description: "Application type. Valid values are custom, HTTP (80), HTTPS (443), Linux login (22), Windows login (3389), MySQL (3306), SQL Server (1433), all TCP ports, all UDP ports, Ping-ICMP, ALL.",
37+
},
38+
"protocol": {
39+
Type: schema.TypeString,
40+
Computed: true,
41+
Description: "Protocol. Valid values are TCP, UDP, ICMP, ALL.",
42+
},
43+
"port": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
Description: "Port. Valid values are ALL, one single port, multiple ports separated by commas, or port range indicated by a minus sign.",
47+
},
48+
"cidr_block": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
Description: "IP range or IP (mutually exclusive). Default value is 0.0.0.0/0, which indicates all sources.",
52+
},
53+
"action": {
54+
Type: schema.TypeString,
55+
Computed: true,
56+
Description: "Valid values are (ACCEPT, DROP). Default value is ACCEPT.",
57+
},
58+
"firewall_rule_description": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
Description: "Firewall rule description.",
62+
},
63+
},
64+
},
65+
},
66+
67+
"result_output_file": {
68+
Type: schema.TypeString,
69+
Optional: true,
70+
Description: "Used to save results.",
71+
},
72+
},
73+
}
74+
}
75+
76+
func dataSourceTencentCloudLighthouseFirewallRulesTemplateRead(d *schema.ResourceData, meta interface{}) error {
77+
defer logElapsed("data_source.tencentcloud_lighthouse_firewall_rules_template.read")()
78+
defer inconsistentCheck(d, meta)()
79+
80+
logId := getLogId(contextNil)
81+
82+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
83+
84+
service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn}
85+
86+
var firewallRuleSet []*lighthouse.FirewallRuleInfo
87+
88+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
89+
result, e := service.DescribeLighthouseFirewallRulesTemplateByFilter(ctx)
90+
if e != nil {
91+
return retryError(e)
92+
}
93+
firewallRuleSet = result
94+
return nil
95+
})
96+
if err != nil {
97+
return err
98+
}
99+
100+
ids := make([]string, 0, len(firewallRuleSet))
101+
tmpList := make([]map[string]interface{}, 0, len(firewallRuleSet))
102+
103+
if firewallRuleSet != nil {
104+
for _, firewallRuleInfo := range firewallRuleSet {
105+
firewallRuleInfoMap := map[string]interface{}{}
106+
107+
if firewallRuleInfo.AppType != nil {
108+
firewallRuleInfoMap["app_type"] = firewallRuleInfo.AppType
109+
}
110+
111+
if firewallRuleInfo.Protocol != nil {
112+
firewallRuleInfoMap["protocol"] = firewallRuleInfo.Protocol
113+
}
114+
115+
if firewallRuleInfo.Port != nil {
116+
firewallRuleInfoMap["port"] = firewallRuleInfo.Port
117+
}
118+
119+
if firewallRuleInfo.CidrBlock != nil {
120+
firewallRuleInfoMap["cidr_block"] = firewallRuleInfo.CidrBlock
121+
}
122+
123+
if firewallRuleInfo.Action != nil {
124+
firewallRuleInfoMap["action"] = firewallRuleInfo.Action
125+
}
126+
127+
if firewallRuleInfo.FirewallRuleDescription != nil {
128+
firewallRuleInfoMap["firewall_rule_description"] = firewallRuleInfo.FirewallRuleDescription
129+
}
130+
firewallRuleInfoJson, err := json.Marshal(*firewallRuleInfo)
131+
if err != nil {
132+
return err
133+
}
134+
ids = append(ids, string(firewallRuleInfoJson))
135+
tmpList = append(tmpList, firewallRuleInfoMap)
136+
}
137+
138+
_ = d.Set("firewall_rule_set", tmpList)
139+
}
140+
141+
d.SetId(helper.DataResourceIdsHash(ids))
142+
output, ok := d.GetOk("result_output_file")
143+
if ok && output.(string) != "" {
144+
if e := writeToFile(output.(string), tmpList); e != nil {
145+
return e
146+
}
147+
}
148+
return nil
149+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudLighthouseFirewallRulesTemplateDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
13+
Providers: testAccProviders,
14+
Steps: []resource.TestStep{
15+
{
16+
Config: testAccLighthouseFirewallRulesTemplateDataSource,
17+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_lighthouse_firewall_rules_template.firewall_rules_template")),
18+
},
19+
},
20+
})
21+
}
22+
23+
const testAccLighthouseFirewallRulesTemplateDataSource = `
24+
25+
data "tencentcloud_lighthouse_firewall_rules_template" "firewall_rules_template" {
26+
}
27+
28+
`

tencentcloud/provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,9 @@ TencentCloud Lighthouse(Lighthouse)
718718
Resource
719719
tencentcloud_lighthouse_instance
720720
tencentcloud_lighthouse_blueprint
721+
tencentcloud_lighthouse_firewall_rule
722+
Data Source
723+
tencentcloud_lighthouse_firewall_rules_template
721724
722725
TencentCloud Elastic Microservice(TEM)
723726
Resource
@@ -1356,6 +1359,7 @@ func Provider() terraform.ResourceProvider {
13561359
"tencentcloud_chdfs_access_groups": dataSourceTencentCloudChdfsAccessGroups(),
13571360
"tencentcloud_chdfs_mount_points": dataSourceTencentCloudChdfsMountPoints(),
13581361
"tencentcloud_tcm_mesh": dataSourceTencentCloudTcmMesh(),
1362+
"tencentcloud_lighthouse_firewall_rules_template": dataSourceTencentCloudLighthouseFirewallRulesTemplate(),
13591363
},
13601364

13611365
ResourcesMap: map[string]*schema.Resource{
@@ -1778,6 +1782,7 @@ func Provider() terraform.ResourceProvider {
17781782
"tencentcloud_cvm_launch_template_version": resourceTencentCloudCvmLaunchTemplateVersion(),
17791783
"tencentcloud_apm_instance": resourceTencentCloudApmInstance(),
17801784
"tencentcloud_cvm_launch_template_default_version": resourceTencentCloudCvmLaunchTemplateDefaultVersion(),
1785+
"tencentcloud_lighthouse_firewall_rule": resourceTencentCloudLighthouseFirewallRule(),
17811786
},
17821787

17831788
ConfigureFunc: providerConfigure,

0 commit comments

Comments
 (0)