|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of as advices |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_as_advices" "advices" { |
| 8 | + auto_scaling_group_ids = ["asc-lo0b94oy"] |
| 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 | + as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419" |
| 20 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 21 | +) |
| 22 | + |
| 23 | +func dataSourceTencentCloudAsAdvices() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Read: dataSourceTencentCloudAsAdvicesRead, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "auto_scaling_group_ids": { |
| 28 | + Required: true, |
| 29 | + Type: schema.TypeSet, |
| 30 | + Elem: &schema.Schema{ |
| 31 | + Type: schema.TypeString, |
| 32 | + }, |
| 33 | + Description: "List of scaling groups to be queried. Upper limit: 100.", |
| 34 | + }, |
| 35 | + |
| 36 | + "auto_scaling_advice_set": { |
| 37 | + Computed: true, |
| 38 | + Type: schema.TypeList, |
| 39 | + Description: "A collection of suggestions for scaling group configurations.", |
| 40 | + Elem: &schema.Resource{ |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "auto_scaling_group_id": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + Description: "Auto scaling group ID.", |
| 46 | + }, |
| 47 | + "level": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "Scaling group warning level. Valid values: NORMAL, WARNING, CRITICAL.", |
| 51 | + }, |
| 52 | + "advices": { |
| 53 | + Type: schema.TypeList, |
| 54 | + Computed: true, |
| 55 | + Description: "A collection of suggestions for scaling group configurations.", |
| 56 | + Elem: &schema.Resource{ |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + "problem": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "Problem Description.", |
| 62 | + }, |
| 63 | + "detail": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "Problem Details.", |
| 67 | + }, |
| 68 | + "solution": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Recommended resolutions.", |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + |
| 80 | + "result_output_file": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Optional: true, |
| 83 | + Description: "Used to save results.", |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func dataSourceTencentCloudAsAdvicesRead(d *schema.ResourceData, meta interface{}) error { |
| 90 | + defer logElapsed("data_source.tencentcloud_as_advices.read")() |
| 91 | + defer inconsistentCheck(d, meta)() |
| 92 | + |
| 93 | + logId := getLogId(contextNil) |
| 94 | + |
| 95 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 96 | + |
| 97 | + paramMap := make(map[string]interface{}) |
| 98 | + if v, ok := d.GetOk("auto_scaling_group_ids"); ok { |
| 99 | + autoScalingGroupIdsSet := v.(*schema.Set).List() |
| 100 | + paramMap["AutoScalingGroupIds"] = helper.InterfacesStringsPoint(autoScalingGroupIdsSet) |
| 101 | + } |
| 102 | + |
| 103 | + service := AsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 104 | + |
| 105 | + var autoScalingAdviceSet []*as.AutoScalingAdvice |
| 106 | + |
| 107 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 108 | + result, e := service.DescribeAsAdvices(ctx, paramMap) |
| 109 | + if e != nil { |
| 110 | + return retryError(e) |
| 111 | + } |
| 112 | + autoScalingAdviceSet = result |
| 113 | + return nil |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + ids := make([]string, 0, len(autoScalingAdviceSet)) |
| 120 | + tmpList := make([]map[string]interface{}, 0, len(autoScalingAdviceSet)) |
| 121 | + |
| 122 | + if autoScalingAdviceSet != nil { |
| 123 | + for _, autoScalingAdvice := range autoScalingAdviceSet { |
| 124 | + autoScalingAdviceMap := map[string]interface{}{} |
| 125 | + |
| 126 | + if autoScalingAdvice.AutoScalingGroupId != nil { |
| 127 | + autoScalingAdviceMap["auto_scaling_group_id"] = autoScalingAdvice.AutoScalingGroupId |
| 128 | + } |
| 129 | + |
| 130 | + if autoScalingAdvice.Level != nil { |
| 131 | + autoScalingAdviceMap["level"] = autoScalingAdvice.Level |
| 132 | + } |
| 133 | + |
| 134 | + if autoScalingAdvice.Advices != nil { |
| 135 | + advicesList := []interface{}{} |
| 136 | + for _, advices := range autoScalingAdvice.Advices { |
| 137 | + advicesMap := map[string]interface{}{} |
| 138 | + |
| 139 | + if advices.Problem != nil { |
| 140 | + advicesMap["problem"] = advices.Problem |
| 141 | + } |
| 142 | + |
| 143 | + if advices.Detail != nil { |
| 144 | + advicesMap["detail"] = advices.Detail |
| 145 | + } |
| 146 | + |
| 147 | + if advices.Solution != nil { |
| 148 | + advicesMap["solution"] = advices.Solution |
| 149 | + } |
| 150 | + |
| 151 | + advicesList = append(advicesList, advicesMap) |
| 152 | + } |
| 153 | + |
| 154 | + autoScalingAdviceMap["advices"] = []interface{}{advicesList} |
| 155 | + } |
| 156 | + |
| 157 | + ids = append(ids, *autoScalingAdvice.AutoScalingGroupId) |
| 158 | + tmpList = append(tmpList, autoScalingAdviceMap) |
| 159 | + } |
| 160 | + |
| 161 | + _ = d.Set("auto_scaling_advice_set", tmpList) |
| 162 | + } |
| 163 | + |
| 164 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 165 | + output, ok := d.GetOk("result_output_file") |
| 166 | + if ok && output.(string) != "" { |
| 167 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 168 | + return e |
| 169 | + } |
| 170 | + } |
| 171 | + return nil |
| 172 | +} |
0 commit comments