yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/elastic_search.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  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  )
    29  
    30  type SElasticSearch struct {
    31  	multicloud.SVirtualResourceBase
    32  	multicloud.SBillingBase
    33  	QcloudTags
    34  	region *SRegion
    35  
    36  	InstanceId   string
    37  	InstanceName string
    38  	Region       string
    39  	Zone         string
    40  	AppId        int
    41  	Uin          string
    42  	VpcUid       string
    43  	SubnetUid    string
    44  	Status       int
    45  	ChargeType   string
    46  	ChargePeriod int
    47  	RenewFlag    string
    48  	NodeType     string
    49  	NodeNum      int
    50  	CpuNum       int
    51  	MemSize      int
    52  	DiskType     string
    53  	DiskSize     int
    54  	EsDomain     string
    55  	EsVip        string
    56  	EsPort       int
    57  	KibanaUrl    string
    58  	EsVersion    string
    59  	EsConfig     string
    60  	EsAcl        struct {
    61  		BlackIpList []string
    62  		WhiteIpList []string
    63  	}
    64  	CreateTime   time.Time
    65  	UpdateTime   time.Time
    66  	Deadline     string
    67  	InstanceType int
    68  	IkConfig     struct {
    69  		MainDict []struct {
    70  			Key  string
    71  			Name string
    72  			Size int
    73  		}
    74  		Stopwords []struct {
    75  			Key  string
    76  			Name string
    77  			Size int
    78  		}
    79  		QQDict []struct {
    80  			Key  string
    81  			Name string
    82  			Size int
    83  		}
    84  		Synonym []struct {
    85  			Key  string
    86  			Name string
    87  			Size int
    88  		}
    89  		UpdateType string
    90  	}
    91  	MasterNodeInfo struct {
    92  		EnableDedicatedMaster bool
    93  		MasterNodeType        string
    94  		MasterNodeNum         int
    95  		MasterNodeCpuNum      int
    96  		MasterNodeMemSize     int
    97  		MasterNodeDiskSize    int
    98  		MasterNodeDiskType    string
    99  	}
   100  	CosBackup struct {
   101  		IsAutoBackup bool
   102  		BackupTime   string
   103  	}
   104  	AllowCosBackup    bool
   105  	LicenseType       string
   106  	EnableHotWarmMode bool
   107  	WarmNodeType      string
   108  	WarmNodeNum       int
   109  	WarmCpuNum        int
   110  	WarmMemSize       int
   111  	WarmDiskType      string
   112  	WarmDiskSize      int
   113  	NodeInfoList      []struct {
   114  		NodeNum       int
   115  		NodeType      string
   116  		Type          string
   117  		DiskType      string
   118  		DiskSize      int
   119  		LocalDiskInfo struct {
   120  			LocalDiskType  string
   121  			LocalDiskSize  int
   122  			LocalDiskCount int
   123  		}
   124  		DiskCount   int
   125  		DiskEncrypt int
   126  	}
   127  	EsPublicUrl   string
   128  	MultiZoneInfo []struct {
   129  		Zone     string
   130  		SubnetId string
   131  	}
   132  	DeployMode   int
   133  	PublicAccess string
   134  	EsPublicAcl  struct {
   135  		BlackIpList []string
   136  		WhiteIpList []string
   137  	}
   138  	KibanaPrivateUrl    string
   139  	KibanaPublicAccess  string
   140  	KibanaPrivateAccess string
   141  	SecurityType        int
   142  	SceneType           int
   143  	KibanaConfig        string
   144  	KibanaNodeInfo      struct {
   145  		KibanaNodeType     string
   146  		KibanaNodeNum      int
   147  		KibanaNodeCpuNum   int
   148  		KibanaNodeMemSize  int
   149  		KibanaNodeDiskType string
   150  		KibanaNodeDiskSize int
   151  	}
   152  }
   153  
   154  func (self *SElasticSearch) GetId() string {
   155  	return self.InstanceId
   156  }
   157  
   158  func (self *SElasticSearch) GetGlobalId() string {
   159  	return self.InstanceId
   160  }
   161  
   162  func (self *SElasticSearch) GetName() string {
   163  	if len(self.InstanceName) > 0 {
   164  		return self.InstanceName
   165  	}
   166  	return self.InstanceId
   167  }
   168  
   169  func (self *SElasticSearch) GetVersion() string {
   170  	return self.EsVersion
   171  }
   172  
   173  func (self *SElasticSearch) Refresh() error {
   174  	es, err := self.region.GetElasticSearch(self.InstanceId)
   175  	if err != nil {
   176  		return err
   177  	}
   178  	return jsonutils.Update(self, es)
   179  }
   180  
   181  func (self *SElasticSearch) GetDiskSizeGb() int {
   182  	return self.DiskSize
   183  }
   184  
   185  func (self *SElasticSearch) GetStorageType() string {
   186  	return self.DiskType
   187  }
   188  
   189  func (self *SElasticSearch) GetCategory() string {
   190  	return self.LicenseType
   191  }
   192  
   193  func (self *SElasticSearch) GetCreatedAt() time.Time {
   194  	return self.CreateTime
   195  }
   196  
   197  func (self *SElasticSearch) GetVpcId() string {
   198  	return self.VpcUid
   199  }
   200  
   201  func (self *SElasticSearch) GetNetworkId() string {
   202  	for _, zone := range self.MultiZoneInfo {
   203  		if len(zone.SubnetId) > 0 {
   204  			return zone.SubnetId
   205  		}
   206  	}
   207  	return self.SubnetUid
   208  }
   209  
   210  func (self *SElasticSearch) GetInstanceType() string {
   211  	return self.NodeType
   212  }
   213  
   214  func (self *SElasticSearch) GetVcpuCount() int {
   215  	return self.CpuNum
   216  }
   217  
   218  func (self *SElasticSearch) GetVmemSizeGb() int {
   219  	return self.MemSize
   220  }
   221  
   222  func (self *SElasticSearch) GetZoneId() string {
   223  	for _, zone := range self.MultiZoneInfo {
   224  		if len(zone.Zone) > 0 {
   225  			return zone.Zone
   226  		}
   227  	}
   228  	return self.Zone
   229  }
   230  
   231  func (self *SElasticSearch) IsMultiAz() bool {
   232  	return len(self.MultiZoneInfo) > 1
   233  }
   234  
   235  func (self *SElasticSearch) GetBillingType() string {
   236  	switch self.ChargeType {
   237  	case "PREPAID", "CDHPAID":
   238  		return billing_api.BILLING_TYPE_PREPAID
   239  	case "POSTPAID_BY_HOUR":
   240  		return billing_api.BILLING_TYPE_POSTPAID
   241  	}
   242  	return billing_api.BILLING_TYPE_POSTPAID
   243  }
   244  
   245  func (self *SElasticSearch) GetStatus() string {
   246  	switch self.Status {
   247  	case 0:
   248  		return api.ELASITC_SEARCH_STATUS_CREATING
   249  	case 1:
   250  		return api.ELASTIC_SEARCH_STATUS_AVAILABLE
   251  	case -1:
   252  		return api.ELASTIC_SEARCH_STATUS_UNAVAILABLE
   253  	case -2, -3:
   254  		return api.ELASTIC_SEARCH_STATUS_DELETING
   255  	}
   256  	return fmt.Sprintf("%d", self.Status)
   257  }
   258  
   259  func (self *SElasticSearch) IsAutoRenew() bool {
   260  	return self.RenewFlag == "NOTIFY_AND_AUTO_RENEW"
   261  }
   262  
   263  func (self *SElasticSearch) Delete() error {
   264  	return self.region.DeleteElasticSearch(self.InstanceId)
   265  }
   266  
   267  func (self *SElasticSearch) GetAccessInfo() (*cloudprovider.ElasticSearchAccessInfo, error) {
   268  	return &cloudprovider.ElasticSearchAccessInfo{
   269  		Port:             self.EsPort,
   270  		Vip:              self.EsVip,
   271  		Domain:           self.EsPublicUrl,
   272  		PrivateDomain:    self.EsDomain,
   273  		KibanaUrl:        self.KibanaUrl,
   274  		KibanaPrivateUrl: self.KibanaPrivateUrl,
   275  	}, nil
   276  }
   277  
   278  func (self *SRegion) GetIElasticSearchs() ([]cloudprovider.ICloudElasticSearch, error) {
   279  	ess := []SElasticSearch{}
   280  	for {
   281  		part, total, err := self.GetElasticSearchs(nil, 100, len(ess))
   282  		if err != nil {
   283  			return nil, errors.Wrapf(err, "GetElasticSearchs")
   284  		}
   285  		ess = append(ess, part...)
   286  		if len(ess) >= total {
   287  			break
   288  		}
   289  	}
   290  	ret := []cloudprovider.ICloudElasticSearch{}
   291  	for i := range ess {
   292  		ess[i].region = self
   293  		ret = append(ret, &ess[i])
   294  	}
   295  	return ret, nil
   296  }
   297  
   298  func (self *SRegion) GetIElasticSearchById(id string) (cloudprovider.ICloudElasticSearch, error) {
   299  	es, err := self.GetElasticSearch(id)
   300  	if err != nil {
   301  		return nil, errors.Wrapf(err, "GetElasitcSearch")
   302  	}
   303  	return es, nil
   304  }
   305  
   306  func (self *SRegion) GetElasticSearch(id string) (*SElasticSearch, error) {
   307  	if len(id) == 0 {
   308  		return nil, cloudprovider.ErrNotFound
   309  	}
   310  	ret, _, err := self.GetElasticSearchs([]string{id}, 1, 0)
   311  	if err != nil {
   312  		return nil, errors.Wrapf(err, "GetElasticSearchs")
   313  	}
   314  	for i := range ret {
   315  		ret[i].region = self
   316  		return &ret[i], nil
   317  	}
   318  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   319  }
   320  
   321  func (self *SRegion) GetElasticSearchs(ids []string, limit, offset int) ([]SElasticSearch, int, error) {
   322  	if limit < 1 || limit > 100 {
   323  		limit = 100
   324  	}
   325  	params := map[string]string{
   326  		"Limit":  fmt.Sprintf("%d", limit),
   327  		"Offset": fmt.Sprintf("%d", offset),
   328  	}
   329  	for i, id := range ids {
   330  		params[fmt.Sprintf("InstanceIds.%d", i)] = id
   331  	}
   332  	resp, err := self.esRequest("DescribeInstances", params)
   333  	if err != nil {
   334  		return nil, 0, errors.Wrapf(err, "DescribeInstances")
   335  	}
   336  	ret := []SElasticSearch{}
   337  	err = resp.Unmarshal(&ret, "InstanceList")
   338  	if err != nil {
   339  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
   340  	}
   341  	totalCount, _ := resp.Float("TotalCount")
   342  	return ret, int(totalCount), nil
   343  }
   344  
   345  func (self *SRegion) DeleteElasticSearch(id string) error {
   346  	params := map[string]string{
   347  		"InstanceId": id,
   348  	}
   349  	_, err := self.esRequest("DeleteInstance", params)
   350  	return errors.Wrapf(err, "DeleteDBInstance")
   351  }