virthttp  0.0
libvirt http interface
network.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <tuple>
3 #include <rapidjson/rapidjson.h>
4 #include "wrapper/depends.hpp"
5 #include "wrapper/dispatch.hpp"
7 #include "base.hpp"
8 #include "flagwork.hpp"
9 #include "hdl_ctx.hpp"
10 #include "logger.hpp"
11 #include "urlparser.hpp"
12 #include "virt_wrap.hpp"
13 
24 
29 
40  template <class... Args> auto error(Args... args) const noexcept { return json_res.error(args...); };
41 
42  public:
44 
52  [[nodiscard]] static constexpr auto search_all_flags(const TargetParser& target) noexcept
53  -> std::optional<virt::enums::connection::list::networks::Flag> {
55  auto flags = Flag::DEFAULT;
56  if (auto activity = target.getBool("active"); activity)
57  flags |= *activity ? Flag::ACTIVE : Flag::INACTIVE;
58  if (auto persistence = target.getBool("persistent"); persistence)
59  flags |= *persistence ? Flag::PERSISTENT : Flag::TRANSIENT;
60  if (auto autostart = target.getBool("autostart"); autostart)
61  flags |= *autostart ? Flag::AUTOSTART : Flag::NO_AUTOSTART;
62  return {flags};
63  }
64 };
65 
71  template <class... Args> auto error(Args... args) const noexcept { return json_res.error(args...); };
72  virt::Network& nw;
73 
74  public:
78  explicit NetworkHandlers(HandlerContext& ctx, virt::Network& nw) : HandlerMethods(ctx), nw(nw) {}
79 
80  auto create(const rapidjson::Value& obj) -> DependsOutcome override {
81  const auto create_nw = [&](std::string_view xml) {
82  nw = virt::Network::createXML(conn, xml.data());
83  if (!nw)
84  return error(-999), DependsOutcome::FAILURE;
85  rapidjson::Value res_val;
86  res_val.SetObject();
87  res_val.AddMember("created", true, json_res.GetAllocator());
88  json_res.result(std::move(res_val));
90  };
91 
92  if (obj.IsString())
93  return create_nw(obj.GetString());
94  if (obj.IsArray()) {
95  for (const auto & item : obj.GetArray()) {
96  if (!item.IsString())
97  return error(-999), DependsOutcome::FAILURE; // Not a string array
98  if (create_nw(item.GetString()) == DependsOutcome::FAILURE)
99  return DependsOutcome::FAILURE; // Error while creating network
100  }
102  }
103  return error(-1), DependsOutcome::FAILURE;
104  }
105 
106  auto query(const rapidjson::Value& action) -> DependsOutcome override {
107  rapidjson::Value res_val;
108  auto& jalloc = json_res.GetAllocator();
109  const auto& path_parts = target.getPathParts();
110  if (path_parts.size() < 5) {
111  rapidjson::Value json_active;
112  {
113  const TFE tfe = nw.isActive();
114  if (tfe.err()) {
115  logger.error("Error occurred while getting network status");
116  return error(500), DependsOutcome::FAILURE;
117  }
118  json_active = to_json(tfe);
119  }
120  rapidjson::Value json_AS;
121  {
122  const TFE tfe = nw.getAutostart();
123  if (tfe.err()) {
124  logger.error("Error occurred while getting network autostart policy");
125  return error(500), DependsOutcome::FAILURE;
126  }
127  json_AS = to_json(tfe);
128  }
129  rapidjson::Value json_is_persistent;
130  {
131  const TFE tfe = nw.isPersistent();
132  if (tfe.err()) {
133  logger.error("Error occurred while getting network persistence");
134  return error(500), DependsOutcome::FAILURE;
135  }
136  json_is_persistent = to_json(tfe);
137  }
138 
139  res_val.SetObject();
140  res_val.AddMember("name", rapidjson::Value(nw.getName(), jalloc), jalloc);
141  res_val.AddMember("uuid", nw.extractUUIDString(), jalloc);
142  res_val.AddMember("active", json_active, jalloc);
143  res_val.AddMember("autostart", json_AS, jalloc);
144  res_val.AddMember("persistent", json_is_persistent, jalloc);
145  if (path_parts.size() == 4)
146  res_val.AddMember("bridge", to_json(nw.getBridgeName(), jalloc), jalloc);
147  json_res.result(std::move(res_val));
149  }
150 
151  const auto outcome = parameterized_depends_scope(
152  subquery("dhcp-leases", "mac", ti<std::string>, SUBQ_LIFT(nw.extractDHCPLeases), fwd_as_if_err(-2)),
153  subquery("dumpxml", "options", ti<virt::enums::network::XMLFlags>, SUBQ_LIFT(nw.getXMLDesc), fwd_as_if_err(-2)))(
154  4, target, res_val, json_res.GetAllocator(), [&](auto... args) { return error(args...); });
155 
156  if (outcome == DependsOutcome::SUCCESS)
157  json_res.result(std::move(res_val));
158  return outcome;
159  }
160 
161  auto alter(const rapidjson::Value& action) -> DependsOutcome override {
162  const auto& action_obj = *action.MemberBegin();
163  const auto& [action_name, action_val] = action_obj;
164  const auto hdl = network_actions_table[std::string_view{action_name.GetString(), action_name.GetStringLength()}];
165  return hdl ? hdl(action_val, json_res, nw) : (error(123), DependsOutcome::FAILURE);
166  }
167 
168  auto vacuum(const rapidjson::Value& action) -> DependsOutcome override {
169  auto& jalloc = json_res.GetAllocator();
170  const auto success = [&] {
171  rapidjson::Value res_val;
172  res_val.SetObject();
173  res_val.AddMember("deleted", true, jalloc);
174  json_res.result(std::move(res_val));
176  };
177  const auto failure = [&] {
178  rapidjson::Value msg_val;
179  msg_val.SetObject();
180  msg_val.AddMember("libvirt", rapidjson::Value(virt::extractLastError().message, jalloc), jalloc);
181  json_res.message(std::move(msg_val));
182  return error(-999), DependsOutcome::FAILURE;
183  };
184  return nw.undefine() ? success() : failure();
185  }
186 };
auto extractDHCPLeases(gsl::czstring<> mac) const -> std::optional< std::vector< virNetworkDHCPLease >>
Definition: Network.hpp:89
DependsOutcome
Definition: depends.hpp:11
NetworkUnawareHandlers(HandlerContext &ctx)
Definition: network.hpp:43
auto to_json(const virt::TypedParams &tp, JAllocator &&jalloc) noexcept-> rapidjson::Value
Definition: virt2json.hpp:6
Definition: network.hpp:34
constexpr auto gen_jdispatchers(const std::tuple< JDVs... > &tup) noexcept-> std::array< JDispatch, sizeof...(JDVs)>
Definition: dispatch.hpp:115
void error(int code)
Definition: json_utils.hpp:57
TFE getAutostart() const noexcept
Definition: Network.hpp:109
auto create(const rapidjson::Value &obj) -> DependsOutcome override
Definition: network.hpp:80
std::tuple< JDispatchVals< JAll >, JDispatchVals< JAll >, JDispatchVals< JAll >, JDispatchVals< JAll >> NetworkJDispatcherVals
Definition: network.hpp:18
Logger logger
Definition: logger.hpp:58
const TargetParser & target
the incoming request&#39;s URI target
Definition: hdl_ctx.hpp:14
static constexpr auto search_all_flags(const TargetParser &target) noexcept-> std::optional< virt::enums::connection::list::networks::Flag >
Definition: network.hpp:52
void error(Ts...msg)
Definition: logger.hpp:25
NetworkActionsTable network_actions_table
Definition: dispatch.hpp:20
constexpr auto subquery
Perfect-forwarding lifted subq_impl::subquery.
Definition: flagwork.hpp:242
constexpr auto network_jdispatchers
Definition: network.hpp:28
virt::Connection & conn
the connection to perform libvirt operations through (plans to change to a vector) ...
Definition: hdl_ctx.hpp:12
passive< const char * > getName() const noexcept
Definition: Network.hpp:36
auto vacuum(const rapidjson::Value &action) -> DependsOutcome override
Definition: network.hpp:168
Definition: hdl_ctx.hpp:11
TFE isPersistent() const noexcept
Definition: Network.hpp:73
Definition: Network.hpp:14
TFE isActive() const noexcept
Definition: Network.hpp:71
JsonRes & json_res
the result of running the handlers to be sent to the client
Definition: hdl_ctx.hpp:13
UniqueZstring getBridgeName() const noexcept
Definition: Network.hpp:27
const auto fwd_as_if_err(int code)
Definition: hdl_ctx.hpp:32
void result(T &&val)
Definition: json_utils.hpp:45
Definition: network.hpp:70
constexpr bool err() const noexcept
Definition: tfe.hpp:11
constexpr auto parameterized_depends_scope(Fcns &&...depends) noexcept((std::is_nothrow_move_constructible_v< Fcns > &&...))
Definition: flagwork.hpp:60
auto alter(const rapidjson::Value &action) -> DependsOutcome override
Definition: network.hpp:161
constexpr const std::vector< std::string_view > & getPathParts() const noexcept
Definition: urlparser.hpp:83
Definition: base.hpp:34
NetworkHandlers(HandlerContext &ctx, virt::Network &nw)
Definition: network.hpp:78
UniqueZstring getXMLDesc(enums::network::XMLFlags flags=enums::network::XMLFlags::DEFAULT) const noexcept
Definition: Network.hpp:60
void message(T &&val)
Definition: json_utils.hpp:50
static Network createXML(Connection &conn, gsl::czstring<> xml)
Definition: Network.hpp:121
#define SUBQ_LIFT(mem_fn)
Definition: flagwork.hpp:15
auto extractLastError() -> Error
Definition: Error.hpp:44
Definition: urlparser.hpp:36
auto extractUUIDString() const -> std::string
Definition: Network.hpp:50
auto query(const rapidjson::Value &action) -> DependsOutcome override
Definition: network.hpp:106
bool undefine() noexcept
Definition: Network.hpp:119
constexpr std::optional< bool > getBool(std::string_view key) const noexcept
Definition: urlparser.hpp:91
constexpr NetworkJDispatcherVals network_jdispatcher_vals
Definition: network.hpp:23
Definition: tfe.hpp:4