yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/kafka.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 aliyun
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    24  
    25  	billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  )
    30  
    31  type SKafka struct {
    32  	multicloud.SBillingBase
    33  	multicloud.SVirtualResourceBase
    34  	AliyunTags
    35  	region *SRegion
    36  
    37  	AllConfig                string `json:"AllConfig"`
    38  	DeployType               int    `json:"DeployType"`
    39  	SpecType                 string `json:"SpecType"`
    40  	PaidType                 int    `json:"PaidType"`
    41  	InstanceId               string `json:"InstanceId"`
    42  	MsgRetain                int    `json:"MsgRetain"`
    43  	ZoneId                   string `json:"ZoneId"`
    44  	IoMax                    int    `json:"IoMax"`
    45  	VSwitchId                string `json:"VSwitchId"`
    46  	VpcId                    string `json:"VpcId"`
    47  	UpgradeServiceDetailInfo struct {
    48  		Current2OpenSourceVersion string `json:"Current2OpenSourceVersion"`
    49  	} `json:"UpgradeServiceDetailInfo"`
    50  	ServiceStatus int    `json:"ServiceStatus"`
    51  	Name          string `json:"Name"`
    52  	TopicNumLimit int    `json:"TopicNumLimit"`
    53  	DiskSize      int    `json:"DiskSize"`
    54  	RegionId      string `json:"RegionId"`
    55  	CreateTime    int64  `json:"CreateTime"`
    56  	SslEndPoint   string `json:"SslEndPoint"`
    57  	EipMax        int    `json:"EipMax"`
    58  	EndPoint      string `json:"EndPoint"`
    59  	ExpiredTime   int64  `json:"ExpiredTime"`
    60  	DiskType      int    `json:"DiskType"`
    61  	SecurityGroup string `json:"SecurityGroup"`
    62  }
    63  
    64  func (self *SKafka) GetName() string {
    65  	return self.Name
    66  }
    67  
    68  func (self *SKafka) GetGlobalId() string {
    69  	return self.InstanceId
    70  }
    71  
    72  func (self *SKafka) GetId() string {
    73  	return self.InstanceId
    74  }
    75  
    76  func (self *SKafka) GetVpcId() string {
    77  	return self.VpcId
    78  }
    79  
    80  func (self *SKafka) GetNetworkId() string {
    81  	return self.VSwitchId
    82  }
    83  
    84  func (self *SKafka) GetCreatedAt() time.Time {
    85  	return time.Unix(self.CreateTime/1000, self.CreateTime%1000)
    86  }
    87  
    88  func (self *SKafka) GetBillingType() string {
    89  	if self.PaidType == 0 {
    90  		return billing_api.BILLING_TYPE_PREPAID
    91  	}
    92  	return billing_api.BILLING_TYPE_POSTPAID
    93  }
    94  
    95  func (self *SKafka) GetInstanceType() string {
    96  	return self.SpecType
    97  }
    98  
    99  func (self *SKafka) GetDiskSizeGb() int {
   100  	return self.DiskSize
   101  }
   102  
   103  func (self *SKafka) GetVersion() string {
   104  	return self.UpgradeServiceDetailInfo.Current2OpenSourceVersion
   105  }
   106  
   107  func (self *SKafka) IsMultiAz() bool {
   108  	return false
   109  }
   110  
   111  func (self *SKafka) GetStorageType() string {
   112  	switch self.DiskType {
   113  	case 0:
   114  		return api.STORAGE_CLOUD_EFFICIENCY
   115  	case 1:
   116  		return api.STORAGE_CLOUD_SSD
   117  	}
   118  	return ""
   119  }
   120  
   121  func (self *SKafka) GetBandwidthMb() int {
   122  	if self.EipMax > 0 {
   123  		return self.EipMax
   124  	}
   125  	return self.IoMax
   126  }
   127  
   128  func (self *SKafka) GetEndpoint() string {
   129  	ret := []string{}
   130  	if len(self.EndPoint) > 0 {
   131  		ret = append(ret, self.EndPoint)
   132  	}
   133  	if len(self.SslEndPoint) > 0 {
   134  		ret = append(ret, self.SslEndPoint)
   135  	}
   136  	return strings.Join(ret, ",")
   137  }
   138  
   139  func (self *SKafka) GetMsgRetentionMinute() int {
   140  	return self.MsgRetain * 60
   141  }
   142  
   143  func (self *SKafka) GetZoneId() string {
   144  	if len(self.ZoneId) > 0 {
   145  		return fmt.Sprintf("%s-%s", self.RegionId, strings.TrimPrefix(self.ZoneId, "zone"))
   146  	}
   147  	return ""
   148  }
   149  
   150  func (self *SKafka) GetStatus() string {
   151  	switch self.ServiceStatus {
   152  	case 0, 1, 2:
   153  		return api.KAFKA_STATUS_CREATING
   154  	case 5:
   155  		return api.KAFKA_STATUS_AVAILABLE
   156  	case 15:
   157  		return api.KAFKA_STATUS_UNAVAILABLE
   158  	}
   159  	return api.KAFKA_STATUS_UNKNOWN
   160  }
   161  
   162  func (self *SKafka) Refresh() error {
   163  	kafka, err := self.region.GetKafka(self.InstanceId)
   164  	if err != nil {
   165  		return errors.Wrapf(err, "GetKafka")
   166  	}
   167  	return jsonutils.Update(self, kafka)
   168  }
   169  
   170  func (self *SKafka) Delete() error {
   171  	return self.region.DeleteKafka(self.InstanceId)
   172  }
   173  
   174  func (self *SRegion) GetICloudKafkaById(id string) (cloudprovider.ICloudKafka, error) {
   175  	kafka, err := self.GetKafka(id)
   176  	if err != nil {
   177  		return nil, errors.Wrapf(err, "GetKafka(%s)", id)
   178  	}
   179  	return kafka, nil
   180  }
   181  
   182  func (self *SRegion) GetICloudKafkas() ([]cloudprovider.ICloudKafka, error) {
   183  	kafkas, err := self.GetKafkas(nil)
   184  	if err != nil {
   185  		return nil, errors.Wrapf(err, "GetKafkas")
   186  	}
   187  	ret := []cloudprovider.ICloudKafka{}
   188  	for i := range kafkas {
   189  		kafkas[i].region = self
   190  		ret = append(ret, &kafkas[i])
   191  	}
   192  	return ret, nil
   193  }
   194  
   195  func (self *SRegion) GetKafka(id string) (*SKafka, error) {
   196  	kafkas, err := self.GetKafkas([]string{id})
   197  	if err != nil {
   198  		return nil, errors.Wrapf(err, "GetKafkas")
   199  	}
   200  	for i := range kafkas {
   201  		if kafkas[i].GetGlobalId() == id {
   202  			kafkas[i].region = self
   203  			return &kafkas[i], nil
   204  		}
   205  	}
   206  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   207  }
   208  
   209  func (self *SRegion) GetKafkas(ids []string) ([]SKafka, error) {
   210  	params := map[string]string{}
   211  	for idx, id := range ids {
   212  		params[fmt.Sprintf("InstanceId.%d", idx)] = id
   213  	}
   214  	resp, err := self.kafkaRequest("GetInstanceList", params)
   215  	if err != nil {
   216  		return nil, errors.Wrapf(err, "GetInstanceList")
   217  	}
   218  	ret := struct {
   219  		Code         int
   220  		Message      string
   221  		RequestId    string
   222  		Success      bool
   223  		InstanceList struct {
   224  			InstanceVO []SKafka
   225  		}
   226  	}{}
   227  	err = resp.Unmarshal(&ret)
   228  	if err != nil {
   229  		return nil, errors.Wrapf(err, "resp.Unmarshal")
   230  	}
   231  	if ret.Code != 200 {
   232  		return nil, errors.Errorf("message: %s requestId: %s", ret.Message, ret.RequestId)
   233  	}
   234  	return ret.InstanceList.InstanceVO, nil
   235  }
   236  
   237  func (self *SRegion) DeleteKafka(id string) error {
   238  	params := map[string]string{
   239  		"RegionId":   self.RegionId,
   240  		"InstanceId": id,
   241  	}
   242  	_, err := self.kafkaRequest("DeleteInstance", params)
   243  	return errors.Wrapf(err, "DeleteInstance")
   244  }
   245  
   246  func (self *SRegion) ReleaseKafka(id string) error {
   247  	params := map[string]string{
   248  		"RegionId":            self.RegionId,
   249  		"InstanceId":          id,
   250  		"ForceDeleteInstance": "true",
   251  	}
   252  	_, err := self.kafkaRequest("ReleaseInstance", params)
   253  	return errors.Wrapf(err, "ReleaseInstance")
   254  }
   255  
   256  func (self *SKafka) GetTopics() ([]cloudprovider.SKafkaTopic, error) {
   257  	ret := []cloudprovider.SKafkaTopic{}
   258  	pageSize := 100
   259  	for {
   260  		part, total, err := self.region.GetKafkaTopics(self.InstanceId, len(ret)/pageSize+1, pageSize)
   261  		if err != nil {
   262  			return nil, errors.Wrapf(err, "GetKafkaTopics")
   263  		}
   264  		ret = append(ret, part...)
   265  		if len(ret) >= total {
   266  			break
   267  		}
   268  	}
   269  	return ret, nil
   270  }
   271  
   272  func (self *SRegion) GetKafkaTopics(id string, page int, pageSize int) ([]cloudprovider.SKafkaTopic, int, error) {
   273  	if page < 1 {
   274  		page = 1
   275  	}
   276  	if pageSize < 1 {
   277  		pageSize = 50
   278  	}
   279  	params := map[string]string{
   280  		"InstanceId":  id,
   281  		"CurrentPage": fmt.Sprintf("%d", page),
   282  		"PageSize":    fmt.Sprintf("%d", pageSize),
   283  	}
   284  	resp, err := self.kafkaRequest("GetTopicList", params)
   285  	if err != nil {
   286  		return nil, 0, errors.Wrapf(err, "GetTopicList")
   287  	}
   288  	result := struct {
   289  		Code      int
   290  		Total     int
   291  		Message   string
   292  		TopicList struct {
   293  			TopicVO []struct {
   294  				CompactTopic bool
   295  				CreateTime   int
   296  				InstanceId   string
   297  				LocalTopic   bool
   298  				PartitionNum int
   299  				RegionId     string
   300  				Remark       string
   301  				Status       int
   302  				StatusName   string
   303  				Topic        string
   304  			}
   305  		}
   306  	}{}
   307  	err = resp.Unmarshal(&result)
   308  	if err != nil {
   309  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
   310  	}
   311  	ret := []cloudprovider.SKafkaTopic{}
   312  	for _, topic := range result.TopicList.TopicVO {
   313  		ret = append(ret, cloudprovider.SKafkaTopic{
   314  			Id:          topic.Topic,
   315  			Name:        topic.Topic,
   316  			Description: topic.Remark,
   317  		})
   318  	}
   319  	return ret, result.Total, nil
   320  }