dbinstances.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 compute
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. )
  22. type DBInstanceCreateOptions struct {
  23. NAME string `help:"DBInstance Name"`
  24. InstanceType string `help:"InstanceType for DBInstance"`
  25. VcpuCount int `help:"Core of cpu for DBInstance"`
  26. VmemSizeMb int `help:"Memory size of DBInstance"`
  27. Port int `help:"Port of DBInstance"`
  28. Category string `help:"Category of DBInstance"`
  29. Network string `help:"Network of DBInstance"`
  30. Address string `help:"Address of DBInstance"`
  31. Engine string `help:"Engine of DBInstance"`
  32. EngineVersion string `help:"EngineVersion of DBInstance Engine"`
  33. StorageType string `help:"StorageTyep of DBInstance"`
  34. Secgroup string `help:"Secgroup name or Id for DBInstance"`
  35. Zone string `help:"ZoneId or name for DBInstance"`
  36. DiskSizeGB int `help:"Storage size for DBInstance"`
  37. Duration string `help:"Duration for DBInstance"`
  38. AllowDelete *bool `help:"not lock dbinstance" `
  39. Tags []string `help:"Tags info,prefix with 'user:', eg: user:project=default" json:"-"`
  40. DBInstancebackupId string `help:"create dbinstance from backup" json:"dbinstancebackup_id"`
  41. MultiAz bool `help:"deploy rds with multi az"`
  42. }
  43. func (opts *DBInstanceCreateOptions) Params() (jsonutils.JSONObject, error) {
  44. params, err := options.StructToParams(opts)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if opts.AllowDelete != nil && *opts.AllowDelete {
  49. params.Add(jsonutils.JSONFalse, "disable_delete")
  50. }
  51. Tagparams := jsonutils.NewDict()
  52. for _, tag := range opts.Tags {
  53. info := strings.Split(tag, "=")
  54. if len(info) == 2 {
  55. if len(info[0]) == 0 {
  56. return nil, fmt.Errorf("invalidate tag info %s", tag)
  57. }
  58. Tagparams.Add(jsonutils.NewString(info[1]), info[0])
  59. } else if len(info) == 1 {
  60. Tagparams.Add(jsonutils.NewString(info[0]), info[0])
  61. } else {
  62. return nil, fmt.Errorf("invalidate tag info %s", tag)
  63. }
  64. }
  65. params.Add(Tagparams, "__meta__")
  66. return params, nil
  67. }
  68. type DBInstanceListOptions struct {
  69. options.BaseListOptions
  70. BillingType string `help:"billing type" choices:"postpaid|prepaid"`
  71. IpAddr []string
  72. SecgroupId string
  73. }
  74. func (opts *DBInstanceListOptions) Params() (jsonutils.JSONObject, error) {
  75. return options.ListStructToParams(opts)
  76. }
  77. type DBInstanceIdOptions struct {
  78. ID string `help:"DBInstance Id"`
  79. }
  80. func (opts *DBInstanceIdOptions) GetId() string {
  81. return opts.ID
  82. }
  83. func (opts *DBInstanceIdOptions) Params() (jsonutils.JSONObject, error) {
  84. return nil, nil
  85. }
  86. type DBInstanceRenewOptions struct {
  87. DBInstanceIdOptions
  88. DURATION string `help:"Duration of renew, ADMIN only command"`
  89. }
  90. func (opts *DBInstanceRenewOptions) Params() (jsonutils.JSONObject, error) {
  91. params := jsonutils.NewDict()
  92. params.Add(jsonutils.NewString(opts.DURATION), "duration")
  93. return params, nil
  94. }
  95. type DBInstanceUpdateOptions struct {
  96. DBInstanceIdOptions
  97. Name string
  98. Description string
  99. Delete string `help:"Lock or not lock dbinstance" choices:"enable|disable"`
  100. }
  101. func (opts *DBInstanceUpdateOptions) Params() (jsonutils.JSONObject, error) {
  102. params, err := options.StructToParams(opts)
  103. if err != nil {
  104. return nil, err
  105. }
  106. if len(opts.Delete) > 0 {
  107. if opts.Delete == "disable" {
  108. params.Add(jsonutils.JSONTrue, "disable_delete")
  109. } else {
  110. params.Add(jsonutils.JSONFalse, "disable_delete")
  111. }
  112. }
  113. return params, nil
  114. }
  115. type DBInstanceChangeConfigOptions struct {
  116. DBInstanceIdOptions
  117. DiskSizeGb int64 `help:"Change DBInstance storage size"`
  118. VcpuCount int64 `help:"Change DBInstance vcpu count"`
  119. VmemSizeMb int64 `help:"Change DBInstance vmem size mb"`
  120. InstanceType string `help:"Change DBInstance instanceType"`
  121. Category string `help:"Change DBInstance category"`
  122. }
  123. func (opts *DBInstanceChangeConfigOptions) Params() (jsonutils.JSONObject, error) {
  124. params := jsonutils.NewDict()
  125. if len(opts.Category) > 0 {
  126. params.Add(jsonutils.NewString(opts.Category), "category")
  127. }
  128. if len(opts.InstanceType) > 0 {
  129. params.Add(jsonutils.NewString(opts.InstanceType), "instance_type")
  130. }
  131. if opts.DiskSizeGb > 0 {
  132. params.Add(jsonutils.NewInt(opts.DiskSizeGb), "disk_size_gb")
  133. }
  134. if opts.VcpuCount > 0 {
  135. params.Add(jsonutils.NewInt(opts.VcpuCount), "vcpu_count")
  136. }
  137. if opts.VmemSizeMb > 0 {
  138. params.Add(jsonutils.NewInt(opts.VmemSizeMb), "vmeme_size_mb")
  139. }
  140. return params, nil
  141. }
  142. type DBInstancePublicConnectionOptions struct {
  143. DBInstanceIdOptions
  144. IS_OPEN string `help:"Open Or Close public connection" choices:"true|false"`
  145. }
  146. func (opts *DBInstancePublicConnectionOptions) Params() (jsonutils.JSONObject, error) {
  147. return jsonutils.Marshal(map[string]bool{"open": opts.IS_OPEN == "true"}), nil
  148. }
  149. type DBInstanceRecoveryOptions struct {
  150. DBInstanceIdOptions
  151. BACKUP string
  152. Databases []string
  153. }
  154. func (opts *DBInstanceRecoveryOptions) Params() (jsonutils.JSONObject, error) {
  155. params := jsonutils.NewDict()
  156. params.Set("dbinstancebackup", jsonutils.NewString(opts.BACKUP))
  157. dbs := jsonutils.NewDict()
  158. for _, database := range opts.Databases {
  159. if len(database) > 0 {
  160. dbInfo := strings.Split(database, ":")
  161. if len(dbInfo) == 1 {
  162. dbs.Add(jsonutils.NewString(dbInfo[0]), dbInfo[0])
  163. } else if len(dbInfo) == 2 {
  164. dbs.Add(jsonutils.NewString(dbInfo[1]), dbInfo[0])
  165. } else {
  166. return nil, fmt.Errorf("Invalid dbinfo: %s", database)
  167. }
  168. }
  169. }
  170. if dbs.Length() > 0 {
  171. params.Add(dbs, "databases")
  172. }
  173. return params, nil
  174. }
  175. type DBInstanceDeleteOptions struct {
  176. DBInstanceIdOptions
  177. KeepBackup bool `help:"Keep dbinstance manual backup after delete dbinstance"`
  178. }
  179. func (opts *DBInstanceDeleteOptions) Params() (jsonutils.JSONObject, error) {
  180. params := jsonutils.NewDict()
  181. if opts.KeepBackup {
  182. params.Add(jsonutils.JSONTrue, "keep_backup")
  183. }
  184. return params, nil
  185. }
  186. type DBInstanceChangeOwnerOptions struct {
  187. DBInstanceIdOptions
  188. PROJECT string `help:"Project ID or change" json:"tenant"`
  189. }
  190. func (opts *DBInstanceChangeOwnerOptions) Params() (jsonutils.JSONObject, error) {
  191. return options.ListStructToParams(opts)
  192. }
  193. type DBInstanceRemoteUpdateOptions struct {
  194. DBInstanceIdOptions
  195. api.DBInstanceRemoteUpdateInput
  196. }
  197. func (opts *DBInstanceRemoteUpdateOptions) Params() (jsonutils.JSONObject, error) {
  198. return options.ListStructToParams(opts)
  199. }
  200. type DBInstanceSetSecgroupOptions struct {
  201. DBInstanceIdOptions
  202. SECGROUP_IDS []string `help:"Security Group Ids"`
  203. }
  204. func (opts *DBInstanceSetSecgroupOptions) Params() (jsonutils.JSONObject, error) {
  205. return jsonutils.Marshal(map[string][]string{"secgroup_ids": opts.SECGROUP_IDS}), nil
  206. }