dbinstance.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 ksyun
  15. import (
  16. "fmt"
  17. "time"
  18. billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. "yunion.io/x/jsonutils"
  23. "yunion.io/x/log"
  24. "yunion.io/x/pkg/errors"
  25. )
  26. type SDBInstance struct {
  27. region *SRegion
  28. multicloud.SDBInstanceBase
  29. SKsyunTags
  30. DBInstanceClass struct {
  31. Id string
  32. Vcpus int
  33. Disk int
  34. RAM int
  35. }
  36. DBInstanceIdentifier string
  37. DBInstanceName string
  38. DBInstanceStatus string
  39. DBInstanceType string
  40. DBParameterGroupId string
  41. GroupId string
  42. Vip string
  43. Port int
  44. Engine string
  45. EngineVersion string
  46. InstanceCreateTime string
  47. MasterUserName string
  48. VpcId string
  49. SubnetId string
  50. DiskUsed int
  51. PubliclyAccessible bool
  52. ReadReplicaDBInstanceIdentifiers []interface{}
  53. BillType string
  54. OrderType string
  55. OrderSource string
  56. ProductType int
  57. MultiAvailabilityZone bool
  58. MasterAvailabilityZone string
  59. SlaveAvailabilityZone string
  60. ProductId string
  61. OrderUse string
  62. SupportIPV6 bool
  63. ProjectId string
  64. ProjectName string
  65. Region string
  66. BillTypeId int
  67. Eip string
  68. EipPort int
  69. NetworkType int
  70. }
  71. func (region *SRegion) GetIDBInstanceById(id string) (cloudprovider.ICloudDBInstance, error) {
  72. vm, err := region.GetDBInstance(id)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return vm, nil
  77. }
  78. func (region *SRegion) GetIDBInstances() ([]cloudprovider.ICloudDBInstance, error) {
  79. vms, err := region.GetDBInstances("")
  80. if err != nil {
  81. return nil, err
  82. }
  83. ret := []cloudprovider.ICloudDBInstance{}
  84. for i := range vms {
  85. vms[i].region = region
  86. ret = append(ret, &vms[i])
  87. }
  88. return ret, nil
  89. }
  90. func (region *SRegion) GetDBInstance(id string) (*SDBInstance, error) {
  91. vms, err := region.GetDBInstances(id)
  92. if err != nil {
  93. return nil, err
  94. }
  95. for i := range vms {
  96. if vms[i].GetGlobalId() == id {
  97. vms[i].region = region
  98. return &vms[i], nil
  99. }
  100. }
  101. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  102. }
  103. func (region *SRegion) GetDBInstances(id string) ([]SDBInstance, error) {
  104. params := map[string]interface{}{}
  105. if len(id) > 0 {
  106. params["DBInstanceIdentifier"] = id
  107. }
  108. ret := []SDBInstance{}
  109. for {
  110. resp, err := region.rdsRequest("DescribeDBInstances", params)
  111. if err != nil {
  112. return nil, err
  113. }
  114. part := struct {
  115. Instances []SDBInstance
  116. TotalCount int
  117. Marker string
  118. }{}
  119. err = resp.Unmarshal(&part, "Data")
  120. if err != nil {
  121. return nil, err
  122. }
  123. ret = append(ret, part.Instances...)
  124. if len(ret) >= part.TotalCount {
  125. break
  126. }
  127. params["Marker"] = part.Marker
  128. }
  129. return ret, nil
  130. }
  131. func (rds *SDBInstance) GetName() string {
  132. if len(rds.DBInstanceName) > 0 {
  133. return rds.DBInstanceName
  134. }
  135. return rds.DBInstanceIdentifier
  136. }
  137. func (rds *SDBInstance) GetId() string {
  138. return rds.DBInstanceIdentifier
  139. }
  140. func (rds *SDBInstance) GetGlobalId() string {
  141. return rds.GetId()
  142. }
  143. func (rds *SDBInstance) GetStatus() string {
  144. switch rds.DBInstanceStatus {
  145. case "ACTIVE":
  146. return api.DBINSTANCE_RUNNING
  147. default:
  148. log.Errorf("Unknown dbinstance status %s", rds.DBInstanceStatus)
  149. return api.DBINSTANCE_UNKNOWN
  150. }
  151. }
  152. func (rds *SDBInstance) GetBillingType() string {
  153. if rds.BillType != "HourlyInstantSettlement" {
  154. return billing_api.BILLING_TYPE_PREPAID
  155. }
  156. return billing_api.BILLING_TYPE_POSTPAID
  157. }
  158. func (rds *SDBInstance) GetExpiredAt() time.Time {
  159. return time.Time{}
  160. }
  161. func (rds *SDBInstance) GetCreatedAt() time.Time {
  162. t, _ := time.Parse("2006-01-02T15:04:05-0700", rds.InstanceCreateTime)
  163. return t
  164. }
  165. func (rds *SDBInstance) GetStorageType() string {
  166. return api.KSYUN_DBINSTANCE_STORAGE_TYPE_DEFAULT
  167. }
  168. func (rds *SDBInstance) GetEngine() string {
  169. return api.DBINSTANCE_TYPE_MYSQL
  170. }
  171. func (rds *SDBInstance) GetEngineVersion() string {
  172. return rds.EngineVersion
  173. }
  174. func (rds *SDBInstance) GetInstanceType() string {
  175. return rds.DBInstanceType
  176. }
  177. func (rds *SDBInstance) GetCategory() string {
  178. if rds.MultiAvailabilityZone {
  179. return api.ALIYUN_DBINSTANCE_CATEGORY_HA
  180. }
  181. return api.ALIYUN_DBINSTANCE_CATEGORY_BASIC
  182. }
  183. func (rds *SDBInstance) GetVcpuCount() int {
  184. return rds.DBInstanceClass.Vcpus
  185. }
  186. func (rds *SDBInstance) GetVmemSizeMB() int {
  187. return rds.DBInstanceClass.RAM * 1024
  188. }
  189. func (rds *SDBInstance) GetDiskSizeGB() int {
  190. return rds.DBInstanceClass.Disk * 1024
  191. }
  192. func (rds *SDBInstance) GetDiskSizeUsedMB() int {
  193. return rds.DiskUsed * 1024
  194. }
  195. func (rds *SDBInstance) GetPort() int {
  196. return rds.Port
  197. }
  198. func (rds *SDBInstance) GetMaintainTime() string {
  199. return ""
  200. }
  201. func (rds *SDBInstance) GetIVpcId() string {
  202. return rds.VpcId
  203. }
  204. func (rds *SDBInstance) Refresh() error {
  205. vm, err := rds.region.GetDBInstance(rds.DBInstanceIdentifier)
  206. if err != nil {
  207. return err
  208. }
  209. return jsonutils.Update(rds, vm)
  210. }
  211. func (rds *SDBInstance) GetZone1Id() string {
  212. return rds.MasterAvailabilityZone
  213. }
  214. func (rds *SDBInstance) GetZone2Id() string {
  215. if rds.SlaveAvailabilityZone != rds.MasterAvailabilityZone {
  216. return rds.SlaveAvailabilityZone
  217. }
  218. return ""
  219. }
  220. func (rds *SDBInstance) GetZone3Id() string {
  221. return ""
  222. }
  223. func (rds *SDBInstance) GetIOPS() int {
  224. return 0
  225. }
  226. func (rds *SDBInstance) GetNetworkAddress() string {
  227. return rds.Vip
  228. }
  229. func (rds *SDBInstance) GetDBNetworks() ([]cloudprovider.SDBInstanceNetwork, error) {
  230. return []cloudprovider.SDBInstanceNetwork{
  231. cloudprovider.SDBInstanceNetwork{
  232. IP: rds.Vip,
  233. NetworkId: rds.SubnetId,
  234. },
  235. }, nil
  236. }
  237. func (rds *SDBInstance) GetInternalConnectionStr() string {
  238. return fmt.Sprintf("%s:%d", rds.Vip, rds.Port)
  239. }
  240. func (rds *SDBInstance) GetConnectionStr() string {
  241. if len(rds.Eip) > 0 {
  242. return fmt.Sprintf("%s:%d", rds.Eip, rds.EipPort)
  243. }
  244. return ""
  245. }
  246. func (rds *SDBInstance) GetProjectId() string {
  247. return rds.ProjectId
  248. }
  249. func (rds *SDBInstance) GetIDBInstanceDatabases() ([]cloudprovider.ICloudDBInstanceDatabase, error) {
  250. databases, err := rds.region.GetDBInstanceDatabases(rds.DBInstanceIdentifier)
  251. if err != nil {
  252. return nil, err
  253. }
  254. ret := []cloudprovider.ICloudDBInstanceDatabase{}
  255. for i := 0; i < len(databases); i++ {
  256. databases[i].instance = rds
  257. ret = append(ret, &databases[i])
  258. }
  259. return ret, nil
  260. }
  261. func (rds *SDBInstance) Delete() error {
  262. return rds.region.DeleteDBInstance(rds.DBInstanceIdentifier)
  263. }
  264. func (region *SRegion) DeleteDBInstance(instanceId string) error {
  265. params := map[string]interface{}{}
  266. params["DBInstanceIdentifier"] = instanceId
  267. _, err := region.rdsRequest("DeleteDBInstance", params)
  268. return err
  269. }