|
| 1 | +package cdc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + cdc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdc/v20201214" |
| 8 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 9 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 10 | +) |
| 11 | + |
| 12 | +func DataSourceTencentCloudCdcDedicatedClusterHosts() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: DataSourceTencentCloudCdcDedicatedClusterHostsRead, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "dedicated_cluster_id": { |
| 17 | + Required: true, |
| 18 | + Type: schema.TypeString, |
| 19 | + Description: "Dedicated Cluster ID.", |
| 20 | + }, |
| 21 | + // computed |
| 22 | + "host_info_set": { |
| 23 | + Computed: true, |
| 24 | + Type: schema.TypeList, |
| 25 | + Description: "Dedicated Cluster Host Info.", |
| 26 | + Elem: &schema.Resource{ |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "host_ip": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Computed: true, |
| 31 | + Description: "Dedicated Cluster Host Ip (Deprecated).", |
| 32 | + }, |
| 33 | + "service_type": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + Description: "Dedicated Cluster Service Type.", |
| 37 | + }, |
| 38 | + "host_status": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + Description: "Dedicated Cluster Host Status.", |
| 42 | + }, |
| 43 | + "host_type": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + Description: "Dedicated Cluster Host Type.", |
| 47 | + }, |
| 48 | + "cpu_available": { |
| 49 | + Type: schema.TypeInt, |
| 50 | + Computed: true, |
| 51 | + Description: "Dedicated Cluster Host CPU Available Count.", |
| 52 | + }, |
| 53 | + "cpu_total": { |
| 54 | + Type: schema.TypeInt, |
| 55 | + Computed: true, |
| 56 | + Description: "Dedicated Cluster Host CPU Total Count.", |
| 57 | + }, |
| 58 | + "mem_available": { |
| 59 | + Type: schema.TypeInt, |
| 60 | + Computed: true, |
| 61 | + Description: "Dedicated Cluster Host Memory Available Count (GB).", |
| 62 | + }, |
| 63 | + "mem_total": { |
| 64 | + Type: schema.TypeInt, |
| 65 | + Computed: true, |
| 66 | + Description: "Dedicated Cluster Host Memory Total Count (GB).", |
| 67 | + }, |
| 68 | + "run_time": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Dedicated Cluster Host Run Time.", |
| 72 | + }, |
| 73 | + "expire_time": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "Dedicated Cluster Host Expire Time.", |
| 77 | + }, |
| 78 | + "host_id": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: "Dedicated Cluster Host ID.", |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + "result_output_file": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Optional: true, |
| 89 | + Description: "Used to save results.", |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func DataSourceTencentCloudCdcDedicatedClusterHostsRead(d *schema.ResourceData, meta interface{}) error { |
| 96 | + defer tccommon.LogElapsed("data_source.tencentcloud_cdc_dedicated_cluster_hosts.read")() |
| 97 | + defer tccommon.InconsistentCheck(d, meta)() |
| 98 | + |
| 99 | + var ( |
| 100 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 101 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 102 | + service = CdcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 103 | + hostInfoSet []*cdc.HostInfo |
| 104 | + ) |
| 105 | + |
| 106 | + paramMap := make(map[string]interface{}) |
| 107 | + if v, ok := d.GetOk("dedicated_cluster_id"); ok { |
| 108 | + paramMap["DedicatedClusterId"] = helper.String(v.(string)) |
| 109 | + } |
| 110 | + |
| 111 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 112 | + result, e := service.DescribeCdcHostByFilter(ctx, paramMap) |
| 113 | + if e != nil { |
| 114 | + return tccommon.RetryError(e) |
| 115 | + } |
| 116 | + |
| 117 | + hostInfoSet = result |
| 118 | + return nil |
| 119 | + }) |
| 120 | + |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + ids := make([]string, 0, len(hostInfoSet)) |
| 126 | + tmpList := make([]map[string]interface{}, 0, len(hostInfoSet)) |
| 127 | + |
| 128 | + if hostInfoSet != nil { |
| 129 | + for _, hostInfo := range hostInfoSet { |
| 130 | + hostInfoMap := map[string]interface{}{} |
| 131 | + |
| 132 | + if hostInfo.HostIp != nil { |
| 133 | + hostInfoMap["host_ip"] = hostInfo.HostIp |
| 134 | + } |
| 135 | + |
| 136 | + if hostInfo.ServiceType != nil { |
| 137 | + hostInfoMap["service_type"] = hostInfo.ServiceType |
| 138 | + } |
| 139 | + |
| 140 | + if hostInfo.HostStatus != nil { |
| 141 | + hostInfoMap["host_status"] = hostInfo.HostStatus |
| 142 | + } |
| 143 | + |
| 144 | + if hostInfo.HostType != nil { |
| 145 | + hostInfoMap["host_type"] = hostInfo.HostType |
| 146 | + } |
| 147 | + |
| 148 | + if hostInfo.CpuAvailable != nil { |
| 149 | + hostInfoMap["cpu_available"] = hostInfo.CpuAvailable |
| 150 | + } |
| 151 | + |
| 152 | + if hostInfo.CpuTotal != nil { |
| 153 | + hostInfoMap["cpu_total"] = hostInfo.CpuTotal |
| 154 | + } |
| 155 | + |
| 156 | + if hostInfo.MemAvailable != nil { |
| 157 | + hostInfoMap["mem_available"] = hostInfo.MemAvailable |
| 158 | + } |
| 159 | + |
| 160 | + if hostInfo.MemTotal != nil { |
| 161 | + hostInfoMap["mem_total"] = hostInfo.MemTotal |
| 162 | + } |
| 163 | + |
| 164 | + if hostInfo.RunTime != nil { |
| 165 | + hostInfoMap["run_time"] = hostInfo.RunTime |
| 166 | + } |
| 167 | + |
| 168 | + if hostInfo.ExpireTime != nil { |
| 169 | + hostInfoMap["expire_time"] = hostInfo.ExpireTime |
| 170 | + } |
| 171 | + |
| 172 | + if hostInfo.HostId != nil { |
| 173 | + hostInfoMap["host_id"] = hostInfo.HostId |
| 174 | + } |
| 175 | + |
| 176 | + ids = append(ids) |
| 177 | + tmpList = append(tmpList, hostInfoMap) |
| 178 | + } |
| 179 | + |
| 180 | + _ = d.Set("host_info_set", tmpList) |
| 181 | + } |
| 182 | + |
| 183 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 184 | + output, ok := d.GetOk("result_output_file") |
| 185 | + if ok && output.(string) != "" { |
| 186 | + if e := tccommon.WriteToFile(output.(string), tmpList); e != nil { |
| 187 | + return e |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return nil |
| 192 | +} |
0 commit comments