|
| 1 | +/* |
| 2 | +Use this data source to query which instance types of Redis are available in a specific region. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cynosdb_zone_config" "foo" { |
| 8 | +} |
| 9 | +``` |
| 10 | +*/ |
| 11 | +package tencentcloud |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "fmt" |
| 16 | + "log" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 19 | +) |
| 20 | + |
| 21 | +func dataSourceTencentCynosdbZoneConfig() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + Read: dataSourceTencentCynosdbZoneConfigRead, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "result_output_file": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + Description: "Used to save results.", |
| 29 | + }, |
| 30 | + |
| 31 | + // Computed values |
| 32 | + "list": { |
| 33 | + Type: schema.TypeList, |
| 34 | + Description: "A list of zone. Each element contains the following attributes:", |
| 35 | + Computed: true, |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "cpu": { |
| 39 | + Type: schema.TypeInt, |
| 40 | + Computed: true, |
| 41 | + Description: "Instance CPU, unit: core.", |
| 42 | + }, |
| 43 | + "memory": { |
| 44 | + Type: schema.TypeInt, |
| 45 | + Computed: true, |
| 46 | + Description: "Instance memory, unit: GB.", |
| 47 | + }, |
| 48 | + "max_storage_size": { |
| 49 | + Type: schema.TypeInt, |
| 50 | + Computed: true, |
| 51 | + Description: "The maximum available storage for the instance, unit GB.", |
| 52 | + }, |
| 53 | + "min_storage_size": { |
| 54 | + Type: schema.TypeInt, |
| 55 | + Computed: true, |
| 56 | + Description: "Minimum available storage of the instance, unit: GB.", |
| 57 | + }, |
| 58 | + "machine_type": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "Machine type.", |
| 62 | + }, |
| 63 | + "max_io_bandwidth": { |
| 64 | + Type: schema.TypeInt, |
| 65 | + Computed: true, |
| 66 | + Description: "Max io bandwidth.", |
| 67 | + }, |
| 68 | + "zone_stock_infos": { |
| 69 | + Type: schema.TypeList, |
| 70 | + Computed: true, |
| 71 | + Description: "Regional inventory information.", |
| 72 | + Elem: &schema.Resource{ |
| 73 | + Schema: map[string]*schema.Schema{ |
| 74 | + "zone": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: "Availability zone.", |
| 78 | + }, |
| 79 | + "has_stock": { |
| 80 | + Type: schema.TypeBool, |
| 81 | + Computed: true, |
| 82 | + Description: "Has stock.", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func dataSourceTencentCynosdbZoneConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 95 | + defer logElapsed("data_source.tencentcloud_cynosdb_zone_config.read")() |
| 96 | + |
| 97 | + logId := getLogId(contextNil) |
| 98 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 99 | + |
| 100 | + service := CynosdbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 101 | + region := meta.(*TencentCloudClient).apiV3Conn.Region |
| 102 | + |
| 103 | + instanceSpecSet, err := service.DescribeRedisZoneConfig(ctx) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("api[DescribeRedisZoneConfig]fail, return %s", err.Error()) |
| 106 | + } |
| 107 | + |
| 108 | + result := make([]map[string]interface{}, 0) |
| 109 | + |
| 110 | + for _, instanceSpec := range instanceSpecSet { |
| 111 | + resultItem := make(map[string]interface{}) |
| 112 | + resultItem["cpu"] = *instanceSpec.Cpu |
| 113 | + resultItem["memory"] = *instanceSpec.Memory |
| 114 | + resultItem["max_storage_size"] = *instanceSpec.MaxStorageSize |
| 115 | + resultItem["min_storage_size"] = *instanceSpec.MinStorageSize |
| 116 | + resultItem["machine_type"] = *instanceSpec.MachineType |
| 117 | + resultItem["max_io_bandwidth"] = *instanceSpec.MaxIoBandWidth |
| 118 | + zoneStockInfos := make([]map[string]interface{}, 0) |
| 119 | + for _, zoneStockInfoItem := range instanceSpec.ZoneStockInfos { |
| 120 | + zoneStockInfo := make(map[string]interface{}) |
| 121 | + zoneStockInfo["zone"] = *zoneStockInfoItem.Zone |
| 122 | + zoneStockInfo["has_stock"] = *zoneStockInfoItem.HasStock |
| 123 | + |
| 124 | + zoneStockInfos = append(zoneStockInfos, zoneStockInfo) |
| 125 | + } |
| 126 | + resultItem["zone_stock_infos"] = zoneStockInfos |
| 127 | + result = append(result, resultItem) |
| 128 | + } |
| 129 | + |
| 130 | + id := "cynosdb_zoneconfig_" + region |
| 131 | + d.SetId(id) |
| 132 | + d.Set("list", result) |
| 133 | + |
| 134 | + if output, ok := d.GetOk("result_output_file"); ok && output.(string) != "" { |
| 135 | + |
| 136 | + if err := writeToFile(output.(string), result); err != nil { |
| 137 | + log.Printf("[CRITAL]%s output file[%s] fail, reason[%s]\n", |
| 138 | + logId, output.(string), err.Error()) |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
0 commit comments