|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of redis instance_node_info |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_redis_instance_node_info" "instance_node_info" { |
| 8 | + instance_id = "crs-c1nl9rpv" |
| 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 | + sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" |
| 20 | + redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudRedisInstanceNodeInfo() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudRedisInstanceNodeInfoRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "instance_id": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "The ID of instance.", |
| 32 | + }, |
| 33 | + |
| 34 | + "proxy_count": { |
| 35 | + Computed: true, |
| 36 | + Type: schema.TypeInt, |
| 37 | + Description: "Number of proxy nodes.", |
| 38 | + }, |
| 39 | + |
| 40 | + "proxy": { |
| 41 | + Computed: true, |
| 42 | + Type: schema.TypeList, |
| 43 | + Description: "Proxy node information.", |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "node_id": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: "Node ID.", |
| 50 | + }, |
| 51 | + "zone_id": { |
| 52 | + Type: schema.TypeInt, |
| 53 | + Computed: true, |
| 54 | + Description: "Zone ID.", |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + |
| 60 | + "redis_count": { |
| 61 | + Computed: true, |
| 62 | + Type: schema.TypeInt, |
| 63 | + Description: "Number of redis nodes.", |
| 64 | + }, |
| 65 | + |
| 66 | + "redis": { |
| 67 | + Computed: true, |
| 68 | + Type: schema.TypeList, |
| 69 | + Description: "Redis node information.", |
| 70 | + Elem: &schema.Resource{ |
| 71 | + Schema: map[string]*schema.Schema{ |
| 72 | + "node_id": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "Node ID.", |
| 76 | + }, |
| 77 | + "node_role": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "Node role.", |
| 81 | + }, |
| 82 | + "cluster_id": { |
| 83 | + Type: schema.TypeInt, |
| 84 | + Computed: true, |
| 85 | + Description: "Shard ID.", |
| 86 | + }, |
| 87 | + "zone_id": { |
| 88 | + Type: schema.TypeInt, |
| 89 | + Computed: true, |
| 90 | + Description: "Zone ID.", |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + |
| 96 | + "result_output_file": { |
| 97 | + Type: schema.TypeString, |
| 98 | + Optional: true, |
| 99 | + Description: "Used to save results.", |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func dataSourceTencentCloudRedisInstanceNodeInfoRead(d *schema.ResourceData, meta interface{}) error { |
| 106 | + defer logElapsed("data_source.tencentcloud_redis_instance_node_info.read")() |
| 107 | + defer inconsistentCheck(d, meta)() |
| 108 | + |
| 109 | + logId := getLogId(contextNil) |
| 110 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 111 | + |
| 112 | + var instanceId string |
| 113 | + paramMap := make(map[string]interface{}) |
| 114 | + if v, ok := d.GetOk("instance_id"); ok { |
| 115 | + instanceId = v.(string) |
| 116 | + paramMap["InstanceId"] = helper.String(v.(string)) |
| 117 | + } |
| 118 | + |
| 119 | + service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 120 | + var instanceNodeInfo *redis.DescribeInstanceNodeInfoResponseParams |
| 121 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 122 | + result, e := service.DescribeRedisInstanceNodeInfoByFilter(ctx, paramMap) |
| 123 | + if e != nil { |
| 124 | + if sdkerr, ok := e.(*sdkErrors.TencentCloudSDKError); ok { |
| 125 | + if sdkerr.Code == "FailedOperation.SystemError" { |
| 126 | + return nil |
| 127 | + } |
| 128 | + } |
| 129 | + return retryError(e) |
| 130 | + } |
| 131 | + instanceNodeInfo = result |
| 132 | + return nil |
| 133 | + }) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + if instanceNodeInfo.ProxyCount != nil { |
| 139 | + _ = d.Set("proxy_count", instanceNodeInfo.ProxyCount) |
| 140 | + } |
| 141 | + |
| 142 | + if instanceNodeInfo.Proxy != nil { |
| 143 | + tmpList := make([]map[string]interface{}, 0, len(instanceNodeInfo.Proxy)) |
| 144 | + for _, proxyNodes := range instanceNodeInfo.Proxy { |
| 145 | + proxyNodesMap := map[string]interface{}{} |
| 146 | + |
| 147 | + if proxyNodes.NodeId != nil { |
| 148 | + proxyNodesMap["node_id"] = proxyNodes.NodeId |
| 149 | + } |
| 150 | + |
| 151 | + if proxyNodes.ZoneId != nil { |
| 152 | + proxyNodesMap["zone_id"] = proxyNodes.ZoneId |
| 153 | + } |
| 154 | + |
| 155 | + tmpList = append(tmpList, proxyNodesMap) |
| 156 | + } |
| 157 | + |
| 158 | + _ = d.Set("proxy", tmpList) |
| 159 | + } |
| 160 | + |
| 161 | + if instanceNodeInfo.RedisCount != nil { |
| 162 | + _ = d.Set("redis_count", instanceNodeInfo.RedisCount) |
| 163 | + } |
| 164 | + |
| 165 | + if instanceNodeInfo.Redis != nil { |
| 166 | + tmpList := make([]map[string]interface{}, 0, len(instanceNodeInfo.Redis)) |
| 167 | + for _, redisNodes := range instanceNodeInfo.Redis { |
| 168 | + redisNodesMap := map[string]interface{}{} |
| 169 | + |
| 170 | + if redisNodes.NodeId != nil { |
| 171 | + redisNodesMap["node_id"] = redisNodes.NodeId |
| 172 | + } |
| 173 | + |
| 174 | + if redisNodes.NodeRole != nil { |
| 175 | + redisNodesMap["node_role"] = redisNodes.NodeRole |
| 176 | + } |
| 177 | + |
| 178 | + if redisNodes.ClusterId != nil { |
| 179 | + redisNodesMap["cluster_id"] = redisNodes.ClusterId |
| 180 | + } |
| 181 | + |
| 182 | + if redisNodes.ZoneId != nil { |
| 183 | + redisNodesMap["zone_id"] = redisNodes.ZoneId |
| 184 | + } |
| 185 | + |
| 186 | + tmpList = append(tmpList, redisNodesMap) |
| 187 | + } |
| 188 | + |
| 189 | + _ = d.Set("redis", tmpList) |
| 190 | + } |
| 191 | + |
| 192 | + d.SetId(instanceId) |
| 193 | + output, ok := d.GetOk("result_output_file") |
| 194 | + if ok && output.(string) != "" { |
| 195 | + if e := writeToFile(output.(string), d); e != nil { |
| 196 | + return e |
| 197 | + } |
| 198 | + } |
| 199 | + return nil |
| 200 | +} |
0 commit comments