|
| 1 | +package cls |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + cls "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls/v20201016" |
| 8 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 9 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 10 | +) |
| 11 | + |
| 12 | +func DataSourceTencentCloudClsLogsets() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: dataSourceTencentCloudClsLogsetsRead, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "filters": { |
| 17 | + Optional: true, |
| 18 | + MaxItems: 10, |
| 19 | + Type: schema.TypeList, |
| 20 | + Description: "Query by filter.", |
| 21 | + Elem: &schema.Resource{ |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "key": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + Description: "Fields that need to be filtered. Support: `logsetName`, `logsetId`, `tagKey`, `tag:tagKey`.", |
| 27 | + }, |
| 28 | + "values": { |
| 29 | + Type: schema.TypeSet, |
| 30 | + Required: true, |
| 31 | + MaxItems: 5, |
| 32 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 33 | + Description: "The values that need to be filtered.", |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + // computed |
| 39 | + "logsets": { |
| 40 | + Computed: true, |
| 41 | + Type: schema.TypeList, |
| 42 | + Description: "logset lists.", |
| 43 | + Elem: &schema.Resource{ |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "logset_id": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "Logset Id.", |
| 49 | + }, |
| 50 | + "logset_name": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "Logset name.", |
| 54 | + }, |
| 55 | + "create_time": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "Create time.", |
| 59 | + }, |
| 60 | + "assumer_name": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "Cloud product identification, when the log set is created by another cloud product, this field will display the cloud product name, such as CDN, TKE.", |
| 64 | + }, |
| 65 | + "tags": { |
| 66 | + Type: schema.TypeList, |
| 67 | + Computed: true, |
| 68 | + Description: "Tags.", |
| 69 | + Elem: &schema.Resource{ |
| 70 | + Schema: map[string]*schema.Schema{ |
| 71 | + "key": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "Tag key.", |
| 75 | + }, |
| 76 | + "value": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "Tag value.", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + "topic_count": { |
| 85 | + Type: schema.TypeInt, |
| 86 | + Computed: true, |
| 87 | + Description: "Topic count.", |
| 88 | + }, |
| 89 | + "role_name": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Computed: true, |
| 92 | + Description: "If `assumer_name` is not empty, it indicates the service role that created the log set.", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + "result_output_file": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Optional: true, |
| 100 | + Description: "Used to save results.", |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func dataSourceTencentCloudClsLogsetsRead(d *schema.ResourceData, meta interface{}) error { |
| 107 | + defer tccommon.LogElapsed("data_source.tencentcloud_cls_logsets.read")() |
| 108 | + defer tccommon.InconsistentCheck(d, meta)() |
| 109 | + |
| 110 | + var ( |
| 111 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 112 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 113 | + service = ClsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 114 | + logsets []*cls.LogsetInfo |
| 115 | + ) |
| 116 | + |
| 117 | + paramMap := make(map[string]interface{}) |
| 118 | + if v, ok := d.GetOk("filters"); ok { |
| 119 | + filtersSet := v.([]interface{}) |
| 120 | + tmpSet := make([]*cls.Filter, 0, len(filtersSet)) |
| 121 | + for _, item := range filtersSet { |
| 122 | + filter := cls.Filter{} |
| 123 | + filterMap := item.(map[string]interface{}) |
| 124 | + if v, ok := filterMap["key"]; ok { |
| 125 | + filter.Key = helper.String(v.(string)) |
| 126 | + } |
| 127 | + |
| 128 | + if v, ok := filterMap["values"]; ok { |
| 129 | + valuesSet := v.(*schema.Set).List() |
| 130 | + filter.Values = helper.InterfacesStringsPoint(valuesSet) |
| 131 | + } |
| 132 | + |
| 133 | + tmpSet = append(tmpSet, &filter) |
| 134 | + } |
| 135 | + |
| 136 | + paramMap["Filters"] = tmpSet |
| 137 | + } |
| 138 | + |
| 139 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 140 | + result, e := service.DescribeClsLogsetsByFilter(ctx, paramMap) |
| 141 | + if e != nil { |
| 142 | + return tccommon.RetryError(e) |
| 143 | + } |
| 144 | + |
| 145 | + logsets = result |
| 146 | + return nil |
| 147 | + }) |
| 148 | + |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + ids := make([]string, 0, len(logsets)) |
| 154 | + tmpList := make([]map[string]interface{}, 0, len(logsets)) |
| 155 | + if logsets != nil { |
| 156 | + for _, logsetInfo := range logsets { |
| 157 | + logsetInfoMap := map[string]interface{}{} |
| 158 | + if logsetInfo.LogsetId != nil { |
| 159 | + logsetInfoMap["logset_id"] = logsetInfo.LogsetId |
| 160 | + } |
| 161 | + |
| 162 | + if logsetInfo.LogsetName != nil { |
| 163 | + logsetInfoMap["logset_name"] = logsetInfo.LogsetName |
| 164 | + } |
| 165 | + |
| 166 | + if logsetInfo.CreateTime != nil { |
| 167 | + logsetInfoMap["create_time"] = logsetInfo.CreateTime |
| 168 | + } |
| 169 | + |
| 170 | + if logsetInfo.AssumerName != nil { |
| 171 | + logsetInfoMap["assumer_name"] = logsetInfo.AssumerName |
| 172 | + } |
| 173 | + |
| 174 | + if logsetInfo.Tags != nil { |
| 175 | + tagsList := []interface{}{} |
| 176 | + for _, tags := range logsetInfo.Tags { |
| 177 | + tagsMap := map[string]interface{}{} |
| 178 | + if tags.Key != nil { |
| 179 | + tagsMap["key"] = tags.Key |
| 180 | + } |
| 181 | + |
| 182 | + if tags.Value != nil { |
| 183 | + tagsMap["value"] = tags.Value |
| 184 | + } |
| 185 | + |
| 186 | + tagsList = append(tagsList, tagsMap) |
| 187 | + } |
| 188 | + |
| 189 | + logsetInfoMap["tags"] = tagsList |
| 190 | + } |
| 191 | + |
| 192 | + if logsetInfo.TopicCount != nil { |
| 193 | + logsetInfoMap["topic_count"] = logsetInfo.TopicCount |
| 194 | + } |
| 195 | + |
| 196 | + if logsetInfo.RoleName != nil { |
| 197 | + logsetInfoMap["role_name"] = logsetInfo.RoleName |
| 198 | + } |
| 199 | + |
| 200 | + ids = append(ids, *logsetInfo.LogsetId) |
| 201 | + tmpList = append(tmpList, logsetInfoMap) |
| 202 | + } |
| 203 | + |
| 204 | + _ = d.Set("logsets", tmpList) |
| 205 | + } |
| 206 | + |
| 207 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 208 | + output, ok := d.GetOk("result_output_file") |
| 209 | + if ok && output.(string) != "" { |
| 210 | + if e := tccommon.WriteToFile(output.(string), tmpList); e != nil { |
| 211 | + return e |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + return nil |
| 216 | +} |
0 commit comments