guest_template.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/onecloud/pkg/mcclient"
  19. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  20. baseoptions "yunion.io/x/onecloud/pkg/mcclient/options"
  21. options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
  22. )
  23. func init() {
  24. type GuestTemplateListOptions struct {
  25. baseoptions.BaseListOptions
  26. }
  27. R(&GuestTemplateListOptions{}, "server-template-list", "List server template", func(s *mcclient.ClientSession,
  28. opts *GuestTemplateListOptions) error {
  29. params, err := baseoptions.ListStructToParams(opts)
  30. if err != nil {
  31. return err
  32. }
  33. result, err := modules.GuestTemplate.List(s, params)
  34. if err != nil {
  35. return err
  36. }
  37. printList(result, modules.GuestTemplate.GetColumns(s))
  38. return nil
  39. })
  40. type GuestTemplateCreateOptions struct {
  41. options.ServerCreateOptionalOptions
  42. NAME string `help:"Name of server template" json:"-"`
  43. }
  44. R(&GuestTemplateCreateOptions{}, "server-template-create", "Create a server template",
  45. func(s *mcclient.ClientSession,
  46. opts *GuestTemplateCreateOptions) error {
  47. params, err := opts.OptionalParams()
  48. if err != nil {
  49. return err
  50. }
  51. if baseoptions.BoolV(opts.DryRun) {
  52. fmt.Println("no support operator")
  53. return nil
  54. }
  55. dict := jsonutils.NewDict()
  56. if opts.GenerateName {
  57. dict.Add(jsonutils.NewString(opts.NAME), "generate_name")
  58. } else {
  59. dict.Add(jsonutils.NewString(opts.NAME), "name")
  60. }
  61. dict.Add(params.JSON(params), "content")
  62. tem, err := modules.GuestTemplate.Create(s, dict)
  63. if err != nil {
  64. return err
  65. }
  66. printObject(tem)
  67. return nil
  68. })
  69. type GuestTemplateUpdateOptions struct {
  70. options.ServerCreateOptionalOptions
  71. ID string `help:"ID of server template"`
  72. name string `help:"name of server template"`
  73. }
  74. R(&GuestTemplateUpdateOptions{}, "server-template-update", "Update a server template",
  75. func(s *mcclient.ClientSession, opts *GuestTemplateUpdateOptions) error {
  76. params, err := opts.OptionalParams()
  77. if err != nil {
  78. return err
  79. }
  80. if baseoptions.BoolV(opts.DryRun) {
  81. fmt.Println("no support operator")
  82. return nil
  83. }
  84. dict := jsonutils.NewDict()
  85. if len(opts.name) != 0 {
  86. dict.Add(jsonutils.NewString(opts.name), "name")
  87. }
  88. dict.Add(params.JSON(params), "content")
  89. tem, err := modules.GuestTemplate.Update(s, opts.ID, dict)
  90. if err != nil {
  91. return err
  92. }
  93. printObject(tem)
  94. return nil
  95. })
  96. type GuestTemplateOptions struct {
  97. ID string `help:"ID or Name of server template"`
  98. }
  99. R(&GuestTemplateOptions{}, "server-template-show", "Show a server template",
  100. func(s *mcclient.ClientSession, opts *GuestTemplateOptions) error {
  101. tem, err := modules.GuestTemplate.Get(s, opts.ID, jsonutils.JSONNull)
  102. if err != nil {
  103. return err
  104. }
  105. printObject(tem)
  106. return nil
  107. })
  108. R(&GuestTemplateOptions{}, "server-tempalte-delete", "Delete a server template",
  109. func(s *mcclient.ClientSession, opts *GuestTemplateOptions) error {
  110. tem, err := modules.GuestTemplate.Delete(s, opts.ID, jsonutils.JSONNull)
  111. if err != nil {
  112. return err
  113. }
  114. printObject(tem)
  115. return nil
  116. },
  117. )
  118. R(&GuestTemplateOptions{}, "server-template-private", "Private server template",
  119. func(s *mcclient.ClientSession, opts *GuestTemplateOptions) error {
  120. tem, err := modules.GuestTemplate.PerformAction(s, opts.ID, "private", jsonutils.JSONNull)
  121. if err != nil {
  122. return err
  123. }
  124. printObject(tem)
  125. return nil
  126. },
  127. )
  128. R(&GuestTemplateOptions{}, "server-template-inspect", "Inspect server template", func(s *mcclient.ClientSession, opts *GuestTemplateOptions) error {
  129. tem, err := modules.GuestTemplate.PerformAction(s, opts.ID, "inspect", jsonutils.JSONNull)
  130. if err != nil {
  131. return err
  132. }
  133. printObject(tem)
  134. return nil
  135. })
  136. type GuestTemplatePublicOptions struct {
  137. ID string `help:"ID or Name of server template"`
  138. PublicScope string `help:"public scope"`
  139. }
  140. R(&GuestTemplatePublicOptions{}, "server-template-public", "Public server template",
  141. func(s *mcclient.ClientSession, opts *GuestTemplatePublicOptions) error {
  142. dict := jsonutils.NewDict()
  143. if len(opts.PublicScope) != 0 {
  144. dict.Add(jsonutils.NewString(opts.PublicScope), "public_scope")
  145. }
  146. tem, err := modules.GuestTemplate.PerformAction(s, opts.ID, "public", dict)
  147. if err != nil {
  148. return err
  149. }
  150. printObject(tem)
  151. return nil
  152. },
  153. )
  154. }