-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathdata_source_tc_ckafka_group_offsets.go
212 lines (176 loc) · 5.7 KB
/
data_source_tc_ckafka_group_offsets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package ckafka
import (
"context"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ckafka "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudCkafkaGroupOffsets() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCkafkaGroupOffsetsRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Required: true,
Type: schema.TypeString,
Description: "InstanceId.",
},
"group": {
Required: true,
Type: schema.TypeString,
Description: "Kafka consumer group name.",
},
"topics": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "An array of topic names subscribed by the group, if there is no such array, it means all topic information under the specified group.",
},
"search_word": {
Optional: true,
Type: schema.TypeString,
Description: "fuzzy match topicName.",
},
"topic_list": {
Type: schema.TypeList,
Computed: true,
Description: "The topic array, where each element is a json object.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"topic": {
Type: schema.TypeString,
Computed: true,
Description: "topicName.",
},
"partitions": {
Type: schema.TypeList,
Computed: true,
Description: "he topic partition array, where each element is a json object.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"partition": {
Type: schema.TypeInt,
Computed: true,
Description: "topic partitionId.",
},
"offset": {
Type: schema.TypeInt,
Computed: true,
Description: "The offset of the position.",
},
"metadata": {
Type: schema.TypeString,
Computed: true,
Description: "When consumers submit messages, they can pass in metadata for other purposes. Currently, it is usually an empty string.",
},
"error_code": {
Type: schema.TypeInt,
Computed: true,
Description: "ErrorCode.",
},
"log_end_offset": {
Type: schema.TypeInt,
Computed: true,
Description: "The latest offset of the current partition.",
},
"lag": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of unconsumed messages.",
},
},
},
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudCkafkaGroupOffsetsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_ckafka_group_offsets.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
paramMap := make(map[string]interface{})
if v, ok := d.GetOk("instance_id"); ok {
paramMap["instance_id"] = helper.String(v.(string))
}
if v, ok := d.GetOk("group"); ok {
paramMap["group"] = helper.String(v.(string))
}
if v, ok := d.GetOk("topics"); ok {
topicsSet := v.(*schema.Set).List()
paramMap["topics"] = helper.InterfacesStringsPoint(topicsSet)
}
if v, ok := d.GetOk("search_word"); ok {
paramMap["search_word"] = helper.String(v.(string))
}
service := CkafkaService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
var groupOffsetTopics []*ckafka.GroupOffsetTopic
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeCkafkaGroupOffsetsByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
groupOffsetTopics = result
return nil
})
if err != nil {
return err
}
ids := make([]string, 0, len(groupOffsetTopics))
groupOffsetResponseMap := map[string]interface{}{}
topicList := []interface{}{}
for _, topic := range groupOffsetTopics {
topicMap := map[string]interface{}{}
if topic.Topic != nil {
topicMap["topic"] = topic.Topic
ids = append(ids, *topic.Topic)
}
if topic.Partitions != nil {
partitionsList := []interface{}{}
for _, partitions := range topic.Partitions {
partitionsMap := map[string]interface{}{}
if partitions.Partition != nil {
partitionsMap["partition"] = partitions.Partition
}
if partitions.Offset != nil {
partitionsMap["offset"] = partitions.Offset
}
if partitions.Metadata != nil {
partitionsMap["metadata"] = partitions.Metadata
}
if partitions.ErrorCode != nil {
partitionsMap["error_code"] = partitions.ErrorCode
}
if partitions.LogEndOffset != nil {
partitionsMap["log_end_offset"] = partitions.LogEndOffset
}
if partitions.Lag != nil {
partitionsMap["lag"] = partitions.Lag
}
partitionsList = append(partitionsList, partitionsMap)
}
topicMap["partitions"] = partitionsList
}
topicList = append(topicList, topicMap)
}
d.SetId(helper.DataResourceIdsHash(ids))
_ = d.Set("topic_list", topicList)
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), groupOffsetResponseMap); e != nil {
return e
}
}
return nil
}