redis.go 11 KB

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