service_catalog.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 ServiceCatalogListOpions struct {
  23. options.BaseListOptions
  24. }
  25. R(&ServiceCatalogListOpions{}, "service-catalog-list", "List service catalog", func(s *mcclient.ClientSession,
  26. opts *ServiceCatalogListOpions) error {
  27. params, err := options.ListStructToParams(opts)
  28. if err != nil {
  29. return err
  30. }
  31. ret, err := modules.ServiceCatalog.List(s, params)
  32. if err != nil {
  33. return err
  34. }
  35. printList(ret, modules.ServiceCatalog.GetColumns(s))
  36. return nil
  37. })
  38. type ServiceCatalogCreateOptions struct {
  39. NAME string `help:"Name of service catalog"`
  40. GuestTemplate string `help:"guest template of service catalog"`
  41. IconUrl string `help:"icon url of service catalog"`
  42. GenerateName bool `help:"whether to generate name"`
  43. }
  44. R(&ServiceCatalogCreateOptions{}, "service-catalog-create", "Create a service catalog",
  45. func(s *mcclient.ClientSession, opts *ServiceCatalogCreateOptions) error {
  46. params := jsonutils.NewDict()
  47. if opts.GenerateName {
  48. params.Add(jsonutils.NewString(opts.NAME), "generate_name")
  49. } else {
  50. params.Add(jsonutils.NewString(opts.NAME), "name")
  51. }
  52. if len(opts.GuestTemplate) != 0 {
  53. params.Add(jsonutils.NewString(opts.GuestTemplate), "guest_template")
  54. }
  55. if len(opts.IconUrl) != 0 {
  56. params.Add(jsonutils.NewString(opts.IconUrl), "icon_url")
  57. }
  58. serviceCatalog, err := modules.ServiceCatalog.Create(s, params)
  59. if err != nil {
  60. return err
  61. }
  62. printObject(serviceCatalog)
  63. return nil
  64. },
  65. )
  66. type ServiceCatalogOptions struct {
  67. ID string `help:"ID or name of service catalog"`
  68. }
  69. R(&ServiceCatalogOptions{}, "service-catalog-show", "show a service catalog", func(s *mcclient.ClientSession,
  70. opts *ServiceCatalogOptions) error {
  71. sc, err := modules.ServiceCatalog.Get(s, opts.ID, jsonutils.JSONNull)
  72. if err != nil {
  73. return err
  74. }
  75. printObject(sc)
  76. return nil
  77. })
  78. R(&ServiceCatalogOptions{}, "service-catalog-delete", "delete a service catalog", func(s *mcclient.ClientSession,
  79. opts *ServiceCatalogOptions) error {
  80. sc, err := modules.ServiceCatalog.Delete(s, opts.ID, jsonutils.JSONNull)
  81. if err != nil {
  82. return err
  83. }
  84. printObject(sc)
  85. return nil
  86. })
  87. type ServiceCatalogUpdateOptions struct {
  88. ServiceCatalogOptions
  89. Name string `help:"Name of service catalog"`
  90. GuestTemplate string `help:"guest template of service catalog"`
  91. IconUrl string `help:"icon url of service catalog"`
  92. }
  93. R(&ServiceCatalogUpdateOptions{}, "service-catalog-update", "update a service catalog",
  94. func(s *mcclient.ClientSession, opts *ServiceCatalogUpdateOptions) error {
  95. params := jsonutils.NewDict()
  96. if len(opts.Name) > 0 {
  97. params.Add(jsonutils.NewString(opts.Name), "name")
  98. }
  99. if len(opts.GuestTemplate) > 0 {
  100. params.Add(jsonutils.NewString(opts.GuestTemplate), "guest_template")
  101. }
  102. if len(opts.IconUrl) > 0 {
  103. params.Add(jsonutils.NewString(opts.IconUrl), "icon_url")
  104. }
  105. sc, err := modules.ServiceCatalog.Update(s, opts.ID, params)
  106. if err != nil {
  107. return err
  108. }
  109. printObject(sc)
  110. return nil
  111. },
  112. )
  113. type ServiceCatalogDeployOptions struct {
  114. ServiceCatalogOptions
  115. Name string `help:"Name of guest"`
  116. GenerateName bool `help:"whether to generate name for guest"`
  117. Count int `help:"count of guest"`
  118. ProjectID string `help:"project id of guest"`
  119. }
  120. R(&ServiceCatalogDeployOptions{}, "service-catalog-deploy", "deploy", func(s *mcclient.ClientSession,
  121. opts *ServiceCatalogDeployOptions) error {
  122. params := jsonutils.NewDict()
  123. if opts.GenerateName {
  124. params.Add(jsonutils.NewString(opts.Name), "generate_name")
  125. } else {
  126. params.Add(jsonutils.NewString(opts.Name), "name")
  127. }
  128. if opts.Count != 0 {
  129. params.Add(jsonutils.NewInt(int64(opts.Count)), "count")
  130. }
  131. if len(opts.ProjectID) > 0 {
  132. params.Add(jsonutils.NewString(opts.ProjectID), "project_id")
  133. }
  134. sc, err := modules.ServiceCatalog.PerformAction(s, opts.ID, "deploy", params)
  135. if err != nil {
  136. return err
  137. }
  138. printObject(sc)
  139. return nil
  140. })
  141. }