robot.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/tristate"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. type RobotListOptions struct {
  22. options.BaseListOptions
  23. Lang string
  24. Type string `choices:"feishu|dingtalk|workwx|webhook"`
  25. Enabled *bool
  26. }
  27. func (rl *RobotListOptions) Params() (jsonutils.JSONObject, error) {
  28. return options.ListStructToParams(rl)
  29. }
  30. type RobotCreateOptions struct {
  31. NAME string
  32. Type string `choices:"feishu|dingtalk|workwx|webhook"`
  33. Address string
  34. Lang string
  35. Header string
  36. Body string
  37. MsgKey string
  38. SecretKey string
  39. UseTemplate bool `help:"just for webhook"`
  40. }
  41. func (rc *RobotCreateOptions) Params() (jsonutils.JSONObject, error) {
  42. dict := jsonutils.NewDict()
  43. jsonutils.Update(&dict, rc)
  44. if len(rc.Header) > 0 {
  45. header, err := jsonutils.Parse([]byte(rc.Header))
  46. if err != nil {
  47. return nil, errors.Wrap(err, "parse header")
  48. }
  49. dict.Set("header", header)
  50. }
  51. if len(rc.Body) > 0 {
  52. body, err := jsonutils.Parse([]byte(rc.Body))
  53. if err != nil {
  54. return nil, errors.Wrap(err, "parse body")
  55. }
  56. dict.Set("body", body)
  57. }
  58. dict.Set("use_template", jsonutils.Marshal(rc.UseTemplate))
  59. return dict, nil
  60. }
  61. type RobotOptions struct {
  62. ID string `help:"Id or Name of robot"`
  63. }
  64. func (r *RobotOptions) GetId() string {
  65. return r.ID
  66. }
  67. func (r *RobotOptions) Params() (jsonutils.JSONObject, error) {
  68. return nil, nil
  69. }
  70. type RobotUpdateOptions struct {
  71. RobotOptions
  72. SrobotUpdateOptions
  73. }
  74. type SrobotUpdateOptions struct {
  75. Address string
  76. Lang string
  77. Header *string
  78. Body *string
  79. MsgKey string
  80. SecretKey string
  81. UseTemplate tristate.TriState
  82. }
  83. func (ru *RobotUpdateOptions) Params() (jsonutils.JSONObject, error) {
  84. dict := jsonutils.NewDict()
  85. jsonutils.Update(&dict, ru)
  86. if ru.Header != nil {
  87. header, err := jsonutils.Parse([]byte(*ru.Header))
  88. if err != nil {
  89. return nil, errors.Wrap(err, "parse header")
  90. }
  91. dict.Set("header", header)
  92. }
  93. if ru.Body != nil {
  94. body, err := jsonutils.Parse([]byte(*ru.Body))
  95. if err != nil {
  96. return nil, errors.Wrap(err, "parse body")
  97. }
  98. dict.Set("body", body)
  99. }
  100. if ru.UseTemplate.IsFalse() {
  101. dict.Set("use_template", jsonutils.JSONFalse)
  102. } else if ru.UseTemplate.IsTrue() {
  103. dict.Set("use_template", jsonutils.JSONTrue)
  104. }
  105. return dict, nil
  106. }