Skip to content

Commit

Permalink
allow neighbor updates in reply to new neighbor webhook
Browse files Browse the repository at this point in the history
- this will allow to use only webhooks to update tags in neighbors

- to recap: now we have two options to set/update neighbor tags in
  smithproxy:
  a) API direct call to smithproxy API
  b) in **'new neighbor' webhook response** sent from webhook server
     (this patch)

Feature allows to change arbitrary neighbor entry, so it can be used
to update all neighbors in proxy.

Caveat:
update (API or response) will not do any CIDR network calculations.
Hostnames must be IP strings as they appear in neighbor  cache.
This may change in the future.
  • Loading branch information
astibal committed May 2, 2024
1 parent 7d0eef4 commit 1c3bdad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/proxy/nbrhood.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,17 @@ class NbrHood {
nbr_cache_t const& cache() const { return nbrs_; }

void update(std::string const& hostname) {
auto lc_ = std::scoped_lock(cache().lock());

if(auto nbr = cache().get_ul(hostname); nbr) {
nbr.value()->update();
}
else {
auto ptr = std::make_shared<Neighbor>(hostname);
ptr->update();
cache().put_ul(hostname, ptr);
sx::http::webhooks::neighbor_new(hostname);
{
auto lc_ = std::scoped_lock(cache().lock());
if (auto nbr = cache().get_ul(hostname); nbr) {
nbr.value()->update();
} else {
auto ptr = std::make_shared<Neighbor>(hostname);
ptr->update();
cache().put_ul(hostname, ptr);
}
}
sx::http::webhooks::neighbor_new(hostname);
}

bool apply(std::string const& hostname, std::function<bool(Neighbor&)> mod) {
Expand Down
21 changes: 19 additions & 2 deletions src/service/http/webhooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <service/core/smithproxy.hpp>
#include <service/http/webhooks.hpp>
#include <service/http/async_request.hpp>

#include <service/http/jsonize.hpp>

#include <unordered_map>

Expand Down Expand Up @@ -35,6 +35,8 @@ namespace sx::http::webhooks {
}

struct default_callback {
default_callback() = default;
virtual ~default_callback() = default;

void operator() (sx::http::expected_reply rep) const {

Expand All @@ -52,6 +54,10 @@ namespace sx::http::webhooks {
entry.update_incr(is_error);
}
}

virtual void on_reply([[maybe_unused]] sx::http::expected_reply const& rep) const {
// this cannot be abstract class, it's used with a no-op response
};
};

void ping() {
Expand Down Expand Up @@ -95,9 +101,20 @@ namespace sx::http::webhooks {
{"type", "proxy"},
{ "address", address_str }
};

struct new_neighbor_reply : public default_callback {
void on_reply(sx::http::expected_reply const& rep) const override {
auto const& body = rep->response.second;
if(not body.empty()) {
SmithProxy::api().neighbor_update(body);
}
}
};


sx::http::AsyncRequest::emit(
to_string(nbr_update),
default_callback());
new_neighbor_reply());
}
}

Expand Down

0 comments on commit 1c3bdad

Please sign in to comment.