devtooltemplate.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 devtool
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/util/printutils"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/devtool"
  21. "yunion.io/x/onecloud/pkg/mcclient/options"
  22. )
  23. func init() {
  24. printAnsiblePlaybookObject := func(obj jsonutils.JSONObject) {
  25. dict := obj.(*jsonutils.JSONDict)
  26. pbJson, err := dict.Get("playbook")
  27. if err != nil {
  28. printObject(obj)
  29. return
  30. }
  31. pbStr := pbJson.YAMLString()
  32. dict.Set("playbook", jsonutils.NewString(pbStr))
  33. printObject(obj)
  34. }
  35. type TemplateListOptions struct {
  36. options.BaseListOptions
  37. Name string `help:"cloud region ID or Name" json:"-"`
  38. }
  39. R(&TemplateListOptions{}, "devtooltemplate-list", "List Devtool Templates", func(s *mcclient.ClientSession, args *TemplateListOptions) error {
  40. params, err := options.ListStructToParams(args)
  41. if err != nil {
  42. return err
  43. }
  44. var result *printutils.ListResult
  45. result, err = modules.DevToolTemplates.List(s, params)
  46. printList(result, modules.DevToolTemplates.GetColumns(s))
  47. return nil
  48. })
  49. R(
  50. &options.DevtoolTemplateCreateOptions{},
  51. "devtooltemplate-create",
  52. "Create a template repo component",
  53. func(s *mcclient.ClientSession, opts *options.DevtoolTemplateCreateOptions) error {
  54. params, err := opts.Params()
  55. if err != nil {
  56. return err
  57. }
  58. log.Infof("devtool playbook create opts: %+v", params)
  59. apb, err := modules.DevToolTemplates.Create(s, params)
  60. if err != nil {
  61. return err
  62. }
  63. printAnsiblePlaybookObject(apb)
  64. return nil
  65. },
  66. )
  67. R(
  68. &options.DevtoolTemplateIdOptions{},
  69. "devtooltemplate-show",
  70. "Show devtool template",
  71. func(s *mcclient.ClientSession, opts *options.DevtoolTemplateIdOptions) error {
  72. apb, err := modules.DevToolTemplates.Get(s, opts.ID, nil)
  73. if err != nil {
  74. return err
  75. }
  76. printAnsiblePlaybookObject(apb)
  77. return nil
  78. },
  79. )
  80. R(
  81. &options.DevtoolTemplateBindingOptions{},
  82. "devtooltemplate-bind",
  83. "Binding devtool template to a host/vm",
  84. func(s *mcclient.ClientSession, opts *options.DevtoolTemplateBindingOptions) error {
  85. params := jsonutils.NewDict()
  86. params.Set("server_id", jsonutils.NewString(opts.ServerID))
  87. apb, err := modules.DevToolTemplates.PerformAction(s, opts.ID, "bind", params)
  88. if err != nil {
  89. return err
  90. }
  91. printAnsiblePlaybookObject(apb)
  92. return nil
  93. },
  94. )
  95. R(
  96. &options.DevtoolTemplateBindingOptions{},
  97. "devtooltemplate-unbind",
  98. "UnBinding devtool template to a host/vm",
  99. func(s *mcclient.ClientSession, opts *options.DevtoolTemplateBindingOptions) error {
  100. params := jsonutils.NewDict()
  101. params.Set("server_id", jsonutils.NewString(opts.ServerID))
  102. apb, err := modules.DevToolTemplates.PerformAction(s, opts.ID, "unbind", params)
  103. if err != nil {
  104. return err
  105. }
  106. printAnsiblePlaybookObject(apb)
  107. return nil
  108. },
  109. )
  110. R(
  111. &options.DevtoolTemplateIdOptions{},
  112. "devtooltemplate-delete",
  113. "Delete devtool template",
  114. func(s *mcclient.ClientSession, opts *options.DevtoolTemplateIdOptions) error {
  115. apb, err := modules.DevToolTemplates.Delete(s, opts.ID, nil)
  116. if err != nil {
  117. return err
  118. }
  119. printAnsiblePlaybookObject(apb)
  120. return nil
  121. },
  122. )
  123. R(
  124. &options.DevtoolTemplateUpdateOptions{},
  125. "devtooltemplate-update",
  126. "Update ansible playbook",
  127. func(s *mcclient.ClientSession, opts *options.DevtoolTemplateUpdateOptions) error {
  128. params, err := opts.Params()
  129. if err != nil {
  130. return err
  131. }
  132. apb, err := modules.DevToolTemplates.Update(s, opts.ID, params)
  133. if err != nil {
  134. return err
  135. }
  136. printAnsiblePlaybookObject(apb)
  137. return nil
  138. },
  139. )
  140. }