|
| 1 | +package cdwch |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + clickhouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwch/v20200915" |
| 11 | + |
| 12 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 13 | +) |
| 14 | + |
| 15 | +func DataSourceTencentCloudClickhouseInstanceNodes() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Read: dataSourceTencentCloudClickhouseInstanceNodesRead, |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "instance_id": { |
| 20 | + Required: true, |
| 21 | + Type: schema.TypeString, |
| 22 | + Description: "InstanceId.", |
| 23 | + }, |
| 24 | + |
| 25 | + "node_role": { |
| 26 | + Optional: true, |
| 27 | + Type: schema.TypeString, |
| 28 | + Description: "Cluster role type, default is `data` data node.", |
| 29 | + }, |
| 30 | + |
| 31 | + "display_policy": { |
| 32 | + Optional: true, |
| 33 | + Type: schema.TypeString, |
| 34 | + Description: "Display strategy, display all when All.", |
| 35 | + }, |
| 36 | + |
| 37 | + "force_all": { |
| 38 | + Optional: true, |
| 39 | + Type: schema.TypeBool, |
| 40 | + Description: "When true, returns all nodes, that is, the Limit is infinitely large.", |
| 41 | + }, |
| 42 | + |
| 43 | + "instance_nodes_list": { |
| 44 | + Computed: true, |
| 45 | + Type: schema.TypeList, |
| 46 | + Description: "Total number of instance nodes.", |
| 47 | + Elem: &schema.Resource{ |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "ip": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + Description: "IP Address.", |
| 53 | + }, |
| 54 | + "spec": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + Description: "Model, such as S1.", |
| 58 | + }, |
| 59 | + "core": { |
| 60 | + Type: schema.TypeInt, |
| 61 | + Computed: true, |
| 62 | + Description: "CPU cores.", |
| 63 | + }, |
| 64 | + "memory": { |
| 65 | + Type: schema.TypeInt, |
| 66 | + Computed: true, |
| 67 | + Description: "Memory size.", |
| 68 | + }, |
| 69 | + "disk_type": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + Description: "Disk type.", |
| 73 | + }, |
| 74 | + "disk_size": { |
| 75 | + Type: schema.TypeInt, |
| 76 | + Computed: true, |
| 77 | + Description: "Disk size.", |
| 78 | + }, |
| 79 | + "cluster": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Computed: true, |
| 82 | + Description: "Name of the clickhouse cluster to which it belongs.", |
| 83 | + }, |
| 84 | + "node_groups": { |
| 85 | + Type: schema.TypeList, |
| 86 | + Computed: true, |
| 87 | + Description: "Group information to which the node belongs.", |
| 88 | + Elem: &schema.Resource{ |
| 89 | + Schema: map[string]*schema.Schema{ |
| 90 | + "group_name": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + Description: "Group Name.", |
| 94 | + }, |
| 95 | + "shard_name": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + Description: "Fragmented variable name.", |
| 99 | + }, |
| 100 | + "replica_name": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Computed: true, |
| 103 | + Description: "Copy variable name.", |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + "rip": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Computed: true, |
| 111 | + Description: "VPC IP.", |
| 112 | + }, |
| 113 | + "is_ch_proxy": { |
| 114 | + Type: schema.TypeBool, |
| 115 | + Computed: true, |
| 116 | + Description: "When true, it indicates that the chproxy process has been deployed on the node.", |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + |
| 122 | + "result_output_file": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Optional: true, |
| 125 | + Description: "Used to save results.", |
| 126 | + }, |
| 127 | + }, |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func dataSourceTencentCloudClickhouseInstanceNodesRead(d *schema.ResourceData, meta interface{}) error { |
| 132 | + defer tccommon.LogElapsed("data_source.tencentcloud_clickhouse_instance_nodes.read")() |
| 133 | + defer tccommon.InconsistentCheck(d, meta)() |
| 134 | + |
| 135 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 136 | + |
| 137 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 138 | + |
| 139 | + paramMap := make(map[string]interface{}) |
| 140 | + if v, ok := d.GetOk("instance_id"); ok { |
| 141 | + paramMap["InstanceId"] = helper.String(v.(string)) |
| 142 | + } |
| 143 | + |
| 144 | + if v, ok := d.GetOk("node_role"); ok { |
| 145 | + paramMap["NodeRole"] = helper.String(v.(string)) |
| 146 | + } |
| 147 | + |
| 148 | + if v, ok := d.GetOk("display_policy"); ok { |
| 149 | + paramMap["DisplayPolicy"] = helper.String(v.(string)) |
| 150 | + } |
| 151 | + |
| 152 | + if v, ok := d.GetOkExists("force_all"); ok { |
| 153 | + paramMap["ForceAll"] = helper.Bool(v.(bool)) |
| 154 | + } |
| 155 | + |
| 156 | + service := CdwchService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 157 | + |
| 158 | + var instanceNodesList []*clickhouse.InstanceNode |
| 159 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 160 | + result, e := service.DescribeClickhouseInstanceNodesByFilter(ctx, paramMap) |
| 161 | + if e != nil { |
| 162 | + return tccommon.RetryError(e) |
| 163 | + } |
| 164 | + instanceNodesList = result |
| 165 | + return nil |
| 166 | + }) |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + tmpList := make([]map[string]interface{}, 0, len(instanceNodesList)) |
| 172 | + |
| 173 | + if instanceNodesList != nil { |
| 174 | + for _, instanceNode := range instanceNodesList { |
| 175 | + instanceNodeMap := map[string]interface{}{} |
| 176 | + |
| 177 | + if instanceNode.Ip != nil { |
| 178 | + instanceNodeMap["ip"] = instanceNode.Ip |
| 179 | + } |
| 180 | + |
| 181 | + if instanceNode.Spec != nil { |
| 182 | + instanceNodeMap["spec"] = instanceNode.Spec |
| 183 | + } |
| 184 | + |
| 185 | + if instanceNode.Core != nil { |
| 186 | + instanceNodeMap["core"] = instanceNode.Core |
| 187 | + } |
| 188 | + |
| 189 | + if instanceNode.Memory != nil { |
| 190 | + instanceNodeMap["memory"] = instanceNode.Memory |
| 191 | + } |
| 192 | + |
| 193 | + if instanceNode.DiskType != nil { |
| 194 | + instanceNodeMap["disk_type"] = instanceNode.DiskType |
| 195 | + } |
| 196 | + |
| 197 | + if instanceNode.DiskSize != nil { |
| 198 | + instanceNodeMap["disk_size"] = instanceNode.DiskSize |
| 199 | + } |
| 200 | + |
| 201 | + if instanceNode.Cluster != nil { |
| 202 | + instanceNodeMap["cluster"] = instanceNode.Cluster |
| 203 | + } |
| 204 | + |
| 205 | + if instanceNode.NodeGroups != nil { |
| 206 | + var nodeGroupsList []interface{} |
| 207 | + for _, nodeGroups := range instanceNode.NodeGroups { |
| 208 | + nodeGroupsMap := map[string]interface{}{} |
| 209 | + |
| 210 | + if nodeGroups.GroupName != nil { |
| 211 | + nodeGroupsMap["group_name"] = nodeGroups.GroupName |
| 212 | + } |
| 213 | + |
| 214 | + if nodeGroups.ShardName != nil { |
| 215 | + nodeGroupsMap["shard_name"] = nodeGroups.ShardName |
| 216 | + } |
| 217 | + |
| 218 | + if nodeGroups.ReplicaName != nil { |
| 219 | + nodeGroupsMap["replica_name"] = nodeGroups.ReplicaName |
| 220 | + } |
| 221 | + |
| 222 | + nodeGroupsList = append(nodeGroupsList, nodeGroupsMap) |
| 223 | + } |
| 224 | + |
| 225 | + instanceNodeMap["node_groups"] = nodeGroupsList |
| 226 | + } |
| 227 | + |
| 228 | + if instanceNode.Rip != nil { |
| 229 | + instanceNodeMap["rip"] = instanceNode.Rip |
| 230 | + } |
| 231 | + |
| 232 | + if instanceNode.IsCHProxy != nil { |
| 233 | + instanceNodeMap["is_ch_proxy"] = instanceNode.IsCHProxy |
| 234 | + } |
| 235 | + |
| 236 | + tmpList = append(tmpList, instanceNodeMap) |
| 237 | + } |
| 238 | + |
| 239 | + _ = d.Set("instance_nodes_list", tmpList) |
| 240 | + } |
| 241 | + |
| 242 | + d.SetId(helper.BuildToken()) |
| 243 | + |
| 244 | + output, ok := d.GetOk("result_output_file") |
| 245 | + if ok && output.(string) != "" { |
| 246 | + if e := tccommon.WriteToFile(output.(string), tmpList); e != nil { |
| 247 | + return e |
| 248 | + } |
| 249 | + } |
| 250 | + return nil |
| 251 | +} |
0 commit comments