1 /*
2  * Copyright 2019 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "buildjson.hpp"
17 
18 #include "file_handler.hpp"
19 #include "general_systemd.hpp"
20 
21 #include <nlohmann/json.hpp>
22 #include <sdbusplus/bus.hpp>
23 
24 #include <algorithm>
25 #include <cstdio>
26 #include <exception>
27 #include <fstream>
28 #include <string>
29 
30 namespace ipmi_flash
31 {
32 
33 std::unique_ptr<TriggerableActionInterface>
34     buildFileSystemd(const nlohmann::json& data)
35 {
36     /* This type of action requires a path and unit, and optionally a mode. */
37     const auto& path = data.at("path");
38     const auto& unit = data.at("unit");
39 
40     /* the mode parameter is optional. */
41     std::string systemdMode = "replace";
42     const auto& mode = data.find("mode");
43     if (mode != data.end())
44     {
45         systemdMode = data.at("mode").get<std::string>();
46     }
47 
48     return SystemdWithStatusFile::CreateSystemdWithStatusFile(
49         sdbusplus::bus::new_default(), path, unit, systemdMode);
50 }
51 
52 std::unique_ptr<TriggerableActionInterface>
53     buildSystemd(const nlohmann::json& data)
54 {
55     /* This type of action requires a unit, and optionally a mode. */
56     const auto& unit = data.at("unit");
57 
58     /* the mode parameter is optional. */
59     std::string systemdMode = "replace";
60     const auto& mode = data.find("mode");
61     if (mode != data.end())
62     {
63         systemdMode = data.at("mode").get<std::string>();
64     }
65 
66     return SystemdNoFile::CreateSystemdNoFile(sdbusplus::bus::new_default(),
67                                               unit, systemdMode);
68 }
69 
70 } // namespace ipmi_flash
71