snapshots.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "yunion.io/x/jsonutils"
  17. "yunion.io/x/onecloud/pkg/mcclient"
  18. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. func init() {
  22. type SnapshotsListOptions struct {
  23. options.BaseListOptions
  24. Disk string `help:"Disk snapshots" json:"disk_id"`
  25. FakeDeleted bool `help:"Show fake deleted snapshot or not"`
  26. Local *bool `help:"Show local snapshots"`
  27. Share *bool `help:"Show shared snapshots"`
  28. DiskType string `help:"Filter by disk type" choices:"sys|data"`
  29. Server string `help:"Filter by server" json:"server_id"`
  30. Unused bool
  31. StorageId string `help:"Filter by storage id"`
  32. OrderByGuest string
  33. OrderByDiskName string
  34. }
  35. R(&SnapshotsListOptions{}, "snapshot-list", "Show snapshots", func(s *mcclient.ClientSession, args *SnapshotsListOptions) error {
  36. params, err := options.ListStructToParams(args)
  37. if err != nil {
  38. return err
  39. }
  40. result, err := modules.Snapshots.List(s, params)
  41. if err != nil {
  42. return err
  43. }
  44. printList(result, modules.Snapshots.GetColumns(s))
  45. return nil
  46. })
  47. type SnapshotDeleteOptions struct {
  48. ID []string `help:"Delete snapshot id"`
  49. }
  50. R(&SnapshotDeleteOptions{}, "snapshot-delete", "Delete snapshots", func(s *mcclient.ClientSession, args *SnapshotDeleteOptions) error {
  51. ret := modules.Snapshots.BatchDelete(s, args.ID, nil)
  52. printBatchResults(ret, modules.Snapshots.GetColumns(s))
  53. return nil
  54. })
  55. type DiskDeleteSnapshotsOptions struct {
  56. DISK string `help:"ID of disk"`
  57. }
  58. R(&DiskDeleteSnapshotsOptions{}, "disk-delete-snapshots", "Delete a disk snapshots", func(s *mcclient.ClientSession, args *DiskDeleteSnapshotsOptions) error {
  59. params := jsonutils.NewDict()
  60. params.Add(jsonutils.NewString(args.DISK), "disk_id")
  61. result, err := modules.Snapshots.PerformClassAction(s, "delete-disk-snapshots", params)
  62. if err != nil {
  63. return err
  64. }
  65. printObject(result)
  66. return nil
  67. })
  68. type SnapshotIdOptions struct {
  69. ID string `help:"ID or Name of snapshot"`
  70. }
  71. R(&SnapshotIdOptions{}, "snapshot-show", "Show snapshot details", func(s *mcclient.ClientSession, args *SnapshotIdOptions) error {
  72. result, err := modules.Snapshots.Get(s, args.ID, nil)
  73. if err != nil {
  74. return err
  75. }
  76. printObject(result)
  77. return nil
  78. })
  79. R(&SnapshotIdOptions{}, "snapshot-syncstatus", "Sync snapshot status", func(s *mcclient.ClientSession, args *SnapshotIdOptions) error {
  80. result, err := modules.Snapshots.PerformAction(s, args.ID, "syncstatus", nil)
  81. if err != nil {
  82. return err
  83. }
  84. printObject(result)
  85. return nil
  86. })
  87. R(&SnapshotIdOptions{}, "snapshot-purge", "Purge Snapshot db records", func(s *mcclient.ClientSession, args *SnapshotIdOptions) error {
  88. result, err := modules.Snapshots.PerformAction(s, args.ID, "purge", nil)
  89. if err != nil {
  90. return err
  91. }
  92. printObject(result)
  93. return nil
  94. })
  95. type SnapshotCreateOptions struct {
  96. Disk string `help:"Id of disk to take snapshot" json:"disk" required:"true"`
  97. NAME string `help:"Name of snapshot" json:"name"`
  98. }
  99. R(&SnapshotCreateOptions{}, "snapshot-create", "Create a snapshot", func(s *mcclient.ClientSession, args *SnapshotCreateOptions) error {
  100. params, err := options.StructToParams(args)
  101. if err != nil {
  102. return err
  103. }
  104. snapshot, err := modules.Snapshots.Create(s, params)
  105. if err != nil {
  106. return err
  107. }
  108. printObject(snapshot)
  109. return nil
  110. })
  111. }