template.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 notify
  15. import (
  16. "yunion.io/x/jsonutils"
  17. api "yunion.io/x/onecloud/pkg/apis/notify"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. modules "yunion.io/x/onecloud/pkg/mcclient/modules/notify"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. )
  22. func init() {
  23. type TemplateCreateInput struct {
  24. ContactType string `help:"Contact type, specifically, setting it to all means all contact type"`
  25. TemplateType string `help:"Template type"`
  26. Topic string `help:"Template topic"`
  27. Content string `help:"Template content"`
  28. Example string `help:"Example for using this template"`
  29. Lang string `help:"Language of Template"`
  30. }
  31. R(&TemplateCreateInput{}, "notify-template-create", "Create notify template", func(s *mcclient.ClientSession, args *TemplateCreateInput) error {
  32. input := api.TemplateCreateInput{
  33. ContactType: args.ContactType,
  34. TemplateType: args.TemplateType,
  35. Topic: args.Topic,
  36. Content: args.Content,
  37. Example: args.Example,
  38. Lang: args.Lang,
  39. }
  40. ret, err := modules.NotifyTemplate.Create(s, jsonutils.Marshal(input))
  41. if err != nil {
  42. return err
  43. }
  44. printObject(ret)
  45. return nil
  46. })
  47. type TemplateSaveInput struct {
  48. ContactType string `help:"contact type" positional:"true"`
  49. Force bool `help:"Whether to force the update of existing templates"`
  50. TemplateType string `help:"Template type"`
  51. Topic string `help:"Template topic"`
  52. Content string `help:"Template content"`
  53. Example string `help:"Example for using this template"`
  54. Lang string `help:"Language of Template"`
  55. }
  56. R(&TemplateSaveInput{}, "notify-template-save", "Save notify templates", func(s *mcclient.ClientSession, args *TemplateSaveInput) error {
  57. templates := []api.TemplateCreateInput{
  58. {
  59. ContactType: args.ContactType,
  60. TemplateType: args.TemplateType,
  61. Topic: args.Topic,
  62. Content: args.Content,
  63. Example: args.Example,
  64. Lang: args.Lang,
  65. },
  66. }
  67. input := api.TemplateManagerSaveInput{
  68. ContactType: args.ContactType,
  69. Templates: templates,
  70. Force: args.Force,
  71. }
  72. ret, err := modules.NotifyTemplate.PerformClassAction(s, "save", jsonutils.Marshal(input))
  73. if err != nil {
  74. return err
  75. }
  76. printObject(ret)
  77. return nil
  78. })
  79. type TemplateListInput struct {
  80. options.BaseListOptions
  81. ContactType string `help:"Contact type"`
  82. TemplateType string `help:"Template type"`
  83. Topic string `help:"Topic"`
  84. Lang string `help:"Lang"`
  85. }
  86. R(&TemplateListInput{}, "notify-template-list", "List notify template", func(s *mcclient.ClientSession, args *TemplateListInput) error {
  87. params, err := options.ListStructToParams(args)
  88. if err != nil {
  89. return err
  90. }
  91. list, err := modules.NotifyTemplate.List(s, params)
  92. if err != nil {
  93. return err
  94. }
  95. printList(list, modules.NotifyTemplate.GetColumns(s))
  96. return nil
  97. })
  98. type TemplateInput struct {
  99. ID string `help:"id or name of template"`
  100. }
  101. R(&TemplateInput{}, "notify-template-get", "Get notify template", func(s *mcclient.ClientSession, args *TemplateInput) error {
  102. ret, err := modules.NotifyTemplate.Get(s, args.ID, nil)
  103. if err != nil {
  104. return err
  105. }
  106. printObject(ret)
  107. return nil
  108. })
  109. type TemplateUpdateInput struct {
  110. ID string `help:"id or name of template"`
  111. Content string `help:"Template content"`
  112. Example string `help:"Example for using this template"`
  113. }
  114. R(&TemplateUpdateInput{}, "notify-template-update", "Update notify template", func(s *mcclient.ClientSession, args *TemplateUpdateInput) error {
  115. input := api.TemplateUpdateInput{
  116. Content: args.Content,
  117. Example: args.Example,
  118. }
  119. ret, err := modules.NotifyTemplate.Update(s, args.ID, jsonutils.Marshal(input))
  120. if err != nil {
  121. return err
  122. }
  123. printObject(ret)
  124. return nil
  125. })
  126. R(&TemplateInput{}, "notify-template-delete", "Delete notify template", func(s *mcclient.ClientSession, args *TemplateInput) error {
  127. ret, err := modules.NotifyTemplate.Delete(s, args.ID, nil)
  128. if err != nil {
  129. return err
  130. }
  131. printObject(ret)
  132. return nil
  133. })
  134. }