|
| 1 | +# SPDX-License-Identifier: PMPL-1.0 |
| 2 | +defmodule Opsm.Api.Router do |
| 3 | + @moduledoc """ |
| 4 | + Minimal REST API for OPSM UI integration. |
| 5 | + """ |
| 6 | + |
| 7 | + use Plug.Router |
| 8 | + |
| 9 | + alias Opsm.SmartInstall |
| 10 | + alias Opsm.Api.Nickel |
| 11 | + |
| 12 | + plug Plug.Logger |
| 13 | + |
| 14 | + plug Plug.Parsers, |
| 15 | + parsers: [:json, :urlencoded], |
| 16 | + pass: ["application/nickel", "text/nickel"], |
| 17 | + json_decoder: Jason |
| 18 | + |
| 19 | + plug :match |
| 20 | + plug :dispatch |
| 21 | + |
| 22 | + get "/health" do |
| 23 | + send_json(conn, 200, %{status: "ok"}) |
| 24 | + end |
| 25 | + |
| 26 | + get "/backends" do |
| 27 | + statuses = |
| 28 | + SmartInstall.backends() |
| 29 | + |> Enum.map(fn backend -> |
| 30 | + {backend, SmartInstall.backend_availability(backend)} |
| 31 | + end) |
| 32 | + |> Enum.map(fn {backend, result} -> |
| 33 | + case result do |
| 34 | + {:ok, _} -> {backend, %{status: "ok"}} |
| 35 | + {:error, reason} -> {backend, %{status: "error", reason: reason}} |
| 36 | + end |
| 37 | + end) |
| 38 | + |> Map.new() |
| 39 | + |
| 40 | + send_json(conn, 200, %{backends: statuses}) |
| 41 | + end |
| 42 | + |
| 43 | + post "/smart/plan" do |
| 44 | + with {:ok, payload} <- read_payload(conn), |
| 45 | + {:ok, tokens} <- fetch_tokens(payload) do |
| 46 | + plan = SmartInstall.parse(tokens) |
| 47 | + send_json(conn, 200, %{plan: plan, status: SmartInstall.plan_status(plan)}) |
| 48 | + else |
| 49 | + {:error, reason} -> send_json(conn, 400, %{error: reason}) |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + post "/smart/apply" do |
| 54 | + with {:ok, payload} <- read_payload(conn), |
| 55 | + {:ok, tokens} <- fetch_tokens(payload) do |
| 56 | + plan = SmartInstall.parse(tokens) |
| 57 | + results = SmartInstall.execute(plan, dry_run: false) |
| 58 | + send_json(conn, 200, %{results: results}) |
| 59 | + else |
| 60 | + {:error, reason} -> send_json(conn, 400, %{error: reason}) |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + match _ do |
| 65 | + send_json(conn, 404, %{error: "not found"}) |
| 66 | + end |
| 67 | + |
| 68 | + defp read_payload(conn) do |
| 69 | + case content_type(conn) do |
| 70 | + {:nickel, _} -> |
| 71 | + {:ok, body, _} = Plug.Conn.read_body(conn) |
| 72 | + Nickel.decode(body) |
| 73 | + |
| 74 | + _ -> |
| 75 | + {:ok, conn.body_params} |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + defp fetch_tokens(%{"tokens" => tokens}) when is_list(tokens), do: {:ok, tokens} |
| 80 | + defp fetch_tokens(%{tokens: tokens}) when is_list(tokens), do: {:ok, tokens} |
| 81 | + defp fetch_tokens(%{"command" => command}) when is_binary(command), do: {:ok, command_to_tokens(command)} |
| 82 | + defp fetch_tokens(%{command: command}) when is_binary(command), do: {:ok, command_to_tokens(command)} |
| 83 | + defp fetch_tokens(_), do: {:error, "payload must include tokens or command"} |
| 84 | + |
| 85 | + defp command_to_tokens(command) do |
| 86 | + command |
| 87 | + |> String.split() |
| 88 | + |> Enum.drop_while(&(&1 != "install")) |
| 89 | + |> Enum.drop(1) |
| 90 | + end |
| 91 | + |
| 92 | + defp send_json(conn, status, data) do |
| 93 | + body = Jason.encode!(data) |
| 94 | + conn |
| 95 | + |> Plug.Conn.put_resp_content_type("application/json") |
| 96 | + |> Plug.Conn.send_resp(status, body) |
| 97 | + end |
| 98 | + |
| 99 | + defp content_type(conn) do |
| 100 | + case Plug.Conn.get_req_header(conn, "content-type") do |
| 101 | + [type | _] when String.contains?(type, "application/nickel") -> {:nickel, type} |
| 102 | + [type | _] when String.contains?(type, "text/nickel") -> {:nickel, type} |
| 103 | + [type | _] -> {:other, type} |
| 104 | + _ -> :unknown |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments