aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/consul/consul.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zenutil/consul/consul.cpp')
-rw-r--r--src/zenutil/consul/consul.cpp66
1 files changed, 54 insertions, 12 deletions
diff --git a/src/zenutil/consul/consul.cpp b/src/zenutil/consul/consul.cpp
index 272096ebb..d463c0938 100644
--- a/src/zenutil/consul/consul.cpp
+++ b/src/zenutil/consul/consul.cpp
@@ -20,7 +20,7 @@ namespace zen::consul {
struct ConsulProcess::Impl
{
- Impl(std::string_view BaseUri) : m_HttpClient(BaseUri) {}
+ Impl(std::string_view BaseUri, std::string_view Token = "") : m_Token(Token), m_HttpClient(BaseUri) {}
~Impl() = default;
void SpawnConsulAgent()
@@ -44,10 +44,16 @@ struct ConsulProcess::Impl
// Poll to check when the agent is ready
+ HttpClient::KeyValueMap AdditionalHeaders;
+ if (!m_Token.empty())
+ {
+ AdditionalHeaders.Entries.emplace("X-Consul-Token", m_Token);
+ }
+
do
{
Sleep(100);
- HttpClient::Response Resp = m_HttpClient.Get("v1/status/leader");
+ HttpClient::Response Resp = m_HttpClient.Get("v1/status/leader", AdditionalHeaders);
if (Resp)
{
ZEN_INFO("Consul agent started successfully (waited {})", NiceTimeSpanMs(Timer.GetElapsedTimeMs()));
@@ -75,6 +81,7 @@ struct ConsulProcess::Impl
private:
ProcessHandle m_ProcessHandle;
+ std::string m_Token;
HttpClient m_HttpClient;
};
@@ -100,7 +107,7 @@ ConsulProcess::StopConsulAgent()
//////////////////////////////////////////////////////////////////////////
-ConsulClient::ConsulClient(std::string_view BaseUri) : m_HttpClient(BaseUri)
+ConsulClient::ConsulClient(std::string_view BaseUri, std::string_view Token) : m_Token(Token), m_HttpClient(BaseUri)
{
}
@@ -111,9 +118,13 @@ ConsulClient::~ConsulClient()
void
ConsulClient::SetKeyValue(std::string_view Key, std::string_view Value)
{
+ HttpClient::KeyValueMap AdditionalHeaders;
+ ApplyCommonHeaders(AdditionalHeaders);
+ AdditionalHeaders.Entries.emplace(HttpClient::Accept(HttpContentType::kJSON));
+
IoBuffer ValueBuffer = IoBufferBuilder::MakeFromMemory(MakeMemoryView(Value));
ValueBuffer.SetContentType(HttpContentType::kText);
- HttpClient::Response Result = m_HttpClient.Put(fmt::format("v1/kv/{}", Key), ValueBuffer, HttpClient::Accept(HttpContentType::kJSON));
+ HttpClient::Response Result = m_HttpClient.Put(fmt::format("v1/kv/{}", Key), ValueBuffer, AdditionalHeaders);
if (!Result)
{
throw runtime_error("ConsulClient::SetKeyValue() failed to set key '{}' ({})", Key, Result.ErrorMessage(""));
@@ -123,7 +134,10 @@ ConsulClient::SetKeyValue(std::string_view Key, std::string_view Value)
std::string
ConsulClient::GetKeyValue(std::string_view Key)
{
- HttpClient::Response Result = m_HttpClient.Get(fmt::format("v1/kv/{}?raw", Key));
+ HttpClient::KeyValueMap AdditionalHeaders;
+ ApplyCommonHeaders(AdditionalHeaders);
+
+ HttpClient::Response Result = m_HttpClient.Get(fmt::format("v1/kv/{}?raw", Key), AdditionalHeaders);
if (!Result)
{
throw runtime_error("ConsulClient::GetKeyValue() failed to get key '{}' ({})", Key, Result.ErrorMessage(""));
@@ -134,7 +148,10 @@ ConsulClient::GetKeyValue(std::string_view Key)
void
ConsulClient::DeleteKey(std::string_view Key)
{
- HttpClient::Response Result = m_HttpClient.Delete(fmt::format("v1/kv/{}", Key));
+ HttpClient::KeyValueMap AdditionalHeaders;
+ ApplyCommonHeaders(AdditionalHeaders);
+
+ HttpClient::Response Result = m_HttpClient.Delete(fmt::format("v1/kv/{}", Key), AdditionalHeaders);
if (!Result)
{
throw runtime_error("ConsulClient::DeleteKey() failed to delete key '{}' ({})", Key, Result.ErrorMessage(""));
@@ -146,6 +163,10 @@ ConsulClient::RegisterService(const ServiceRegistrationInfo& Info)
{
using namespace std::literals;
+ HttpClient::KeyValueMap AdditionalHeaders;
+ ApplyCommonHeaders(AdditionalHeaders);
+ AdditionalHeaders.Entries.emplace(HttpClient::Accept(HttpContentType::kJSON));
+
CbObjectWriter Writer;
{
Writer.AddString("ID"sv, Info.ServiceId);
@@ -178,7 +199,7 @@ ConsulClient::RegisterService(const ServiceRegistrationInfo& Info)
IoBuffer PayloadBuffer(IoBuffer::Wrap, SB.Data(), SB.Size());
PayloadBuffer.SetContentType(HttpContentType::kJSON);
- HttpClient::Response Result = m_HttpClient.Put("v1/agent/service/register", PayloadBuffer, HttpClient::Accept(HttpContentType::kJSON));
+ HttpClient::Response Result = m_HttpClient.Put("v1/agent/service/register", PayloadBuffer, AdditionalHeaders);
if (!Result)
{
@@ -192,8 +213,11 @@ ConsulClient::RegisterService(const ServiceRegistrationInfo& Info)
bool
ConsulClient::DeregisterService(std::string_view ServiceId)
{
- HttpClient::Response Result =
- m_HttpClient.Put(fmt::format("v1/agent/service/deregister/{}", ServiceId), HttpClient::Accept(HttpContentType::kJSON));
+ HttpClient::KeyValueMap AdditionalHeaders;
+ ApplyCommonHeaders(AdditionalHeaders);
+ AdditionalHeaders.Entries.emplace(HttpClient::Accept(HttpContentType::kJSON));
+
+ HttpClient::Response Result = m_HttpClient.Put(fmt::format("v1/agent/service/deregister/{}", ServiceId), AdditionalHeaders);
if (!Result)
{
@@ -204,6 +228,15 @@ ConsulClient::DeregisterService(std::string_view ServiceId)
return true;
}
+void
+ConsulClient::ApplyCommonHeaders(HttpClient::KeyValueMap& InOutHeaderMap)
+{
+ if (!m_Token.empty())
+ {
+ InOutHeaderMap.Entries.emplace("X-Consul-Token", m_Token);
+ }
+}
+
bool
ConsulClient::FindServiceInJson(std::string_view Json, std::string_view ServiceId)
{
@@ -235,7 +268,10 @@ ConsulClient::FindServiceInJson(std::string_view Json, std::string_view ServiceI
bool
ConsulClient::HasService(std::string_view ServiceId)
{
- HttpClient::Response Result = m_HttpClient.Get("v1/agent/services");
+ HttpClient::KeyValueMap AdditionalHeaders;
+ ApplyCommonHeaders(AdditionalHeaders);
+
+ HttpClient::Response Result = m_HttpClient.Get("v1/agent/services", AdditionalHeaders);
if (!Result)
{
return false;
@@ -246,10 +282,13 @@ ConsulClient::HasService(std::string_view ServiceId)
bool
ConsulClient::WatchService(std::string_view ServiceId, uint64_t& InOutIndex, int WaitSeconds)
{
+ HttpClient::KeyValueMap AdditionalHeaders;
+ ApplyCommonHeaders(AdditionalHeaders);
+
// Note: m_HttpClient uses unlimited HTTP timeout (Timeout{0}); the WaitSeconds parameter
// governs the server-side bound on the blocking query. Do not add a separate client timeout.
HttpClient::KeyValueMap Parameters({{"index", std::to_string(InOutIndex)}, {"wait", fmt::format("{}s", WaitSeconds)}});
- HttpClient::Response Result = m_HttpClient.Get("v1/agent/services", {}, Parameters);
+ HttpClient::Response Result = m_HttpClient.Get("v1/agent/services", AdditionalHeaders, Parameters);
if (!Result)
{
return false;
@@ -271,7 +310,10 @@ ConsulClient::WatchService(std::string_view ServiceId, uint64_t& InOutIndex, int
std::string
ConsulClient::GetAgentServicesJson()
{
- HttpClient::Response Result = m_HttpClient.Get("v1/agent/services");
+ HttpClient::KeyValueMap AdditionalHeaders;
+ ApplyCommonHeaders(AdditionalHeaders);
+
+ HttpClient::Response Result = m_HttpClient.Get("v1/agent/services", AdditionalHeaders);
if (!Result)
{
return "{}";