|
| 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 | +} |
0 commit comments