|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of tse nacos_server_interfaces |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_tse_nacos_server_interfaces" "nacos_server_interfaces" { |
| 8 | + instance_id = "ins-xxxxxx" |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 19 | + tse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse/v20201207" |
| 20 | +) |
| 21 | + |
| 22 | +func dataSourceTencentCloudTseNacosServerInterfaces() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Read: dataSourceTencentCloudTseNacosServerInterfacesRead, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "instance_id": { |
| 27 | + Optional: true, |
| 28 | + Type: schema.TypeString, |
| 29 | + Description: "engine instance ID.", |
| 30 | + }, |
| 31 | + |
| 32 | + "content": { |
| 33 | + Computed: true, |
| 34 | + Type: schema.TypeList, |
| 35 | + Description: "interface list.", |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "interface": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + Description: "interface nameNote: This field may return null, indicating that a valid value is not available.", |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + |
| 47 | + "result_output_file": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Optional: true, |
| 50 | + Description: "Used to save results.", |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func dataSourceTencentCloudTseNacosServerInterfacesRead(d *schema.ResourceData, meta interface{}) error { |
| 57 | + defer logElapsed("data_source.tencentcloud_tse_nacos_server_interfaces.read")() |
| 58 | + defer inconsistentCheck(d, meta)() |
| 59 | + |
| 60 | + logId := getLogId(contextNil) |
| 61 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 62 | + |
| 63 | + var instanceId string |
| 64 | + if v, ok := d.GetOk("instance_id"); ok { |
| 65 | + instanceId = v.(string) |
| 66 | + } |
| 67 | + |
| 68 | + service := TseService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 69 | + var content []*tse.NacosServerInterface |
| 70 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 71 | + result, e := service.DescribeTseNacosServerInterfacesByFilter(ctx, instanceId) |
| 72 | + if e != nil { |
| 73 | + return retryError(e) |
| 74 | + } |
| 75 | + content = result |
| 76 | + return nil |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + tmpList := make([]map[string]interface{}, 0, len(content)) |
| 83 | + |
| 84 | + if content != nil { |
| 85 | + for _, nacosServerInterface := range content { |
| 86 | + nacosServerInterfaceMap := map[string]interface{}{} |
| 87 | + |
| 88 | + if nacosServerInterface.Interface != nil { |
| 89 | + nacosServerInterfaceMap["interface"] = nacosServerInterface.Interface |
| 90 | + } |
| 91 | + |
| 92 | + tmpList = append(tmpList, nacosServerInterfaceMap) |
| 93 | + } |
| 94 | + |
| 95 | + _ = d.Set("content", tmpList) |
| 96 | + } |
| 97 | + |
| 98 | + d.SetId(instanceId) |
| 99 | + output, ok := d.GetOk("result_output_file") |
| 100 | + if ok && output.(string) != "" { |
| 101 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 102 | + return e |
| 103 | + } |
| 104 | + } |
| 105 | + return nil |
| 106 | +} |
0 commit comments