rds.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 jdcloud
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "time"
  20. commodels "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
  21. "github.com/jdcloud-api/jdcloud-sdk-go/services/rds/apis"
  22. "github.com/jdcloud-api/jdcloud-sdk-go/services/rds/client"
  23. "github.com/jdcloud-api/jdcloud-sdk-go/services/rds/models"
  24. "yunion.io/x/jsonutils"
  25. "yunion.io/x/pkg/errors"
  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. type SDBInstance struct {
  31. multicloud.SDBInstanceBase
  32. JdcloudTags
  33. region *SRegion
  34. models.DBInstance
  35. }
  36. func (self *SDBInstance) GetName() string {
  37. return self.InstanceName
  38. }
  39. func (self *SDBInstance) GetInstanceType() string {
  40. return self.InstanceClass
  41. }
  42. func (self *SDBInstance) GetCategory() string {
  43. return self.InstanceType
  44. }
  45. func (self *SDBInstance) GetDiskSizeGB() int {
  46. return self.InstanceStorageGB
  47. }
  48. func (self *SDBInstance) GetEngine() string {
  49. return self.Engine
  50. }
  51. func (self *SDBInstance) GetEngineVersion() string {
  52. return self.EngineVersion
  53. }
  54. func (self *SDBInstance) GetGlobalId() string {
  55. return self.InstanceId
  56. }
  57. func (self *SDBInstance) GetId() string {
  58. return self.InstanceId
  59. }
  60. func (self *SDBInstance) GetIVpcId() string {
  61. return self.VpcId
  62. }
  63. func (self *SDBInstance) Refresh() error {
  64. rds, err := self.region.GetDBInstance(self.InstanceId)
  65. if err != nil {
  66. return err
  67. }
  68. return jsonutils.Update(self, rds)
  69. }
  70. func (self *SDBInstance) GetMaintainTime() string {
  71. return ""
  72. }
  73. func (self *SDBInstance) GetMasterInstanceId() string {
  74. return self.SourceInstanceId
  75. }
  76. func (self *SDBInstance) GetPort() int {
  77. if len(self.InstancePort) == 0 {
  78. self.Refresh()
  79. }
  80. port, _ := strconv.Atoi(self.InstancePort)
  81. return int(port)
  82. }
  83. func (self *SDBInstance) GetCreatedAt() time.Time {
  84. return parseTime(self.CreateTime)
  85. }
  86. func (self *SDBInstance) GetExpiredAt() time.Time {
  87. return expireAt(&self.Charge)
  88. }
  89. func (self *SDBInstance) GetStatus() string {
  90. switch self.InstanceStatus {
  91. case "BUILDING", "DDLING", "PARAMETERGROUP_MODIFYING", "AUDIT_OPENING", "AUDIT_CLOSING", "SECURITY_OPENING", "SECURITY_CLOSING", "SSL_OPENING", "SSL_CLOSING":
  92. return api.DBINSTANCE_DEPLOYING
  93. case "RUNNING":
  94. return api.DBINSTANCE_RUNNING
  95. case "DELETING":
  96. return api.DBINSTANCE_DELETING
  97. case "FAILOVER", "AZ_MIGRATING":
  98. return api.DBINSTANCE_MIGRATING
  99. case "RESTORING", "DB_RESTORING":
  100. return api.DBINSTANCE_RESTORING
  101. case "MODIFYING":
  102. return api.DBINSTANCE_CHANGE_CONFIG
  103. case "BUILD_READONLY":
  104. return api.DBINSTANCE_DEPLOYING
  105. case "REBOOTING":
  106. return api.DBINSTANCE_REBOOTING
  107. case "MAINTENANCE":
  108. return api.DBINSTANCE_MAINTENANCE
  109. default:
  110. return self.InstanceStatus
  111. }
  112. }
  113. func (self *SDBInstance) GetStorageType() string {
  114. return self.InstanceStorageType
  115. }
  116. func (self *SDBInstance) GetVcpuCount() int {
  117. return self.InstanceCPU
  118. }
  119. func (self *SDBInstance) GetVmemSizeMB() int {
  120. return self.InstanceMemoryMB
  121. }
  122. func (self *SDBInstance) GetZone1Id() string {
  123. if len(self.AzId) > 0 {
  124. return self.AzId[0]
  125. }
  126. return ""
  127. }
  128. func (self *SDBInstance) GetZone2Id() string {
  129. if len(self.AzId) > 1 {
  130. return self.AzId[1]
  131. }
  132. return ""
  133. }
  134. func (self *SDBInstance) GetZone3Id() string {
  135. if len(self.AzId) > 2 {
  136. return self.AzId[2]
  137. }
  138. return ""
  139. }
  140. func (self *SDBInstance) GetConnectionStr() string {
  141. return self.PublicDomainName
  142. }
  143. func (self *SDBInstance) GetInternalConnectionStr() string {
  144. return self.InternalDomainName
  145. }
  146. func (self *SDBInstance) Delete() error {
  147. return self.region.DeleteDBInstance(self.InstanceId)
  148. }
  149. func (self *SRegion) GetIDBInstances() ([]cloudprovider.ICloudDBInstance, error) {
  150. rds := []SDBInstance{}
  151. n := 1
  152. for {
  153. part, total, err := self.GetDBInstances(n, 100)
  154. if err != nil {
  155. return nil, errors.Wrapf(err, "GetDBInstances")
  156. }
  157. rds = append(rds, part...)
  158. if len(rds) >= total {
  159. break
  160. }
  161. n++
  162. }
  163. ret := []cloudprovider.ICloudDBInstance{}
  164. for i := range rds {
  165. ret = append(ret, &rds[i])
  166. }
  167. return ret, nil
  168. }
  169. func (self *SRegion) GetIDBInstanceById(id string) (cloudprovider.ICloudDBInstance, error) {
  170. rds, err := self.GetDBInstance(id)
  171. if err != nil {
  172. return nil, errors.Wrapf(err, "GetDBInstance(%s)", id)
  173. }
  174. return rds, nil
  175. }
  176. func (self *SRegion) GetDBInstance(id string) (*SDBInstance, error) {
  177. req := apis.NewDescribeInstanceAttributesRequest(self.ID, id)
  178. client := client.NewRdsClient(self.getCredential())
  179. client.Logger = Logger{debug: self.client.debug}
  180. resp, err := client.DescribeInstanceAttributes(req)
  181. if err != nil {
  182. return nil, errors.Wrapf(err, "DescribeInstanceAttributes")
  183. }
  184. if resp.Error.Code == 404 || strings.Contains(resp.Error.Status, "NotFound") {
  185. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", jsonutils.Marshal(resp.Error).String())
  186. } else if resp.Error.Code != 0 {
  187. return nil, errors.Error(jsonutils.Marshal(resp.Error).String())
  188. }
  189. ret := SDBInstance{
  190. region: self,
  191. }
  192. jsonutils.Update(&ret.DBInstance, resp.Result.DbInstanceAttributes)
  193. return &ret, nil
  194. }
  195. func (self *SRegion) DeleteDBInstance(id string) error {
  196. req := apis.NewDeleteInstanceRequest(self.ID, id)
  197. client := client.NewRdsClient(self.getCredential())
  198. client.Logger = Logger{}
  199. resp, err := client.DeleteInstance(req)
  200. if err != nil {
  201. return err
  202. }
  203. if resp.Error.Code == 404 {
  204. return nil
  205. }
  206. return nil
  207. }
  208. func (self *SRegion) GetDBInstances(pageNumber int, pageSize int) ([]SDBInstance, int, error) {
  209. req := apis.NewDescribeInstancesRequestWithAllParams(self.ID, &pageNumber, &pageSize, []commodels.Filter{}, []commodels.TagFilter{})
  210. client := client.NewRdsClient(self.getCredential())
  211. client.Logger = Logger{debug: self.client.debug}
  212. resp, err := client.DescribeInstances(req)
  213. if err != nil {
  214. return nil, 0, errors.Wrapf(err, "DescribeInstances")
  215. }
  216. if resp.Error.Code >= 400 {
  217. err = fmt.Errorf("%s", resp.Error.Message)
  218. return nil, 0, err
  219. }
  220. total := resp.Result.TotalCount
  221. ret := []SDBInstance{}
  222. for i := range resp.Result.DbInstances {
  223. ret = append(ret, SDBInstance{
  224. region: self,
  225. DBInstance: resp.Result.DbInstances[i],
  226. })
  227. }
  228. return ret, total, nil
  229. }