|
| 1 | +package tencentcloud |
| 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 | + dlc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dlc/v20210125" |
| 9 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourceTencentCloudDlcDescribeDataEngineEvents() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: dataSourceTencentCloudDlcDescribeDataEngineEventsRead, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "data_engine_name": { |
| 17 | + Required: true, |
| 18 | + Type: schema.TypeString, |
| 19 | + Description: "Data engine name.", |
| 20 | + }, |
| 21 | + |
| 22 | + "events": { |
| 23 | + Computed: true, |
| 24 | + Type: schema.TypeList, |
| 25 | + Description: "Event details.", |
| 26 | + Elem: &schema.Resource{ |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "time": { |
| 29 | + Type: schema.TypeSet, |
| 30 | + Elem: &schema.Schema{ |
| 31 | + Type: schema.TypeString, |
| 32 | + }, |
| 33 | + Computed: true, |
| 34 | + Description: "Event time.", |
| 35 | + }, |
| 36 | + "events_action": { |
| 37 | + Type: schema.TypeSet, |
| 38 | + Elem: &schema.Schema{ |
| 39 | + Type: schema.TypeString, |
| 40 | + }, |
| 41 | + Computed: true, |
| 42 | + Description: "Event action.", |
| 43 | + }, |
| 44 | + "cluster_info": { |
| 45 | + Type: schema.TypeSet, |
| 46 | + Elem: &schema.Schema{ |
| 47 | + Type: schema.TypeString, |
| 48 | + }, |
| 49 | + Computed: true, |
| 50 | + Description: "Cluster information.", |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + |
| 56 | + "result_output_file": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + Description: "Used to save results.", |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func dataSourceTencentCloudDlcDescribeDataEngineEventsRead(d *schema.ResourceData, meta interface{}) error { |
| 66 | + defer logElapsed("data_source.tencentcloud_dlc_describe_data_engine_events.read")() |
| 67 | + defer inconsistentCheck(d, meta)() |
| 68 | + |
| 69 | + logId := getLogId(contextNil) |
| 70 | + |
| 71 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 72 | + |
| 73 | + paramMap := make(map[string]interface{}) |
| 74 | + if v, ok := d.GetOk("data_engine_name"); ok { |
| 75 | + paramMap["DataEngineName"] = helper.String(v.(string)) |
| 76 | + } |
| 77 | + |
| 78 | + service := DlcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 79 | + |
| 80 | + var events []*dlc.HouseEventsInfo |
| 81 | + |
| 82 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 83 | + result, e := service.DescribeDlcDescribeDataEngineEventsByFilter(ctx, paramMap) |
| 84 | + if e != nil { |
| 85 | + return retryError(e) |
| 86 | + } |
| 87 | + events = result |
| 88 | + return nil |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + ids := make([]string, 0, len(events)) |
| 95 | + tmpList := make([]map[string]interface{}, 0, len(events)) |
| 96 | + |
| 97 | + if events != nil { |
| 98 | + for _, houseEventsInfo := range events { |
| 99 | + houseEventsInfoMap := map[string]interface{}{} |
| 100 | + |
| 101 | + if houseEventsInfo.Time != nil { |
| 102 | + houseEventsInfoMap["time"] = houseEventsInfo.Time |
| 103 | + } |
| 104 | + |
| 105 | + if houseEventsInfo.EventsAction != nil { |
| 106 | + houseEventsInfoMap["events_action"] = houseEventsInfo.EventsAction |
| 107 | + } |
| 108 | + |
| 109 | + if houseEventsInfo.ClusterInfo != nil { |
| 110 | + houseEventsInfoMap["cluster_info"] = houseEventsInfo.ClusterInfo |
| 111 | + } |
| 112 | + |
| 113 | + tmpList = append(tmpList, houseEventsInfoMap) |
| 114 | + } |
| 115 | + |
| 116 | + _ = d.Set("events", tmpList) |
| 117 | + } |
| 118 | + |
| 119 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 120 | + output, ok := d.GetOk("result_output_file") |
| 121 | + if ok && output.(string) != "" { |
| 122 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 123 | + return e |
| 124 | + } |
| 125 | + } |
| 126 | + return nil |
| 127 | +} |
0 commit comments