yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/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 qcloud 16 17 import ( 18 "fmt" 19 "strings" 20 "time" 21 22 "yunion.io/x/jsonutils" 23 "yunion.io/x/pkg/errors" 24 "yunion.io/x/pkg/utils" 25 26 billing_api "yunion.io/x/cloudmux/pkg/apis/billing" 27 api "yunion.io/x/cloudmux/pkg/apis/compute" 28 "yunion.io/x/cloudmux/pkg/cloudprovider" 29 "yunion.io/x/cloudmux/pkg/multicloud" 30 ) 31 32 type SKafka struct { 33 multicloud.SVirtualResourceBase 34 multicloud.SBillingBase 35 QcloudTags 36 region *SRegion 37 38 InstanceId string 39 InstanceName string 40 Vip string 41 Vport string 42 VipList []struct { 43 Vip string 44 Vport string 45 } 46 Status int 47 Bandwidth int 48 DiskSize int 49 ZoneId int 50 VpcId string 51 SubnetId string 52 RenewFlag int 53 Healthy int 54 HealthyMessage string 55 CreateTime int 56 ExpireTime int 57 IsInternal int 58 TopicNum int 59 Version string 60 ZoneIds []int 61 Cvm int 62 InstanceType string 63 DiskType string 64 MaxTopicNumber int 65 MaxPartitionNubmer int 66 RebalanceTime string 67 MsgRetentionTime int 68 } 69 70 func (self *SKafka) GetName() string { 71 if len(self.InstanceName) > 0 { 72 return self.InstanceName 73 } 74 return self.InstanceId 75 } 76 77 func (self *SKafka) GetId() string { 78 return self.InstanceId 79 } 80 81 func (self *SKafka) GetGlobalId() string { 82 return self.InstanceId 83 } 84 85 func (self *SKafka) GetVpcId() string { 86 return self.VpcId 87 } 88 89 func (self *SKafka) GetNetworkId() string { 90 return self.SubnetId 91 } 92 93 func (self *SKafka) IsAutoRenew() bool { 94 return self.RenewFlag == 1 95 } 96 97 func (self *SKafka) GetBillingType() string { 98 return billing_api.BILLING_TYPE_PREPAID 99 } 100 101 func (self *SKafka) GetCreatedAt() time.Time { 102 return time.Unix(int64(self.CreateTime), 0) 103 } 104 105 func (self *SKafka) GetExpiredAt() time.Time { 106 return time.Unix(int64(self.ExpireTime), 0) 107 } 108 109 func (self *SKafka) GetInstanceType() string { 110 return self.InstanceType 111 } 112 113 func (self *SKafka) GetDiskSizeGb() int { 114 return self.DiskSize 115 } 116 117 func (self *SKafka) GetVersion() string { 118 return self.Version 119 } 120 121 func (self *SKafka) IsMultiAz() bool { 122 return len(self.ZoneIds) > 1 123 } 124 125 func (self *SKafka) GetBandwidthMb() int { 126 return self.Bandwidth / 8 127 } 128 129 func (self *SKafka) GetStorageType() string { 130 return self.DiskType 131 } 132 133 func (self *SKafka) GetEndpoint() string { 134 endpoints := []string{} 135 var add = func(vip, vport string) { 136 endpoint := fmt.Sprintf("%s:%s", vip, vport) 137 if len(vip) > 0 && len(vport) > 0 && !utils.IsInStringArray(endpoint, endpoints) { 138 endpoints = append(endpoints, endpoint) 139 } 140 } 141 for _, ed := range self.VipList { 142 add(ed.Vip, ed.Vport) 143 } 144 add(self.Vip, self.Vport) 145 return strings.Join(endpoints, ",") 146 } 147 148 func (self *SKafka) GetStatus() string { 149 switch self.Status { 150 case 0: 151 return api.KAFKA_STATUS_CREATING 152 case 1: 153 return api.KAFKA_STATUS_AVAILABLE 154 case 2: 155 return api.KAFKA_STATUS_DELETING 156 default: 157 return fmt.Sprintf("%d", self.Status) 158 } 159 } 160 161 func (self *SKafka) GetMsgRetentionMinute() int { 162 if self.MsgRetentionTime == 0 { 163 self.Refresh() 164 } 165 return self.MsgRetentionTime 166 } 167 168 func (self *SKafka) GetZoneId() string { 169 return fmt.Sprintf("%s-%d", self.region.Region, self.ZoneId) 170 } 171 172 func (self *SKafka) Refresh() error { 173 kafka, err := self.region.GetKafka(self.InstanceId) 174 if err != nil { 175 return err 176 } 177 return jsonutils.Update(self, kafka) 178 } 179 180 func (self *SKafka) Delete() error { 181 return cloudprovider.ErrNotSupported 182 } 183 184 func (self *SRegion) GetICloudKafkaById(id string) (cloudprovider.ICloudKafka, error) { 185 kafka, err := self.GetKafka(id) 186 if err != nil { 187 return nil, errors.Wrapf(err, "GetKafka(%s)", id) 188 } 189 return kafka, nil 190 } 191 192 func (self *SRegion) GetICloudKafkas() ([]cloudprovider.ICloudKafka, error) { 193 kafkas := []SKafka{} 194 for { 195 part, total, err := self.GetKafkas("", 20, len(kafkas)) 196 if err != nil { 197 return nil, errors.Wrapf(err, "GetKafkas") 198 } 199 kafkas = append(kafkas, part...) 200 if len(kafkas) >= total { 201 break 202 } 203 } 204 ret := []cloudprovider.ICloudKafka{} 205 for i := range kafkas { 206 kafkas[i].region = self 207 ret = append(ret, &kafkas[i]) 208 } 209 return ret, nil 210 } 211 212 func (self *SRegion) GetKafka(id string) (*SKafka, error) { 213 params := map[string]string{ 214 "InstanceId": id, 215 } 216 resp, err := self.kafkaRequest("DescribeInstanceAttributes", params) 217 if err != nil { 218 return nil, errors.Wrapf(err, "DescribeInstanceAttributes") 219 } 220 ret := SKafka{region: self} 221 err = resp.Unmarshal(&ret, "Result") 222 if err != nil { 223 return nil, errors.Wrapf(err, "resp.Unmarshal") 224 } 225 return &ret, nil 226 } 227 228 func (self *SRegion) GetKafkas(id string, limit, offset int) ([]SKafka, int, error) { 229 if limit < 1 || limit > 20 { 230 limit = 20 231 } 232 params := map[string]string{ 233 "Limit": fmt.Sprintf("%d", limit), 234 "Offset": fmt.Sprintf("%d", offset), 235 } 236 if len(id) > 0 { 237 params["InstanceId"] = id 238 } 239 resp, err := self.kafkaRequest("DescribeInstancesDetail", params) 240 if err != nil { 241 return nil, 0, errors.Wrapf(err, "DescribeInstancesDetail") 242 } 243 ret := []SKafka{} 244 err = resp.Unmarshal(&ret, "Result", "InstanceList") 245 if err != nil { 246 return nil, 0, errors.Wrapf(err, "resp.Unmarshal") 247 } 248 totalCount, _ := resp.Int("Result", "TotalCount") 249 return ret, int(totalCount), nil 250 }