This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_source_tc_clb_redirections.go
163 lines (149 loc) · 4.75 KB
/
data_source_tc_clb_redirections.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Use this data source to query detailed information of CLB redirections
Example Usage
```hcl
data "tencentcloud_clb_redirections" "foo" {
clb_id = "lb-p7olt9e5"
source_listener_id = "lbl-jc1dx6ju"
target_listener_id = "lbl-asj1hzuo"
source_rule_id = "loc-ft8fmngv"
target_rule_id = "loc-4xxr2cy7"
result_output_file = "mytestpath"
}
```
*/
package tencentcloud
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func dataSourceTencentCloudClbRedirections() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudClbRedirectionsRead,
Schema: map[string]*schema.Schema{
"clb_id": {
Type: schema.TypeString,
Required: true,
Description: "Id of the CLB to be queried.",
},
"source_listener_id": {
Type: schema.TypeString,
Required: true,
Description: "Id of source listener to be queried.",
},
"target_listener_id": {
Type: schema.TypeString,
Optional: true,
Description: "Id of target listener to be queried.",
},
"source_rule_id": {
Type: schema.TypeString,
Required: true,
Description: "Rule id of source listener to be queried.",
},
"target_rule_id": {
Type: schema.TypeString,
Optional: true,
Description: "Rule id of target listener to be queried.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
"redirection_list": {
Type: schema.TypeList,
Computed: true,
Description: "A list of cloud load balancer redirection configurations. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"clb_id": {
Type: schema.TypeString,
Computed: true,
Description: "Id of the CLB.",
},
"source_listener_id": {
Type: schema.TypeString,
Computed: true,
Description: "Id of source listener.",
},
"target_listener_id": {
Type: schema.TypeString,
Computed: true,
Description: "Id of target listener.",
},
"source_rule_id": {
Type: schema.TypeString,
Computed: true,
Description: "Rule id of source listener.",
},
"target_rule_id": {
Type: schema.TypeString,
Computed: true,
Description: "Rule id of target listener.",
},
},
},
},
},
}
}
func dataSourceTencentCloudClbRedirectionsRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_clb_redirections.read")()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
params := make(map[string]string)
params["clb_id"] = d.Get("clb_id").(string)
params["source_listener_id"] = d.Get("source_listener_id").(string)
params["source_rule_id"] = d.Get("source_rule_id").(string)
if v, ok := d.GetOk("target_listener_id"); ok {
params["target_listener_id"] = v.(string)
}
if v, ok := d.GetOk("target_rule_id"); ok {
params["target_rule_id"] = v.(string)
}
clbService := ClbService{
client: meta.(*TencentCloudClient).apiV3Conn,
}
var redirections []*map[string]string
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
results, e := clbService.DescribeRedirectionsByFilter(ctx, params)
if e != nil {
return retryError(e)
}
redirections = results
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read CLB redirections failed, reason:%+v", logId, err)
return err
}
redirectionList := make([]map[string]interface{}, 0, len(redirections))
ids := make([]string, 0, len(redirections))
for _, r := range redirections {
mapping := map[string]interface{}{
"clb_id": (*r)["clb_id"],
"source_listener_id": (*r)["source_listener_id"],
"target_listener_id": (*r)["target_listener_id"],
"source_rule_id": (*r)["source_rule_id"],
"target_rule_id": (*r)["target_rule_id"],
}
redirectionList = append(redirectionList, mapping)
ids = append(ids, (*r)["source_rule_id"]+"#"+(*r)["target_rule_id"]+(*r)["source_listener_id"]+"#"+(*r)["target_listener_id"]+"#"+(*r)["clb_id"])
}
d.SetId(helper.DataResourceIdsHash(ids))
if e := d.Set("redirection_list", redirectionList); e != nil {
log.Printf("[CRITAL]%s provider set CLB redirection list fail, reason:%+v", logId, e)
return e
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := writeToFile(output.(string), redirectionList); e != nil {
return e
}
}
return nil
}