Browse Source

read hosts conf

develop
Robin Thoni 8 years ago
parent
commit
c8a59b3641

+ 11
- 0
src/Business/PdnsSlave.cpp View File

@@ -4,6 +4,7 @@
4 4
 
5 5
 #include <fstream>
6 6
 #include "DataAccess/PdnsSlaveConfig.h"
7
+#include "DataAccess/HostsConfig.h"
7 8
 #include "PdnsSlave.h"
8 9
 
9 10
 PdnsSlave::PdnsSlave(const std::string &filePath)
@@ -41,3 +42,13 @@ BResult PdnsSlave::readDhcpdTemplate()
41 42
     dhcpTemplate.close();
42 43
     return true;
43 44
 }
45
+
46
+Result<Actions> PdnsSlave::readHosts()
47
+{
48
+    HostsConfig conf(_hostsPath);
49
+    auto res = conf.readConfig();
50
+    if (!res)
51
+        return res;
52
+    _actions = res.getData();
53
+    return res;
54
+}

+ 6
- 0
src/Business/PdnsSlave.h View File

@@ -6,7 +6,9 @@
6 6
 #define PDNS_SLAVE_PDNSSLAVE_H
7 7
 
8 8
 # include <string>
9
+# include <memory>
9 10
 # include "DBO/SqlConfiguration.h"
11
+# include "DBO/Actions/Action.h"
10 12
 
11 13
 class PdnsSlave
12 14
 {
@@ -17,6 +19,8 @@ public:
17 19
 
18 20
     BResult readDhcpdTemplate();
19 21
 
22
+    Result<Actions> readHosts();
23
+
20 24
 private:
21 25
     std::string _filePath;
22 26
 
@@ -31,6 +35,8 @@ private:
31 35
     SqlConfiguration _slaveConfig;
32 36
 
33 37
     std::string _dhcpdTemplateContent;
38
+
39
+    Actions _actions;
34 40
 };
35 41
 
36 42
 

+ 15
- 7
src/CMakeLists.txt View File

@@ -6,21 +6,29 @@ set(SOURCE_FILES
6 6
   Interface/MainClass.h
7 7
   Interface/CommandLineParser.cpp
8 8
   Interface/CommandLineParser.h
9
-  DBO/CommandLineOption.cpp
10
-  DBO/CommandLineOption.h
11 9
   Business/PdnsSlave.cpp
12 10
   Business/PdnsSlave.h
13
-  DBO/SqlConfiguration.cpp
14
-  DBO/SqlConfiguration.h
15 11
   DataAccess/PdnsSlaveConfig.cpp
16 12
   DataAccess/PdnsSlaveConfig.h
17 13
   DataAccess/HostsConfig.cpp
18 14
   DataAccess/HostsConfig.h
19
-  DBO/Actions/Action.cpp
20
-  DBO/Actions/Action.h
15
+  DBO/CommandLineOption.cpp
16
+  DBO/CommandLineOption.h
17
+  DBO/SqlConfiguration.cpp
18
+  DBO/SqlConfiguration.h
21 19
   DBO/Result.hxx
22 20
   DBO/Result.h
21
+  DBO/Actions/Action.cpp
22
+  DBO/Actions/Action.h
23
+  DBO/Actions/ActionDelHost.cpp
24
+  DBO/Actions/ActionDelHost.h
25
+  DBO/Actions/ActionAddHost.cpp
26
+  DBO/Actions/ActionAddHost.h
27
+  DBO/Actions/ActionDelDomain.cpp
28
+  DBO/Actions/ActionDelDomain.h
29
+  DBO/Actions/ActionAddDomain.cpp
30
+  DBO/Actions/ActionAddDomain.h
23 31
   )
24 32
 set(LIBS ${LIBS} jsoncpp)
25
-add_executable(pdns-slave ${SOURCE_FILES} DBO/Actions/ActionDelHost.cpp DBO/Actions/ActionDelHost.h DBO/Actions/ActionAddHost.cpp DBO/Actions/ActionAddHost.h)
33
+add_executable(pdns-slave ${SOURCE_FILES})
26 34
 target_link_libraries(pdns-slave ${LIBS})

+ 78
- 28
src/DataAccess/HostsConfig.cpp View File

@@ -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
+}

+ 17
- 3
src/DataAccess/HostsConfig.h View File

@@ -6,17 +6,31 @@
6 6
 #define PDNS_SLAVE_HOSTSCONFIG_H
7 7
 
8 8
 # include <string>
9
-# include <DBO/Result.h>
9
+# include "DBO/Result.h"
10
+# include "DBO/Actions/Action.h"
10 11
 
11 12
 class HostsConfig
12 13
 {
13 14
 public:
14 15
     HostsConfig(const std::string& filePath);
15 16
 
16
-    BResult readConfig();
17
+    Result<Actions> readConfig();
17 18
 
18 19
 private:
19
-    Json::Value readValue(const Json::Value &value, const Json::Value &defaults, const std::string& name);
20
+    Json::Value readValue(const Json::Value &value, const Json::Value &defaults, const std::string& name) const;
21
+
22
+    std::shared_ptr<Action> readAddHost(const Json::Value &act, const Json::Value &defaults,
23
+                                        int i, Result<Actions>& res) const;
24
+
25
+    std::shared_ptr<Action> readDelHost(const Json::Value &act, const Json::Value &defaults,
26
+                                        int i, Result<Actions>& res) const;
27
+
28
+    std::shared_ptr<Action> readAddDomain(const Json::Value &act, const Json::Value &defaults,
29
+                                        int i, Result<Actions>& res) const;
30
+
31
+    std::shared_ptr<Action> readDelDomain(const Json::Value &act, const Json::Value &defaults,
32
+                                        int i, Result<Actions>& res) const;
33
+
20 34
     std::string _filePath;
21 35
 };
22 36
 

+ 16
- 0
src/Interface/MainClass.cpp View File

@@ -33,18 +33,34 @@ int MainClass::main()
33 33
 
34 34
     PdnsSlave pdnsSlave(filePath);
35 35
     BResult res;
36
+    Result<Actions> res2;
37
+
38
+    std::cout << "Reading pdns-slave configuration" << std::endl;
36 39
     if (!(res = pdnsSlave.readConfig()))
37 40
     {
38 41
         std::cerr << "Failed to read pdns-slave configuration" << std::endl;
39 42
         res.print();
40 43
         return 1;
41 44
     }
45
+    res.print();
46
+
47
+    std::cout << "Reading dhcpd template" << std::endl;
42 48
     if (!(res = pdnsSlave.readDhcpdTemplate()))
43 49
     {
44 50
         std::cerr << "Failed to read dhcpd template" << std::endl;
45 51
         res.print();
46 52
         return 2;
47 53
     }
54
+    res.print();
55
+
56
+    std::cout << "Reading hosts configuration" << std::endl;
57
+    if (!(res2 = pdnsSlave.readHosts()))
58
+    {
59
+        std::cerr << "Failed to read hosts configuration" << std::endl;
60
+        res2.print();
61
+        return 3;
62
+    }
63
+    res2.print();
48 64
 
49 65
     return 0;
50 66
 }

Loading…
Cancel
Save