|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of redis instance_shards |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_redis_instance_shards" "instance_shards" { |
| 8 | + instance_id = "crs-c1nl9rpv" |
| 9 | + filter_slave = false |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 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 dataSourceTencentCloudRedisInstanceShards() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudRedisInstanceShardsRead, |
| 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 | + "filter_slave": { |
| 35 | + Optional: true, |
| 36 | + Type: schema.TypeBool, |
| 37 | + Description: "Whether to filter out slave information.", |
| 38 | + }, |
| 39 | + |
| 40 | + "instance_shards": { |
| 41 | + Computed: true, |
| 42 | + Type: schema.TypeList, |
| 43 | + Description: "Instance shard list information.", |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "shard_name": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: "Shard node name.", |
| 50 | + }, |
| 51 | + "shard_id": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: "Shard node ID.", |
| 55 | + }, |
| 56 | + "role": { |
| 57 | + Type: schema.TypeInt, |
| 58 | + Computed: true, |
| 59 | + Description: "role.", |
| 60 | + }, |
| 61 | + "keys": { |
| 62 | + Type: schema.TypeInt, |
| 63 | + Computed: true, |
| 64 | + Description: "Number of keys.", |
| 65 | + }, |
| 66 | + "slots": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Slot information.", |
| 70 | + }, |
| 71 | + "storage": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Computed: true, |
| 74 | + Description: "Used capacity.", |
| 75 | + }, |
| 76 | + "storage_slope": { |
| 77 | + Type: schema.TypeFloat, |
| 78 | + Computed: true, |
| 79 | + Description: "Capacity tilt.", |
| 80 | + }, |
| 81 | + "runid": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Computed: true, |
| 84 | + Description: "The node ID of the instance runtime.", |
| 85 | + }, |
| 86 | + "connected": { |
| 87 | + Type: schema.TypeInt, |
| 88 | + Computed: true, |
| 89 | + Description: "Service status: 0-down;1-on.", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + |
| 95 | + "result_output_file": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Optional: true, |
| 98 | + Description: "Used to save results.", |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func dataSourceTencentCloudRedisInstanceShardsRead(d *schema.ResourceData, meta interface{}) error { |
| 105 | + defer logElapsed("data_source.tencentcloud_redis_instance_shards.read")() |
| 106 | + defer inconsistentCheck(d, meta)() |
| 107 | + |
| 108 | + logId := getLogId(contextNil) |
| 109 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 110 | + var instanceId string |
| 111 | + |
| 112 | + paramMap := make(map[string]interface{}) |
| 113 | + if v, ok := d.GetOk("instance_id"); ok { |
| 114 | + instanceId = v.(string) |
| 115 | + paramMap["InstanceId"] = helper.String(v.(string)) |
| 116 | + } |
| 117 | + |
| 118 | + if v, _ := d.GetOk("filter_slave"); v != nil { |
| 119 | + paramMap["FilterSlave"] = helper.Bool(v.(bool)) |
| 120 | + } |
| 121 | + |
| 122 | + service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 123 | + |
| 124 | + var instanceShards []*redis.InstanceClusterShard |
| 125 | + |
| 126 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 127 | + result, e := service.DescribeRedisInstanceShardsByFilter(ctx, paramMap) |
| 128 | + if e != nil { |
| 129 | + return retryError(e) |
| 130 | + } |
| 131 | + instanceShards = result |
| 132 | + return nil |
| 133 | + }) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + tmpList := make([]map[string]interface{}, 0, len(instanceShards)) |
| 139 | + if instanceShards != nil { |
| 140 | + for _, instanceClusterShard := range instanceShards { |
| 141 | + instanceClusterShardMap := map[string]interface{}{} |
| 142 | + |
| 143 | + if instanceClusterShard.ShardName != nil { |
| 144 | + instanceClusterShardMap["shard_name"] = instanceClusterShard.ShardName |
| 145 | + } |
| 146 | + |
| 147 | + if instanceClusterShard.ShardId != nil { |
| 148 | + instanceClusterShardMap["shard_id"] = instanceClusterShard.ShardId |
| 149 | + } |
| 150 | + |
| 151 | + if instanceClusterShard.Role != nil { |
| 152 | + instanceClusterShardMap["role"] = instanceClusterShard.Role |
| 153 | + } |
| 154 | + |
| 155 | + if instanceClusterShard.Keys != nil { |
| 156 | + instanceClusterShardMap["keys"] = instanceClusterShard.Keys |
| 157 | + } |
| 158 | + |
| 159 | + if instanceClusterShard.Slots != nil { |
| 160 | + instanceClusterShardMap["slots"] = instanceClusterShard.Slots |
| 161 | + } |
| 162 | + |
| 163 | + if instanceClusterShard.Storage != nil { |
| 164 | + instanceClusterShardMap["storage"] = instanceClusterShard.Storage |
| 165 | + } |
| 166 | + |
| 167 | + if instanceClusterShard.StorageSlope != nil { |
| 168 | + instanceClusterShardMap["storage_slope"] = instanceClusterShard.StorageSlope |
| 169 | + } |
| 170 | + |
| 171 | + if instanceClusterShard.Runid != nil { |
| 172 | + instanceClusterShardMap["runid"] = instanceClusterShard.Runid |
| 173 | + } |
| 174 | + |
| 175 | + if instanceClusterShard.Connected != nil { |
| 176 | + instanceClusterShardMap["connected"] = instanceClusterShard.Connected |
| 177 | + } |
| 178 | + |
| 179 | + tmpList = append(tmpList, instanceClusterShardMap) |
| 180 | + } |
| 181 | + |
| 182 | + _ = d.Set("instance_shards", tmpList) |
| 183 | + } |
| 184 | + |
| 185 | + d.SetId(helper.DataResourceIdsHash([]string{instanceId})) |
| 186 | + output, ok := d.GetOk("result_output_file") |
| 187 | + if ok && output.(string) != "" { |
| 188 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 189 | + return e |
| 190 | + } |
| 191 | + } |
| 192 | + return nil |
| 193 | +} |
0 commit comments