|
| 1 | +package dlc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + dlcv20210125 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dlc/v20210125" |
| 9 | + |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceTencentCloudDlcEngineNodeSpecifications() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudDlcEngineNodeSpecificationsRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "data_engine_name": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Optional: true, |
| 21 | + Description: "Engine Name.", |
| 22 | + }, |
| 23 | + |
| 24 | + "driver_spec": { |
| 25 | + Type: schema.TypeList, |
| 26 | + Computed: true, |
| 27 | + Description: "Driver available specifications.", |
| 28 | + Elem: &schema.Resource{ |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + Description: "Specification name.", |
| 34 | + }, |
| 35 | + "cu": { |
| 36 | + Type: schema.TypeInt, |
| 37 | + Required: true, |
| 38 | + Description: "Current specification of CU number.", |
| 39 | + }, |
| 40 | + "cpu": { |
| 41 | + Type: schema.TypeInt, |
| 42 | + Required: true, |
| 43 | + Description: "Current CPU specifications.", |
| 44 | + }, |
| 45 | + "memory": { |
| 46 | + Type: schema.TypeInt, |
| 47 | + Required: true, |
| 48 | + Description: "The current memory size, in GB.", |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + |
| 54 | + "executor_spec": { |
| 55 | + Type: schema.TypeList, |
| 56 | + Computed: true, |
| 57 | + Description: "Available executor specifications.", |
| 58 | + Elem: &schema.Resource{ |
| 59 | + Schema: map[string]*schema.Schema{ |
| 60 | + "name": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Required: true, |
| 63 | + Description: "Specification name.", |
| 64 | + }, |
| 65 | + "cu": { |
| 66 | + Type: schema.TypeInt, |
| 67 | + Required: true, |
| 68 | + Description: "Current specification of CU number.", |
| 69 | + }, |
| 70 | + "cpu": { |
| 71 | + Type: schema.TypeInt, |
| 72 | + Required: true, |
| 73 | + Description: "Current CPU specifications.", |
| 74 | + }, |
| 75 | + "memory": { |
| 76 | + Type: schema.TypeInt, |
| 77 | + Required: true, |
| 78 | + Description: "The current memory size, in GB.", |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + |
| 84 | + "result_output_file": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Optional: true, |
| 87 | + Description: "Used to save results.", |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func dataSourceTencentCloudDlcEngineNodeSpecificationsRead(d *schema.ResourceData, meta interface{}) error { |
| 94 | + defer tccommon.LogElapsed("data_source.tencentcloud_dlc_engine_node_specifications.read")() |
| 95 | + defer tccommon.InconsistentCheck(d, meta)() |
| 96 | + |
| 97 | + var ( |
| 98 | + logId = tccommon.GetLogId(nil) |
| 99 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 100 | + service = DlcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 101 | + ) |
| 102 | + |
| 103 | + paramMap := make(map[string]interface{}) |
| 104 | + if v, ok := d.GetOk("data_engine_name"); ok { |
| 105 | + paramMap["DataEngineName"] = helper.String(v.(string)) |
| 106 | + } |
| 107 | + |
| 108 | + var respData *dlcv20210125.DescribeEngineNodeSpecResponseParams |
| 109 | + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 110 | + result, e := service.DescribeDlcEngineNodeSpecificationsByFilter(ctx, paramMap) |
| 111 | + if e != nil { |
| 112 | + return tccommon.RetryError(e) |
| 113 | + } |
| 114 | + |
| 115 | + respData = result |
| 116 | + return nil |
| 117 | + }) |
| 118 | + |
| 119 | + if reqErr != nil { |
| 120 | + return reqErr |
| 121 | + } |
| 122 | + |
| 123 | + driverSpecList := make([]map[string]interface{}, 0, len(respData.DriverSpec)) |
| 124 | + if respData.DriverSpec != nil { |
| 125 | + for _, driverSpec := range respData.DriverSpec { |
| 126 | + driverSpecMap := map[string]interface{}{} |
| 127 | + if driverSpec.Name != nil { |
| 128 | + driverSpecMap["name"] = driverSpec.Name |
| 129 | + } |
| 130 | + |
| 131 | + if driverSpec.Cu != nil { |
| 132 | + driverSpecMap["cu"] = driverSpec.Cu |
| 133 | + } |
| 134 | + |
| 135 | + if driverSpec.Cpu != nil { |
| 136 | + driverSpecMap["cpu"] = driverSpec.Cpu |
| 137 | + } |
| 138 | + |
| 139 | + if driverSpec.Memory != nil { |
| 140 | + driverSpecMap["memory"] = driverSpec.Memory |
| 141 | + } |
| 142 | + |
| 143 | + driverSpecList = append(driverSpecList, driverSpecMap) |
| 144 | + } |
| 145 | + |
| 146 | + _ = d.Set("driver_spec", driverSpecList) |
| 147 | + } |
| 148 | + |
| 149 | + executorSpecList := make([]map[string]interface{}, 0, len(respData.ExecutorSpec)) |
| 150 | + if respData.ExecutorSpec != nil { |
| 151 | + for _, executorSpec := range respData.ExecutorSpec { |
| 152 | + executorSpecMap := map[string]interface{}{} |
| 153 | + if executorSpec.Name != nil { |
| 154 | + executorSpecMap["name"] = executorSpec.Name |
| 155 | + } |
| 156 | + |
| 157 | + if executorSpec.Cu != nil { |
| 158 | + executorSpecMap["cu"] = executorSpec.Cu |
| 159 | + } |
| 160 | + |
| 161 | + if executorSpec.Cpu != nil { |
| 162 | + executorSpecMap["cpu"] = executorSpec.Cpu |
| 163 | + } |
| 164 | + |
| 165 | + if executorSpec.Memory != nil { |
| 166 | + executorSpecMap["memory"] = executorSpec.Memory |
| 167 | + } |
| 168 | + |
| 169 | + executorSpecList = append(executorSpecList, executorSpecMap) |
| 170 | + } |
| 171 | + |
| 172 | + _ = d.Set("executor_spec", executorSpecList) |
| 173 | + } |
| 174 | + |
| 175 | + d.SetId(helper.BuildToken()) |
| 176 | + output, ok := d.GetOk("result_output_file") |
| 177 | + if ok && output.(string) != "" { |
| 178 | + if e := tccommon.WriteToFile(output.(string), d); e != nil { |
| 179 | + return e |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return nil |
| 184 | +} |
0 commit comments