serverdisks.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/printutils"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  21. "yunion.io/x/onecloud/pkg/mcclient/options"
  22. )
  23. func init() {
  24. type ServerDiskListOptions struct {
  25. options.BaseListOptions
  26. Server string `help:"ID or Name of Server"`
  27. Disk string `help:"ID or name of disk"`
  28. Index int64 `help:"disk index" default:"-1"`
  29. }
  30. R(&ServerDiskListOptions{}, "server-disk-list", "List server disk pairs", func(s *mcclient.ClientSession, args *ServerDiskListOptions) error {
  31. var params *jsonutils.JSONDict
  32. {
  33. var err error
  34. params, err = args.BaseListOptions.Params()
  35. if err != nil {
  36. return err
  37. }
  38. }
  39. if args.Index >= 0 {
  40. params.Add(jsonutils.NewInt(args.Index), "index")
  41. }
  42. var result *printutils.ListResult
  43. var err error
  44. if len(args.Server) > 0 {
  45. result, err = modules.Serverdisks.ListDescendent(s, args.Server, params)
  46. } else if len(args.Disk) > 0 {
  47. result, err = modules.Serverdisks.ListDescendent2(s, args.Disk, params)
  48. } else {
  49. result, err = modules.Serverdisks.List(s, params)
  50. }
  51. if err != nil {
  52. return err
  53. }
  54. printList(result, modules.Serverdisks.GetColumns(s))
  55. return nil
  56. })
  57. type ServerDiskDetailOptions struct {
  58. SERVER string `help:"ID or Name of Server"`
  59. DISK string `help:"ID or Name of Disk"`
  60. }
  61. R(&ServerDiskDetailOptions{}, "server-disk-show", "Show server disk details", func(s *mcclient.ClientSession, args *ServerDiskDetailOptions) error {
  62. result, err := modules.Serverdisks.Get(s, args.SERVER, args.DISK, nil)
  63. if err != nil {
  64. return err
  65. }
  66. printObject(result)
  67. return nil
  68. })
  69. type ServerDiskUpdateOptions struct {
  70. SERVER string `help:"ID or Name of server"`
  71. DISK string `help:"ID or Name of Disk"`
  72. Driver string `help:"Driver of vDisk" choices:"virtio|ide|sata|scsi|pvscsi"`
  73. Cache string `help:"Cache mode of vDisk" choices:"writethrough|none|writeback|directsync"`
  74. Aio string `help:"Asynchronous IO mode of vDisk" choices:"native|threads"`
  75. Index int64 `help:"Index of vDisk" default:"-1"`
  76. }
  77. R(&ServerDiskUpdateOptions{}, "server-disk-update", "Update details of a virtual disk of a virtual server", func(s *mcclient.ClientSession, args *ServerDiskUpdateOptions) error {
  78. params := jsonutils.NewDict()
  79. if len(args.Driver) > 0 {
  80. params.Add(jsonutils.NewString(args.Driver), "driver")
  81. }
  82. if len(args.Cache) > 0 {
  83. params.Add(jsonutils.NewString(args.Cache), "cache_mode")
  84. }
  85. if len(args.Aio) > 0 {
  86. params.Add(jsonutils.NewString(args.Aio), "aio_mode")
  87. }
  88. if args.Index >= 0 {
  89. params.Add(jsonutils.NewInt(args.Index), "index")
  90. }
  91. if params.Size() == 0 {
  92. return InvalidUpdateError()
  93. }
  94. srv, err := modules.Serverdisks.Update(s, args.SERVER, args.DISK, nil, params)
  95. if err != nil {
  96. return err
  97. }
  98. printObject(srv)
  99. return nil
  100. })
  101. type ServerCreateDiskOptions struct {
  102. SERVER string `help:"ID or Name of server"`
  103. DISK []string `help:"Disk description of a virtual disk"`
  104. }
  105. R(&ServerCreateDiskOptions{}, "server-create-disk", "Create a disk and attach it to a virtual server", func(s *mcclient.ClientSession, args *ServerCreateDiskOptions) error {
  106. params := jsonutils.NewDict()
  107. for i, d := range args.DISK {
  108. params.Add(jsonutils.NewString(d), fmt.Sprintf("disk.%d", i))
  109. }
  110. srv, err := modules.Servers.PerformAction(s, args.SERVER, "createdisk", params)
  111. if err != nil {
  112. return err
  113. }
  114. printObject(srv)
  115. return nil
  116. })
  117. type ServerAttachDiskOptions struct {
  118. SERVER string `help:"ID or name of server"`
  119. DISK string `help:"ID of name of disk to attach"`
  120. Driver string `help:"Driver" choices:"virtio|ide|scsi"`
  121. Cache string `help:"Cache mode" choices:"writeback|none|writethrought"`
  122. }
  123. R(&ServerAttachDiskOptions{}, "server-attach-disk", "Attach an existing virtual disks to a virtual server", func(s *mcclient.ClientSession, args *ServerAttachDiskOptions) error {
  124. params := jsonutils.NewDict()
  125. params.Add(jsonutils.NewString(args.DISK), "disk_id")
  126. if len(args.Driver) > 0 {
  127. params.Add(jsonutils.NewString(args.Driver), "driver")
  128. }
  129. if len(args.Cache) > 0 {
  130. params.Add(jsonutils.NewString(args.Cache), "cache")
  131. }
  132. srv, err := modules.Servers.PerformAction(s, args.SERVER, "attachdisk", params)
  133. if err != nil {
  134. return err
  135. }
  136. printObject(srv)
  137. return nil
  138. })
  139. type ServerDetachDiskOptions struct {
  140. SERVER string `help:"ID or name of server"`
  141. DISK string `help:"ID or name of disk to detach"`
  142. DeleteDisk bool `help:"Delete disk if the disk not has flag of auto_delete when detached"`
  143. }
  144. R(&ServerDetachDiskOptions{}, "server-detach-disk", "Detach a disk from a virtual server", func(s *mcclient.ClientSession, args *ServerDetachDiskOptions) error {
  145. params := jsonutils.NewDict()
  146. params.Add(jsonutils.NewString(args.DISK), "disk_id")
  147. if args.DeleteDisk {
  148. params.Add(jsonutils.JSONFalse, "keep_disk")
  149. } else {
  150. params.Add(jsonutils.JSONTrue, "keep_disk")
  151. }
  152. srv, err := modules.Servers.PerformAction(s, args.SERVER, "detachdisk", params)
  153. if err != nil {
  154. return err
  155. }
  156. printObject(srv)
  157. return nil
  158. })
  159. }