Browse Source

pdns-slave.json data access

develop
Robin Thoni 9 years ago
parent
commit
791b0b8af0
2 changed files with 147 additions and 0 deletions
  1. 98
    0
      src/DataAccess/PdnsSlaveConfig.cpp
  2. 49
    0
      src/DataAccess/PdnsSlaveConfig.h

+ 98
- 0
src/DataAccess/PdnsSlaveConfig.cpp View File

@@ -0,0 +1,98 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#include <fstream>
6
+#include <iostream>
7
+#include "PdnsSlaveConfig.h"
8
+
9
+PdnsSlaveConfig::PdnsSlaveConfig(const std::string &filePath)
10
+    : _filePath(filePath)
11
+{
12
+}
13
+
14
+bool PdnsSlaveConfig::readConfig()
15
+{
16
+    std::ifstream file(_filePath);
17
+    if (!file)
18
+        return false;
19
+    try
20
+    {
21
+        Json::Value root;
22
+        file >> root;
23
+        file.close();
24
+        if (!readString(root, "dhcpd-file", _dhcpdFilePath))
25
+            return false;
26
+        if (!readString(root, "dhcpd-template", _dhcpdTemplatePath))
27
+            return false;
28
+        if (!readString(root, "hosts-file", _hostsPath))
29
+            return false;
30
+        if (!readSqlConfiguration(root, "master", _masterConfig))
31
+            return false;
32
+        if (!readSqlConfiguration(root, "slave", _slaveConfig))
33
+            return false;
34
+    }
35
+    catch (...)
36
+    {
37
+        file.close();
38
+        return false;
39
+    }
40
+
41
+    return true;
42
+}
43
+
44
+bool PdnsSlaveConfig::readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration &sqlConfiguration)
45
+{
46
+    std::string str;
47
+    if (!readString(value, name + "-host", str))
48
+        return false;
49
+    sqlConfiguration.setHost(str);
50
+
51
+    if (!readString(value, name + "-user", str))
52
+        return false;
53
+    sqlConfiguration.setUser(str);
54
+
55
+    if (!readString(value, name + "-password", str))
56
+        return false;
57
+    sqlConfiguration.setPassword(str);
58
+
59
+    if (!readString(value, name + "-database", str))
60
+        return false;
61
+    sqlConfiguration.setDatabase(str);
62
+
63
+    return true;
64
+}
65
+
66
+bool PdnsSlaveConfig::readString(const Json::Value &value, const std::string &name, std::string &dest)
67
+{
68
+    auto v = value[name];
69
+    if (!v)
70
+        return false;
71
+    dest = v.asString();
72
+    return true;
73
+}
74
+
75
+const std::string &PdnsSlaveConfig::getDhcpdFilePath() const
76
+{
77
+    return _dhcpdFilePath;
78
+}
79
+
80
+const std::string &PdnsSlaveConfig::getDhcpdTemplatePath() const
81
+{
82
+    return _dhcpdTemplatePath;
83
+}
84
+
85
+const std::string &PdnsSlaveConfig::getHostsPath() const
86
+{
87
+    return _hostsPath;
88
+}
89
+
90
+const SqlConfiguration &PdnsSlaveConfig::getMasterConfig() const
91
+{
92
+    return _masterConfig;
93
+}
94
+
95
+const SqlConfiguration &PdnsSlaveConfig::getSlaveConfig() const
96
+{
97
+    return _slaveConfig;
98
+}

+ 49
- 0
src/DataAccess/PdnsSlaveConfig.h View File

@@ -0,0 +1,49 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_PDNSSLAVECONFIG_H
6
+#define PDNS_SLAVE_PDNSSLAVECONFIG_H
7
+
8
+# include <string>
9
+# include <json/json.h>
10
+# include "DBO/SqlConfiguration.h"
11
+
12
+class PdnsSlaveConfig
13
+{
14
+public:
15
+    PdnsSlaveConfig(const std::string& filePath);
16
+
17
+    bool readConfig();
18
+
19
+    const std::string &getDhcpdFilePath() const;
20
+
21
+    const std::string &getDhcpdTemplatePath() const;
22
+
23
+    const std::string &getHostsPath() const;
24
+
25
+    const SqlConfiguration &getMasterConfig() const;
26
+
27
+    const SqlConfiguration &getSlaveConfig() const;
28
+
29
+private:
30
+    bool readString(const Json::Value& value, const std::string& name, std::string& dest);
31
+
32
+    bool readSqlConfiguration(const Json::Value &value, const std::string &name, SqlConfiguration& sqlConfiguration);
33
+
34
+    std::string _filePath;
35
+
36
+private:
37
+    std::string _dhcpdFilePath;
38
+
39
+    std::string _dhcpdTemplatePath;
40
+
41
+    std::string _hostsPath;
42
+
43
+    SqlConfiguration _masterConfig;
44
+
45
+    SqlConfiguration _slaveConfig;
46
+};
47
+
48
+
49
+#endif //PDNS_SLAVE_PDNSSLAVECONFIG_H

Loading…
Cancel
Save