summaryrefslogtreecommitdiff
path: root/meta-ibs/meta-cp2-5422/recipes-phosphor/interfaces/bmcweb/0020-set-smtp-send-post-request.patch
blob: 557607ecf00be984715d016a4c1dcf64a5223fcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
From 9a477a7f1c8c7fcb8a4206fd6b8be3c6cb52d2b7 Mon Sep 17 00:00:00 2001
From: claiff <claiff@mail.ru>
Date: Mon, 10 Oct 2022 17:42:28 +0300
Subject: [PATCH] set smtp send post request

---
 redfish-core/include/redfish.hpp |  3 +-
 redfish-core/lib/smtp.hpp        | 72 ++++++++++++++++++++++++--------
 2 files changed, 56 insertions(+), 19 deletions(-)

diff --git a/redfish-core/include/redfish.hpp b/redfish-core/include/redfish.hpp
index ebaabdfa..494d6709 100644
--- a/redfish-core/include/redfish.hpp
+++ b/redfish-core/include/redfish.hpp
@@ -229,7 +229,8 @@ class RedfishService
         requestRoutesTriggerCollection(app);
         requestRoutesTrigger(app);
         requestRoutesSmtp(app);
-        requestSmtpFunctions(app);
+        requestSmtpGetFunctions(app);
+        requestSmtpPostFunctions(app);
 
         requestRoutesSyslog(app);
         requestRoutesSyslogUpdateConfig(app);
diff --git a/redfish-core/lib/smtp.hpp b/redfish-core/lib/smtp.hpp
index 49ac9340..83bbf9b5 100644
--- a/redfish-core/lib/smtp.hpp
+++ b/redfish-core/lib/smtp.hpp
@@ -33,9 +33,10 @@ namespace redfish
             return function_with_parameters.substr( ZERO_POSITION, position );
         }
 
-        inline void SendMail( std::string const& function_with_parameters, std::shared_ptr<bmcweb::AsyncResp> const& asyncResp )
+        inline void SendMail( std::string const& from, std::string const& subject,
+                              std::string const& text, std::shared_ptr<bmcweb::AsyncResp> const& asyncResp )
         {
-            auto parsing_result = parser::SendMessage{function_with_parameters};
+            static const std::string SEND_MAIL_METHOD_NAME = "SendMail";
 
             crow::connections::systemBus->async_method_call([asyncResp](const boost::system::error_code& error_code, bool result){
                     if (error_code.value() == EBADR ||
@@ -54,10 +55,10 @@ namespace redfish
                     asyncResp->res.jsonValue["result"] = result;
                 },
                 SERVICE_PATH, OBJECT_MESSENGER_PATH, INTERFACE_MESSENGER_PATH,
-                parsing_result.GetMethodName(),
-                parsing_result.GetFrom(),
-                parsing_result.GetSubject(),
-                parsing_result.GetText());
+                SEND_MAIL_METHOD_NAME,
+                from,
+                subject,
+                text);
         }
 
         inline void SetSettings( std::string const& function_with_parameters, std::shared_ptr<bmcweb::AsyncResp> const& asyncResp )
@@ -236,18 +237,56 @@ namespace redfish
         });
     }
 
+    inline void requestSmtpPostFunctions(App& app)
+    {
+        BMCWEB_ROUTE(app, "/redfish/v1/Smtp/<str>/")
+        .privileges(redfish::privileges::getSmtpCollection)
+        .methods(boost::beast::http::verb::post)(
+        [&app](const crow::Request& req,
+               const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+               const std::string& params)
+        {
+            static constexpr std::string_view SEND_MAIL_METHOD_NAME = "SendMail";
+            static constexpr std::string_view FROM_FIELD = "from";
+            static constexpr std::string_view SUBJECT_FIELD = "subject";
+            static constexpr std::string_view TEXT_FIELD = "text";
+
+            if (!redfish::setUpRedfishRoute(app, req, asyncResp))
+            {
+                return;
+            }
+
+            if(params != SEND_MAIL_METHOD_NAME)
+            {
+                asyncResp->res.jsonValue["result"] = false;
+                return;
+            }
+
+            std::string from, subject, text;
+            if (!json_util::readJsonPatch(req, asyncResp->res,
+                                          FROM_FIELD, from,
+                                          SUBJECT_FIELD, subject,
+                                          TEXT_FIELD, text))
+            {
+                asyncResp->res.jsonValue["result"] = false;
+                return;
+            }
+            smtp::SendMail(from, subject, text, asyncResp);
+            return;
 
+        });
+    }
 
-    inline void requestSmtpFunctions(App& app)
+    inline void requestSmtpGetFunctions(App& app)
     {
         BMCWEB_ROUTE(app, "/redfish/v1/Smtp/<str>/")
         .privileges(redfish::privileges::getSmtpCollection)
         .methods(boost::beast::http::verb::get)(
         [&app](const crow::Request& req,
                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-               const std::string& function_with_parameters)
+               const std::string& params)
         {
-            static constexpr std::string_view SEND_MAIL_METHOD_NAME = "SendMail";
+
 
             static constexpr std::string_view GET_SETTINGS_METHOD_NAME = "GetSettings";
             static constexpr std::string_view SET_SETTINGS_METHOD_NAME = "SetSettings";
@@ -260,19 +299,16 @@ namespace redfish
             {
                 return;
             }
-            auto function_name = smtp::GetFunctionName(function_with_parameters);
+
+            auto function_name = smtp::GetFunctionName( params );
 
             asyncResp->res.jsonValue["@odata.type"] ="#SmtpCollection";
             asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Smtp/" + function_name;
             asyncResp->res.jsonValue["Name"] = "Smtp Collection";
 
-            if(function_name == SEND_MAIL_METHOD_NAME)
-            {
-                smtp::SendMail(function_with_parameters, asyncResp);
-            }
-            else if(function_name == SET_SETTINGS_METHOD_NAME)
+            if(function_name == SET_SETTINGS_METHOD_NAME)
             {
-                smtp::SetSettings(function_with_parameters, asyncResp);
+                smtp::SetSettings(params, asyncResp);
             }
             else if(function_name == GET_SETTINGS_METHOD_NAME)
             {
@@ -284,11 +320,11 @@ namespace redfish
             }
             else if(function_name == ADD_MAILS_METHOD_NAME)
             {
-                smtp::AddMails(function_with_parameters, asyncResp);
+                smtp::AddMails(params, asyncResp);
             }
             else if(function_name == DELETE_MAILS_METHOD_NAME)
             {
-                smtp::DeleteMails(function_with_parameters, asyncResp);
+                smtp::DeleteMails(params, asyncResp);
             }
         });
     }