emailqueues.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "encoding/base64"
  17. "io/ioutil"
  18. "path/filepath"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. api "yunion.io/x/onecloud/pkg/apis/notify"
  23. "yunion.io/x/onecloud/pkg/mcclient/options"
  24. )
  25. type EmailQueueListOptions struct {
  26. options.BaseListOptions
  27. // Id []int `json:"id"`
  28. To []string `json:"to"`
  29. Subject string `json:"subject"`
  30. SessionId []string `json:"session_id"`
  31. }
  32. func (rl *EmailQueueListOptions) Params() (jsonutils.JSONObject, error) {
  33. return options.ListStructToParams(rl)
  34. }
  35. type EmailQueueCreateOptions struct {
  36. SUBJECT string `help:"email subject"`
  37. BODY string `help:"email body"`
  38. TO []string `json:"to" help:"receiver email"`
  39. Cc []string `json:"cc" help:"cc receivers"`
  40. Bcc []string `json:"bcc" help:"bcc receivers"`
  41. SessionId string `help:"session id of sending email"`
  42. Attach []string `help:"path to attachment"`
  43. }
  44. func (rc *EmailQueueCreateOptions) Params() (jsonutils.JSONObject, error) {
  45. input := api.EmailQueueCreateInput{}
  46. input.To = rc.TO
  47. input.Cc = rc.Cc
  48. input.Bcc = rc.Bcc
  49. input.Subject = rc.SUBJECT
  50. body, err := ioutil.ReadFile(rc.BODY)
  51. if err != nil {
  52. return nil, errors.Wrap(err, "Read content")
  53. }
  54. input.Body = string(body)
  55. for _, attach := range rc.Attach {
  56. contBytes, err := ioutil.ReadFile(attach)
  57. if err != nil {
  58. return nil, errors.Wrapf(err, "read %s", attach)
  59. }
  60. input.Attachments = append(input.Attachments, api.SEmailAttachment{
  61. Filename: filepath.Base(attach),
  62. Base64Content: base64.StdEncoding.EncodeToString(contBytes),
  63. })
  64. }
  65. log.Debugf("%s", jsonutils.Marshal(input))
  66. return jsonutils.Marshal(input), nil
  67. }
  68. type EmailQueueOptions struct {
  69. ID string `help:"Id of email queue" json:"-"`
  70. }
  71. func (r *EmailQueueOptions) GetId() string {
  72. return r.ID
  73. }
  74. func (r *EmailQueueOptions) Params() (jsonutils.JSONObject, error) {
  75. return jsonutils.Marshal(r), nil
  76. }
  77. type EmailQueueSendOptions struct {
  78. EmailQueueOptions
  79. Sync bool `json:"sync" help:"send email synchronously"`
  80. }
  81. func (r *EmailQueueSendOptions) Params() (jsonutils.JSONObject, error) {
  82. return jsonutils.Marshal(r), nil
  83. }