Skip to content

Commit 0bcbb4d

Browse files
gitmknanonymous
andauthored
Feat/tcm mesh (#1600)
* feat: add datatsource tcm_mesh * feat: add doc * feat: add test * feat: support datasource tencentcloud_tcm_mesh * fix: update query field --------- Co-authored-by: anonymous <anonymous@mail.org>
1 parent 3197183 commit 0bcbb4d

File tree

7 files changed

+548
-1
lines changed

7 files changed

+548
-1
lines changed

.changelog/1600.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
tencentcloud_tcm_mesh
3+
```
Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
/*
2+
Use this data source to query detailed information of tcm mesh
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_tcm_mesh" "mesh" {
8+
mesh_id = ["mesh-xxxxxx"]
9+
mesh_name = ["KEEP_MASH"]
10+
tags = ["key"]
11+
mesh_cluster = ["cls-xxxx"]
12+
}
13+
```
14+
*/
15+
package tencentcloud
16+
17+
import (
18+
"context"
19+
20+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
21+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
22+
tcm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcm/v20210413"
23+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
24+
)
25+
26+
func dataSourceTencentCloudTcmMesh() *schema.Resource {
27+
return &schema.Resource{
28+
Read: dataSourceTencentCloudTcmMeshRead,
29+
Schema: map[string]*schema.Schema{
30+
"mesh_id": {
31+
Optional: true,
32+
Type: schema.TypeSet,
33+
Elem: &schema.Schema{
34+
Type: schema.TypeString,
35+
},
36+
Description: "Mesh instance Id.",
37+
},
38+
39+
"mesh_name": {
40+
Optional: true,
41+
Type: schema.TypeSet,
42+
Elem: &schema.Schema{
43+
Type: schema.TypeString,
44+
},
45+
Description: "Display name.",
46+
},
47+
48+
"tags": {
49+
Optional: true,
50+
Type: schema.TypeSet,
51+
Elem: &schema.Schema{
52+
Type: schema.TypeString,
53+
},
54+
Description: "tag key.",
55+
},
56+
57+
"mesh_cluster": {
58+
Optional: true,
59+
Type: schema.TypeSet,
60+
Elem: &schema.Schema{
61+
Type: schema.TypeString,
62+
},
63+
Description: "Mesh name.",
64+
},
65+
66+
"mesh_list": {
67+
Computed: true,
68+
Type: schema.TypeList,
69+
Description: "The mesh information is queriedNote: This field may return null, indicating that a valid value is not available.",
70+
Elem: &schema.Resource{
71+
Schema: map[string]*schema.Schema{
72+
"mesh_id": {
73+
Type: schema.TypeString,
74+
Computed: true,
75+
Description: "Mesh instance Id.",
76+
},
77+
"display_name": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
Description: "Mesh name.",
81+
},
82+
"version": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
Description: "Mesh version.",
86+
},
87+
"type": {
88+
Type: schema.TypeString,
89+
Computed: true,
90+
Description: "Mesh type. Value range:- `STANDALONE`: Standalone mesh- `HOSTED`: hosted the mesh.",
91+
},
92+
"config": {
93+
Type: schema.TypeList,
94+
Computed: true,
95+
Description: "Mesh configuration.",
96+
Elem: &schema.Resource{
97+
Schema: map[string]*schema.Schema{
98+
"istio": {
99+
Type: schema.TypeList,
100+
Computed: true,
101+
Description: "Istio configuration.",
102+
Elem: &schema.Resource{
103+
Schema: map[string]*schema.Schema{
104+
"outbound_traffic_policy": {
105+
Type: schema.TypeString,
106+
Computed: true,
107+
Description: "Outbound traffic policy.",
108+
},
109+
"disable_policy_checks": {
110+
Type: schema.TypeBool,
111+
Computed: true,
112+
Description: "Disable policy checks.",
113+
},
114+
"enable_pilot_http": {
115+
Type: schema.TypeBool,
116+
Computed: true,
117+
Description: "Enable HTTP/1.0 support.",
118+
},
119+
"disable_http_retry": {
120+
Type: schema.TypeBool,
121+
Computed: true,
122+
Description: "Disable http retry.",
123+
},
124+
"smart_dns": {
125+
Type: schema.TypeList,
126+
Computed: true,
127+
Description: "SmartDNS configuration.",
128+
Elem: &schema.Resource{
129+
Schema: map[string]*schema.Schema{
130+
"istio_meta_dns_capture": {
131+
Type: schema.TypeBool,
132+
Computed: true,
133+
Description: "Enable dns proxy.",
134+
},
135+
"istio_meta_dns_auto_allocate": {
136+
Type: schema.TypeBool,
137+
Computed: true,
138+
Description: "Enable auto allocate address.",
139+
},
140+
},
141+
},
142+
},
143+
},
144+
},
145+
},
146+
},
147+
},
148+
},
149+
"tag_list": {
150+
Type: schema.TypeList,
151+
Computed: true,
152+
Description: "A list of associated tags.",
153+
Elem: &schema.Resource{
154+
Schema: map[string]*schema.Schema{
155+
"key": {
156+
Type: schema.TypeString,
157+
Computed: true,
158+
Description: "Tag key.",
159+
},
160+
"value": {
161+
Type: schema.TypeString,
162+
Computed: true,
163+
Description: "Tag value.",
164+
},
165+
"passthrough": {
166+
Type: schema.TypeBool,
167+
Computed: true,
168+
Description: "Passthrough to other related product.",
169+
},
170+
},
171+
},
172+
},
173+
},
174+
},
175+
},
176+
177+
"result_output_file": {
178+
Type: schema.TypeString,
179+
Optional: true,
180+
Description: "Used to save results.",
181+
},
182+
},
183+
}
184+
}
185+
186+
func dataSourceTencentCloudTcmMeshRead(d *schema.ResourceData, meta interface{}) error {
187+
defer logElapsed("data_source.tencentcloud_tcm_mesh.read")()
188+
defer inconsistentCheck(d, meta)()
189+
190+
logId := getLogId(contextNil)
191+
192+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
193+
194+
paramMap := make(map[string][]*string)
195+
if v, ok := d.GetOk("mesh_id"); ok {
196+
meshIdSet := v.(*schema.Set).List()
197+
paramMap["MeshId"] = helper.InterfacesStringsPoint(meshIdSet)
198+
}
199+
200+
if v, ok := d.GetOk("mesh_name"); ok {
201+
meshName := v.(*schema.Set).List()
202+
paramMap["MeshName"] = helper.InterfacesStringsPoint(meshName)
203+
}
204+
205+
if v, ok := d.GetOk("tags"); ok {
206+
tagsSet := v.(*schema.Set).List()
207+
paramMap["Tags"] = helper.InterfacesStringsPoint(tagsSet)
208+
}
209+
210+
if v, ok := d.GetOk("mesh_cluster"); ok {
211+
meshClusterSet := v.(*schema.Set).List()
212+
paramMap["MeshCluster"] = helper.InterfacesStringsPoint(meshClusterSet)
213+
}
214+
215+
service := TcmService{client: meta.(*TencentCloudClient).apiV3Conn}
216+
217+
var meshList []*tcm.Mesh
218+
219+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
220+
result, e := service.DescribeTcmMeshByFilter(ctx, paramMap)
221+
if e != nil {
222+
return retryError(e)
223+
}
224+
meshList = result
225+
return nil
226+
})
227+
if err != nil {
228+
return err
229+
}
230+
231+
ids := make([]string, 0, len(meshList))
232+
tmpList := make([]map[string]interface{}, 0, len(meshList))
233+
234+
if meshList != nil {
235+
for _, mesh := range meshList {
236+
meshMap := map[string]interface{}{}
237+
238+
if mesh.MeshId != nil {
239+
meshMap["mesh_id"] = mesh.MeshId
240+
}
241+
242+
if mesh.DisplayName != nil {
243+
meshMap["display_name"] = mesh.DisplayName
244+
}
245+
246+
if mesh.Version != nil {
247+
meshMap["version"] = mesh.Version
248+
}
249+
250+
if mesh.Type != nil {
251+
meshMap["type"] = mesh.Type
252+
}
253+
254+
if mesh.Config != nil {
255+
configMap := map[string]interface{}{}
256+
257+
if mesh.Config.Istio != nil {
258+
istioMap := map[string]interface{}{}
259+
260+
if mesh.Config.Istio.OutboundTrafficPolicy != nil {
261+
istioMap["outbound_traffic_policy"] = mesh.Config.Istio.OutboundTrafficPolicy
262+
}
263+
264+
if mesh.Config.Istio.DisablePolicyChecks != nil {
265+
istioMap["disable_policy_checks"] = mesh.Config.Istio.DisablePolicyChecks
266+
}
267+
268+
if mesh.Config.Istio.EnablePilotHTTP != nil {
269+
istioMap["enable_pilot_http"] = mesh.Config.Istio.EnablePilotHTTP
270+
}
271+
272+
if mesh.Config.Istio.DisableHTTPRetry != nil {
273+
istioMap["disable_http_retry"] = mesh.Config.Istio.DisableHTTPRetry
274+
}
275+
276+
if mesh.Config.Istio.SmartDNS != nil {
277+
smartDNSMap := map[string]interface{}{}
278+
279+
if mesh.Config.Istio.SmartDNS.IstioMetaDNSCapture != nil {
280+
smartDNSMap["istio_meta_dns_capture"] = mesh.Config.Istio.SmartDNS.IstioMetaDNSCapture
281+
}
282+
283+
if mesh.Config.Istio.SmartDNS.IstioMetaDNSAutoAllocate != nil {
284+
smartDNSMap["istio_meta_dns_auto_allocate"] = mesh.Config.Istio.SmartDNS.IstioMetaDNSAutoAllocate
285+
}
286+
287+
istioMap["smart_dns"] = []interface{}{smartDNSMap}
288+
}
289+
290+
configMap["istio"] = []interface{}{istioMap}
291+
}
292+
293+
meshMap["config"] = []interface{}{configMap}
294+
}
295+
296+
if mesh.TagList != nil {
297+
tagListList := []interface{}{}
298+
for _, tagList := range mesh.TagList {
299+
tagListMap := map[string]interface{}{}
300+
301+
if tagList.Key != nil {
302+
tagListMap["key"] = tagList.Key
303+
}
304+
305+
if tagList.Value != nil {
306+
tagListMap["value"] = tagList.Value
307+
}
308+
309+
if tagList.Passthrough != nil {
310+
tagListMap["passthrough"] = tagList.Passthrough
311+
}
312+
313+
tagListList = append(tagListList, tagListMap)
314+
}
315+
316+
meshMap["tag_list"] = tagListList
317+
}
318+
319+
ids = append(ids, *mesh.MeshId)
320+
tmpList = append(tmpList, meshMap)
321+
}
322+
323+
err := d.Set("mesh_list", tmpList)
324+
if err != nil {
325+
return err
326+
}
327+
}
328+
329+
d.SetId(helper.DataResourceIdsHash(ids))
330+
output, ok := d.GetOk("result_output_file")
331+
if ok && output.(string) != "" {
332+
if e := writeToFile(output.(string), tmpList); e != nil {
333+
return e
334+
}
335+
}
336+
return nil
337+
}

0 commit comments

Comments
 (0)