imageguest.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 image
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. api "yunion.io/x/onecloud/pkg/apis/image"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/image"
  21. "yunion.io/x/onecloud/pkg/mcclient/options"
  22. )
  23. func init() {
  24. type GuestImageCreateOptions struct {
  25. NAME string `help:"Name of guest image"`
  26. ImageNumber int `help:"common image number of guest image"`
  27. Protected bool `help:"if guest image is protected"`
  28. Image []string `help:"list of images"`
  29. }
  30. R(&GuestImageCreateOptions{}, "guest-image-create", "Create guest image's metadata", func(s *mcclient.ClientSession,
  31. args *GuestImageCreateOptions) error {
  32. params := jsonutils.NewDict()
  33. params.Add(jsonutils.NewString(args.NAME), "name")
  34. if args.ImageNumber > 0 {
  35. params.Add(jsonutils.NewInt(int64(args.ImageNumber)), "image_number")
  36. }
  37. if args.Protected {
  38. params.Add(jsonutils.JSONTrue, "protected")
  39. }
  40. if len(args.Image) > 0 {
  41. images := make([]api.GuestImageCreateInputSubimage, 0)
  42. for _, img := range args.Image {
  43. images = append(images, api.GuestImageCreateInputSubimage{
  44. Id: img,
  45. })
  46. }
  47. params.Add(jsonutils.Marshal(images), "images")
  48. }
  49. ret, err := modules.GuestImages.Create(s, params)
  50. if err != nil {
  51. return err
  52. }
  53. printObject(ret)
  54. return nil
  55. },
  56. )
  57. type GuestImageListOptions struct {
  58. options.BaseListOptions
  59. Name string `help:"Name filter"`
  60. }
  61. R(&GuestImageListOptions{}, "guest-image-list", "List guest images", func(s *mcclient.ClientSession,
  62. args *GuestImageListOptions) error {
  63. params, err := args.Params()
  64. if err != nil {
  65. return err
  66. }
  67. if len(args.Name) > 0 {
  68. params.Add(jsonutils.NewString(args.Name), "name")
  69. }
  70. params.Add(jsonutils.JSONTrue, "details")
  71. rets, err := modules.GuestImages.List(s, params)
  72. if err != nil {
  73. return err
  74. }
  75. printList(rets, modules.GuestImages.GetColumns(s))
  76. return nil
  77. },
  78. )
  79. type GuestImageDeleteOptions struct {
  80. ID []string `help:"Image ID or name"`
  81. OverridePendingDelete *bool `help:"Delete image directly instead of pending delete" short-token:"f"`
  82. }
  83. R(&GuestImageDeleteOptions{}, "guest-image-delete", "Delete a image", func(s *mcclient.ClientSession,
  84. args *GuestImageDeleteOptions) error {
  85. params, err := options.StructToParams(args)
  86. if err != nil {
  87. return err
  88. }
  89. ret := modules.GuestImages.BatchDeleteWithParam(s, args.ID, params, nil)
  90. printBatchResults(ret, modules.GuestImages.GetColumns(s))
  91. return nil
  92. })
  93. type GuestImageCancelDeleteOptions struct {
  94. ID string `help:"Guest Image id or name"`
  95. }
  96. R(&GuestImageCancelDeleteOptions{}, "guest-image-cancel-delete", "Cancel pending delete images",
  97. func(s *mcclient.ClientSession,
  98. args *GuestImageCancelDeleteOptions) error {
  99. if image, e := modules.GuestImages.PerformAction(s, args.ID, "cancel-delete", nil); e != nil {
  100. return e
  101. } else {
  102. printObject(image)
  103. }
  104. return nil
  105. },
  106. )
  107. type GuestImageUpdateOptions struct {
  108. ID string `help:"id of guest image"`
  109. Name string `help:"new name of guest image"`
  110. Protected string `help:"delete protection" choices:"enable|disable"`
  111. OsType string `help:"os type"`
  112. OsDistribution string `help:"os distribution"`
  113. DiskDriver string `help:"disk driver"`
  114. NetDriver string `help:"net driver"`
  115. }
  116. R(&GuestImageUpdateOptions{}, "guest-image-update", "update guest image", func(s *mcclient.ClientSession,
  117. args *GuestImageUpdateOptions) error {
  118. params := jsonutils.NewDict()
  119. if len(args.Name) > 0 {
  120. params.Add(jsonutils.NewString(args.Name), "name")
  121. }
  122. if len(args.Protected) > 0 {
  123. if args.Protected == "enable" {
  124. params.Add(jsonutils.JSONTrue, "protected")
  125. } else {
  126. params.Add(jsonutils.JSONFalse, "protected")
  127. }
  128. }
  129. properties := jsonutils.NewDict()
  130. if len(args.OsType) > 0 {
  131. properties.Add(jsonutils.NewString(args.OsType), "os_type")
  132. }
  133. if len(args.OsDistribution) > 0 {
  134. properties.Add(jsonutils.NewString(args.OsDistribution), "os_distribution")
  135. }
  136. if len(args.DiskDriver) > 0 {
  137. properties.Add(jsonutils.NewString(args.DiskDriver), "disk_driver")
  138. }
  139. if len(args.NetDriver) > 0 {
  140. properties.Add(jsonutils.NewString(args.NetDriver), "net_driver")
  141. }
  142. params.Add(properties, "properties")
  143. _, err := modules.GuestImages.Update(s, args.ID, params)
  144. if err != nil {
  145. return err
  146. }
  147. return nil
  148. })
  149. type GuestImageOptions struct {
  150. ID string `help:"Guest Image id or name"`
  151. }
  152. R(&GuestImageOptions{}, "guest-image-mark-protected", "Mark image protected", func(s *mcclient.ClientSession,
  153. args *GuestImageOptions) error {
  154. params := jsonutils.NewDict()
  155. params.Add(jsonutils.JSONTrue, "protected")
  156. result, err := modules.GuestImages.Update(s, args.ID, params)
  157. if err != nil {
  158. return err
  159. }
  160. printObject(result)
  161. return nil
  162. })
  163. R(&GuestImageOptions{}, "guest-image-mark-unprotected", "Mark image protected", func(s *mcclient.ClientSession,
  164. args *GuestImageOptions) error {
  165. params := jsonutils.NewDict()
  166. params.Add(jsonutils.JSONFalse, "protected")
  167. result, err := modules.GuestImages.Update(s, args.ID, params)
  168. if err != nil {
  169. return err
  170. }
  171. printObject(result)
  172. return nil
  173. })
  174. type GuestImageOperationOptions struct {
  175. ID []string `help:"Guest Image ID or Name"`
  176. }
  177. type GuestImagePublicOptions struct {
  178. GuestImageOperationOptions
  179. Scope string `help:"sharing scope" choices:"system|domain"`
  180. ShareToProject []string `help:"Share to prject"`
  181. }
  182. R(&GuestImageOperationOptions{}, "guest-image-private", "Make a guest image private",
  183. func(s *mcclient.ClientSession, args *GuestImageOperationOptions) error {
  184. if len(args.ID) == 0 {
  185. return errors.Error("No guest image ID provided")
  186. } else if len(args.ID) == 1 {
  187. result, err := modules.GuestImages.PerformAction(s, args.ID[0], "private", nil)
  188. if err != nil {
  189. return err
  190. }
  191. printObject(result)
  192. } else {
  193. results := modules.GuestImages.BatchPerformAction(s, args.ID, "private", nil)
  194. printBatchResults(results, modules.GuestImages.GetColumns(s))
  195. }
  196. return nil
  197. },
  198. )
  199. R(&GuestImagePublicOptions{}, "guest-image-public", "Make a guest image public",
  200. func(s *mcclient.ClientSession, args *GuestImagePublicOptions) error {
  201. params, err := options.StructToParams(args)
  202. if err != nil {
  203. return err
  204. }
  205. if len(args.ID) == 0 {
  206. return errors.Error("No guest image ID provided")
  207. } else if len(args.ID) == 1 {
  208. result, err := modules.GuestImages.PerformAction(s, args.ID[0], "public", params)
  209. if err != nil {
  210. return err
  211. }
  212. printObject(result)
  213. } else {
  214. results := modules.GuestImages.BatchPerformAction(s, args.ID, "public", params)
  215. printBatchResults(results, modules.GuestImages.GetColumns(s))
  216. }
  217. return nil
  218. },
  219. )
  220. }