backup.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. "os"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. )
  22. type DiskBackupListOptions struct {
  23. options.BaseListOptions
  24. DiskId string `help:"disk id" json:"disk_id"`
  25. BackupStorageId string `help:"backup storage id" json:"backup_storage_id"`
  26. IsInstanceBackup *bool `help:"if part of instance backup" json:"is_instance_backup"`
  27. OrderByDiskName string
  28. }
  29. func (opts *DiskBackupListOptions) Params() (jsonutils.JSONObject, error) {
  30. return options.ListStructToParams(opts)
  31. }
  32. type DiskBackupIdOptions struct {
  33. ID string `help:"disk backup id" json:"-"`
  34. }
  35. func (opts *DiskBackupIdOptions) GetId() string {
  36. return opts.ID
  37. }
  38. func (opts *DiskBackupIdOptions) Params() (jsonutils.JSONObject, error) {
  39. return nil, nil
  40. }
  41. type DiskBackupDeleteOptions struct {
  42. DiskBackupIdOptions
  43. Force bool `help:"force delete"`
  44. }
  45. func (opts *DiskBackupDeleteOptions) QueryParams() (jsonutils.JSONObject, error) {
  46. return jsonutils.Marshal(opts), nil
  47. }
  48. type DiskBackupCreateOptions struct {
  49. options.BaseCreateOptions
  50. AsTarContainerId string `help:"container id of tar process"`
  51. AsTarIncludeFile []string `help:"include file path of tar process"`
  52. AsTarIncludePattern []string `help:"include pattern of tar process"`
  53. AsTarExcludeFile []string `help:"exclude file path of tar process"`
  54. AsTarIgnoreNotExistFile bool `help:"ignore not exist file when using tar"`
  55. DISKID string `help:"disk id" json:"disk_id"`
  56. BACKUPSTORAGEID string `help:"back storage id" json:"backup_storage_id"`
  57. }
  58. func (opts *DiskBackupCreateOptions) Params() (jsonutils.JSONObject, error) {
  59. input := &computeapi.DiskBackupCreateInput{
  60. DiskId: opts.DISKID,
  61. BackupStorageId: opts.BACKUPSTORAGEID,
  62. BackupAsTar: new(computeapi.DiskBackupAsTarInput),
  63. }
  64. input.Name = opts.NAME
  65. input.Description = opts.Desc
  66. if opts.AsTarContainerId != "" {
  67. input.BackupAsTar.ContainerId = opts.AsTarContainerId
  68. }
  69. if len(opts.AsTarIncludeFile) > 0 {
  70. input.BackupAsTar.IncludeFiles = opts.AsTarIncludeFile
  71. }
  72. if len(opts.AsTarIncludePattern) > 0 {
  73. input.BackupAsTar.IncludePatterns = opts.AsTarIncludePattern
  74. }
  75. if len(opts.AsTarExcludeFile) > 0 {
  76. input.BackupAsTar.ExcludeFiles = opts.AsTarExcludeFile
  77. }
  78. if opts.AsTarIgnoreNotExistFile {
  79. input.BackupAsTar.IgnoreNotExistFile = opts.AsTarIgnoreNotExistFile
  80. }
  81. return jsonutils.Marshal(input), nil
  82. }
  83. type DiskBackupRecoveryOptions struct {
  84. DiskBackupIdOptions
  85. Name string `help:"disk name" json:"name"`
  86. }
  87. func (opt *DiskBackupRecoveryOptions) GetId() string {
  88. return opt.ID
  89. }
  90. func (opt *DiskBackupRecoveryOptions) Params() (jsonutils.JSONObject, error) {
  91. params := jsonutils.NewDict()
  92. params.Set("name", jsonutils.NewString(opt.Name))
  93. return params, nil
  94. }
  95. type DiskBackupSyncstatusOptions struct {
  96. DiskBackupIdOptions
  97. }
  98. func (opt *DiskBackupSyncstatusOptions) GetId() string {
  99. return opt.ID
  100. }
  101. func (opt *DiskBackupSyncstatusOptions) Params() (jsonutils.JSONObject, error) {
  102. return nil, nil
  103. }
  104. type BackupStorageListOptions struct {
  105. options.BaseListOptions
  106. }
  107. func (opts *BackupStorageListOptions) Params() (jsonutils.JSONObject, error) {
  108. return options.ListStructToParams(opts)
  109. }
  110. type BackupStorageIdOptions struct {
  111. ID string `help:"backup storage id"`
  112. }
  113. func (opts *BackupStorageIdOptions) GetId() string {
  114. return opts.ID
  115. }
  116. func (opts *BackupStorageIdOptions) Params() (jsonutils.JSONObject, error) {
  117. return nil, nil
  118. }
  119. type BackupStorageCreateOptions struct {
  120. options.BaseCreateOptions
  121. StorageType string `help:"storage type" choices:"nfs|object"`
  122. NfsHost string `help:"nfs host, required when storage_type is nfs"`
  123. NfsSharedDir string `help:"nfs shared dir, required when storage_type is nfs" `
  124. ObjectBucketUrl string `help:"object bucket url, required when storage_type is object"`
  125. ObjectAccessKey string `help:"object storage access key, required when storage_type is object"`
  126. ObjectSecret string `help:"object storage secret, required when storage_type is object"`
  127. ObjectSignVer string `help:"object storage signing alogirithm version, optional" choices:"v2|v4"`
  128. ObjectBucketUrlExt string `help:"object storage external access url, optional"`
  129. CapacityMb int `help:"capacity, unit mb"`
  130. }
  131. func (opts *BackupStorageCreateOptions) Params() (jsonutils.JSONObject, error) {
  132. return jsonutils.Marshal(opts), nil
  133. }
  134. type BackupStorageUpdateOptions struct {
  135. options.BaseUpdateOptions
  136. NfsHost string `help:"nfs host, required when storage_type is nfs"`
  137. NfsSharedDir string `help:"nfs shared dir, required when storage_type is nfs" `
  138. ObjectBucketUrl string `help:"object bucket url, required when storage_type is object"`
  139. ObjectAccessKey string `help:"object storage access key, required when storage_type is object"`
  140. ObjectSecret string `help:"object storage secret, required when storage_type is object"`
  141. ObjectSignVer string `help:"object storage signing alogirithm version, optional" choices:"v2|v4"`
  142. ObjectBucketUrlExt string `help:"object storage external access url, optional"`
  143. }
  144. func (opts *BackupStorageUpdateOptions) Params() (jsonutils.JSONObject, error) {
  145. return jsonutils.Marshal(opts), nil
  146. }
  147. type InstanceBackupListOptions struct {
  148. options.BaseListOptions
  149. OrderByGuest string
  150. }
  151. func (opts *InstanceBackupListOptions) Params() (jsonutils.JSONObject, error) {
  152. return options.ListStructToParams(opts)
  153. }
  154. type InstanceBackupIdOptions struct {
  155. ID string `help:"instance backup id" json:"-"`
  156. }
  157. func (opts *InstanceBackupIdOptions) GetId() string {
  158. return opts.ID
  159. }
  160. func (opts *InstanceBackupIdOptions) Params() (jsonutils.JSONObject, error) {
  161. return nil, nil
  162. }
  163. type InstanceBackupDeleteOptions struct {
  164. InstanceBackupIdOptions
  165. Force bool `help:"force delete"`
  166. }
  167. func (opts *InstanceBackupDeleteOptions) QueryParams() (jsonutils.JSONObject, error) {
  168. return jsonutils.Marshal(opts), nil
  169. }
  170. type InstanceBackupRecoveryOptions struct {
  171. DiskBackupIdOptions
  172. Name string `help:"server name" json:"name"`
  173. }
  174. func (opts *InstanceBackupRecoveryOptions) GetId() string {
  175. return opts.ID
  176. }
  177. func (opts *InstanceBackupRecoveryOptions) Params() (jsonutils.JSONObject, error) {
  178. return jsonutils.Marshal(opts), nil
  179. }
  180. type InstanceBackupPackOptions struct {
  181. DiskBackupIdOptions
  182. PackageName string `help:"package name" json:"package_name"`
  183. }
  184. func (opts *InstanceBackupPackOptions) Params() (jsonutils.JSONObject, error) {
  185. return jsonutils.Marshal(opts), nil
  186. }
  187. type InstanceBackupManagerCreateFromPackageOptions struct {
  188. PackageName string `help:"package name" json:"package_name"`
  189. Name string `help:"instance backup name" json:"name"`
  190. BackupStorageId string `help:"backup storage id" json:"backup_storage_id"`
  191. ProjectId string `help:"target project id" json:"project_id"`
  192. }
  193. func (opts *InstanceBackupManagerCreateFromPackageOptions) Params() (jsonutils.JSONObject, error) {
  194. return jsonutils.Marshal(opts), nil
  195. }
  196. type HostBackupStorageListOptions struct {
  197. options.BaseListOptions
  198. Host string `json:"-" help:"filter by host"`
  199. Backupstorage string `json:"-" help:"filter by backupstorage"`
  200. }
  201. func (opts HostBackupStorageListOptions) GetMasterOpt() string {
  202. return opts.Host
  203. }
  204. func (opts HostBackupStorageListOptions) GetSlaveOpt() string {
  205. return opts.Backupstorage
  206. }
  207. func (opts *HostBackupStorageListOptions) Params() (jsonutils.JSONObject, error) {
  208. return options.ListStructToParams(opts)
  209. }
  210. type HostBackupStorageJoinOptions struct {
  211. HOST string `json:"-" help:"host id"`
  212. BACKUPSTORAGE string `json:"-" help:"backup storage id"`
  213. }
  214. func (opts HostBackupStorageJoinOptions) GetMasterId() string {
  215. return opts.HOST
  216. }
  217. func (opts HostBackupStorageJoinOptions) GetSlaveId() string {
  218. return opts.BACKUPSTORAGE
  219. }
  220. func (opts HostBackupStorageJoinOptions) Params() (jsonutils.JSONObject, error) {
  221. return jsonutils.NewDict(), nil
  222. }
  223. type DiskBackupExportOptions struct {
  224. DiskBackupIdOptions
  225. Output string `help:"output file path" json:"output"`
  226. }
  227. type DiskBackupImportOptions struct {
  228. NAME string `help:"backup name" json:"name"`
  229. BACKUPSTORAGEID string `help:"backup storage id" json:"backup_storage_id"`
  230. IMPORT string `help:"backup info" json:"-"`
  231. TenantId string `help:"tenant id" json:"tenant_id"`
  232. }
  233. func (opts *DiskBackupImportOptions) Params() (jsonutils.JSONObject, error) {
  234. content, err := os.ReadFile(opts.IMPORT)
  235. if err != nil {
  236. return nil, errors.Wrap(err, "os.ReadFile")
  237. }
  238. inputJson, err := jsonutils.Parse(content)
  239. if err != nil {
  240. return nil, errors.Wrap(err, "jsonutils.Parse")
  241. }
  242. info := &computeapi.DiskBackupImportInput{}
  243. err = inputJson.Unmarshal(info)
  244. if err != nil {
  245. return nil, errors.Wrap(err, "jsonutils.Unmarshal")
  246. }
  247. info.Name = opts.NAME
  248. info.BackupStorageId = opts.BACKUPSTORAGEID
  249. if len(opts.TenantId) > 0 {
  250. info.ProjectId = opts.TenantId
  251. }
  252. return jsonutils.Marshal(info), nil
  253. }