yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/kafka_topic.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package qcloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  )
    24  
    25  func (self *SKafka) GetTopics() ([]cloudprovider.SKafkaTopic, error) {
    26  	result := []cloudprovider.SKafkaTopic{}
    27  	for {
    28  		part, total, err := self.region.GetKafkaTopics(self.InstanceId, 50, len(result))
    29  		if err != nil {
    30  			return nil, errors.Wrapf(err, "GetKafkaTopics")
    31  		}
    32  		result = append(result, part...)
    33  		if len(result) >= total {
    34  			break
    35  		}
    36  	}
    37  	return result, nil
    38  }
    39  
    40  func (self *SRegion) GetKafkaTopics(insId string, limit, offset int) ([]cloudprovider.SKafkaTopic, int, error) {
    41  	if limit < 1 || limit > 50 {
    42  		limit = 50
    43  	}
    44  	params := map[string]string{
    45  		"InstanceId": insId,
    46  		"Limit":      fmt.Sprintf("%d", limit),
    47  		"Offset":     fmt.Sprintf("%d", offset),
    48  	}
    49  	resp, err := self.kafkaRequest("DescribeTopic", params)
    50  	if err != nil {
    51  		return nil, 0, errors.Wrapf(err, "DescribeTopic")
    52  	}
    53  	result := struct {
    54  		Result struct {
    55  			TopicList []struct {
    56  				TopicId   string
    57  				TopicName string
    58  				Note      string
    59  			}
    60  			TotalCount float64
    61  		}
    62  	}{}
    63  	err = resp.Unmarshal(&result)
    64  	if err != nil {
    65  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
    66  	}
    67  	ret := []cloudprovider.SKafkaTopic{}
    68  	for _, t := range result.Result.TopicList {
    69  		ret = append(ret, cloudprovider.SKafkaTopic{
    70  			Id:          t.TopicId,
    71  			Name:        t.TopicName,
    72  			Description: t.Note,
    73  		})
    74  	}
    75  	return ret, int(result.Result.TotalCount), nil
    76  }