|
| 1 | +package trocket |
| 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 | + trocketv20230308 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/trocket/v20230308" |
| 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 DataSourceTencentCloudTrocketRocketmqInstances() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudTrocketRocketmqInstancesRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "filters": { |
| 19 | + Type: schema.TypeList, |
| 20 | + Optional: true, |
| 21 | + Description: "Filter query criteria list.", |
| 22 | + Elem: &schema.Resource{ |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "name": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + Description: "Filter name.", |
| 28 | + }, |
| 29 | + "values": { |
| 30 | + Type: schema.TypeSet, |
| 31 | + Required: true, |
| 32 | + Description: "Filter values.", |
| 33 | + Elem: &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + |
| 41 | + "tag_filters": { |
| 42 | + Type: schema.TypeList, |
| 43 | + Optional: true, |
| 44 | + Description: "Tag filters.", |
| 45 | + Elem: &schema.Resource{ |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + "tag_key": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Required: true, |
| 50 | + Description: "Tag key.", |
| 51 | + }, |
| 52 | + "tag_values": { |
| 53 | + Type: schema.TypeSet, |
| 54 | + Required: true, |
| 55 | + Description: "Tag values.", |
| 56 | + Elem: &schema.Schema{ |
| 57 | + Type: schema.TypeString, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + |
| 64 | + "data": { |
| 65 | + Type: schema.TypeList, |
| 66 | + Computed: true, |
| 67 | + Description: "Instance list.", |
| 68 | + Elem: &schema.Resource{ |
| 69 | + Schema: map[string]*schema.Schema{ |
| 70 | + "instance_id": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Required: true, |
| 73 | + Description: "Instance ID.", |
| 74 | + }, |
| 75 | + "instance_name": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Required: true, |
| 78 | + Description: "Instance name.", |
| 79 | + }, |
| 80 | + "version": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Required: true, |
| 83 | + Description: "version.", |
| 84 | + }, |
| 85 | + "instance_type": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Required: true, |
| 88 | + Description: "Instance type. EXPERIMENT: trial version; BASIC: Basic Edition; PRO: Professional Edition; PLATINUM: Platinum Edition.", |
| 89 | + }, |
| 90 | + "instance_status": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Required: true, |
| 93 | + Description: "Instance status. RUNNING, Running; MAINTAINING: Under maintenance; ABNORMAL: abnormal; OVERDUE: arrears; DESTROYED: Deleted; CREATING: Creating; MODIFYING: In the process of transformation; CREATE_FAILURE: Creation failed; MODIFY_FAILURE: Transformation failed; DELETING: deleting.", |
| 94 | + }, |
| 95 | + "topic_num_limit": { |
| 96 | + Type: schema.TypeInt, |
| 97 | + Required: true, |
| 98 | + Description: "Maximum number of instance topics.", |
| 99 | + }, |
| 100 | + "group_num_limit": { |
| 101 | + Type: schema.TypeInt, |
| 102 | + Required: true, |
| 103 | + Description: "Maximum number of instance consumer groups.", |
| 104 | + }, |
| 105 | + "pay_mode": { |
| 106 | + Type: schema.TypeString, |
| 107 | + Required: true, |
| 108 | + Description: "Pay mode. - POSTPAID: postpaid; - PREPAID: prepaid.", |
| 109 | + }, |
| 110 | + "expiry_time": { |
| 111 | + Type: schema.TypeInt, |
| 112 | + Required: true, |
| 113 | + Description: "Expiration timestamp, **Unix timestamp (in milliseconds)**.", |
| 114 | + }, |
| 115 | + "remark": { |
| 116 | + Type: schema.TypeString, |
| 117 | + Required: true, |
| 118 | + Description: "Remark.", |
| 119 | + }, |
| 120 | + "topic_num": { |
| 121 | + Type: schema.TypeInt, |
| 122 | + Required: true, |
| 123 | + Description: "Topic nums.", |
| 124 | + }, |
| 125 | + "group_num": { |
| 126 | + Type: schema.TypeInt, |
| 127 | + Required: true, |
| 128 | + Description: "Group nums.", |
| 129 | + }, |
| 130 | + "tag_list": { |
| 131 | + Type: schema.TypeList, |
| 132 | + Required: true, |
| 133 | + Description: "Tag list.", |
| 134 | + Elem: &schema.Resource{ |
| 135 | + Schema: map[string]*schema.Schema{ |
| 136 | + "tag_key": { |
| 137 | + Type: schema.TypeString, |
| 138 | + Required: true, |
| 139 | + Description: "Tag key.", |
| 140 | + }, |
| 141 | + "tag_value": { |
| 142 | + Type: schema.TypeString, |
| 143 | + Required: true, |
| 144 | + Description: "Tag value.", |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + "sku_code": { |
| 150 | + Type: schema.TypeString, |
| 151 | + Required: true, |
| 152 | + Description: "Product Specifications.", |
| 153 | + }, |
| 154 | + "tps_limit": { |
| 155 | + Type: schema.TypeInt, |
| 156 | + Required: true, |
| 157 | + Description: "TPS current limit value.", |
| 158 | + }, |
| 159 | + "scaled_tps_limit": { |
| 160 | + Type: schema.TypeInt, |
| 161 | + Required: true, |
| 162 | + Description: "Elastic TPS current limit value.", |
| 163 | + }, |
| 164 | + "message_retention": { |
| 165 | + Type: schema.TypeInt, |
| 166 | + Required: true, |
| 167 | + Description: "Message retention time, in hours.", |
| 168 | + }, |
| 169 | + "max_message_delay": { |
| 170 | + Type: schema.TypeInt, |
| 171 | + Required: true, |
| 172 | + Description: "Maximum delay message duration in hours.", |
| 173 | + }, |
| 174 | + "renew_flag": { |
| 175 | + Type: schema.TypeInt, |
| 176 | + Required: true, |
| 177 | + Description: "Whether to renew automatically, only for prepaid clusters (0: not renew automatically; 1: renew automatically).", |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + |
| 183 | + "result_output_file": { |
| 184 | + Type: schema.TypeString, |
| 185 | + Optional: true, |
| 186 | + Description: "Used to save results.", |
| 187 | + }, |
| 188 | + }, |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +func dataSourceTencentCloudTrocketRocketmqInstancesRead(d *schema.ResourceData, meta interface{}) error { |
| 193 | + defer tccommon.LogElapsed("data_source.tencentcloud_trocket_rocketmq_instances.read")() |
| 194 | + defer tccommon.InconsistentCheck(d, meta)() |
| 195 | + |
| 196 | + var ( |
| 197 | + logId = tccommon.GetLogId(nil) |
| 198 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 199 | + service = TrocketService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 200 | + ) |
| 201 | + |
| 202 | + paramMap := make(map[string]interface{}) |
| 203 | + if v, ok := d.GetOk("filters"); ok { |
| 204 | + filtersSet := v.([]interface{}) |
| 205 | + tmpSet := make([]*trocketv20230308.Filter, 0, len(filtersSet)) |
| 206 | + for _, item := range filtersSet { |
| 207 | + filtersMap := item.(map[string]interface{}) |
| 208 | + filter := trocketv20230308.Filter{} |
| 209 | + if v, ok := filtersMap["name"].(string); ok && v != "" { |
| 210 | + filter.Name = helper.String(v) |
| 211 | + } |
| 212 | + |
| 213 | + if v, ok := filtersMap["values"]; ok { |
| 214 | + valuesSet := v.(*schema.Set).List() |
| 215 | + for i := range valuesSet { |
| 216 | + values := valuesSet[i].(string) |
| 217 | + filter.Values = append(filter.Values, helper.String(values)) |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + tmpSet = append(tmpSet, &filter) |
| 222 | + } |
| 223 | + |
| 224 | + paramMap["Filters"] = tmpSet |
| 225 | + } |
| 226 | + |
| 227 | + if v, ok := d.GetOk("tag_filters"); ok { |
| 228 | + tagFiltersSet := v.([]interface{}) |
| 229 | + tmpSet := make([]*trocketv20230308.TagFilter, 0, len(tagFiltersSet)) |
| 230 | + for _, item := range tagFiltersSet { |
| 231 | + tagFiltersMap := item.(map[string]interface{}) |
| 232 | + tagFilter := trocketv20230308.TagFilter{} |
| 233 | + if v, ok := tagFiltersMap["tag_key"].(string); ok && v != "" { |
| 234 | + tagFilter.TagKey = helper.String(v) |
| 235 | + } |
| 236 | + |
| 237 | + if v, ok := tagFiltersMap["tag_values"]; ok { |
| 238 | + tagValuesSet := v.(*schema.Set).List() |
| 239 | + for i := range tagValuesSet { |
| 240 | + tagValues := tagValuesSet[i].(string) |
| 241 | + tagFilter.TagValues = append(tagFilter.TagValues, helper.String(tagValues)) |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + tmpSet = append(tmpSet, &tagFilter) |
| 246 | + } |
| 247 | + |
| 248 | + paramMap["TagFilters"] = tmpSet |
| 249 | + } |
| 250 | + |
| 251 | + var respData []*trocketv20230308.InstanceItem |
| 252 | + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 253 | + result, e := service.DescribeTrocketRocketmqInstancesByFilter(ctx, paramMap) |
| 254 | + if e != nil { |
| 255 | + return tccommon.RetryError(e) |
| 256 | + } |
| 257 | + |
| 258 | + respData = result |
| 259 | + return nil |
| 260 | + }) |
| 261 | + |
| 262 | + if reqErr != nil { |
| 263 | + return reqErr |
| 264 | + } |
| 265 | + |
| 266 | + dataList := make([]map[string]interface{}, 0, len(respData)) |
| 267 | + if respData != nil { |
| 268 | + for _, data := range respData { |
| 269 | + dataMap := map[string]interface{}{} |
| 270 | + if data.InstanceId != nil { |
| 271 | + dataMap["instance_id"] = data.InstanceId |
| 272 | + } |
| 273 | + |
| 274 | + if data.InstanceName != nil { |
| 275 | + dataMap["instance_name"] = data.InstanceName |
| 276 | + } |
| 277 | + |
| 278 | + if data.Version != nil { |
| 279 | + dataMap["version"] = data.Version |
| 280 | + } |
| 281 | + |
| 282 | + if data.InstanceType != nil { |
| 283 | + dataMap["instance_type"] = data.InstanceType |
| 284 | + } |
| 285 | + |
| 286 | + if data.InstanceStatus != nil { |
| 287 | + dataMap["instance_status"] = data.InstanceStatus |
| 288 | + } |
| 289 | + |
| 290 | + if data.TopicNumLimit != nil { |
| 291 | + dataMap["topic_num_limit"] = data.TopicNumLimit |
| 292 | + } |
| 293 | + |
| 294 | + if data.GroupNumLimit != nil { |
| 295 | + dataMap["group_num_limit"] = data.GroupNumLimit |
| 296 | + } |
| 297 | + |
| 298 | + if data.PayMode != nil { |
| 299 | + dataMap["pay_mode"] = data.PayMode |
| 300 | + } |
| 301 | + |
| 302 | + if data.ExpiryTime != nil { |
| 303 | + dataMap["expiry_time"] = data.ExpiryTime |
| 304 | + } |
| 305 | + |
| 306 | + if data.Remark != nil { |
| 307 | + dataMap["remark"] = data.Remark |
| 308 | + } |
| 309 | + |
| 310 | + if data.TopicNum != nil { |
| 311 | + dataMap["topic_num"] = data.TopicNum |
| 312 | + } |
| 313 | + |
| 314 | + if data.GroupNum != nil { |
| 315 | + dataMap["group_num"] = data.GroupNum |
| 316 | + } |
| 317 | + |
| 318 | + tagListList := make([]map[string]interface{}, 0, len(data.TagList)) |
| 319 | + if data.TagList != nil { |
| 320 | + for _, tagList := range data.TagList { |
| 321 | + tagListMap := map[string]interface{}{} |
| 322 | + |
| 323 | + if tagList.TagKey != nil { |
| 324 | + tagListMap["tag_key"] = tagList.TagKey |
| 325 | + } |
| 326 | + |
| 327 | + if tagList.TagValue != nil { |
| 328 | + tagListMap["tag_value"] = tagList.TagValue |
| 329 | + } |
| 330 | + |
| 331 | + tagListList = append(tagListList, tagListMap) |
| 332 | + } |
| 333 | + |
| 334 | + dataMap["tag_list"] = tagListList |
| 335 | + } |
| 336 | + if data.SkuCode != nil { |
| 337 | + dataMap["sku_code"] = data.SkuCode |
| 338 | + } |
| 339 | + |
| 340 | + if data.TpsLimit != nil { |
| 341 | + dataMap["tps_limit"] = data.TpsLimit |
| 342 | + } |
| 343 | + |
| 344 | + if data.ScaledTpsLimit != nil { |
| 345 | + dataMap["scaled_tps_limit"] = data.ScaledTpsLimit |
| 346 | + } |
| 347 | + |
| 348 | + if data.MessageRetention != nil { |
| 349 | + dataMap["message_retention"] = data.MessageRetention |
| 350 | + } |
| 351 | + |
| 352 | + if data.MaxMessageDelay != nil { |
| 353 | + dataMap["max_message_delay"] = data.MaxMessageDelay |
| 354 | + } |
| 355 | + |
| 356 | + if data.RenewFlag != nil { |
| 357 | + dataMap["renew_flag"] = data.RenewFlag |
| 358 | + } |
| 359 | + |
| 360 | + dataList = append(dataList, dataMap) |
| 361 | + } |
| 362 | + |
| 363 | + _ = d.Set("data", dataList) |
| 364 | + } |
| 365 | + |
| 366 | + d.SetId(helper.BuildToken()) |
| 367 | + output, ok := d.GetOk("result_output_file") |
| 368 | + if ok && output.(string) != "" { |
| 369 | + if e := tccommon.WriteToFile(output.(string), dataList); e != nil { |
| 370 | + return e |
| 371 | + } |
| 372 | + } |
| 373 | + |
| 374 | + return nil |
| 375 | +} |
0 commit comments