|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of waf instance_qps_limit |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_waf_instance_qps_limit" "example" { |
| 8 | + instance_id = "waf_2kxtlbky00b3b4qz" |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 19 | + waf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125" |
| 20 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 21 | +) |
| 22 | + |
| 23 | +func dataSourceTencentCloudWafInstanceQpsLimit() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Read: dataSourceTencentCloudWafInstanceQpsLimitRead, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "instance_id": { |
| 28 | + Required: true, |
| 29 | + Type: schema.TypeString, |
| 30 | + Description: "Unique ID of Instance.", |
| 31 | + }, |
| 32 | + "type": { |
| 33 | + Optional: true, |
| 34 | + Type: schema.TypeString, |
| 35 | + Description: "Instance type.", |
| 36 | + }, |
| 37 | + "qps_data": { |
| 38 | + Computed: true, |
| 39 | + Type: schema.TypeList, |
| 40 | + Description: "Qps info.", |
| 41 | + Elem: &schema.Resource{ |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "elastic_billing_default": { |
| 44 | + Type: schema.TypeInt, |
| 45 | + Computed: true, |
| 46 | + Description: "Elastic qps default value.", |
| 47 | + }, |
| 48 | + "elastic_billing_min": { |
| 49 | + Type: schema.TypeInt, |
| 50 | + Computed: true, |
| 51 | + Description: "Minimum elastic qps.", |
| 52 | + }, |
| 53 | + "elastic_billing_max": { |
| 54 | + Type: schema.TypeInt, |
| 55 | + Computed: true, |
| 56 | + Description: "Maximum elastic qps.", |
| 57 | + }, |
| 58 | + "qps_extend_max": { |
| 59 | + Type: schema.TypeInt, |
| 60 | + Computed: true, |
| 61 | + Description: "Maximum qps of extend package.", |
| 62 | + }, |
| 63 | + "qps_extend_intl_max": { |
| 64 | + Type: schema.TypeInt, |
| 65 | + Computed: true, |
| 66 | + Description: "Maximum qps of extend package for overseas.", |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + "result_output_file": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Optional: true, |
| 74 | + Description: "Used to save results.", |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func dataSourceTencentCloudWafInstanceQpsLimitRead(d *schema.ResourceData, meta interface{}) error { |
| 81 | + defer logElapsed("data_source.tencentcloud_waf_instance_qps_limit.read")() |
| 82 | + defer inconsistentCheck(d, meta)() |
| 83 | + |
| 84 | + var ( |
| 85 | + logId = getLogId(contextNil) |
| 86 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 87 | + service = WafService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 88 | + qpsData *waf.QpsData |
| 89 | + instanceId string |
| 90 | + ) |
| 91 | + |
| 92 | + paramMap := make(map[string]interface{}) |
| 93 | + if v, ok := d.GetOk("instance_id"); ok { |
| 94 | + paramMap["InstanceId"] = helper.String(v.(string)) |
| 95 | + instanceId = v.(string) |
| 96 | + } |
| 97 | + |
| 98 | + if v, ok := d.GetOk("type"); ok { |
| 99 | + paramMap["Type"] = helper.String(v.(string)) |
| 100 | + } |
| 101 | + |
| 102 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 103 | + result, e := service.DescribeWafInstanceQpsLimitByFilter(ctx, paramMap) |
| 104 | + if e != nil { |
| 105 | + return retryError(e) |
| 106 | + } |
| 107 | + |
| 108 | + qpsData = result |
| 109 | + return nil |
| 110 | + }) |
| 111 | + |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + if qpsData != nil { |
| 117 | + tmqList := []interface{}{} |
| 118 | + qpsDataMap := map[string]interface{}{} |
| 119 | + |
| 120 | + if qpsData.ElasticBillingDefault != nil { |
| 121 | + qpsDataMap["elastic_billing_default"] = qpsData.ElasticBillingDefault |
| 122 | + } |
| 123 | + |
| 124 | + if qpsData.ElasticBillingMin != nil { |
| 125 | + qpsDataMap["elastic_billing_min"] = qpsData.ElasticBillingMin |
| 126 | + } |
| 127 | + |
| 128 | + if qpsData.ElasticBillingMax != nil { |
| 129 | + qpsDataMap["elastic_billing_max"] = qpsData.ElasticBillingMax |
| 130 | + } |
| 131 | + |
| 132 | + if qpsData.QPSExtendMax != nil { |
| 133 | + qpsDataMap["qps_extend_max"] = qpsData.QPSExtendMax |
| 134 | + } |
| 135 | + |
| 136 | + if qpsData.QPSExtendIntlMax != nil { |
| 137 | + qpsDataMap["qps_extend_intl_max"] = qpsData.QPSExtendIntlMax |
| 138 | + } |
| 139 | + |
| 140 | + tmqList = append(tmqList, qpsDataMap) |
| 141 | + _ = d.Set("qps_data", tmqList) |
| 142 | + } |
| 143 | + |
| 144 | + d.SetId(instanceId) |
| 145 | + output, ok := d.GetOk("result_output_file") |
| 146 | + if ok && output.(string) != "" { |
| 147 | + if e := writeToFile(output.(string), d); e != nil { |
| 148 | + return e |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
0 commit comments