You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PdnsSlaveConfig.cpp 2.4KB

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