Просмотр исходного кода

use Result class; begin hosts parse

develop
Robin Thoni 8 лет назад
Родитель
Сommit
eee5c0f847

+ 6
- 5
src/Business/PdnsSlave.cpp Просмотреть файл

11
 {
11
 {
12
 }
12
 }
13
 
13
 
14
-bool PdnsSlave::readConfig()
14
+BResult PdnsSlave::readConfig()
15
 {
15
 {
16
     PdnsSlaveConfig conf(_filePath);
16
     PdnsSlaveConfig conf(_filePath);
17
-    if (!conf.readConfig())
18
-        return false;
17
+    BResult res;
18
+    if (!(res = conf.readConfig()))
19
+        return res;
19
 
20
 
20
     _dhcpdFilePath = conf.getDhcpdFilePath();
21
     _dhcpdFilePath = conf.getDhcpdFilePath();
21
     _dhcpdTemplatePath = conf.getDhcpdTemplatePath();
22
     _dhcpdTemplatePath = conf.getDhcpdTemplatePath();
26
     return true;
27
     return true;
27
 }
28
 }
28
 
29
 
29
-bool PdnsSlave::readDhcpdTemplate()
30
+BResult PdnsSlave::readDhcpdTemplate()
30
 {
31
 {
31
     std::ifstream dhcpTemplate(_dhcpdTemplatePath);
32
     std::ifstream dhcpTemplate(_dhcpdTemplatePath);
32
     if (!dhcpTemplate)
33
     if (!dhcpTemplate)
33
-        return false;
34
+        return BResult().error("Could not open file");
34
     while (!dhcpTemplate.eof())
35
     while (!dhcpTemplate.eof())
35
     {
36
     {
36
         std::string line;
37
         std::string line;

+ 2
- 2
src/Business/PdnsSlave.h Просмотреть файл

13
 public:
13
 public:
14
     PdnsSlave(const std::string& filePath);
14
     PdnsSlave(const std::string& filePath);
15
 
15
 
16
-    bool readConfig();
16
+    BResult readConfig();
17
 
17
 
18
-    bool readDhcpdTemplate();
18
+    BResult readDhcpdTemplate();
19
 
19
 
20
 private:
20
 private:
21
     std::string _filePath;
21
     std::string _filePath;

+ 7
- 1
src/CMakeLists.txt Просмотреть файл

14
   DBO/SqlConfiguration.h
14
   DBO/SqlConfiguration.h
15
   DataAccess/PdnsSlaveConfig.cpp
15
   DataAccess/PdnsSlaveConfig.cpp
16
   DataAccess/PdnsSlaveConfig.h
16
   DataAccess/PdnsSlaveConfig.h
17
+  DataAccess/HostsConfig.cpp
18
+  DataAccess/HostsConfig.h
19
+  DBO/Actions/Action.cpp
20
+  DBO/Actions/Action.h
21
+  DBO/Result.hxx
22
+  DBO/Result.h
17
   )
23
   )
18
 set(LIBS ${LIBS} jsoncpp)
24
 set(LIBS ${LIBS} jsoncpp)
19
-add_executable(pdns-slave ${SOURCE_FILES} DataAccess/HostsConfig.cpp DataAccess/HostsConfig.h)
25
+add_executable(pdns-slave ${SOURCE_FILES} DBO/Actions/ActionDelHost.cpp DBO/Actions/ActionDelHost.h DBO/Actions/ActionAddHost.cpp DBO/Actions/ActionAddHost.h)
20
 target_link_libraries(pdns-slave ${LIBS})
26
 target_link_libraries(pdns-slave ${LIBS})

+ 67
- 5
src/DataAccess/HostsConfig.cpp Просмотреть файл

4
 
4
 
5
 #include <json/json.h>
5
 #include <json/json.h>
6
 #include <fstream>
6
 #include <fstream>
7
+#include <memory>
8
+#include <DBO/Actions/Action.h>
9
+#include <DBO/Actions/ActionAddHost.h>
10
+#include <DBO/Actions/ActionDelHost.h>
7
 #include "HostsConfig.h"
11
 #include "HostsConfig.h"
8
 
12
 
13
+#define SET_VALUE(VALUE, METHOD, TYPE) do {             \
14
+    if (action)                                         \
15
+    {                                                   \
16
+        auto v = readValue(act, defaults, VALUE);       \
17
+        if (!v || !v.is##TYPE())                        \
18
+        {                                               \
19
+            action = nullptr;                           \
20
+        }                                               \
21
+        else                                            \
22
+        {                                               \
23
+            action->METHOD(v.as##TYPE());               \
24
+        }                                               \
25
+    }                                                   \
26
+    } while (0)
27
+
28
+#define SET_VALUE_STRING(VALUE, METHOD) SET_VALUE(VALUE, METHOD, String)
29
+
30
+#define SET_VALUE_BOOL(VALUE, METHOD) SET_VALUE(VALUE, METHOD, Bool)
31
+
9
 HostsConfig::HostsConfig(const std::string &filePath)
32
 HostsConfig::HostsConfig(const std::string &filePath)
10
     : _filePath(filePath)
33
     : _filePath(filePath)
11
 {
34
 {
12
 }
35
 }
13
 
36
 
14
-bool HostsConfig::readConfig()
37
+BResult HostsConfig::readConfig()
15
 {
38
 {
39
+    BResult res;
16
     std::ifstream file(_filePath);
40
     std::ifstream file(_filePath);
17
     if (!file)
41
     if (!file)
18
-        return false;
42
+        return res.error("Could not open file");
19
     Json::Value root;
43
     Json::Value root;
20
     try
44
     try
21
     {
45
     {
25
     catch (...)
49
     catch (...)
26
     {
50
     {
27
         file.close();
51
         file.close();
28
-        return false;
52
+        return res.error("Could not parse JSON");
29
     }
53
     }
30
     auto defaults = root["defaults"];
54
     auto defaults = root["defaults"];
31
     auto actions = root["actions"];
55
     auto actions = root["actions"];
32
     for (Json::ArrayIndex i = 0; i < actions.size(); ++i)
56
     for (Json::ArrayIndex i = 0; i < actions.size(); ++i)
33
     {
57
     {
34
-        auto action = actions[i];
35
-        auto action_type = readValue(action, defaults, "action");
58
+        auto act = actions[i];
59
+        auto action_type = readValue(act, defaults, "action");
60
+        if (!action_type)
61
+            res.addWarning("Could no find action value for item " + i);
62
+        else
63
+        {
64
+            auto action_str = action_type.asString();
65
+            std::shared_ptr<Action> actionAbs = nullptr;
66
+            if (action_str == "add_domain")
67
+            {
68
+
69
+            }
70
+            else if (action_str == "del_domain")
71
+            {
72
+
73
+            }
74
+            else if (action_str == "add_host")
75
+            {
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;
81
+            }
82
+            else if (action_str == "del_host")
83
+            {
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);
94
+            }
95
+            else
96
+                res.addWarning("Errors occured while processing item " + i);
97
+        }
36
     }
98
     }
37
     return true;
99
     return true;
38
 }
100
 }

+ 2
- 1
src/DataAccess/HostsConfig.h Просмотреть файл

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
 
10
 
10
 class HostsConfig
11
 class HostsConfig
11
 {
12
 {
12
 public:
13
 public:
13
     HostsConfig(const std::string& filePath);
14
     HostsConfig(const std::string& filePath);
14
 
15
 
15
-    bool readConfig();
16
+    BResult readConfig();
16
 
17
 
17
 private:
18
 private:
18
     Json::Value readValue(const Json::Value &value, const Json::Value &defaults, const std::string& name);
19
     Json::Value readValue(const Json::Value &value, const Json::Value &defaults, const std::string& name);

+ 26
- 25
src/DataAccess/PdnsSlaveConfig.cpp Просмотреть файл

12
 {
12
 {
13
 }
13
 }
14
 
14
 
15
-bool PdnsSlaveConfig::readConfig()
15
+BResult PdnsSlaveConfig::readConfig()
16
 {
16
 {
17
     std::ifstream file(_filePath);
17
     std::ifstream file(_filePath);
18
     if (!file)
18
     if (!file)
19
-        return false;
19
+        return BResult().error("Could not open file");
20
     Json::Value root;
20
     Json::Value root;
21
     try
21
     try
22
     {
22
     {
26
     catch (...)
26
     catch (...)
27
     {
27
     {
28
         file.close();
28
         file.close();
29
-        return false;
29
+        return BResult().error("Could not parse JSON");
30
     }
30
     }
31
-    if (!readString(root, "dhcpd-file", _dhcpdFilePath))
32
-        return false;
33
-    if (!readString(root, "dhcpd-template", _dhcpdTemplatePath))
34
-        return false;
35
-    if (!readString(root, "hosts-file", _hostsPath))
36
-        return false;
37
-    if (!readSqlConfiguration(root, "master", _masterConfig))
38
-        return false;
39
-    if (!readSqlConfiguration(root, "slave", _slaveConfig))
40
-        return false;
41
-
31
+    BResult res;
32
+    if (!(res = readString(root, "dhcpd-file", _dhcpdFilePath)))
33
+        return res;
34
+    if (!(res = readString(root, "dhcpd-template", _dhcpdTemplatePath)))
35
+        return res;
36
+    if (!(res = readString(root, "hosts-file", _hostsPath)))
37
+        return res;
38
+    if (!(res = readSqlConfiguration(root, "master", _masterConfig)))
39
+        return res;
40
+    if (!(res = readSqlConfiguration(root, "slave", _slaveConfig)))
41
+        return res;
42
     return true;
42
     return true;
43
 }
43
 }
44
 
44
 
45
-bool PdnsSlaveConfig::readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration &sqlConfiguration)
45
+BResult PdnsSlaveConfig::readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration &sqlConfiguration)
46
 {
46
 {
47
     std::string str;
47
     std::string str;
48
-    if (!readString(value, name + "-host", str))
49
-        return false;
48
+    BResult res;
49
+    if (!(res = readString(value, name + "-host", str)))
50
+        return res;
50
     sqlConfiguration.setHost(str);
51
     sqlConfiguration.setHost(str);
51
 
52
 
52
-    if (!readString(value, name + "-user", str))
53
-        return false;
53
+    if (!(res = readString(value, name + "-user", str)))
54
+        return res;
54
     sqlConfiguration.setUser(str);
55
     sqlConfiguration.setUser(str);
55
 
56
 
56
-    if (!readString(value, name + "-password", str))
57
-        return false;
57
+    if (!(res = readString(value, name + "-password", str)))
58
+        return res;
58
     sqlConfiguration.setPassword(str);
59
     sqlConfiguration.setPassword(str);
59
 
60
 
60
-    if (!readString(value, name + "-database", str))
61
-        return false;
61
+    if (!(res = readString(value, name + "-database", str)))
62
+        return res;
62
     sqlConfiguration.setDatabase(str);
63
     sqlConfiguration.setDatabase(str);
63
 
64
 
64
     return true;
65
     return true;
65
 }
66
 }
66
 
67
 
67
-bool PdnsSlaveConfig::readString(const Json::Value &value, const std::string &name, std::string &dest)
68
+BResult PdnsSlaveConfig::readString(const Json::Value &value, const std::string &name, std::string &dest)
68
 {
69
 {
69
     auto v = value[name];
70
     auto v = value[name];
70
     if (!v)
71
     if (!v)
71
-        return false;
72
+        return BResult().error("Could not find value " + name);
72
     dest = v.asString();
73
     dest = v.asString();
73
     return true;
74
     return true;
74
 }
75
 }

+ 4
- 3
src/DataAccess/PdnsSlaveConfig.h Просмотреть файл

6
 #define PDNS_SLAVE_PDNSSLAVECONFIG_H
6
 #define PDNS_SLAVE_PDNSSLAVECONFIG_H
7
 
7
 
8
 # include <string>
8
 # include <string>
9
+# include "DBO/Result.h"
9
 # include "DBO/SqlConfiguration.h"
10
 # include "DBO/SqlConfiguration.h"
10
 
11
 
11
 namespace Json
12
 namespace Json
18
 public:
19
 public:
19
     PdnsSlaveConfig(const std::string& filePath);
20
     PdnsSlaveConfig(const std::string& filePath);
20
 
21
 
21
-    bool readConfig();
22
+    BResult readConfig();
22
 
23
 
23
     const std::string &getDhcpdFilePath() const;
24
     const std::string &getDhcpdFilePath() const;
24
 
25
 
31
     const SqlConfiguration &getSlaveConfig() const;
32
     const SqlConfiguration &getSlaveConfig() const;
32
 
33
 
33
 private:
34
 private:
34
-    bool readString(const Json::Value& value, const std::string& name, std::string& dest);
35
+    BResult readString(const Json::Value& value, const std::string& name, std::string& dest);
35
 
36
 
36
-    bool readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration& sqlConfiguration);
37
+    BResult readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration& sqlConfiguration);
37
 
38
 
38
     std::string _filePath;
39
     std::string _filePath;
39
 
40
 

+ 6
- 2
src/Interface/MainClass.cpp Просмотреть файл

4
 
4
 
5
 #include <iostream>
5
 #include <iostream>
6
 #include <sysexits.h>
6
 #include <sysexits.h>
7
+#include <DBO/Result.h>
7
 #include "CommandLineParser.h"
8
 #include "CommandLineParser.h"
8
 #include "Business/PdnsSlave.h"
9
 #include "Business/PdnsSlave.h"
9
 #include "MainClass.h"
10
 #include "MainClass.h"
31
     std::cout << "Using configuration file " << filePath << std::endl;
32
     std::cout << "Using configuration file " << filePath << std::endl;
32
 
33
 
33
     PdnsSlave pdnsSlave(filePath);
34
     PdnsSlave pdnsSlave(filePath);
34
-    if (!pdnsSlave.readConfig())
35
+    BResult res;
36
+    if (!(res = pdnsSlave.readConfig()))
35
     {
37
     {
36
         std::cerr << "Failed to read pdns-slave configuration" << std::endl;
38
         std::cerr << "Failed to read pdns-slave configuration" << std::endl;
39
+        res.print();
37
         return 1;
40
         return 1;
38
     }
41
     }
39
-    if (!pdnsSlave.readDhcpdTemplate())
42
+    if (!(res = pdnsSlave.readDhcpdTemplate()))
40
     {
43
     {
41
         std::cerr << "Failed to read dhcpd template" << std::endl;
44
         std::cerr << "Failed to read dhcpd template" << std::endl;
45
+        res.print();
42
         return 2;
46
         return 2;
43
     }
47
     }
44
 
48
 

Загрузка…
Отмена
Сохранить