|
| 1 | +/* |
| 2 | +Use this data source to query purchasable specification configuration for each availability zone in this specific region. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_sqlserver_zone_config" "mysqlserver" { |
| 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 dataSourceTencentSqlserverZoneConfig() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + Read: dataSourceTencentSqlserverZoneConfigRead, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "result_output_file": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + Description: "Used to store results.", |
| 29 | + }, |
| 30 | + "zone_list": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Computed: true, |
| 33 | + Description: "A list of availability zones. Each element contains the following attributes:", |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "availability_zone": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + Description: "Alphabet ID of availability zone.", |
| 40 | + }, |
| 41 | + "zone_id": { |
| 42 | + Type: schema.TypeInt, |
| 43 | + Computed: true, |
| 44 | + Description: "Number ID of availability zone.", |
| 45 | + }, |
| 46 | + "specinfo_list": { |
| 47 | + Type: schema.TypeList, |
| 48 | + Computed: true, |
| 49 | + Description: "A list of specinfo configurations for the specific availability zone. Each element contains the following attributes:", |
| 50 | + Elem: &schema.Resource{ |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "spec_id": { |
| 53 | + Type: schema.TypeInt, |
| 54 | + Computed: true, |
| 55 | + Description: "Instance specification ID.", |
| 56 | + }, |
| 57 | + "machine_type": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "Model ID.", |
| 61 | + }, |
| 62 | + "db_version": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "Database version information. Valid values: `2008R2 (SQL Server 2008 Enterprise)`, `2012SP3 (SQL Server 2012 Enterprise)`, `2016SP1 (SQL Server 2016 Enterprise)`, `201602 (SQL Server 2016 Standard)`, `2017 (SQL Server 2017 Enterprise)`.", |
| 66 | + }, |
| 67 | + "db_version_name": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: "Version name corresponding to the `db_version` field.", |
| 71 | + }, |
| 72 | + "memory": { |
| 73 | + Type: schema.TypeInt, |
| 74 | + Computed: true, |
| 75 | + Description: "Memory size in GB.", |
| 76 | + }, |
| 77 | + "cpu": { |
| 78 | + Type: schema.TypeInt, |
| 79 | + Computed: true, |
| 80 | + Description: "Number of CPU cores.", |
| 81 | + }, |
| 82 | + "min_storage_size": { |
| 83 | + Type: schema.TypeInt, |
| 84 | + Computed: true, |
| 85 | + Description: "Minimum disk size under this specification in GB.", |
| 86 | + }, |
| 87 | + "max_storage_size": { |
| 88 | + Type: schema.TypeInt, |
| 89 | + Computed: true, |
| 90 | + Description: "Maximum disk size under this specification in GB.", |
| 91 | + }, |
| 92 | + "qps": { |
| 93 | + Type: schema.TypeInt, |
| 94 | + Computed: true, |
| 95 | + Description: "QPS of this specification.", |
| 96 | + }, |
| 97 | + "charge_type": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "Billing mode under this specification. Valid values are `POSTPAID_BY_HOUR`, `PREPAID` and `ALL` which means both POSTPAID_BY_HOUR and PREPAID.", |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func dataSourceTencentSqlserverZoneConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 113 | + defer logElapsed("data_source.tencent_sqlserver_zone_config.read")() |
| 114 | + |
| 115 | + logId := getLogId(contextNil) |
| 116 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 117 | + |
| 118 | + sqlserverService := SqlserverService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 119 | + |
| 120 | + // get zoneinfo |
| 121 | + zoneInfoList, err := sqlserverService.DescribeZones(ctx) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("api[DescribeZones]fail, return %s", err.Error()) |
| 124 | + } |
| 125 | + zoneSet := make(map[string]map[string]interface{}) |
| 126 | + for _, zoneInfo := range zoneInfoList { |
| 127 | + zoneSetInfo := make(map[string]interface{}, 1) |
| 128 | + zoneSetInfo["id"] = zoneInfo.ZoneId |
| 129 | + zoneSet[*zoneInfo.Zone] = zoneSetInfo |
| 130 | + } |
| 131 | + |
| 132 | + var zoneList []interface{} |
| 133 | + for k, v := range zoneSet { |
| 134 | + var zoneListItem = make(map[string]interface{}) |
| 135 | + zoneListItem["availability_zone"] = k |
| 136 | + zoneListItem["zone_id"] = v["id"] |
| 137 | + |
| 138 | + // get specinfo for each zone |
| 139 | + specinfoList, err := sqlserverService.DescribeProductConfig(ctx, k) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("api[DescribeProductConfig]fail, return %s", err.Error()) |
| 142 | + } |
| 143 | + var specinfoConfigs []interface{} |
| 144 | + for _, specinfoItem := range specinfoList { |
| 145 | + var specinfoConfig = make(map[string]interface{}) |
| 146 | + specinfoConfig["spec_id"] = specinfoItem.SpecId |
| 147 | + specinfoConfig["machine_type"] = specinfoItem.MachineType |
| 148 | + specinfoConfig["db_version"] = specinfoItem.Version |
| 149 | + specinfoConfig["db_version_name"] = specinfoItem.VersionName |
| 150 | + specinfoConfig["memory"] = specinfoItem.Memory |
| 151 | + specinfoConfig["cpu"] = specinfoItem.CPU |
| 152 | + specinfoConfig["min_storage_size"] = specinfoItem.MinStorage |
| 153 | + specinfoConfig["max_storage_size"] = specinfoItem.MaxStorage |
| 154 | + specinfoConfig["qps"] = specinfoItem.QPS |
| 155 | + specinfoConfig["charge_type"] = SQLSERVER_CHARGE_TYPE_NAME[*specinfoItem.PayModeStatus] |
| 156 | + |
| 157 | + specinfoConfigs = append(specinfoConfigs, specinfoConfig) |
| 158 | + } |
| 159 | + zoneListItem["specinfo_list"] = specinfoConfigs |
| 160 | + zoneList = append(zoneList, zoneListItem) |
| 161 | + } |
| 162 | + |
| 163 | + // set zone_list |
| 164 | + if err := d.Set("zone_list", zoneList); err != nil { |
| 165 | + return fmt.Errorf("[CRITAL]%s provider set zone_list fail, reason:%s\n ", logId, err.Error()) |
| 166 | + } |
| 167 | + |
| 168 | + d.SetId("zone_config") |
| 169 | + |
| 170 | + if output, ok := d.GetOk("result_output_file"); ok && output.(string) != "" { |
| 171 | + if err := writeToFile(output.(string), zoneList); err != nil { |
| 172 | + log.Printf("[CRITAL]%s output file[%s] fail, reason[%s]\n", |
| 173 | + logId, output.(string), err.Error()) |
| 174 | + } |
| 175 | + |
| 176 | + } |
| 177 | + return nil |
| 178 | +} |
0 commit comments