yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/redis.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 azure
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strings"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    24  
    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 SRedisCache struct {
    31  	multicloud.SElasticcacheBase
    32  	AzureTags
    33  
    34  	region     *SRegion
    35  	ID         string `json:"id"`
    36  	Location   string `json:"location"`
    37  	Name       string `json:"name"`
    38  	Type       string `json:"type"`
    39  	Properties struct {
    40  		Provisioningstate string `json:"provisioningState"`
    41  		Redisversion      string `json:"redisVersion"`
    42  		Sku               struct {
    43  			Name     string `json:"name"`
    44  			Family   string `json:"family"`
    45  			Capacity int    `json:"capacity"`
    46  		} `json:"sku"`
    47  		Enablenonsslport bool `json:"enableNonSslPort"`
    48  		Instances        []struct {
    49  			Sslport  int  `json:"sslPort"`
    50  			Shardid  int  `json:"shardId"`
    51  			Ismaster bool `json:"isMaster"`
    52  		} `json:"instances"`
    53  		Publicnetworkaccess string `json:"publicNetworkAccess"`
    54  		Redisconfiguration  struct {
    55  			Maxclients                     string `json:"maxclients"`
    56  			MaxmemoryReserved              string `json:"maxmemory-reserved"`
    57  			MaxfragmentationmemoryReserved string `json:"maxfragmentationmemory-reserved"`
    58  			MaxmemoryDelta                 string `json:"maxmemory-delta"`
    59  		} `json:"redisConfiguration"`
    60  		Accesskeys    interface{}   `json:"accessKeys"`
    61  		Hostname      string        `json:"hostName"`
    62  		Port          int           `json:"port"`
    63  		Sslport       int           `json:"sslPort"`
    64  		Shardcount    int           `json:"shardCount"`
    65  		SubnetId      string        `json:"subnetId"`
    66  		StaticIP      string        `json:"staticIP"`
    67  		Linkedservers []interface{} `json:"linkedServers"`
    68  	} `json:"properties"`
    69  }
    70  
    71  func (self *SRegion) GetRedisCache(id string) (*SRedisCache, error) {
    72  	cache := &SRedisCache{region: self}
    73  	return cache, self.get(id, url.Values{}, cache)
    74  }
    75  
    76  func (self *SRegion) GetRedisCaches() ([]SRedisCache, error) {
    77  	redis := []SRedisCache{}
    78  	err := self.list("Microsoft.Cache/redis", url.Values{}, &redis)
    79  	if err != nil {
    80  		return nil, errors.Wrapf(err, "list")
    81  	}
    82  	return redis, nil
    83  }
    84  
    85  func (self *SRedisCache) GetId() string {
    86  	return self.ID
    87  }
    88  
    89  func (self *SRedisCache) GetName() string {
    90  	return self.Name
    91  }
    92  
    93  func (self *SRedisCache) GetProjectId() string {
    94  	return getResourceGroup(self.ID)
    95  }
    96  
    97  func (self *SRedisCache) GetStatus() string {
    98  	switch self.Properties.Provisioningstate {
    99  	case "Creating":
   100  		return api.ELASTIC_CACHE_STATUS_DEPLOYING
   101  	case "Deleting":
   102  		return api.ELASTIC_CACHE_STATUS_DELETING
   103  	case "Disabled":
   104  		return api.ELASTIC_CACHE_STATUS_INACTIVE
   105  	case "Failed":
   106  		return api.ELASTIC_CACHE_STATUS_CREATE_FAILED
   107  	case "Linking":
   108  		return api.ELASTIC_CACHE_STATUS_RUNNING
   109  	case "Provisioning":
   110  		return api.ELASTIC_CACHE_STATUS_RUNNING
   111  	case "RecoveringScaleFailure":
   112  		return api.ELASTIC_CACHE_STATUS_CHANGE_FAILED
   113  	case "Scaling":
   114  		return api.ELASTIC_CACHE_STATUS_CHANGING
   115  	case "Succeeded":
   116  		return api.ELASTIC_CACHE_STATUS_RUNNING
   117  	case "Unlinking":
   118  		return api.ELASTIC_CACHE_STATUS_RUNNING
   119  	case "Unprovisioning":
   120  		return api.ELASTIC_CACHE_STATUS_RUNNING
   121  	case "Updating":
   122  		return api.ELASTIC_CACHE_STATUS_RUNNING
   123  	default:
   124  		return strings.ToLower(self.Properties.Provisioningstate)
   125  	}
   126  }
   127  
   128  func (self *SRedisCache) GetGlobalId() string {
   129  	return strings.ToLower(self.ID)
   130  }
   131  
   132  func (self *SRedisCache) GetInstanceType() string {
   133  	return self.Properties.Sku.Name
   134  }
   135  
   136  func (self *SRedisCache) GetCapacityMB() int {
   137  	switch self.Properties.Sku.Family {
   138  	case "P":
   139  		switch self.Properties.Sku.Capacity {
   140  		case 1:
   141  			return 6 * 1024
   142  		case 2:
   143  			return 13 * 1024
   144  		case 3:
   145  			return 26 * 1024
   146  		case 4:
   147  			return 53 * 1024
   148  		case 5:
   149  			return 120 * 1024
   150  		}
   151  	case "C":
   152  		switch self.Properties.Sku.Capacity {
   153  		case 0:
   154  			return 250
   155  		case 1:
   156  			return 1024
   157  		case 2:
   158  			return 2.5 * 1024
   159  		case 3:
   160  			return 6 * 1024
   161  		case 4:
   162  			return 13 * 1024
   163  		case 5:
   164  			return 26 * 1024
   165  		case 6:
   166  			return 53 * 1024
   167  		}
   168  	}
   169  	return 0
   170  }
   171  
   172  func (self *SRedisCache) GetArchType() string {
   173  	if self.Properties.Sku.Family == "P" {
   174  		return api.ELASTIC_CACHE_ARCH_TYPE_MASTER
   175  	}
   176  	return api.ELASTIC_CACHE_ARCH_TYPE_SINGLE
   177  }
   178  
   179  func (self *SRedisCache) GetNodeType() string {
   180  	switch len(self.Properties.Instances) {
   181  	case 1:
   182  		return api.ELASTIC_CACHE_NODE_TYPE_SINGLE
   183  	case 2:
   184  		return api.ELASTIC_CACHE_NODE_TYPE_DOUBLE
   185  	case 3:
   186  		return api.ELASTIC_CACHE_NODE_TYPE_THREE
   187  	case 4:
   188  		return api.ELASTIC_CACHE_NODE_TYPE_FOUR
   189  	case 5:
   190  		return api.ELASTIC_CACHE_NODE_TYPE_FIVE
   191  	case 6:
   192  		return api.ELASTIC_CACHE_NODE_TYPE_SIX
   193  	}
   194  	return fmt.Sprintf("%d", self.Properties.Shardcount)
   195  }
   196  
   197  func (self *SRedisCache) GetEngine() string {
   198  	return "Redis"
   199  }
   200  
   201  func (self *SRedisCache) GetEngineVersion() string {
   202  	return self.Properties.Redisversion
   203  }
   204  
   205  func (self *SRedisCache) GetVpcId() string {
   206  	if len(self.Properties.SubnetId) > 0 {
   207  		info := strings.Split(self.Properties.SubnetId, "/")
   208  		if len(info) > 2 {
   209  			return strings.Join(info[:len(info)-2], "/")
   210  		}
   211  	}
   212  	return ""
   213  }
   214  
   215  func (self *SRedisCache) GetZoneId() string {
   216  	return self.region.getZone().GetGlobalId()
   217  }
   218  
   219  func (self *SRedisCache) GetNetworkType() string {
   220  	if len(self.Properties.SubnetId) > 0 {
   221  		return api.LB_NETWORK_TYPE_VPC
   222  	}
   223  	return api.LB_NETWORK_TYPE_CLASSIC
   224  }
   225  
   226  func (self *SRedisCache) GetNetworkId() string {
   227  	return strings.ToLower(self.Properties.SubnetId)
   228  }
   229  
   230  func (self *SRedisCache) GetPrivateDNS() string {
   231  	return ""
   232  }
   233  
   234  func (self *SRedisCache) GetPrivateIpAddr() string {
   235  	return self.Properties.StaticIP
   236  }
   237  
   238  func (self *SRedisCache) GetPrivateConnectPort() int {
   239  	return self.Properties.Port
   240  }
   241  
   242  func (self *SRedisCache) GetPublicDNS() string {
   243  	return self.Properties.Hostname
   244  }
   245  
   246  func (self *SRedisCache) GetPublicIpAddr() string {
   247  	return ""
   248  }
   249  
   250  func (self *SRedisCache) GetPublicConnectPort() int {
   251  	return self.Properties.Sslport
   252  }
   253  
   254  func (self *SRedisCache) GetMaintainStartTime() string {
   255  	return ""
   256  }
   257  
   258  func (self *SRedisCache) GetMaintainEndTime() string {
   259  	return ""
   260  }
   261  
   262  func (self *SRedisCache) AllocatePublicConnection(port int) (string, error) {
   263  	return "", errors.Wrapf(cloudprovider.ErrNotImplemented, "AllocatePublicConnection")
   264  }
   265  
   266  func (self *SRedisCache) ChangeInstanceSpec(spec string) error {
   267  	return errors.Wrapf(cloudprovider.ErrNotImplemented, "ChangeInstanceSpec")
   268  }
   269  
   270  func (self *SRedisCache) CreateAccount(account cloudprovider.SCloudElasticCacheAccountInput) (cloudprovider.ICloudElasticcacheAccount, error) {
   271  	return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "CreateAccount")
   272  }
   273  
   274  func (self *SRedisCache) CreateAcl(aclName, securityIps string) (cloudprovider.ICloudElasticcacheAcl, error) {
   275  	return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "CreateAcl")
   276  }
   277  
   278  func (self *SRedisCache) CreateBackup(desc string) (cloudprovider.ICloudElasticcacheBackup, error) {
   279  	return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "CreateBackup")
   280  }
   281  
   282  func (self *SRedisCache) Delete() error {
   283  	return self.region.Delete(self.ID)
   284  }
   285  
   286  func (self *SRedisCache) FlushInstance(input cloudprovider.SCloudElasticCacheFlushInstanceInput) error {
   287  	return errors.Wrapf(cloudprovider.ErrNotSupported, "FlushInstance")
   288  }
   289  
   290  func (self *SRedisCache) GetAuthMode() string {
   291  	return "on"
   292  }
   293  
   294  func (self *SRedisCache) GetICloudElasticcacheAccounts() ([]cloudprovider.ICloudElasticcacheAccount, error) {
   295  	return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "GetICloudElasticcacheAccounts")
   296  }
   297  
   298  func (self *SRedisCache) GetICloudElasticcacheAcls() ([]cloudprovider.ICloudElasticcacheAcl, error) {
   299  	acls, err := self.region.GetRedisAcls(self.ID)
   300  	if err != nil {
   301  		return nil, errors.Wrapf(err, "GetRedisAcls")
   302  	}
   303  	ret := []cloudprovider.ICloudElasticcacheAcl{}
   304  	for i := range acls {
   305  		acls[i].redis = self
   306  		ret = append(ret, &acls[i])
   307  	}
   308  	return ret, nil
   309  }
   310  
   311  func (self *SRedisCache) GetICloudElasticcacheAcl(aclId string) (cloudprovider.ICloudElasticcacheAcl, error) {
   312  	acl, err := self.region.GetRedisAcl(aclId)
   313  	if err != nil {
   314  		return nil, errors.Wrapf(err, "GetRedisAcl")
   315  	}
   316  	acl.redis = self
   317  	return acl, nil
   318  }
   319  
   320  func (self *SRedisCache) GetICloudElasticcacheBackups() ([]cloudprovider.ICloudElasticcacheBackup, error) {
   321  	return []cloudprovider.ICloudElasticcacheBackup{}, nil
   322  }
   323  
   324  func (self *SRedisCache) GetICloudElasticcacheParameters() ([]cloudprovider.ICloudElasticcacheParameter, error) {
   325  	return []cloudprovider.ICloudElasticcacheParameter{}, nil
   326  }
   327  
   328  func (self *SRedisCache) GetICloudElasticcacheAccount(accountId string) (cloudprovider.ICloudElasticcacheAccount, error) {
   329  	return nil, cloudprovider.ErrNotFound
   330  }
   331  
   332  func (self *SRedisCache) GetICloudElasticcacheBackup(backupId string) (cloudprovider.ICloudElasticcacheBackup, error) {
   333  	return nil, cloudprovider.ErrNotFound
   334  }
   335  
   336  func (self *SRedisCache) GetSecurityGroupIds() ([]string, error) {
   337  	return []string{}, nil
   338  }
   339  
   340  func (self *SRedisCache) ReleasePublicConnection() error {
   341  	return cloudprovider.ErrNotSupported
   342  }
   343  
   344  func (self *SRedisCache) Restart() error {
   345  	return cloudprovider.ErrNotImplemented
   346  }
   347  
   348  func (self *SRedisCache) SetMaintainTime(start, end string) error {
   349  	return cloudprovider.ErrNotImplemented
   350  }
   351  
   352  func (self *SRedisCache) UpdateAuthMode(noPasswordAccess bool, password string) error {
   353  	return cloudprovider.ErrNotSupported
   354  }
   355  
   356  func (self *SRedisCache) UpdateBackupPolicy(config cloudprovider.SCloudElasticCacheBackupPolicyUpdateInput) error {
   357  	return cloudprovider.ErrNotImplemented
   358  }
   359  
   360  func (self *SRedisCache) UpdateInstanceParameters(config jsonutils.JSONObject) error {
   361  	return cloudprovider.ErrNotImplemented
   362  }
   363  
   364  func (self *SRedisCache) UpdateSecurityGroups(secgroupIds []string) error {
   365  	return cloudprovider.ErrNotImplemented
   366  }
   367  
   368  func (self *SRegion) GetIElasticcaches() ([]cloudprovider.ICloudElasticcache, error) {
   369  	redis, err := self.GetRedisCaches()
   370  	if err != nil {
   371  		return nil, errors.Wrapf(err, "GetRedisCaches")
   372  	}
   373  	ret := []cloudprovider.ICloudElasticcache{}
   374  	for i := range redis {
   375  		redis[i].region = self
   376  		ret = append(ret, &redis[i])
   377  	}
   378  	switch self.client.GetAccessEnv() {
   379  	// 国际区才有企业版
   380  	case api.CLOUD_ACCESS_ENV_AZURE_GLOBAL:
   381  		enterpriseRedis, err := self.GetEnterpriseRedisCaches()
   382  		if err != nil {
   383  			return nil, errors.Wrapf(err, "GetEnterpriseRedisCaches")
   384  		}
   385  		for i := range enterpriseRedis {
   386  			enterpriseRedis[i].region = self
   387  			ret = append(ret, &enterpriseRedis[i])
   388  		}
   389  	}
   390  	return ret, nil
   391  }
   392  
   393  func (self *SRegion) GetIElasticcacheById(id string) (cloudprovider.ICloudElasticcache, error) {
   394  	id = strings.ToLower(id)
   395  	if strings.Index(id, "microsoft.cache/redis") > 0 {
   396  		return self.GetRedisCache(id)
   397  	} else if strings.Index(id, "Microsoft.Cache/redisEnterprise") > 0 {
   398  		return self.GetEnterpriseRedisCache(id)
   399  	} else {
   400  		return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   401  	}
   402  }