From 20727cb7f854fbf73a7cf4349345074780c2595e Mon Sep 17 00:00:00 2001 From: Zack Siri Date: Mon, 6 Jan 2025 17:30:52 +0700 Subject: [PATCH] Update refresh logic to correctly clear out old credentials --- lib/uplink/monitors/router.ex | 5 +++++ lib/uplink/pipelines.ex | 10 ++++++++-- test/uplink/monitors/router_test.exs | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/uplink/monitors/router.ex b/lib/uplink/monitors/router.ex index 95811ca..d88fe3d 100644 --- a/lib/uplink/monitors/router.ex +++ b/lib/uplink/monitors/router.ex @@ -16,6 +16,11 @@ defmodule Uplink.Monitors.Router do plug :dispatch post "/refresh" do + [:metrics] + |> Enum.each(fn context -> + Uplink.Pipelines.reset_monitors(context) + end) + Uplink.Monitors.run() json(conn, :ok, %{message: "monitors refreshed."}) diff --git a/lib/uplink/pipelines.ex b/lib/uplink/pipelines.ex index 0161f68..5beea24 100644 --- a/lib/uplink/pipelines.ex +++ b/lib/uplink/pipelines.ex @@ -1,11 +1,17 @@ defmodule Uplink.Pipelines do alias Uplink.Cache - def get_monitors(context) do + @valid_contexts [:metrics] + + def get_monitors(context) when context in @valid_contexts do Cache.get({:monitors, context}) || [] end - def append_monitors(context, monitors) do + def reset_monitors(context) when context in @valid_contexts do + Cache.put({:monitors, context}, []) + end + + def append_monitors(context, monitors) when context in @valid_contexts do Cache.get_and_update({:monitors, context}, fn existing_monitors -> {existing_monitors, existing_monitors ++ monitors} end) diff --git a/test/uplink/monitors/router_test.exs b/test/uplink/monitors/router_test.exs index 5215db9..f3eddba 100644 --- a/test/uplink/monitors/router_test.exs +++ b/test/uplink/monitors/router_test.exs @@ -45,6 +45,24 @@ defmodule Uplink.Monitors.RouterTest do endpoint: "http://localhost:#{bypass.port}/uplink" ) + Uplink.Cache.put({:monitors, :metrics}, [ + %{ + "attributes" => %{ + "current_state" => "active", + "endpoint" => "https://elastic:9200", + "expires_at" => "2024-11-21T03:14:17Z", + "id" => 1, + "token" => "some-token", + "type" => "metrics", + "uid" => "some-other-uid" + }, + "id" => "1", + "links" => %{"self" => "http://localhost:4000/uplink/self/monitors/1"}, + "relationships" => %{}, + "type" => "monitors" + } + ]) + {:ok, bypass: bypass} end @@ -59,6 +77,10 @@ defmodule Uplink.Monitors.RouterTest do end test "can refresh monitors list", %{signature: signature, bypass: bypass} do + [monitor] = Uplink.Pipelines.get_monitors(:metrics) + + assert monitor["attributes"]["uid"] == "some-other-uid" + Bypass.expect_once(bypass, "GET", "/uplink/self/monitors", fn conn -> conn |> Plug.Conn.put_resp_header("content-type", "application/json") @@ -71,6 +93,10 @@ defmodule Uplink.Monitors.RouterTest do |> put_req_header("content-type", "application/json") |> Router.call(@opts) + [monitor] = Uplink.Pipelines.get_monitors(:metrics) + + assert monitor["attributes"]["uid"] == "some-uid" + assert conn.status == 200 end end