|
| 1 | +/* |
| 2 | +Use this data source to query cvm instances modification. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cvm_instances_modification" "foo" { |
| 8 | + instance_ids = ["ins-xxxxxxx"] |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "log" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 19 | + cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" |
| 20 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 21 | +) |
| 22 | + |
| 23 | +func dataSourceTencentCloudCvmInstancesModification() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Read: dataSourceTencentCloudCvmInstancesModificationRead, |
| 26 | + |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "instance_ids": { |
| 29 | + Optional: true, |
| 30 | + Type: schema.TypeSet, |
| 31 | + Elem: &schema.Schema{ |
| 32 | + Type: schema.TypeString, |
| 33 | + }, |
| 34 | + Description: "One or more instance ID to be queried. It can be obtained from the InstanceId in the returned value of API DescribeInstances. The maximum number of instances in batch for each request is 20.", |
| 35 | + }, |
| 36 | + "filters": { |
| 37 | + Optional: true, |
| 38 | + Type: schema.TypeList, |
| 39 | + Description: "The upper limit of Filters for each request is 10 and the upper limit for Filter.Values is 2.", |
| 40 | + Elem: &schema.Resource{ |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "name": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + Description: "Fields to be filtered.", |
| 46 | + }, |
| 47 | + "values": { |
| 48 | + Type: schema.TypeSet, |
| 49 | + Elem: &schema.Schema{ |
| 50 | + Type: schema.TypeString, |
| 51 | + }, |
| 52 | + Required: true, |
| 53 | + Description: "Value of the field.", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + |
| 59 | + "result_output_file": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Optional: true, |
| 62 | + Description: "Used to save results.", |
| 63 | + }, |
| 64 | + |
| 65 | + "instance_type_config_status_list": { |
| 66 | + Computed: true, |
| 67 | + Type: schema.TypeList, |
| 68 | + Description: "The list of model configurations that can be adjusted by the instance.", |
| 69 | + Elem: &schema.Resource{ |
| 70 | + Schema: map[string]*schema.Schema{ |
| 71 | + "status": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "State description.", |
| 75 | + }, |
| 76 | + "message": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "Status description information.", |
| 80 | + }, |
| 81 | + "instance_type_config": { |
| 82 | + Type: schema.TypeList, |
| 83 | + Computed: true, |
| 84 | + Description: "Configuration information.", |
| 85 | + Elem: &schema.Resource{ |
| 86 | + Schema: map[string]*schema.Schema{ |
| 87 | + "zone": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "Availability zone.", |
| 91 | + }, |
| 92 | + "instance_type": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "Instance type.", |
| 96 | + }, |
| 97 | + "instance_family": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "Instance family.", |
| 101 | + }, |
| 102 | + "gpu": { |
| 103 | + Type: schema.TypeInt, |
| 104 | + Computed: true, |
| 105 | + Description: "The number of GPU kernels, in cores.", |
| 106 | + }, |
| 107 | + "cpu": { |
| 108 | + Type: schema.TypeInt, |
| 109 | + Computed: true, |
| 110 | + Description: "The number of CPU kernels, in cores.", |
| 111 | + }, |
| 112 | + "memory": { |
| 113 | + Type: schema.TypeInt, |
| 114 | + Computed: true, |
| 115 | + Description: "Memory capacity (in GB).", |
| 116 | + }, |
| 117 | + "fpga": { |
| 118 | + Type: schema.TypeInt, |
| 119 | + Computed: true, |
| 120 | + Description: "The number of FPGA kernels, in cores.", |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func dataSourceTencentCloudCvmInstancesModificationRead(d *schema.ResourceData, meta interface{}) error { |
| 133 | + defer logElapsed("data_source.tencentcloud_cvm_instances_modification.read")() |
| 134 | + logId := getLogId(contextNil) |
| 135 | + |
| 136 | + var ( |
| 137 | + request = cvm.NewDescribeInstancesModificationRequest() |
| 138 | + response = cvm.NewDescribeInstancesModificationResponse() |
| 139 | + ) |
| 140 | + if v, ok := d.GetOk("instance_ids"); ok { |
| 141 | + request.InstanceIds = helper.InterfacesStringsPoint(v.(*schema.Set).List()) |
| 142 | + } |
| 143 | + |
| 144 | + if v, ok := d.GetOk("filters"); ok { |
| 145 | + filters := make([]*cvm.Filter, 0) |
| 146 | + for _, item := range v.(*schema.Set).List() { |
| 147 | + filter := item.(map[string]interface{}) |
| 148 | + name := filter["name"].(string) |
| 149 | + filters = append(filters, &cvm.Filter{ |
| 150 | + Name: &name, |
| 151 | + Values: helper.StringsStringsPoint(filter["values"].([]string)), |
| 152 | + }) |
| 153 | + } |
| 154 | + request.Filters = filters |
| 155 | + } |
| 156 | + |
| 157 | + instanceTypeConfigStatusList := make([]map[string]interface{}, 0) |
| 158 | + |
| 159 | + var innerErr error |
| 160 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 161 | + response, innerErr = meta.(*TencentCloudClient).apiV3Conn.UseCvmClient().DescribeInstancesModification(request) |
| 162 | + if innerErr != nil { |
| 163 | + return retryError(innerErr) |
| 164 | + } |
| 165 | + return nil |
| 166 | + }) |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + ids := make([]string, 0) |
| 172 | + for _, instanceTypeConfigStatusSetItem := range response.Response.InstanceTypeConfigStatusSet { |
| 173 | + instanceTypeConfigStatus := make(map[string]interface{}) |
| 174 | + instanceTypeConfigStatus["status"] = instanceTypeConfigStatusSetItem.Status |
| 175 | + instanceTypeConfigStatus["message"] = instanceTypeConfigStatusSetItem.Message |
| 176 | + |
| 177 | + instanceTypeConfigMaps := make([]map[string]interface{}, 0) |
| 178 | + instanceTypeConfigMap := make(map[string]interface{}) |
| 179 | + instanceTypeConfig := instanceTypeConfigStatusSetItem.InstanceTypeConfig |
| 180 | + instanceTypeConfigMap["zone"] = instanceTypeConfig.Zone |
| 181 | + ids = append(ids, *instanceTypeConfig.InstanceType) |
| 182 | + instanceTypeConfigMap["instance_type"] = instanceTypeConfig.InstanceType |
| 183 | + instanceTypeConfigMap["instance_family"] = instanceTypeConfig.InstanceFamily |
| 184 | + instanceTypeConfigMap["gpu"] = instanceTypeConfig.GPU |
| 185 | + instanceTypeConfigMap["cpu"] = instanceTypeConfig.CPU |
| 186 | + instanceTypeConfigMap["memory"] = instanceTypeConfig.Memory |
| 187 | + instanceTypeConfigMap["fpga"] = instanceTypeConfig.FPGA |
| 188 | + instanceTypeConfigMaps = append(instanceTypeConfigMaps, instanceTypeConfigMap) |
| 189 | + instanceTypeConfigStatus["instance_type_config"] = instanceTypeConfigMaps |
| 190 | + |
| 191 | + instanceTypeConfigStatusList = append(instanceTypeConfigStatusList, instanceTypeConfigStatus) |
| 192 | + } |
| 193 | + |
| 194 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 195 | + err = d.Set("instance_type_config_status_list", instanceTypeConfigStatusList) |
| 196 | + if err != nil { |
| 197 | + log.Printf("[CRITAL]%s provider set instance list fail, reason:%s\n ", logId, err.Error()) |
| 198 | + return err |
| 199 | + } |
| 200 | + |
| 201 | + output, ok := d.GetOk("result_output_file") |
| 202 | + if ok && output.(string) != "" { |
| 203 | + if err := writeToFile(output.(string), instanceTypeConfigStatusList); err != nil { |
| 204 | + return err |
| 205 | + } |
| 206 | + } |
| 207 | + return nil |
| 208 | + |
| 209 | +} |
0 commit comments