|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of rum offlineLogConfig |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_rum_offline_log_config" "offlineLogConfig" { |
| 8 | + project_key = "ZEYrYfvaYQ30jRdmPx" |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + "fmt" |
| 17 | + "log" |
| 18 | + |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 21 | + rum "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/rum/v20210622" |
| 22 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func dataSourceTencentCloudRumOfflineLogConfig() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: dataSourceTencentCloudRumOfflineLogConfigRead, |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "project_key": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + Description: "Unique project key for reporting.", |
| 33 | + }, |
| 34 | + |
| 35 | + "unique_id_set": { |
| 36 | + Type: schema.TypeSet, |
| 37 | + Elem: &schema.Schema{ |
| 38 | + Type: schema.TypeString, |
| 39 | + }, |
| 40 | + Computed: true, |
| 41 | + Description: "Unique identifier of the user to be listened on(aid or uin).", |
| 42 | + }, |
| 43 | + |
| 44 | + "msg": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + Description: "API call information.", |
| 48 | + }, |
| 49 | + |
| 50 | + "result_output_file": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + Description: "Used to save results.", |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func dataSourceTencentCloudRumOfflineLogConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 60 | + defer logElapsed("data_source.tencentcloud_rum_offline_log_config.read")() |
| 61 | + defer inconsistentCheck(d, meta)() |
| 62 | + |
| 63 | + logId := getLogId(contextNil) |
| 64 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 65 | + var projectKey string |
| 66 | + |
| 67 | + paramMap := make(map[string]interface{}) |
| 68 | + if v, ok := d.GetOk("project_key"); ok { |
| 69 | + projectKey = v.(string) |
| 70 | + paramMap["project_key"] = helper.String(v.(string)) |
| 71 | + } |
| 72 | + |
| 73 | + rumService := RumService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 74 | + |
| 75 | + var logConfigs *rum.DescribeOfflineLogConfigsResponseParams |
| 76 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 77 | + results, e := rumService.DescribeRumOfflineLogConfigByFilter(ctx, paramMap) |
| 78 | + if e != nil { |
| 79 | + return retryError(e) |
| 80 | + } |
| 81 | + logConfigs = results |
| 82 | + return nil |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + log.Printf("[CRITAL]%s read Rum uniqueIDSet failed, reason:%+v", logId, err) |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + if logConfigs == nil { |
| 90 | + return fmt.Errorf("Query by id %v is empty", projectKey) |
| 91 | + } |
| 92 | + |
| 93 | + var uniqueID []string |
| 94 | + if logConfigs.UniqueIDSet != nil && len(logConfigs.UniqueIDSet) > 0 { |
| 95 | + for _, v := range logConfigs.UniqueIDSet { |
| 96 | + uniqueID = append(uniqueID, *v) |
| 97 | + } |
| 98 | + _ = d.Set("unique_id_set", uniqueID) |
| 99 | + } |
| 100 | + |
| 101 | + if logConfigs.Msg != nil { |
| 102 | + _ = d.Set("msg", *logConfigs.Msg) |
| 103 | + } |
| 104 | + |
| 105 | + d.SetId(helper.DataResourceIdsHash(uniqueID)) |
| 106 | + |
| 107 | + output, ok := d.GetOk("result_output_file") |
| 108 | + if ok && output.(string) != "" { |
| 109 | + if e := writeToFile(output.(string), map[string]interface{}{ |
| 110 | + "project_key": projectKey, |
| 111 | + "unique_id_set": uniqueID, |
| 112 | + "msg": *logConfigs.Msg, |
| 113 | + }); e != nil { |
| 114 | + return e |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
0 commit comments