|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of tse gateway_canary_rules |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_tse_gateway_canary_rules" "gateway_canary_rules" { |
| 8 | + gateway_id = "gateway-xxxxxx" |
| 9 | + service_id = "451a9920-e67a-4519-af41-fccac0e72005" |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 20 | + tse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse/v20201207" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudTseGatewayCanaryRules() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudTseGatewayCanaryRulesRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "gateway_id": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "gateway ID.", |
| 32 | + }, |
| 33 | + |
| 34 | + "service_id": { |
| 35 | + Required: true, |
| 36 | + Type: schema.TypeString, |
| 37 | + Description: "service ID.", |
| 38 | + }, |
| 39 | + |
| 40 | + "result": { |
| 41 | + Computed: true, |
| 42 | + Type: schema.TypeList, |
| 43 | + Description: "canary rule configuration.", |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "canary_rule_list": { |
| 47 | + Type: schema.TypeList, |
| 48 | + Computed: true, |
| 49 | + Description: "canary rule list.", |
| 50 | + Elem: &schema.Resource{ |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "priority": { |
| 53 | + Type: schema.TypeInt, |
| 54 | + Computed: true, |
| 55 | + Description: "priority. The value ranges from 0 to 100; the larger the value, the higher the priority; the priority cannot be repeated between different rules.", |
| 56 | + }, |
| 57 | + "enabled": { |
| 58 | + Type: schema.TypeBool, |
| 59 | + Computed: true, |
| 60 | + Description: "the status of canary rule.", |
| 61 | + }, |
| 62 | + "condition_list": { |
| 63 | + Type: schema.TypeList, |
| 64 | + Computed: true, |
| 65 | + Description: "parameter matching condition list.", |
| 66 | + Elem: &schema.Resource{ |
| 67 | + Schema: map[string]*schema.Schema{ |
| 68 | + "type": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "type.Reference value:- path- method- query- header- cookie- body- system.", |
| 72 | + }, |
| 73 | + "key": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "parameter name.", |
| 77 | + }, |
| 78 | + "operator": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: "operator.Reference value:`le`, `eq`, `lt`, `ne`, `ge`, `gt`, `regex`, `exists`, `in`, `not in`, `prefix`, `exact`, `regex`.", |
| 82 | + }, |
| 83 | + "value": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + Description: "parameter value.", |
| 87 | + }, |
| 88 | + "delimiter": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + Description: "delimiter. valid when operator is in or not in, reference value:`,`, `;`,`\\n`.", |
| 92 | + }, |
| 93 | + "global_config_id": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + Description: "global configuration ID.", |
| 97 | + }, |
| 98 | + "global_config_name": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Computed: true, |
| 101 | + Description: "global configuration name.", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + "balanced_service_list": { |
| 107 | + Type: schema.TypeList, |
| 108 | + Computed: true, |
| 109 | + Description: "service weight configuration.", |
| 110 | + Elem: &schema.Resource{ |
| 111 | + Schema: map[string]*schema.Schema{ |
| 112 | + "service_id": { |
| 113 | + Type: schema.TypeString, |
| 114 | + Computed: true, |
| 115 | + Description: "service ID.", |
| 116 | + }, |
| 117 | + "service_name": { |
| 118 | + Type: schema.TypeString, |
| 119 | + Computed: true, |
| 120 | + Description: "service name.", |
| 121 | + }, |
| 122 | + "upstream_name": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Computed: true, |
| 125 | + Description: "upstream name.", |
| 126 | + }, |
| 127 | + "percent": { |
| 128 | + Type: schema.TypeFloat, |
| 129 | + Computed: true, |
| 130 | + Description: "percent, 10 is 10%, valid values: 0 to 100.", |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + "service_id": { |
| 136 | + Type: schema.TypeString, |
| 137 | + Computed: true, |
| 138 | + Description: "service ID.", |
| 139 | + }, |
| 140 | + "service_name": { |
| 141 | + Type: schema.TypeString, |
| 142 | + Computed: true, |
| 143 | + Description: "service name.", |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + "total_count": { |
| 149 | + Type: schema.TypeInt, |
| 150 | + Computed: true, |
| 151 | + Description: "total count.", |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + |
| 157 | + "result_output_file": { |
| 158 | + Type: schema.TypeString, |
| 159 | + Optional: true, |
| 160 | + Description: "Used to save results.", |
| 161 | + }, |
| 162 | + }, |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func dataSourceTencentCloudTseGatewayCanaryRulesRead(d *schema.ResourceData, meta interface{}) error { |
| 167 | + defer logElapsed("data_source.tencentcloud_tse_gateway_canary_rules.read")() |
| 168 | + defer inconsistentCheck(d, meta)() |
| 169 | + |
| 170 | + logId := getLogId(contextNil) |
| 171 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 172 | + |
| 173 | + var gatewayId string |
| 174 | + paramMap := make(map[string]interface{}) |
| 175 | + if v, ok := d.GetOk("gateway_id"); ok { |
| 176 | + gatewayId = v.(string) |
| 177 | + paramMap["GatewayId"] = helper.String(v.(string)) |
| 178 | + } |
| 179 | + |
| 180 | + if v, ok := d.GetOk("service_id"); ok { |
| 181 | + paramMap["ServiceId"] = helper.String(v.(string)) |
| 182 | + } |
| 183 | + |
| 184 | + service := TseService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 185 | + |
| 186 | + var result *tse.CloudAPIGatewayCanaryRuleList |
| 187 | + |
| 188 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 189 | + response, e := service.DescribeTseGatewayCanaryRulesByFilter(ctx, paramMap) |
| 190 | + if e != nil { |
| 191 | + return retryError(e) |
| 192 | + } |
| 193 | + result = response |
| 194 | + return nil |
| 195 | + }) |
| 196 | + if err != nil { |
| 197 | + return err |
| 198 | + } |
| 199 | + |
| 200 | + cloudAPIGatewayCanaryRuleListMap := map[string]interface{}{} |
| 201 | + if result != nil { |
| 202 | + if result.CanaryRuleList != nil { |
| 203 | + canaryRuleListList := []interface{}{} |
| 204 | + for _, canaryRuleList := range result.CanaryRuleList { |
| 205 | + canaryRuleListMap := map[string]interface{}{} |
| 206 | + |
| 207 | + if canaryRuleList.Priority != nil { |
| 208 | + canaryRuleListMap["priority"] = canaryRuleList.Priority |
| 209 | + } |
| 210 | + |
| 211 | + if canaryRuleList.Enabled != nil { |
| 212 | + canaryRuleListMap["enabled"] = canaryRuleList.Enabled |
| 213 | + } |
| 214 | + |
| 215 | + if canaryRuleList.ConditionList != nil { |
| 216 | + conditionListList := []interface{}{} |
| 217 | + for _, conditionList := range canaryRuleList.ConditionList { |
| 218 | + conditionListMap := map[string]interface{}{} |
| 219 | + |
| 220 | + if conditionList.Type != nil { |
| 221 | + conditionListMap["type"] = conditionList.Type |
| 222 | + } |
| 223 | + |
| 224 | + if conditionList.Key != nil { |
| 225 | + conditionListMap["key"] = conditionList.Key |
| 226 | + } |
| 227 | + |
| 228 | + if conditionList.Operator != nil { |
| 229 | + conditionListMap["operator"] = conditionList.Operator |
| 230 | + } |
| 231 | + |
| 232 | + if conditionList.Value != nil { |
| 233 | + conditionListMap["value"] = conditionList.Value |
| 234 | + } |
| 235 | + |
| 236 | + if conditionList.Delimiter != nil { |
| 237 | + conditionListMap["delimiter"] = conditionList.Delimiter |
| 238 | + } |
| 239 | + |
| 240 | + if conditionList.GlobalConfigId != nil { |
| 241 | + conditionListMap["global_config_id"] = conditionList.GlobalConfigId |
| 242 | + } |
| 243 | + |
| 244 | + if conditionList.GlobalConfigName != nil { |
| 245 | + conditionListMap["global_config_name"] = conditionList.GlobalConfigName |
| 246 | + } |
| 247 | + |
| 248 | + conditionListList = append(conditionListList, conditionListMap) |
| 249 | + } |
| 250 | + |
| 251 | + canaryRuleListMap["condition_list"] = conditionListList |
| 252 | + } |
| 253 | + |
| 254 | + if canaryRuleList.BalancedServiceList != nil { |
| 255 | + balancedServiceListList := []interface{}{} |
| 256 | + for _, balancedServiceList := range canaryRuleList.BalancedServiceList { |
| 257 | + balancedServiceListMap := map[string]interface{}{} |
| 258 | + |
| 259 | + if balancedServiceList.ServiceID != nil { |
| 260 | + balancedServiceListMap["service_id"] = balancedServiceList.ServiceID |
| 261 | + } |
| 262 | + |
| 263 | + if balancedServiceList.ServiceName != nil { |
| 264 | + balancedServiceListMap["service_name"] = balancedServiceList.ServiceName |
| 265 | + } |
| 266 | + |
| 267 | + if balancedServiceList.UpstreamName != nil { |
| 268 | + balancedServiceListMap["upstream_name"] = balancedServiceList.UpstreamName |
| 269 | + } |
| 270 | + |
| 271 | + if balancedServiceList.Percent != nil { |
| 272 | + balancedServiceListMap["percent"] = balancedServiceList.Percent |
| 273 | + } |
| 274 | + |
| 275 | + balancedServiceListList = append(balancedServiceListList, balancedServiceListMap) |
| 276 | + } |
| 277 | + |
| 278 | + canaryRuleListMap["balanced_service_list"] = balancedServiceListList |
| 279 | + } |
| 280 | + |
| 281 | + if canaryRuleList.ServiceId != nil { |
| 282 | + canaryRuleListMap["service_id"] = canaryRuleList.ServiceId |
| 283 | + } |
| 284 | + |
| 285 | + if canaryRuleList.ServiceName != nil { |
| 286 | + canaryRuleListMap["service_name"] = canaryRuleList.ServiceName |
| 287 | + } |
| 288 | + |
| 289 | + canaryRuleListList = append(canaryRuleListList, canaryRuleListMap) |
| 290 | + } |
| 291 | + |
| 292 | + cloudAPIGatewayCanaryRuleListMap["canary_rule_list"] = canaryRuleListList |
| 293 | + } |
| 294 | + |
| 295 | + if result.TotalCount != nil { |
| 296 | + cloudAPIGatewayCanaryRuleListMap["total_count"] = result.TotalCount |
| 297 | + } |
| 298 | + |
| 299 | + _ = d.Set("result", []interface{}{cloudAPIGatewayCanaryRuleListMap}) |
| 300 | + } |
| 301 | + |
| 302 | + d.SetId(gatewayId) |
| 303 | + output, ok := d.GetOk("result_output_file") |
| 304 | + if ok && output.(string) != "" { |
| 305 | + if e := writeToFile(output.(string), cloudAPIGatewayCanaryRuleListMap); e != nil { |
| 306 | + return e |
| 307 | + } |
| 308 | + } |
| 309 | + return nil |
| 310 | +} |
0 commit comments