Files

74 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

// Agent registry page: thin client over /agent/registry/*. Every Google call
// is a POST; the status route says whether a credential is configured.
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("r-out"), err = $("r-error");
function show(text, isError) {
(isError ? err : out).textContent = text;
if (isError) out.textContent = ""; else err.textContent = "";
}
function post(path, body) {
return fetch(path, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body || {})
}).then(function (res) {
return res.text().then(function (text) {
if (!res.ok) {
var msg = "HTTP " + res.status;
try { var j = JSON.parse(text); msg = (j.error || "") + ": " + (j.message || msg); } catch (e) {}
throw new Error(msg);
}
return JSON.parse(text);
});
});
}
function get(path) {
return fetch(path).then(function (res) { return res.json(); });
}
// A list that hit its cap says so loudly; it is never presented as complete.
function banner(j) {
var el = $("r-truncated");
if (j && j.truncated === true) {
el.textContent = "TRUNCATED: showing " + j.count + " of more than " + j.cap_items +
" items (" + j.pages + " pages). Narrow the query or continue from the page token.";
el.hidden = false;
} else {
el.hidden = true;
el.textContent = "";
}
}
function run(promise) {
promise.then(function (j) { banner(j); show(JSON.stringify(j, null, 2), false); })
.catch(function (e) { show(e.message, true); });
}
$("r-list").addEventListener("click", function () {
run(post("/agent/registry/list", { kind: $("r-kind").value, version: $("r-version").value }));
});
$("r-search").addEventListener("click", function () {
run(post("/agent/registry/search", { kind: $("r-kind").value, q: $("r-q").value }));
});
$("r-get").addEventListener("click", function () {
run(post("/agent/registry/get", { kind: $("r-kind").value, id: $("r-q").value }));
});
$("r-gov-go").addEventListener("click", function () {
var which = $("r-gov").value;
if (which === "gateways") run(post("/agent/registry/gateways/list"));
else if (which === "policies") run(post("/agent/registry/policies/list"));
else if (which === "identity") run(post("/agent/registry/identity", { engine_id: $("r-q").value }));
else if (which.indexOf("ops-") === 0)
run(post("/agent/registry/operations/list", { scope: which.substring(4) }));
else if (which === "approvals") run(get("/agent/registry/approvals"));
else run(get("/agent/registry/audit"));
});
get("/agent/registry/status").then(function (s) {
$("r-config").textContent = s.configured && s.project_set
? "Google credential and project are set."
: "Not configured: set a Google credential and GOOGLE_CLOUD_PROJECT to call Google.";
}).catch(function () { $("r-config").textContent = "Status unavailable."; });
})();