memcached.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 qcloud
  15. import (
  16. "fmt"
  17. "strconv"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/billing"
  22. billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
  23. api "yunion.io/x/cloudmux/pkg/apis/compute"
  24. "yunion.io/x/cloudmux/pkg/cloudprovider"
  25. "yunion.io/x/cloudmux/pkg/multicloud"
  26. )
  27. type SMemcached struct {
  28. multicloud.SElasticcacheBase
  29. QcloudTags
  30. region *SRegion
  31. InstanceId string `json:"InstanceId"`
  32. InstanceName string `json:"InstanceName"`
  33. AppId int `json:"AppId"`
  34. ProjectId int `json:"ProjectId"`
  35. InstanceDesc string `json:"InstanceDesc"`
  36. Vip string `json:"Vip"`
  37. Vport int `json:"Vport"`
  38. Status int `json:"Status"`
  39. AutoRenewFlag int `json:"AutoRenewFlag"`
  40. VpcId int `json:"VpcId"`
  41. SubnetId int `json:"SubnetId"`
  42. PayMode int `json:"PayMode"`
  43. ZoneId int `json:"ZoneId"`
  44. Expire int `json:"Expire"`
  45. RegionId int `json:"RegionId"`
  46. AddTimeStamp time.Time `json:"AddTimeStamp"`
  47. ModtimeStamp time.Time `json:"ModTimeStamp"`
  48. IsolateTimesSamp time.Time `json:"IsolateTimeStamp"`
  49. UniqVpcId string `json:"UniqVpcId"`
  50. UniqSubnetId string `json:"UniqSubnetId"`
  51. DeadlineTimeStamp string `json:"DeadlineTimeStamp"`
  52. SetId int `json:"SetId"`
  53. CmemId int `json:"CmemId"`
  54. }
  55. func (self *SMemcached) GetId() string {
  56. return self.InstanceId
  57. }
  58. func (self *SMemcached) GetName() string {
  59. return self.InstanceName
  60. }
  61. func (self *SMemcached) GetGlobalId() string {
  62. return self.GetId()
  63. }
  64. func (self *SMemcached) GetStatus() string {
  65. switch self.Status {
  66. case 0:
  67. return api.ELASTIC_CACHE_STATUS_DEPLOYING
  68. case 1:
  69. return api.ELASTIC_CACHE_STATUS_RUNNING
  70. case 2:
  71. return api.ELASTIC_CACHE_STATUS_CREATE_FAILED
  72. case 4, 5, 6, 7:
  73. return api.ELASTIC_CACHE_STATUS_RELEASING
  74. default:
  75. return fmt.Sprintf("%d", self.Status)
  76. }
  77. }
  78. func (self *SMemcached) GetProjectId() string {
  79. return strconv.Itoa(self.ProjectId)
  80. }
  81. func (self *SMemcached) GetBillingType() string {
  82. // 计费模式:0-按量计费,1-包年包月
  83. if self.PayMode == 1 {
  84. return billing_api.BILLING_TYPE_PREPAID
  85. } else {
  86. return billing_api.BILLING_TYPE_POSTPAID
  87. }
  88. }
  89. func (self *SMemcached) GetCreatedAt() time.Time {
  90. return self.AddTimeStamp
  91. }
  92. func (self *SMemcached) GetExpiredAt() time.Time {
  93. return time.Time{}
  94. }
  95. func (self *SMemcached) SetAutoRenew(bc billing.SBillingCycle) error {
  96. return cloudprovider.ErrNotSupported
  97. }
  98. func (self *SMemcached) IsAutoRenew() bool {
  99. return self.AutoRenewFlag == 1
  100. }
  101. func (self *SMemcached) GetInstanceType() string {
  102. return ""
  103. }
  104. func (self *SMemcached) GetCapacityMB() int {
  105. return 0
  106. }
  107. func (self *SMemcached) GetArchType() string {
  108. return api.ELASTIC_CACHE_NODE_TYPE_SINGLE
  109. }
  110. func (self *SMemcached) GetNodeType() string {
  111. return api.ELASTIC_CACHE_ARCH_TYPE_SINGLE
  112. }
  113. func (self *SMemcached) GetEngine() string {
  114. return api.ELASTIC_CACHE_ENGINE_MEMCACHED
  115. }
  116. func (self *SMemcached) GetEngineVersion() string {
  117. return "latest"
  118. }
  119. func (self *SMemcached) GetVpcId() string {
  120. return self.UniqVpcId
  121. }
  122. func (self *SMemcached) GetZoneId() string {
  123. return fmt.Sprintf("%s/%s-%d", self.region.GetGlobalId(), self.region.Region, self.ZoneId%10)
  124. }
  125. func (self *SMemcached) GetNetworkType() string {
  126. if len(self.UniqSubnetId) > 0 {
  127. return api.LB_NETWORK_TYPE_VPC
  128. }
  129. return api.LB_NETWORK_TYPE_CLASSIC
  130. }
  131. func (self *SMemcached) GetNetworkId() string {
  132. return self.UniqSubnetId
  133. }
  134. func (self *SMemcached) GetPrivateDNS() string {
  135. return ""
  136. }
  137. func (self *SMemcached) GetPrivateIpAddr() string {
  138. return self.Vip
  139. }
  140. func (self *SMemcached) GetPrivateConnectPort() int {
  141. return self.Vport
  142. }
  143. func (self *SMemcached) GetPublicDNS() string {
  144. return ""
  145. }
  146. func (self *SMemcached) GetPublicIpAddr() string {
  147. return ""
  148. }
  149. func (self *SMemcached) GetPublicConnectPort() int {
  150. return 0
  151. }
  152. func (self *SMemcached) GetMaintainStartTime() string {
  153. return ""
  154. }
  155. func (self *SMemcached) GetMaintainEndTime() string {
  156. return ""
  157. }
  158. func (self *SMemcached) GetAuthMode() string {
  159. return "off"
  160. }
  161. func (self *SMemcached) GetSecurityGroupIds() ([]string, error) {
  162. return []string{}, nil
  163. }
  164. func (self *SMemcached) GetICloudElasticcacheAccounts() ([]cloudprovider.ICloudElasticcacheAccount, error) {
  165. return []cloudprovider.ICloudElasticcacheAccount{}, nil
  166. }
  167. func (self *SMemcached) GetICloudElasticcacheAcls() ([]cloudprovider.ICloudElasticcacheAcl, error) {
  168. return []cloudprovider.ICloudElasticcacheAcl{}, nil
  169. }
  170. func (self *SMemcached) GetICloudElasticcacheBackups() ([]cloudprovider.ICloudElasticcacheBackup, error) {
  171. return []cloudprovider.ICloudElasticcacheBackup{}, nil
  172. }
  173. func (self *SMemcached) GetICloudElasticcacheParameters() ([]cloudprovider.ICloudElasticcacheParameter, error) {
  174. return []cloudprovider.ICloudElasticcacheParameter{}, nil
  175. }
  176. func (self *SMemcached) GetICloudElasticcacheAccount(accountId string) (cloudprovider.ICloudElasticcacheAccount, error) {
  177. return nil, cloudprovider.ErrNotFound
  178. }
  179. func (self *SMemcached) GetICloudElasticcacheAcl(aclId string) (cloudprovider.ICloudElasticcacheAcl, error) {
  180. return nil, cloudprovider.ErrNotFound
  181. }
  182. func (self *SMemcached) GetICloudElasticcacheBackup(backupId string) (cloudprovider.ICloudElasticcacheBackup, error) {
  183. return nil, cloudprovider.ErrNotFound
  184. }
  185. func (self *SMemcached) Restart() error {
  186. return errors.Wrap(cloudprovider.ErrNotSupported, "Restart")
  187. }
  188. func (self *SMemcached) Delete() error {
  189. return cloudprovider.ErrNotSupported
  190. }
  191. func (self *SMemcached) CleanupInstance() error {
  192. return cloudprovider.ErrNotSupported
  193. }
  194. func (self *SMemcached) ChangeInstanceSpec(spec string) error {
  195. return cloudprovider.ErrNotSupported
  196. }
  197. func (self *SMemcached) SetMaintainTime(maintainStartTime, maintainEndTime string) error {
  198. return cloudprovider.ErrNotSupported
  199. }
  200. func (self *SMemcached) AllocatePublicConnection(port int) (string, error) {
  201. return "", cloudprovider.ErrNotSupported
  202. }
  203. func (self *SMemcached) ReleasePublicConnection() error {
  204. return errors.Wrap(cloudprovider.ErrNotSupported, "ReleasePublicConnection")
  205. }
  206. func (self *SMemcached) CreateAccount(account cloudprovider.SCloudElasticCacheAccountInput) (cloudprovider.ICloudElasticcacheAccount, error) {
  207. return nil, cloudprovider.ErrNotSupported
  208. }
  209. func (self *SMemcached) CreateAcl(aclName, securityIps string) (cloudprovider.ICloudElasticcacheAcl, error) {
  210. return nil, errors.Wrap(cloudprovider.ErrNotSupported, "CreateAcl")
  211. }
  212. func (self *SMemcached) CreateBackup(desc string) (cloudprovider.ICloudElasticcacheBackup, error) {
  213. return nil, cloudprovider.ErrNotSupported
  214. }
  215. func (self *SMemcached) FlushInstance(input cloudprovider.SCloudElasticCacheFlushInstanceInput) error {
  216. return cloudprovider.ErrNotSupported
  217. }
  218. func (self *SMemcached) UpdateAuthMode(noPasswordAccess bool, password string) error {
  219. return cloudprovider.ErrNotSupported
  220. }
  221. func (self *SMemcached) UpdateInstanceParameters(config jsonutils.JSONObject) error {
  222. return cloudprovider.ErrNotSupported
  223. }
  224. func (self *SMemcached) UpdateBackupPolicy(config cloudprovider.SCloudElasticCacheBackupPolicyUpdateInput) error {
  225. return cloudprovider.ErrNotSupported
  226. }
  227. func (self *SMemcached) UpdateSecurityGroups(secgroupIds []string) error {
  228. return cloudprovider.ErrNotSupported
  229. }
  230. func (self *SMemcached) Renew(bc billing.SBillingCycle) error {
  231. return cloudprovider.ErrNotSupported
  232. }
  233. func (self *SRegion) GetMemcaches(ids []string, limit, offset int) ([]SMemcached, int, error) {
  234. params := map[string]string{}
  235. if limit <= 0 {
  236. limit = 100
  237. }
  238. params["Limit"] = fmt.Sprintf("%d", limit)
  239. params["Offset"] = fmt.Sprintf("%d", offset)
  240. for i, id := range ids {
  241. params[fmt.Sprintf("InstanceIds.%d", i)] = id
  242. }
  243. resp, err := self.memcachedRequest("DescribeInstances", params)
  244. if err != nil {
  245. return nil, 0, errors.Wrapf(err, "DescribeInstances")
  246. }
  247. ret := []SMemcached{}
  248. err = resp.Unmarshal(&ret, "InstanceList")
  249. if err != nil {
  250. return nil, 0, errors.Wrapf(err, "resp.Unmarshal InstanceList")
  251. }
  252. total, _ := resp.Float("TotalCount")
  253. return ret, int(total), nil
  254. }