|
| 1 | +/* |
| 2 | +Use this data source to query zone available plans. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_teo_zone_available_plans" "available_plans" {} |
| 8 | +``` |
| 9 | +*/ |
| 10 | +package tencentcloud |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "log" |
| 15 | + |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 18 | + teo "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220106" |
| 19 | +) |
| 20 | + |
| 21 | +func dataSourceTencentCloudTeoZoneAvailablePlans() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + Read: dataSourceTencentCloudTeoZoneAvailablePlansRead, |
| 24 | + |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "plan_info_list": { |
| 27 | + Type: schema.TypeList, |
| 28 | + Computed: true, |
| 29 | + Description: "Available plans for a zone.", |
| 30 | + Elem: &schema.Resource{ |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "plan_type": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + Description: "Plan type.", |
| 36 | + }, |
| 37 | + "currency": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + Description: "Currency type. Valid values: `CNY`, `USD`.", |
| 41 | + }, |
| 42 | + "flux": { |
| 43 | + Type: schema.TypeInt, |
| 44 | + Computed: true, |
| 45 | + Description: "The number of fluxes included in the zone plan. Unit: Byte.", |
| 46 | + }, |
| 47 | + "frequency": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "Billing cycle. Valid values: `y`: Billed by the year; `m`: Billed by the month; `h`: Billed by the hour; `M`: Billed by the minute; `s`: Billed by the second.", |
| 51 | + }, |
| 52 | + "price": { |
| 53 | + Type: schema.TypeInt, |
| 54 | + Computed: true, |
| 55 | + Description: "Price of the plan. Unit: cent.", |
| 56 | + }, |
| 57 | + "request": { |
| 58 | + Type: schema.TypeInt, |
| 59 | + Computed: true, |
| 60 | + Description: "The number of requests included in the zone plan.", |
| 61 | + }, |
| 62 | + "site_number": { |
| 63 | + Type: schema.TypeInt, |
| 64 | + Computed: true, |
| 65 | + Description: "The number of zones this zone plan can bind.", |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + "result_output_file": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Optional: true, |
| 73 | + Description: "Used for save results.", |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func dataSourceTencentCloudTeoZoneAvailablePlansRead(d *schema.ResourceData, meta interface{}) error { |
| 80 | + defer logElapsed("data_source.tencentcloud_teo_zone_available_plans.read")() |
| 81 | + |
| 82 | + var ( |
| 83 | + logId = getLogId(contextNil) |
| 84 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 85 | + service = TeoService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 86 | + availablePlans *teo.DescribeAvailablePlansResponseParams |
| 87 | + err error |
| 88 | + ) |
| 89 | + |
| 90 | + var outErr, inErr error |
| 91 | + availablePlans, outErr = service.DescribeAvailablePlans(ctx) |
| 92 | + if outErr != nil { |
| 93 | + outErr = resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 94 | + availablePlans, inErr = service.DescribeAvailablePlans(ctx) |
| 95 | + if inErr != nil { |
| 96 | + return retryError(inErr) |
| 97 | + } |
| 98 | + return nil |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + planInfos := availablePlans.PlanInfoList |
| 103 | + planInfoList := make([]map[string]interface{}, 0, len(planInfos)) |
| 104 | + for _, v := range planInfos { |
| 105 | + planInfo := map[string]interface{}{ |
| 106 | + "plan_type": v.PlanType, |
| 107 | + "currency": v.Currency, |
| 108 | + "flux": v.Flux, |
| 109 | + "frequency": v.Frequency, |
| 110 | + "price": v.Price, |
| 111 | + "request": v.Request, |
| 112 | + "site_number": v.SiteNumber, |
| 113 | + } |
| 114 | + planInfoList = append(planInfoList, planInfo) |
| 115 | + } |
| 116 | + if err = d.Set("plan_info_list", planInfoList); err != nil { |
| 117 | + log.Printf("[CRITAL]%s provider set list fail, reason:%s", logId, err.Error()) |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + d.SetId("zone_available_plans") |
| 122 | + |
| 123 | + output, ok := d.GetOk("result_output_file") |
| 124 | + if ok && output.(string) != "" { |
| 125 | + if e := writeToFile(output.(string), planInfoList); e != nil { |
| 126 | + return e |
| 127 | + } |
| 128 | + } |
| 129 | + return nil |
| 130 | +} |
0 commit comments