yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/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 aliyun
    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  	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 SElasticSearch struct {
    32  	multicloud.SVirtualResourceBase
    33  	multicloud.SBillingBase
    34  	region *SRegion
    35  
    36  	Tags []struct {
    37  		TagKey   string
    38  		TagValue string
    39  	}
    40  	AdvancedDedicateMaster bool
    41  	AdvancedSetting        struct {
    42  		gcName string
    43  	}
    44  	AliwsDicts []struct {
    45  		FileSize   int
    46  		Name       string
    47  		SourceType string
    48  		Type       string
    49  	}
    50  	ClientNodeConfiguration struct {
    51  		Amount   int
    52  		Disk     int
    53  		DiskType string
    54  		Spec     string
    55  	}
    56  	InstanceCategory string
    57  	CreatedAt        time.Time
    58  	DedicateMaster   bool
    59  	Description      string
    60  	DictList         []struct {
    61  		FileSize   int
    62  		Name       string
    63  		SourceType string
    64  		Type       string
    65  	}
    66  	Domain                       string
    67  	ElasticDataNodeConfiguration struct {
    68  		Amount   int
    69  		Disk     int
    70  		DiskType string
    71  		Spec     string
    72  	}
    73  	EnableKibanaPrivateNetwork bool
    74  	EnableKibanaPublicNetwork  bool
    75  	EnablePublic               bool
    76  	EsConfig                   map[string]string
    77  	EsIPBlacklist              []string
    78  	EsIPWhitelist              []string
    79  	EsVersion                  string
    80  	ExtendConfigs              []struct {
    81  		ConfigType      string
    82  		Value           string
    83  		MaintainEndTime string
    84  		AliVersion      string
    85  	}
    86  	HaveClientNode      bool
    87  	HaveKibana          bool
    88  	InstanceId          string
    89  	KibanaConfiguration struct {
    90  		Amount int
    91  		Spec   string
    92  	}
    93  	KibanaDomain             string
    94  	KibanaIPWhitelist        []string
    95  	KibanaPort               int
    96  	KibanaPrivateIPWhitelist []string
    97  	MasterConfiguration      struct {
    98  		Amount   int
    99  		Disk     int
   100  		DiskType string
   101  		Spec     string
   102  	}
   103  	NetworkConfig struct {
   104  		Type      string
   105  		VpcId     string
   106  		VsArea    string
   107  		VswitchId string
   108  	}
   109  	NodeAmount int
   110  	NodeSpec   struct {
   111  		Disk           int
   112  		DiskEncryption bool
   113  		DiskType       string
   114  		Spec           string
   115  	}
   116  	PaymentType               string
   117  	Port                      int
   118  	PrivateNetworkIpWhiteList []string
   119  	Protocol                  string
   120  	PublicDomain              string
   121  	PublicIpWhitelist         []string
   122  	PublicPort                int
   123  	ResourceGroupId           string
   124  	Status                    string
   125  	SynonymsDicts             []struct {
   126  		FileSize   int
   127  		Name       string
   128  		SourceType string
   129  		Type       string
   130  	}
   131  	UpdatedAt             time.Time
   132  	VpcInstanceId         string
   133  	WarmNode              bool
   134  	WarmNodeConfiguration struct {
   135  		Amount         int
   136  		Disk           int
   137  		DiskEncryption bool
   138  		DiskType       string
   139  		Spec           string
   140  	}
   141  	ZoneCount int
   142  	ZoneInfos []struct {
   143  		status string
   144  		zoneId string
   145  	}
   146  }
   147  
   148  func (self *SElasticSearch) GetTags() (map[string]string, error) {
   149  	ret := map[string]string{}
   150  	for _, tag := range self.Tags {
   151  		if strings.HasPrefix(tag.TagKey, "aliyun") || strings.HasPrefix(tag.TagKey, "acs:") {
   152  			continue
   153  		}
   154  		if len(tag.TagKey) > 0 {
   155  			ret[tag.TagKey] = tag.TagValue
   156  		}
   157  	}
   158  	return ret, nil
   159  }
   160  
   161  func (self *SElasticSearch) GetSysTags() map[string]string {
   162  	ret := map[string]string{}
   163  	for _, tag := range self.Tags {
   164  		if strings.HasPrefix(tag.TagKey, "aliyun") || strings.HasPrefix(tag.TagKey, "acs:") {
   165  			if len(tag.TagKey) > 0 {
   166  				ret[tag.TagKey] = tag.TagValue
   167  			}
   168  		}
   169  	}
   170  	return ret
   171  }
   172  
   173  func (self *SElasticSearch) SetTags(tags map[string]string, replace bool) error {
   174  	return errors.Wrap(cloudprovider.ErrNotImplemented, "SetTags")
   175  }
   176  
   177  func (self *SElasticSearch) GetId() string {
   178  	return self.InstanceId
   179  }
   180  
   181  func (self *SElasticSearch) GetGlobalId() string {
   182  	return self.InstanceId
   183  }
   184  
   185  func (self *SElasticSearch) GetName() string {
   186  	if len(self.Description) > 0 {
   187  		return self.Description
   188  	}
   189  	return self.InstanceId
   190  }
   191  
   192  func (self *SElasticSearch) GetDiskSizeGb() int {
   193  	return self.NodeSpec.Disk
   194  }
   195  
   196  func (self *SElasticSearch) GetStorageType() string {
   197  	return self.NodeSpec.DiskType
   198  }
   199  
   200  func (self *SElasticSearch) GetCategory() string {
   201  	return self.InstanceCategory
   202  }
   203  
   204  func (self *SElasticSearch) GetVersion() string {
   205  	return strings.Split(self.EsVersion, "_")[0]
   206  }
   207  
   208  func (self *SElasticSearch) GetVpcId() string {
   209  	return self.NetworkConfig.VpcId
   210  }
   211  
   212  func (self *SElasticSearch) GetNetworkId() string {
   213  	return self.NetworkConfig.VswitchId
   214  }
   215  
   216  func (self *SElasticSearch) GetZoneId() string {
   217  	return self.NetworkConfig.VsArea
   218  }
   219  
   220  func (self *SElasticSearch) IsMultiAz() bool {
   221  	return self.ZoneCount > 1
   222  }
   223  
   224  func (self *SElasticSearch) GetVcpuCount() int {
   225  	spec, ok := esSpec[self.NodeSpec.Spec]
   226  	if ok {
   227  		return spec.VcpuCount
   228  	}
   229  	return 0
   230  }
   231  
   232  func (self *SElasticSearch) GetVmemSizeGb() int {
   233  	spec, ok := esSpec[self.NodeSpec.Spec]
   234  	if ok {
   235  		return spec.VmemSizeGb
   236  	}
   237  	return 0
   238  }
   239  
   240  func (self *SElasticSearch) GetInstanceType() string {
   241  	return self.NodeSpec.Spec
   242  }
   243  
   244  func (self *SElasticSearch) Refresh() error {
   245  	es, err := self.region.GetElasitcSearch(self.InstanceId)
   246  	if err != nil {
   247  		return err
   248  	}
   249  	return jsonutils.Update(self, es)
   250  }
   251  
   252  func (self *SElasticSearch) GetCreatedAt() time.Time {
   253  	return self.CreatedAt
   254  }
   255  
   256  func (self *SElasticSearch) GetBillingType() string {
   257  	return self.PaymentType
   258  }
   259  
   260  func (self *SElasticSearch) GetProjectId() string {
   261  	return self.ResourceGroupId
   262  }
   263  
   264  func (self *SElasticSearch) GetStatus() string {
   265  	switch self.Status {
   266  	case "active":
   267  		return api.ELASTIC_SEARCH_STATUS_AVAILABLE
   268  	case "activating":
   269  		return api.ELASITC_SEARCH_STATUS_CREATING
   270  	case "inactive":
   271  		return api.ELASTIC_SEARCH_STATUS_UNAVAILABLE
   272  	case "invalid":
   273  		return api.ELASTIC_SEARCH_STATUS_DELETING
   274  	default:
   275  		return self.Status
   276  	}
   277  }
   278  
   279  func (self *SElasticSearch) GetAccessInfo() (*cloudprovider.ElasticSearchAccessInfo, error) {
   280  	return &cloudprovider.ElasticSearchAccessInfo{
   281  		Domain:        self.PublicDomain,
   282  		PrivateDomain: self.Domain,
   283  		Port:          self.PublicPort,
   284  		PrivatePort:   self.Port,
   285  		KibanaUrl:     self.KibanaDomain,
   286  	}, nil
   287  }
   288  
   289  func (self *SRegion) GetIElasticSearchs() ([]cloudprovider.ICloudElasticSearch, error) {
   290  	ret := []SElasticSearch{}
   291  	for {
   292  		part, total, err := self.GetElasticSearchs(100, len(ret)/100+1)
   293  		if err != nil {
   294  			return nil, errors.Wrapf(err, "GetElasitcSearchs")
   295  		}
   296  		ret = append(ret, part...)
   297  		if len(ret) >= total {
   298  			break
   299  		}
   300  	}
   301  	result := []cloudprovider.ICloudElasticSearch{}
   302  	for i := range ret {
   303  		ret[i].region = self
   304  		result = append(result, &ret[i])
   305  	}
   306  	return result, nil
   307  }
   308  
   309  func (self *SRegion) GetIElasticSearchById(id string) (cloudprovider.ICloudElasticSearch, error) {
   310  	es, err := self.GetElasitcSearch(id)
   311  	if err != nil {
   312  		return nil, err
   313  	}
   314  	return es, nil
   315  }
   316  
   317  func (self *SRegion) GetElasticSearchs(size, page int) ([]SElasticSearch, int, error) {
   318  	if size < 1 || size > 100 {
   319  		size = 100
   320  	}
   321  	if page < 1 {
   322  		page = 1
   323  	}
   324  	params := map[string]string{
   325  		"PathPattern": "/openapi/instances",
   326  		"size":        fmt.Sprintf("%d", size),
   327  		"page":        fmt.Sprintf("%d", page),
   328  	}
   329  	resp, err := self.esRequest("ListInstance", params)
   330  	if err != nil {
   331  		return nil, 0, errors.Wrapf(err, "ListInstance")
   332  	}
   333  	ret := []SElasticSearch{}
   334  	err = resp.Unmarshal(&ret, "Result")
   335  	if err != nil {
   336  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
   337  	}
   338  	totalCount, _ := resp.Int("Headers", "X-Total-Count")
   339  	return ret, int(totalCount), nil
   340  }
   341  
   342  func (self *SRegion) GetElasitcSearch(id string) (*SElasticSearch, error) {
   343  	if len(id) == 0 {
   344  		return nil, errors.Wrapf(cloudprovider.ErrNotFound, "empty id")
   345  	}
   346  	params := map[string]string{
   347  		"PathPattern": fmt.Sprintf("/openapi/instances/%s", id),
   348  	}
   349  	resp, err := self.esRequest("DescribeInstance", params)
   350  	if err != nil {
   351  		return nil, errors.Wrapf(err, "DescribeInstance")
   352  	}
   353  	ret := SElasticSearch{region: self}
   354  	err = resp.Unmarshal(&ret, "Result")
   355  	if err != nil {
   356  		return nil, errors.Wrapf(err, "resp.Unmarshal")
   357  	}
   358  	return &ret, nil
   359  }
   360  
   361  func (self *SElasticSearch) Delete() error {
   362  	return self.region.DeleteElasticSearch(self.InstanceId)
   363  }
   364  
   365  func (self *SRegion) DeleteElasticSearch(id string) error {
   366  	params := map[string]string{
   367  		"clientToken": utils.GenRequestId(20),
   368  		"deleteType":  "immediate",
   369  		"PathPattern": fmt.Sprintf("/openapi/instances/%s", id),
   370  	}
   371  	_, err := self.esRequest("DeleteInstance", params)
   372  	return errors.Wrapf(err, "DeleteInstance")
   373  }