Files
moloapp/example/assets/google_data.js
T

98 lines
4.4 KiB
JavaScript
Raw Normal View History

// Google data page: thin client over /google/data/* (all limits server side).
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("gd-out"), err = $("gd-error");
var actions = {};
// One example body per action; the server validates everything.
var examples = {
"bigquery": {
dry_run: { sql: "SELECT 1 AS one" },
query: { sql: "SELECT 1 AS one", confirm: false },
datasets: {}, jobs: {},
tables: { dataset: "DATASET" }, table: { dataset: "DATASET", table: "TABLE" },
rows: { dataset: "DATASET", table: "TABLE", max_results: 10 },
insert: { dataset: "DATASET", table: "TABLE", rows: [{ json: { a: 1 } }], confirm: false }
},
"storage": {
buckets: {}, objects: { bucket: "BUCKET", prefix: "" },
object: { bucket: "BUCKET", object: "path/file.txt" },
download: { bucket: "BUCKET", object: "path/file.txt" },
upload: { bucket: "BUCKET", object: "demo.txt", text: "hello", content_type: "text/plain", confirm: false },
compose: { bucket: "BUCKET", sources: ["a.txt", "b.txt"], destination: "ab.txt", confirm: false }
},
"pubsub": { topics: {}, subscriptions: {}, publish: { topic: "TOPIC", messages: ["hello"], confirm: false } },
"firestore": { databases: {}, collections: {}, get: { path: "users/alice" } },
"spanner": { instances: {}, databases: { instance: "INSTANCE" } },
"bigtable": { instances: {}, tables: { instance: "INSTANCE" } },
"secrets": { list: {}, versions: { secret: "SECRET" }, access: { secret: "SECRET", version: "latest", confirm: false } },
"dataplex": { locations: {}, search: { location: "global", query: "type=table" } },
"dlp": { info_types: {}, inspect: { text: "mail jane@example.com", info_types: ["EMAIL_ADDRESS"], confirm: false } },
"kms": { locations: {}, rings: { location: "global" } },
"datastore": { query: { gql: { queryString: "SELECT * FROM __kind__ LIMIT 1" } } },
"storage/sign": { sign: { bucket: "BUCKET", object: "path/file.txt", method: "GET", expires_seconds: 900, confirm: false } },
"call": { call: { rpc: "bigquery.datasets.list", path: { projectId: "PROJECT" } } }
};
function post(path, body) {
err.textContent = "";
out.textContent = "...";
return fetch(path, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
}).then(function (res) {
return res.text().then(function (t) {
var j;
try { j = JSON.parse(t); } catch (e) { j = { message: t }; }
if (!res.ok) { err.textContent = j.message || ("HTTP " + res.status); out.textContent = ""; }
else { out.textContent = JSON.stringify(j, null, 2); }
});
}).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
}
function fillActions() {
var svc = $("gd-service").value, sel = $("gd-action");
sel.innerHTML = "";
var names = Object.keys(examples[svc] || {});
var known = (actions[svc] || []).map(function (a) { return a.action; });
known.forEach(function (n) { if (names.indexOf(n) < 0) names.push(n); });
names.forEach(function (n) {
var o = document.createElement("option");
o.value = n;
var meta = (actions[svc] || []).filter(function (a) { return a.action === n; })[0];
o.textContent = n + (meta && meta.confirm ? " (confirm)" : "");
sel.appendChild(o);
});
fillBody();
}
function fillBody() {
var svc = $("gd-service").value, action = $("gd-action").value;
var ex = (examples[svc] || {})[action] || {};
var b = {};
if (svc !== "call" && svc !== "storage/sign") b.action = action;
Object.keys(ex).forEach(function (k) { b[k] = ex[k]; });
$("gd-body").value = JSON.stringify(b, null, 1);
}
fetch("/google/data/status").then(function (r) { return r.json(); }).then(function (s) {
actions = s.actions || {};
$("gd-config").textContent = s.configured
? "Google credential found." + (s.project_set ? "" : " Set GOOGLE_CLOUD_PROJECT.")
: "No Google credential: calls answer 503 until one is configured.";
fillActions();
});
$("gd-service").onchange = fillActions;
$("gd-action").onchange = fillBody;
$("gd-send").onclick = function () {
var body;
try { body = JSON.parse($("gd-body").value || "{}"); }
catch (e) { err.textContent = "Body is not valid JSON"; return; }
post("/google/data/" + $("gd-service").value, body);
};
fillActions();
})();