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.

HostsConfig.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Created by robin on 8/8/15.
  3. //
  4. #include <json/json.h>
  5. #include <fstream>
  6. #include <memory>
  7. #include <DBO/Actions/Action.h>
  8. #include <DBO/Actions/ActionAddHost.h>
  9. #include <DBO/Actions/ActionDelHost.h>
  10. #include <DBO/Actions/ActionAddDomain.h>
  11. #include <DBO/Actions/ActionDelDomain.h>
  12. #include "HostsConfig.h"
  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. res.addWarning("Could not find value " \
  20. VALUE " for item " + i); \
  21. action = nullptr; \
  22. } \
  23. else \
  24. { \
  25. action->METHOD(v.as##TYPE()); \
  26. } \
  27. } \
  28. } while (0)
  29. #define SET_VALUE_STRING(VALUE, METHOD) SET_VALUE(VALUE, METHOD, String)
  30. #define SET_VALUE_BOOL(VALUE, METHOD) SET_VALUE(VALUE, METHOD, Bool)
  31. HostsConfig::HostsConfig(const std::string &filePath)
  32. : _filePath(filePath)
  33. {
  34. }
  35. Result<Actions> HostsConfig::readConfig()
  36. {
  37. Result<Actions> res;
  38. std::ifstream file(_filePath);
  39. if (!file)
  40. return res.error("Could not open file");
  41. Json::Value root;
  42. try
  43. {
  44. file >> root;
  45. file.close();
  46. }
  47. catch (...)
  48. {
  49. file.close();
  50. return res.error("Could not parse JSON");
  51. }
  52. auto defaults = root["defaults"];
  53. if (!defaults || !defaults.isObject())
  54. return res.error("Could not find defaults object");
  55. auto acts = root["actions"];
  56. if (!acts || !acts.isArray())
  57. return res.error("Could not find actions array");
  58. std::vector<std::shared_ptr<Action>> actions;
  59. for (Json::ArrayIndex i = 0; i < acts.size(); ++i)
  60. {
  61. auto act = acts[i];
  62. auto action_type = readValue(act, defaults, "action");
  63. if (!action_type || !action_type.isString())
  64. res.addWarning("Could not find action value for item " + std::to_string(i));
  65. else
  66. {
  67. auto action_str = action_type.asString();
  68. if (action_str == "add_domain")
  69. {
  70. auto del = readDelDomain(act, defaults, i, res);
  71. auto add = readAddDomain(act, defaults, i, res);
  72. if (del && add)
  73. {
  74. actions.push_back(del);
  75. actions.push_back(add);
  76. }
  77. }
  78. else if (action_str == "del_domain")
  79. {
  80. auto del = readDelDomain(act, defaults, i, res);
  81. if (del)
  82. actions.push_back(del);
  83. }
  84. else if (action_str == "add_host")
  85. {
  86. auto del = readDelHost(act, defaults, i, res);
  87. auto add = readAddHost(act, defaults, i, res);
  88. if (del && add)
  89. {
  90. actions.push_back(del);
  91. actions.push_back(add);
  92. }
  93. }
  94. else if (action_str == "del_host")
  95. {
  96. auto del = readDelHost(act, defaults, i, res);
  97. if (del)
  98. actions.push_back(del);
  99. }
  100. else
  101. res.addWarning("Unknown action " + action_str);
  102. }
  103. }
  104. return res.ok(actions);
  105. }
  106. Json::Value HostsConfig::readValue(const Json::Value &value, const Json::Value &defaults, const std::string &name) const
  107. {
  108. auto v = value[name];
  109. if (!v)
  110. return defaults[name];
  111. return v;
  112. }
  113. std::shared_ptr<Action> HostsConfig::readAddHost(const Json::Value &act, const Json::Value &defaults,
  114. int i, Result<Actions>& res) const
  115. {
  116. auto action = std::make_shared<ActionAddHost>();
  117. SET_VALUE_STRING("domain", setDomain);
  118. SET_VALUE_STRING("host", setHost);
  119. SET_VALUE_STRING("record_value", setRecordValue);
  120. SET_VALUE_STRING("record_type", setRecordType);
  121. SET_VALUE_STRING("dhcp_mac", setDhcpMac);
  122. SET_VALUE_STRING("reverse_domain", setReverseDomain);
  123. SET_VALUE_BOOL("reverse_enabled", setReverseEnabled);
  124. return action;
  125. }
  126. std::shared_ptr<Action> HostsConfig::readDelHost(const Json::Value &act, const Json::Value &defaults, int i,
  127. Result<Actions> &res) const
  128. {
  129. auto action = std::make_shared<ActionDelHost>();
  130. SET_VALUE_STRING("domain", setDomain);
  131. SET_VALUE_STRING("host", setHost);
  132. return action;
  133. }
  134. std::shared_ptr<Action> HostsConfig::readAddDomain(const Json::Value &act, const Json::Value &defaults, int i,
  135. Result<Actions> &res) const
  136. {
  137. auto action = std::make_shared<ActionAddDomain>();
  138. SET_VALUE_STRING("domain", setDomain);
  139. return action;
  140. }
  141. std::shared_ptr<Action> HostsConfig::readDelDomain(const Json::Value &act, const Json::Value &defaults, int i,
  142. Result<Actions> &res) const
  143. {
  144. auto action = std::make_shared<ActionDelDomain>();
  145. SET_VALUE_STRING("domain", setDomain);
  146. return action;
  147. }