Browse Source

read hosts conf

develop
Robin Thoni 9 years ago
parent
commit
c8a59b3641

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

4
 
4
 
5
 #include <fstream>
5
 #include <fstream>
6
 #include "DataAccess/PdnsSlaveConfig.h"
6
 #include "DataAccess/PdnsSlaveConfig.h"
7
+#include "DataAccess/HostsConfig.h"
7
 #include "PdnsSlave.h"
8
 #include "PdnsSlave.h"
8
 
9
 
9
 PdnsSlave::PdnsSlave(const std::string &filePath)
10
 PdnsSlave::PdnsSlave(const std::string &filePath)
41
     dhcpTemplate.close();
42
     dhcpTemplate.close();
42
     return true;
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
 #define PDNS_SLAVE_PDNSSLAVE_H
6
 #define PDNS_SLAVE_PDNSSLAVE_H
7
 
7
 
8
 # include <string>
8
 # include <string>
9
+# include <memory>
9
 # include "DBO/SqlConfiguration.h"
10
 # include "DBO/SqlConfiguration.h"
11
+# include "DBO/Actions/Action.h"
10
 
12
 
11
 class PdnsSlave
13
 class PdnsSlave
12
 {
14
 {
17
 
19
 
18
     BResult readDhcpdTemplate();
20
     BResult readDhcpdTemplate();
19
 
21
 
22
+    Result<Actions> readHosts();
23
+
20
 private:
24
 private:
21
     std::string _filePath;
25
     std::string _filePath;
22
 
26
 
31
     SqlConfiguration _slaveConfig;
35
     SqlConfiguration _slaveConfig;
32
 
36
 
33
     std::string _dhcpdTemplateContent;
37
     std::string _dhcpdTemplateContent;
38
+
39
+    Actions _actions;
34
 };
40
 };
35
 
41
 
36
 
42
 

+ 15
- 7
src/CMakeLists.txt View File

6
   Interface/MainClass.h
6
   Interface/MainClass.h
7
   Interface/CommandLineParser.cpp
7
   Interface/CommandLineParser.cpp
8
   Interface/CommandLineParser.h
8
   Interface/CommandLineParser.h
9
-  DBO/CommandLineOption.cpp
10
-  DBO/CommandLineOption.h
11
   Business/PdnsSlave.cpp
9
   Business/PdnsSlave.cpp
12
   Business/PdnsSlave.h
10
   Business/PdnsSlave.h
13
-  DBO/SqlConfiguration.cpp
14
-  DBO/SqlConfiguration.h
15
   DataAccess/PdnsSlaveConfig.cpp
11
   DataAccess/PdnsSlaveConfig.cpp
16
   DataAccess/PdnsSlaveConfig.h
12
   DataAccess/PdnsSlaveConfig.h
17
   DataAccess/HostsConfig.cpp
13
   DataAccess/HostsConfig.cpp
18
   DataAccess/HostsConfig.h
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
   DBO/Result.hxx
19
   DBO/Result.hxx
22
   DBO/Result.h
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
 set(LIBS ${LIBS} jsoncpp)
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
 target_link_libraries(pdns-slave ${LIBS})
34
 target_link_libraries(pdns-slave ${LIBS})

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

8
 #include <DBO/Actions/Action.h>
8
 #include <DBO/Actions/Action.h>
9
 #include <DBO/Actions/ActionAddHost.h>
9
 #include <DBO/Actions/ActionAddHost.h>
10
 #include <DBO/Actions/ActionDelHost.h>
10
 #include <DBO/Actions/ActionDelHost.h>
11
+#include <DBO/Actions/ActionAddDomain.h>
12
+#include <DBO/Actions/ActionDelDomain.h>
11
 #include "HostsConfig.h"
13
 #include "HostsConfig.h"
12
 
14
 
13
 #define SET_VALUE(VALUE, METHOD, TYPE) do {             \
15
 #define SET_VALUE(VALUE, METHOD, TYPE) do {             \
16
         auto v = readValue(act, defaults, VALUE);       \
18
         auto v = readValue(act, defaults, VALUE);       \
17
         if (!v || !v.is##TYPE())                        \
19
         if (!v || !v.is##TYPE())                        \
18
         {                                               \
20
         {                                               \
21
+            res.addWarning("Could not find value "      \
22
+                VALUE " for item " + i);                \
19
             action = nullptr;                           \
23
             action = nullptr;                           \
20
         }                                               \
24
         }                                               \
21
         else                                            \
25
         else                                            \
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
     std::ifstream file(_filePath);
44
     std::ifstream file(_filePath);
41
     if (!file)
45
     if (!file)
42
         return res.error("Could not open file");
46
         return res.error("Could not open file");
52
         return res.error("Could not parse JSON");
56
         return res.error("Could not parse JSON");
53
     }
57
     }
54
     auto defaults = root["defaults"];
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
         auto action_type = readValue(act, defaults, "action");
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
         else
71
         else
63
         {
72
         {
64
             auto action_str = action_type.asString();
73
             auto action_str = action_type.asString();
65
-            std::shared_ptr<Action> actionAbs = nullptr;
66
             if (action_str == "add_domain")
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
             else if (action_str == "del_domain")
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
             else if (action_str == "add_host")
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
             else if (action_str == "del_host")
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
             else
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
     auto v = value[name];
115
     auto v = value[name];
105
     if (!v)
116
     if (!v)
106
         return defaults[name];
117
         return defaults[name];
107
     return v;
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
 #define PDNS_SLAVE_HOSTSCONFIG_H
6
 #define PDNS_SLAVE_HOSTSCONFIG_H
7
 
7
 
8
 # include <string>
8
 # include <string>
9
-# include <DBO/Result.h>
9
+# include "DBO/Result.h"
10
+# include "DBO/Actions/Action.h"
10
 
11
 
11
 class HostsConfig
12
 class HostsConfig
12
 {
13
 {
13
 public:
14
 public:
14
     HostsConfig(const std::string& filePath);
15
     HostsConfig(const std::string& filePath);
15
 
16
 
16
-    BResult readConfig();
17
+    Result<Actions> readConfig();
17
 
18
 
18
 private:
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
     std::string _filePath;
34
     std::string _filePath;
21
 };
35
 };
22
 
36
 

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

33
 
33
 
34
     PdnsSlave pdnsSlave(filePath);
34
     PdnsSlave pdnsSlave(filePath);
35
     BResult res;
35
     BResult res;
36
+    Result<Actions> res2;
37
+
38
+    std::cout << "Reading pdns-slave configuration" << std::endl;
36
     if (!(res = pdnsSlave.readConfig()))
39
     if (!(res = pdnsSlave.readConfig()))
37
     {
40
     {
38
         std::cerr << "Failed to read pdns-slave configuration" << std::endl;
41
         std::cerr << "Failed to read pdns-slave configuration" << std::endl;
39
         res.print();
42
         res.print();
40
         return 1;
43
         return 1;
41
     }
44
     }
45
+    res.print();
46
+
47
+    std::cout << "Reading dhcpd template" << std::endl;
42
     if (!(res = pdnsSlave.readDhcpdTemplate()))
48
     if (!(res = pdnsSlave.readDhcpdTemplate()))
43
     {
49
     {
44
         std::cerr << "Failed to read dhcpd template" << std::endl;
50
         std::cerr << "Failed to read dhcpd template" << std::endl;
45
         res.print();
51
         res.print();
46
         return 2;
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
     return 0;
65
     return 0;
50
 }
66
 }

Loading…
Cancel
Save