|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of tse gateway_nodes |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_tse_gateway_nodes" "gateway_nodes" { |
| 8 | + gateway_id = "gateway-ddbb709b" |
| 9 | + group_id = "group-013c0d8e" |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 20 | + tse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse/v20201207" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudTseGatewayNodes() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudTseGatewayNodesRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "gateway_id": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "gateway ID.", |
| 32 | + }, |
| 33 | + |
| 34 | + "group_id": { |
| 35 | + Optional: true, |
| 36 | + Type: schema.TypeString, |
| 37 | + Description: "gateway group ID.", |
| 38 | + }, |
| 39 | + |
| 40 | + "node_list": { |
| 41 | + Type: schema.TypeList, |
| 42 | + Computed: true, |
| 43 | + Description: "nodes information.", |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "node_id": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: "gateway node id.", |
| 50 | + }, |
| 51 | + "node_ip": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: "gateway node ip.", |
| 55 | + }, |
| 56 | + "zone_id": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "Zone idNote: This field may return null, indicating that a valid value is not available.", |
| 60 | + }, |
| 61 | + "zone": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: "ZoneNote: This field may return null, indicating that a valid value is not available.", |
| 65 | + }, |
| 66 | + "group_id": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Group IDNote: This field may return null, indicating that a valid value is not available.", |
| 70 | + }, |
| 71 | + "group_name": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "Group nameNote: This field may return null, indicating that a valid value is not available.", |
| 75 | + }, |
| 76 | + "status": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "statusNote: This field may return null, indicating that a valid value is not available.", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + |
| 85 | + "result_output_file": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Optional: true, |
| 88 | + Description: "Used to save results.", |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func dataSourceTencentCloudTseGatewayNodesRead(d *schema.ResourceData, meta interface{}) error { |
| 95 | + defer logElapsed("data_source.tencentcloud_tse_gateway_nodes.read")() |
| 96 | + defer inconsistentCheck(d, meta)() |
| 97 | + |
| 98 | + logId := getLogId(contextNil) |
| 99 | + |
| 100 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 101 | + |
| 102 | + paramMap := make(map[string]interface{}) |
| 103 | + if v, ok := d.GetOk("gateway_id"); ok { |
| 104 | + paramMap["GatewayId"] = helper.String(v.(string)) |
| 105 | + } |
| 106 | + |
| 107 | + if v, ok := d.GetOk("group_id"); ok { |
| 108 | + paramMap["GroupId"] = helper.String(v.(string)) |
| 109 | + } |
| 110 | + |
| 111 | + service := TseService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 112 | + |
| 113 | + var result []*tse.CloudNativeAPIGatewayNode |
| 114 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 115 | + response, e := service.DescribeTseGatewayNodesByFilter(ctx, paramMap) |
| 116 | + if e != nil { |
| 117 | + return retryError(e) |
| 118 | + } |
| 119 | + result = response |
| 120 | + return nil |
| 121 | + }) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + ids := make([]string, 0, len(result)) |
| 127 | + nodeListList := []interface{}{} |
| 128 | + if result != nil { |
| 129 | + for _, nodeList := range result { |
| 130 | + nodeListMap := map[string]interface{}{} |
| 131 | + |
| 132 | + if nodeList.NodeId != nil { |
| 133 | + nodeListMap["node_id"] = nodeList.NodeId |
| 134 | + } |
| 135 | + |
| 136 | + if nodeList.NodeIp != nil { |
| 137 | + nodeListMap["node_ip"] = nodeList.NodeIp |
| 138 | + } |
| 139 | + |
| 140 | + if nodeList.ZoneId != nil { |
| 141 | + nodeListMap["zone_id"] = nodeList.ZoneId |
| 142 | + } |
| 143 | + |
| 144 | + if nodeList.Zone != nil { |
| 145 | + nodeListMap["zone"] = nodeList.Zone |
| 146 | + } |
| 147 | + |
| 148 | + if nodeList.GroupId != nil { |
| 149 | + nodeListMap["group_id"] = nodeList.GroupId |
| 150 | + } |
| 151 | + |
| 152 | + if nodeList.GroupName != nil { |
| 153 | + nodeListMap["group_name"] = nodeList.GroupName |
| 154 | + } |
| 155 | + |
| 156 | + if nodeList.Status != nil { |
| 157 | + nodeListMap["status"] = nodeList.Status |
| 158 | + } |
| 159 | + |
| 160 | + nodeListList = append(nodeListList, nodeListMap) |
| 161 | + ids = append(ids, *nodeList.NodeId) |
| 162 | + } |
| 163 | + |
| 164 | + _ = d.Set("node_list", nodeListList) |
| 165 | + } |
| 166 | + |
| 167 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 168 | + output, ok := d.GetOk("result_output_file") |
| 169 | + if ok && output.(string) != "" { |
| 170 | + if e := writeToFile(output.(string), nodeListList); e != nil { |
| 171 | + return e |
| 172 | + } |
| 173 | + } |
| 174 | + return nil |
| 175 | +} |
0 commit comments