|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of ckafka group_info |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_ckafka_group_info" "group_info" { |
| 8 | + instance_id = "ckafka-xxxxxx" |
| 9 | + group_list = ["xxxxxx"] |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 20 | + ckafka "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudCkafkaGroupInfo() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudCkafkaGroupInfoRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "instance_id": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "InstanceId.", |
| 32 | + }, |
| 33 | + |
| 34 | + "group_list": { |
| 35 | + Required: true, |
| 36 | + Type: schema.TypeSet, |
| 37 | + Elem: &schema.Schema{ |
| 38 | + Type: schema.TypeString, |
| 39 | + }, |
| 40 | + Description: "Kafka consumption group, Consumer-group, here is an array format, format GroupList.0=xxx&GroupList.1=yyy.", |
| 41 | + }, |
| 42 | + |
| 43 | + "result": { |
| 44 | + Computed: true, |
| 45 | + Type: schema.TypeList, |
| 46 | + Description: "result.", |
| 47 | + Elem: &schema.Resource{ |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "error_code": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + Description: "Error code, normally 0.", |
| 53 | + }, |
| 54 | + "state": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + Description: "Group state description (commonly Empty, Stable, and Dead states): Dead: The consumption group does not exist Empty: The consumption group does not currently have any consumer subscriptions PreparingRebalance: The consumption group is in the rebalance state CompletingRebalance: The consumption group is in the rebalance state Stable: Each consumer in the consumption group has joined and is in a stable state.", |
| 58 | + }, |
| 59 | + "protocol_type": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "The protocol type selected by the consumption group is normally the consumer, but some systems use their own protocol, such as kafka-connect, which uses connect. Only the standard consumer protocol, this interface knows the format of the specific allocation method, and can analyze the specific partition allocation.", |
| 63 | + }, |
| 64 | + "protocol": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + Description: "Common consumer partition allocation algorithms are as follows (the default option for Kafka consumer SDK is range) range|roundrobin|sticky.", |
| 68 | + }, |
| 69 | + "members": { |
| 70 | + Type: schema.TypeList, |
| 71 | + Computed: true, |
| 72 | + Description: "This array contains information only if state is Stable and protocol_type is consumer.", |
| 73 | + Elem: &schema.Resource{ |
| 74 | + Schema: map[string]*schema.Schema{ |
| 75 | + "member_id": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "ID that the coordinator generated for consumer.", |
| 79 | + }, |
| 80 | + "client_id": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + Description: "The client.id information set by the client consumer SDK itself.", |
| 84 | + }, |
| 85 | + "client_host": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + Description: "Generally store the customer's IP address.", |
| 89 | + }, |
| 90 | + "assignment": { |
| 91 | + Type: schema.TypeList, |
| 92 | + Computed: true, |
| 93 | + Description: "Stores the partition information assigned to the consumer.", |
| 94 | + Elem: &schema.Resource{ |
| 95 | + Schema: map[string]*schema.Schema{ |
| 96 | + "version": { |
| 97 | + Type: schema.TypeInt, |
| 98 | + Computed: true, |
| 99 | + Description: "assignment version information.", |
| 100 | + }, |
| 101 | + "topics": { |
| 102 | + Type: schema.TypeList, |
| 103 | + Computed: true, |
| 104 | + Description: "topic list.", |
| 105 | + Elem: &schema.Resource{ |
| 106 | + Schema: map[string]*schema.Schema{ |
| 107 | + "topic": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + Description: "Assigned topic name.", |
| 111 | + }, |
| 112 | + "partitions": { |
| 113 | + Type: schema.TypeSet, |
| 114 | + Elem: &schema.Schema{ |
| 115 | + Type: schema.TypeInt, |
| 116 | + }, |
| 117 | + Computed: true, |
| 118 | + Description: "Allocated partition information.", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + "group": { |
| 130 | + Type: schema.TypeString, |
| 131 | + Computed: true, |
| 132 | + Description: "Kafka consumer group.", |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + |
| 138 | + "result_output_file": { |
| 139 | + Type: schema.TypeString, |
| 140 | + Optional: true, |
| 141 | + Description: "Used to save results.", |
| 142 | + }, |
| 143 | + }, |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func dataSourceTencentCloudCkafkaGroupInfoRead(d *schema.ResourceData, meta interface{}) error { |
| 148 | + defer logElapsed("data_source.tencentcloud_ckafka_group_info.read")() |
| 149 | + defer inconsistentCheck(d, meta)() |
| 150 | + |
| 151 | + logId := getLogId(contextNil) |
| 152 | + |
| 153 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 154 | + |
| 155 | + paramMap := make(map[string]interface{}) |
| 156 | + if v, ok := d.GetOk("instance_id"); ok { |
| 157 | + paramMap["instance_id"] = helper.String(v.(string)) |
| 158 | + } |
| 159 | + |
| 160 | + if v, ok := d.GetOk("group_list"); ok { |
| 161 | + groupListSet := v.(*schema.Set).List() |
| 162 | + paramMap["group_list"] = helper.InterfacesStringsPoint(groupListSet) |
| 163 | + } |
| 164 | + |
| 165 | + service := CkafkaService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 166 | + |
| 167 | + var result []*ckafka.GroupInfoResponse |
| 168 | + |
| 169 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 170 | + groupInfo, e := service.DescribeCkafkaGroupInfoByFilter(ctx, paramMap) |
| 171 | + if e != nil { |
| 172 | + return retryError(e) |
| 173 | + } |
| 174 | + result = groupInfo |
| 175 | + return nil |
| 176 | + }) |
| 177 | + if err != nil { |
| 178 | + return err |
| 179 | + } |
| 180 | + |
| 181 | + ids := make([]string, 0, len(result)) |
| 182 | + tmpList := make([]map[string]interface{}, 0, len(result)) |
| 183 | + |
| 184 | + if result != nil { |
| 185 | + for _, groupInfoResponse := range result { |
| 186 | + groupInfoResponseMap := map[string]interface{}{} |
| 187 | + |
| 188 | + if groupInfoResponse.ErrorCode != nil { |
| 189 | + groupInfoResponseMap["error_code"] = groupInfoResponse.ErrorCode |
| 190 | + } |
| 191 | + |
| 192 | + if groupInfoResponse.State != nil { |
| 193 | + groupInfoResponseMap["state"] = groupInfoResponse.State |
| 194 | + } |
| 195 | + |
| 196 | + if groupInfoResponse.ProtocolType != nil { |
| 197 | + groupInfoResponseMap["protocol_type"] = groupInfoResponse.ProtocolType |
| 198 | + } |
| 199 | + |
| 200 | + if groupInfoResponse.Protocol != nil { |
| 201 | + groupInfoResponseMap["protocol"] = groupInfoResponse.Protocol |
| 202 | + } |
| 203 | + |
| 204 | + if groupInfoResponse.Members != nil { |
| 205 | + membersList := []interface{}{} |
| 206 | + for _, members := range groupInfoResponse.Members { |
| 207 | + membersMap := map[string]interface{}{} |
| 208 | + |
| 209 | + if members.MemberId != nil { |
| 210 | + membersMap["member_id"] = members.MemberId |
| 211 | + } |
| 212 | + |
| 213 | + if members.ClientId != nil { |
| 214 | + membersMap["client_id"] = members.ClientId |
| 215 | + } |
| 216 | + |
| 217 | + if members.ClientHost != nil { |
| 218 | + membersMap["client_host"] = members.ClientHost |
| 219 | + } |
| 220 | + |
| 221 | + if members.Assignment != nil { |
| 222 | + assignmentMap := map[string]interface{}{} |
| 223 | + |
| 224 | + if members.Assignment.Version != nil { |
| 225 | + assignmentMap["version"] = members.Assignment.Version |
| 226 | + } |
| 227 | + |
| 228 | + if members.Assignment.Topics != nil { |
| 229 | + topicsList := []interface{}{} |
| 230 | + for _, topics := range members.Assignment.Topics { |
| 231 | + topicsMap := map[string]interface{}{} |
| 232 | + |
| 233 | + if topics.Topic != nil { |
| 234 | + topicsMap["topic"] = topics.Topic |
| 235 | + } |
| 236 | + |
| 237 | + if topics.Partitions != nil { |
| 238 | + topicsMap["partitions"] = topics.Partitions |
| 239 | + } |
| 240 | + |
| 241 | + topicsList = append(topicsList, topicsMap) |
| 242 | + } |
| 243 | + |
| 244 | + assignmentMap["topics"] = topicsList |
| 245 | + } |
| 246 | + |
| 247 | + membersMap["assignment"] = []interface{}{assignmentMap} |
| 248 | + } |
| 249 | + |
| 250 | + membersList = append(membersList, membersMap) |
| 251 | + } |
| 252 | + |
| 253 | + groupInfoResponseMap["members"] = membersList |
| 254 | + } |
| 255 | + |
| 256 | + if groupInfoResponse.Group != nil { |
| 257 | + groupInfoResponseMap["group"] = groupInfoResponse.Group |
| 258 | + ids = append(ids, *groupInfoResponse.Group) |
| 259 | + } |
| 260 | + |
| 261 | + tmpList = append(tmpList, groupInfoResponseMap) |
| 262 | + } |
| 263 | + |
| 264 | + _ = d.Set("result", tmpList) |
| 265 | + } |
| 266 | + |
| 267 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 268 | + output, ok := d.GetOk("result_output_file") |
| 269 | + if ok && output.(string) != "" { |
| 270 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 271 | + return e |
| 272 | + } |
| 273 | + } |
| 274 | + return nil |
| 275 | +} |
0 commit comments