|
@@ -8,6 +8,8 @@
|
8
|
8
|
#include <DBO/Actions/Action.h>
|
9
|
9
|
#include <DBO/Actions/ActionAddHost.h>
|
10
|
10
|
#include <DBO/Actions/ActionDelHost.h>
|
|
11
|
+#include <DBO/Actions/ActionAddDomain.h>
|
|
12
|
+#include <DBO/Actions/ActionDelDomain.h>
|
11
|
13
|
#include "HostsConfig.h"
|
12
|
14
|
|
13
|
15
|
#define SET_VALUE(VALUE, METHOD, TYPE) do { \
|
|
@@ -16,6 +18,8 @@
|
16
|
18
|
auto v = readValue(act, defaults, VALUE); \
|
17
|
19
|
if (!v || !v.is##TYPE()) \
|
18
|
20
|
{ \
|
|
21
|
+ res.addWarning("Could not find value " \
|
|
22
|
+ VALUE " for item " + i); \
|
19
|
23
|
action = nullptr; \
|
20
|
24
|
} \
|
21
|
25
|
else \
|
|
@@ -34,9 +38,9 @@ HostsConfig::HostsConfig(const std::string &filePath)
|
34
|
38
|
{
|
35
|
39
|
}
|
36
|
40
|
|
37
|
|
-BResult HostsConfig::readConfig()
|
|
41
|
+Result<Actions> HostsConfig::readConfig()
|
38
|
42
|
{
|
39
|
|
- BResult res;
|
|
43
|
+ Result<Actions> res;
|
40
|
44
|
std::ifstream file(_filePath);
|
41
|
45
|
if (!file)
|
42
|
46
|
return res.error("Could not open file");
|
|
@@ -52,57 +56,103 @@ BResult HostsConfig::readConfig()
|
52
|
56
|
return res.error("Could not parse JSON");
|
53
|
57
|
}
|
54
|
58
|
auto defaults = root["defaults"];
|
55
|
|
- auto actions = root["actions"];
|
56
|
|
- for (Json::ArrayIndex i = 0; i < actions.size(); ++i)
|
|
59
|
+ if (!defaults || !defaults.isObject())
|
|
60
|
+ return res.error("Could not find defaults object");
|
|
61
|
+ auto acts = root["actions"];
|
|
62
|
+ if (!acts || !acts.isArray())
|
|
63
|
+ return res.error("Could not find actions array");
|
|
64
|
+ std::vector<std::shared_ptr<Action>> actions;
|
|
65
|
+ for (Json::ArrayIndex i = 0; i < acts.size(); ++i)
|
57
|
66
|
{
|
58
|
|
- auto act = actions[i];
|
|
67
|
+ auto act = acts[i];
|
59
|
68
|
auto action_type = readValue(act, defaults, "action");
|
60
|
|
- if (!action_type)
|
61
|
|
- res.addWarning("Could no find action value for item " + i);
|
|
69
|
+ if (!action_type || !action_type.isString())
|
|
70
|
+ res.addWarning("Could not find action value for item " + std::to_string(i));
|
62
|
71
|
else
|
63
|
72
|
{
|
64
|
73
|
auto action_str = action_type.asString();
|
65
|
|
- std::shared_ptr<Action> actionAbs = nullptr;
|
66
|
74
|
if (action_str == "add_domain")
|
67
|
75
|
{
|
68
|
|
-
|
|
76
|
+ auto del = readDelDomain(act, defaults, i, res);
|
|
77
|
+ auto add = readAddDomain(act, defaults, i, res);
|
|
78
|
+ if (del && add)
|
|
79
|
+ {
|
|
80
|
+ actions.push_back(del);
|
|
81
|
+ actions.push_back(add);
|
|
82
|
+ }
|
69
|
83
|
}
|
70
|
84
|
else if (action_str == "del_domain")
|
71
|
85
|
{
|
72
|
|
-
|
|
86
|
+ auto del = readDelDomain(act, defaults, i, res);
|
|
87
|
+ if (del)
|
|
88
|
+ actions.push_back(del);
|
73
|
89
|
}
|
74
|
90
|
else if (action_str == "add_host")
|
75
|
91
|
{
|
76
|
|
- auto action = std::make_shared<ActionAddHost>();
|
77
|
|
- SET_VALUE_STRING("host", setHost);
|
78
|
|
- SET_VALUE_STRING("record_value", setRecordValue);
|
79
|
|
- SET_VALUE_STRING("record_type", setRecordType);
|
80
|
|
- actionAbs = action;
|
|
92
|
+ auto del = readDelHost(act, defaults, i, res);
|
|
93
|
+ auto add = readAddHost(act, defaults, i, res);
|
|
94
|
+ if (del && add)
|
|
95
|
+ {
|
|
96
|
+ actions.push_back(del);
|
|
97
|
+ actions.push_back(add);
|
|
98
|
+ }
|
81
|
99
|
}
|
82
|
100
|
else if (action_str == "del_host")
|
83
|
101
|
{
|
84
|
|
- auto action = std::make_shared<ActionDelHost>();
|
85
|
|
- SET_VALUE_STRING("host", setHost);
|
86
|
|
- actionAbs = action;
|
87
|
|
- }
|
88
|
|
- else
|
89
|
|
- res.addWarning("Unkown action " + action_str);
|
90
|
|
- if (actionAbs)
|
91
|
|
- {
|
92
|
|
- auto action = actionAbs;
|
93
|
|
- SET_VALUE_STRING("domain", setDomain);
|
|
102
|
+ auto del = readDelHost(act, defaults, i, res);
|
|
103
|
+ if (del)
|
|
104
|
+ actions.push_back(del);
|
94
|
105
|
}
|
95
|
106
|
else
|
96
|
|
- res.addWarning("Errors occured while processing item " + i);
|
|
107
|
+ res.addWarning("Unknown action " + action_str);
|
97
|
108
|
}
|
98
|
109
|
}
|
99
|
|
- return true;
|
|
110
|
+ return res.ok(actions);
|
100
|
111
|
}
|
101
|
112
|
|
102
|
|
-Json::Value HostsConfig::readValue(const Json::Value &value, const Json::Value &defaults, const std::string &name)
|
|
113
|
+Json::Value HostsConfig::readValue(const Json::Value &value, const Json::Value &defaults, const std::string &name) const
|
103
|
114
|
{
|
104
|
115
|
auto v = value[name];
|
105
|
116
|
if (!v)
|
106
|
117
|
return defaults[name];
|
107
|
118
|
return v;
|
108
|
119
|
}
|
|
120
|
+
|
|
121
|
+std::shared_ptr<Action> HostsConfig::readAddHost(const Json::Value &act, const Json::Value &defaults,
|
|
122
|
+ int i, Result<Actions>& res) const
|
|
123
|
+{
|
|
124
|
+ auto action = std::make_shared<ActionAddHost>();
|
|
125
|
+ SET_VALUE_STRING("domain", setDomain);
|
|
126
|
+ SET_VALUE_STRING("host", setHost);
|
|
127
|
+ SET_VALUE_STRING("record_value", setRecordValue);
|
|
128
|
+ SET_VALUE_STRING("record_type", setRecordType);
|
|
129
|
+ SET_VALUE_STRING("dhcp_mac", setDhcpMac);
|
|
130
|
+ SET_VALUE_STRING("reverse_domain", setReverseDomain);
|
|
131
|
+ SET_VALUE_BOOL("reverse_enabled", setReverseEnabled);
|
|
132
|
+ return action;
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+std::shared_ptr<Action> HostsConfig::readDelHost(const Json::Value &act, const Json::Value &defaults, int i,
|
|
136
|
+ Result<Actions> &res) const
|
|
137
|
+{
|
|
138
|
+ auto action = std::make_shared<ActionDelHost>();
|
|
139
|
+ SET_VALUE_STRING("domain", setDomain);
|
|
140
|
+ SET_VALUE_STRING("host", setHost);
|
|
141
|
+ return action;
|
|
142
|
+}
|
|
143
|
+
|
|
144
|
+std::shared_ptr<Action> HostsConfig::readAddDomain(const Json::Value &act, const Json::Value &defaults, int i,
|
|
145
|
+ Result<Actions> &res) const
|
|
146
|
+{
|
|
147
|
+ auto action = std::make_shared<ActionAddDomain>();
|
|
148
|
+ SET_VALUE_STRING("domain", setDomain);
|
|
149
|
+ return action;
|
|
150
|
+}
|
|
151
|
+
|
|
152
|
+std::shared_ptr<Action> HostsConfig::readDelDomain(const Json::Value &act, const Json::Value &defaults, int i,
|
|
153
|
+ Result<Actions> &res) const
|
|
154
|
+{
|
|
155
|
+ auto action = std::make_shared<ActionDelDomain>();
|
|
156
|
+ SET_VALUE_STRING("domain", setDomain);
|
|
157
|
+ return action;
|
|
158
|
+}
|