|
| 1 | +package igtm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + igtmv20231024 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/igtm/v20231024" |
| 9 | + |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceTencentCloudIgtmDetectTaskPackageList() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudIgtmDetectTaskPackageListRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "filters": { |
| 19 | + Type: schema.TypeList, |
| 20 | + Optional: true, |
| 21 | + Description: "Detect task filter conditions.", |
| 22 | + Elem: &schema.Resource{ |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "name": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + Description: "Filter field name, supported list as follows:\n- ResourceId: detect task resource id.\n- PeriodStart: minimum expiration time.\n- PeriodEnd: maximum expiration time.", |
| 28 | + }, |
| 29 | + "value": { |
| 30 | + Type: schema.TypeSet, |
| 31 | + Required: true, |
| 32 | + Description: "Filter field value.", |
| 33 | + Elem: &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + }, |
| 36 | + }, |
| 37 | + "fuzzy": { |
| 38 | + Type: schema.TypeBool, |
| 39 | + Optional: true, |
| 40 | + Description: "Whether to enable fuzzy query, only supports filter field name as domain.\nWhen fuzzy query is enabled, maximum Value length is 1, otherwise maximum Value length is 5. (Reserved field, not currently used).", |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + |
| 46 | + "task_package_set": { |
| 47 | + Type: schema.TypeList, |
| 48 | + Computed: true, |
| 49 | + Description: "Detect task package list.", |
| 50 | + Elem: &schema.Resource{ |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "resource_id": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: "Resource ID.", |
| 56 | + }, |
| 57 | + "resource_type": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "Resource type\nTASK Detect task.", |
| 61 | + }, |
| 62 | + "quota": { |
| 63 | + Type: schema.TypeInt, |
| 64 | + Computed: true, |
| 65 | + Description: "Quota.", |
| 66 | + }, |
| 67 | + "current_deadline": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: "Package expiration time.", |
| 71 | + }, |
| 72 | + "create_time": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "Package creation time.", |
| 76 | + }, |
| 77 | + "is_expire": { |
| 78 | + Type: schema.TypeInt, |
| 79 | + Computed: true, |
| 80 | + Description: "Whether expired 0 no 1 yes.", |
| 81 | + }, |
| 82 | + "status": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: "Status\nENABLED: Normal\nISOLATED: Isolated\nDESTROYED: Destroyed\nREFUNDED: Refunded.", |
| 86 | + }, |
| 87 | + "auto_renew_flag": { |
| 88 | + Type: schema.TypeInt, |
| 89 | + Computed: true, |
| 90 | + Description: "Whether auto-renew 0 no 1 yes.", |
| 91 | + }, |
| 92 | + "remark": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "Remark.", |
| 96 | + }, |
| 97 | + "cost_item_list": { |
| 98 | + Type: schema.TypeList, |
| 99 | + Computed: true, |
| 100 | + Description: "Billing item.", |
| 101 | + Elem: &schema.Resource{ |
| 102 | + Schema: map[string]*schema.Schema{ |
| 103 | + "cost_name": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + Description: "Billing item name.", |
| 107 | + }, |
| 108 | + "cost_value": { |
| 109 | + Type: schema.TypeInt, |
| 110 | + Computed: true, |
| 111 | + Description: "Billing item value.", |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + "group": { |
| 117 | + Type: schema.TypeInt, |
| 118 | + Computed: true, |
| 119 | + Description: "Detect task type: 100 system setting; 200 billing; 300 management system; 110D monitoring migration free task; 120 disaster recovery switch task.", |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + |
| 125 | + "result_output_file": { |
| 126 | + Type: schema.TypeString, |
| 127 | + Optional: true, |
| 128 | + Description: "Used to save results.", |
| 129 | + }, |
| 130 | + }, |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func dataSourceTencentCloudIgtmDetectTaskPackageListRead(d *schema.ResourceData, meta interface{}) error { |
| 135 | + defer tccommon.LogElapsed("data_source.tencentcloud_igtm_detect_task_package_list.read")() |
| 136 | + defer tccommon.InconsistentCheck(d, meta)() |
| 137 | + |
| 138 | + var ( |
| 139 | + logId = tccommon.GetLogId(nil) |
| 140 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 141 | + service = IgtmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 142 | + ) |
| 143 | + |
| 144 | + paramMap := make(map[string]interface{}) |
| 145 | + if v, ok := d.GetOk("filters"); ok { |
| 146 | + filtersSet := v.([]interface{}) |
| 147 | + tmpSet := make([]*igtmv20231024.ResourceFilter, 0, len(filtersSet)) |
| 148 | + for _, item := range filtersSet { |
| 149 | + filtersMap := item.(map[string]interface{}) |
| 150 | + resourceFilter := igtmv20231024.ResourceFilter{} |
| 151 | + if v, ok := filtersMap["name"].(string); ok && v != "" { |
| 152 | + resourceFilter.Name = helper.String(v) |
| 153 | + } |
| 154 | + |
| 155 | + if v, ok := filtersMap["value"]; ok { |
| 156 | + valueSet := v.(*schema.Set).List() |
| 157 | + for i := range valueSet { |
| 158 | + value := valueSet[i].(string) |
| 159 | + resourceFilter.Value = append(resourceFilter.Value, helper.String(value)) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if v, ok := filtersMap["fuzzy"].(bool); ok { |
| 164 | + resourceFilter.Fuzzy = helper.Bool(v) |
| 165 | + } |
| 166 | + |
| 167 | + tmpSet = append(tmpSet, &resourceFilter) |
| 168 | + } |
| 169 | + |
| 170 | + paramMap["Filters"] = tmpSet |
| 171 | + } |
| 172 | + |
| 173 | + var respData []*igtmv20231024.DetectTaskPackage |
| 174 | + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 175 | + result, e := service.DescribeIgtmDetectTaskPackageListByFilter(ctx, paramMap) |
| 176 | + if e != nil { |
| 177 | + return tccommon.RetryError(e) |
| 178 | + } |
| 179 | + |
| 180 | + respData = result |
| 181 | + return nil |
| 182 | + }) |
| 183 | + |
| 184 | + if reqErr != nil { |
| 185 | + return reqErr |
| 186 | + } |
| 187 | + |
| 188 | + taskPackageSetList := make([]map[string]interface{}, 0, len(respData)) |
| 189 | + if respData != nil { |
| 190 | + for _, taskPackageSet := range respData { |
| 191 | + taskPackageSetMap := map[string]interface{}{} |
| 192 | + if taskPackageSet.ResourceId != nil { |
| 193 | + taskPackageSetMap["resource_id"] = taskPackageSet.ResourceId |
| 194 | + } |
| 195 | + |
| 196 | + if taskPackageSet.ResourceType != nil { |
| 197 | + taskPackageSetMap["resource_type"] = taskPackageSet.ResourceType |
| 198 | + } |
| 199 | + |
| 200 | + if taskPackageSet.Quota != nil { |
| 201 | + taskPackageSetMap["quota"] = taskPackageSet.Quota |
| 202 | + } |
| 203 | + |
| 204 | + if taskPackageSet.CurrentDeadline != nil { |
| 205 | + taskPackageSetMap["current_deadline"] = taskPackageSet.CurrentDeadline |
| 206 | + } |
| 207 | + |
| 208 | + if taskPackageSet.CreateTime != nil { |
| 209 | + taskPackageSetMap["create_time"] = taskPackageSet.CreateTime |
| 210 | + } |
| 211 | + |
| 212 | + if taskPackageSet.IsExpire != nil { |
| 213 | + taskPackageSetMap["is_expire"] = taskPackageSet.IsExpire |
| 214 | + } |
| 215 | + |
| 216 | + if taskPackageSet.Status != nil { |
| 217 | + taskPackageSetMap["status"] = taskPackageSet.Status |
| 218 | + } |
| 219 | + |
| 220 | + if taskPackageSet.AutoRenewFlag != nil { |
| 221 | + taskPackageSetMap["auto_renew_flag"] = taskPackageSet.AutoRenewFlag |
| 222 | + } |
| 223 | + |
| 224 | + if taskPackageSet.Remark != nil { |
| 225 | + taskPackageSetMap["remark"] = taskPackageSet.Remark |
| 226 | + } |
| 227 | + |
| 228 | + costItemListList := make([]map[string]interface{}, 0, len(taskPackageSet.CostItemList)) |
| 229 | + if taskPackageSet.CostItemList != nil { |
| 230 | + for _, costItemList := range taskPackageSet.CostItemList { |
| 231 | + costItemListMap := map[string]interface{}{} |
| 232 | + if costItemList.CostName != nil { |
| 233 | + costItemListMap["cost_name"] = costItemList.CostName |
| 234 | + } |
| 235 | + |
| 236 | + if costItemList.CostValue != nil { |
| 237 | + costItemListMap["cost_value"] = costItemList.CostValue |
| 238 | + } |
| 239 | + |
| 240 | + costItemListList = append(costItemListList, costItemListMap) |
| 241 | + } |
| 242 | + |
| 243 | + taskPackageSetMap["cost_item_list"] = costItemListList |
| 244 | + } |
| 245 | + |
| 246 | + if taskPackageSet.Group != nil { |
| 247 | + taskPackageSetMap["group"] = taskPackageSet.Group |
| 248 | + } |
| 249 | + |
| 250 | + taskPackageSetList = append(taskPackageSetList, taskPackageSetMap) |
| 251 | + } |
| 252 | + |
| 253 | + _ = d.Set("task_package_set", taskPackageSetList) |
| 254 | + } |
| 255 | + |
| 256 | + d.SetId(helper.BuildToken()) |
| 257 | + output, ok := d.GetOk("result_output_file") |
| 258 | + if ok && output.(string) != "" { |
| 259 | + if e := tccommon.WriteToFile(output.(string), taskPackageSetList); e != nil { |
| 260 | + return e |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + return nil |
| 265 | +} |
0 commit comments