diff --git a/CHANGELOG.md b/CHANGELOG.md index fd4efc51c1..f2f847fa4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ the release. ([#2644](https://github.com/open-telemetry/opentelemetry-demo/pull/2644)) * [frontend] fix item calculation and shipping ([#2684](https://github.com/open-telemetry/opentelemetry-demo/pull/2684)) +* [flagd-ui] add back legacy REST APIs to empower programmatic usage ([#2720](https://github.com/open-telemetry/opentelemetry-demo/pull/2720)) ## 2.1.3 diff --git a/src/flagd-ui/lib/flagd_ui_web/controllers/feature_controller.ex b/src/flagd-ui/lib/flagd_ui_web/controllers/feature_controller.ex index 551f225c2f..906971aef9 100644 --- a/src/flagd-ui/lib/flagd_ui_web/controllers/feature_controller.ex +++ b/src/flagd-ui/lib/flagd_ui_web/controllers/feature_controller.ex @@ -9,4 +9,11 @@ defmodule FlagdUiWeb.FeatureController do json(conn, %{"flags" => flags}) end + + def write(conn, %{"data" => data}) do + payload = Jason.encode!(data) + GenServer.cast(Storage, {:replace, payload}) + + json(conn, %{}) + end end diff --git a/src/flagd-ui/lib/flagd_ui_web/router.ex b/src/flagd-ui/lib/flagd_ui_web/router.ex index df7d8bae18..b32e66136d 100644 --- a/src/flagd-ui/lib/flagd_ui_web/router.ex +++ b/src/flagd-ui/lib/flagd_ui_web/router.ex @@ -29,6 +29,9 @@ defmodule FlagdUiWeb.Router do pipe_through :api get "/read", FeatureController, :read + get "/read-file", FeatureController, :read + post "/write", FeatureController, :write + post "/write-to-file", FeatureController, :write end # Enable LiveDashboard and Swoosh mailbox preview in development diff --git a/src/flagd-ui/mix.exs b/src/flagd-ui/mix.exs index 7fa0130066..a8fb490851 100644 --- a/src/flagd-ui/mix.exs +++ b/src/flagd-ui/mix.exs @@ -7,7 +7,7 @@ defmodule FlagdUi.MixProject do def project do [ app: :flagd_ui, - version: "0.1.0", + version: "0.1.1", elixir: "~> 1.15", elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod, diff --git a/src/flagd-ui/test/flagd_ui_web/controllers/feature_controller_test.exs b/src/flagd-ui/test/flagd_ui_web/controllers/feature_controller_test.exs new file mode 100644 index 0000000000..8d24cc3d81 --- /dev/null +++ b/src/flagd-ui/test/flagd_ui_web/controllers/feature_controller_test.exs @@ -0,0 +1,136 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +defmodule FlagdUiWeb.FeatureControllerTest do + use FlagdUiWeb.ConnCase + + setup do + file_content = GenServer.call(Storage, :read) + + on_exit(fn -> + GenServer.cast(Storage, {:replace, Jason.encode!(file_content)}) + end) + end + + test "GET /api/read", %{conn: conn} do + conn = get(conn, ~p"/api/read") + + assert %{ + "flags" => %{ + "adFailure" => %{ + "defaultVariant" => "off", + "description" => "Fail ad service", + "state" => "ENABLED", + "variants" => %{"off" => false, "on" => true} + }, + "adHighCpu" => %{ + "defaultVariant" => "off", + "description" => "Triggers high cpu load in the ad service", + "state" => "ENABLED", + "variants" => %{"off" => false, "on" => true} + } + } + } = json_response(conn, 200) + end + + test "GET /api/read-file (legacy API)", %{conn: conn} do + conn = get(conn, ~p"/api/read-file") + + assert %{ + "flags" => %{ + "adFailure" => %{ + "defaultVariant" => "off", + "description" => "Fail ad service", + "state" => "ENABLED", + "variants" => %{"off" => false, "on" => true} + }, + "adHighCpu" => %{ + "defaultVariant" => "off", + "description" => "Triggers high cpu load in the ad service", + "state" => "ENABLED", + "variants" => %{"off" => false, "on" => true} + } + } + } = json_response(conn, 200) + end + + test "POST /api/write", %{conn: conn} do + data = %{ + "flags" => %{ + "adFailure" => %{ + "defaultVariant" => "off", + "description" => "Fail ad service", + "state" => "ENABLED", + "variants" => %{"off" => true, "on" => false} + }, + "adHighCpu" => %{ + "defaultVariant" => "off", + "description" => "Triggers high cpu load in the ad service", + "state" => "ENABLED", + "variants" => %{"off" => true, "on" => false} + } + } + } + + conn = post(conn, ~p"/api/write", %{"data" => data}) + + conn = get(conn, ~p"/api/read") + + assert %{ + "flags" => %{ + "adFailure" => %{ + "defaultVariant" => "off", + "description" => "Fail ad service", + "state" => "ENABLED", + "variants" => %{"off" => true, "on" => false} + }, + "adHighCpu" => %{ + "defaultVariant" => "off", + "description" => "Triggers high cpu load in the ad service", + "state" => "ENABLED", + "variants" => %{"off" => true, "on" => false} + } + } + } = json_response(conn, 200) + end + + test "POST /api/write-to-file (legacy API)", %{conn: conn} do + data = %{ + "flags" => %{ + "adFailure" => %{ + "defaultVariant" => "off", + "description" => "Fail ad service", + "state" => "ENABLED", + "variants" => %{"off" => true, "on" => false} + }, + "adHighCpu" => %{ + "defaultVariant" => "off", + "description" => "Triggers high cpu load in the ad service", + "state" => "ENABLED", + "variants" => %{"off" => true, "on" => false} + } + } + } + + conn = post(conn, ~p"/api/write-to-file", %{"data" => data}) + + conn = get(conn, ~p"/api/read-file") + + assert %{ + "flags" => %{ + "adFailure" => %{ + "defaultVariant" => "off", + "description" => "Fail ad service", + "state" => "ENABLED", + "variants" => %{"off" => true, "on" => false} + }, + "adHighCpu" => %{ + "defaultVariant" => "off", + "description" => "Triggers high cpu load in the ad service", + "state" => "ENABLED", + "variants" => %{"off" => true, "on" => false} + } + } + } = json_response(conn, 200) + end +end