Add support for getting tenant metadata as a JSON document.

This commit is contained in:
A.J. Beamon 2022-06-23 14:21:23 -07:00
parent 542adb4b9f
commit f990fba44d
2 changed files with 66 additions and 22 deletions

View File

@ -234,12 +234,27 @@ Note that :ref:`characters can be escaped <cli-escaping>` when specifying keys (
gettenant gettenant
--------- ---------
The ``gettenant`` command fetches metadata for a given tenant and displays it. Its syntax is ``gettenant <TENANT_NAME>``. The ``gettenant`` command fetches metadata for a given tenant and displays it. Its syntax is ``gettenant <TENANT_NAME> [JSON]``.
Included in the output of this command are the ``id`` and ``prefix`` assigned to the tenant. If the tenant does not exist, ``fdbcli`` will report an error. Included in the output of this command are the ``id`` and ``prefix`` assigned to the tenant. If the tenant does not exist, ``fdbcli`` will report an error. If ``JSON`` is specified, then the output will be written as a JSON document::
getversion {
---------- "tenant": {
"id": 0,
"prefix": "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"
},
"type": "success"
}
In the event of an error, the output will include an error message::
{
"error": "...",
"type": "error"
}
getversion
----------
The ``getversion`` command fetches the current read version of the cluster or currently running transaction. The ``getversion`` command fetches the current read version of the cluster or currently running transaction.

View File

@ -209,11 +209,12 @@ CommandFactory listTenantsFactory(
// gettenant command // gettenant command
ACTOR Future<bool> getTenantCommandActor(Reference<IDatabase> db, std::vector<StringRef> tokens) { ACTOR Future<bool> getTenantCommandActor(Reference<IDatabase> db, std::vector<StringRef> tokens) {
if (tokens.size() != 2) { if (tokens.size() < 2 || tokens.size() > 3 || (tokens.size() == 3 && tokens[2] != "JSON"_sr)) {
printUsage(tokens[0]); printUsage(tokens[0]);
return false; return false;
} }
state bool useJson = tokens.size() == 3;
state Key tenantNameKey = fdb_cli::tenantSpecialKeyRange.begin.withSuffix(tokens[1]); state Key tenantNameKey = fdb_cli::tenantSpecialKeyRange.begin.withSuffix(tokens[1]);
state Reference<ITransaction> tr = db->createTransaction(); state Reference<ITransaction> tr = db->createTransaction();
@ -228,30 +229,58 @@ ACTOR Future<bool> getTenantCommandActor(Reference<IDatabase> db, std::vector<St
json_spirit::mValue jsonObject; json_spirit::mValue jsonObject;
json_spirit::read_string(tenant.get().toString(), jsonObject); json_spirit::read_string(tenant.get().toString(), jsonObject);
JSONDoc doc(jsonObject);
int64_t id; if (useJson) {
std::string prefix; json_spirit::mObject resultObj;
doc.get("id", id); resultObj["tenant"] = jsonObject;
doc.get("prefix", prefix); resultObj["type"] = "success";
printf("%s\n",
json_spirit::write_string(json_spirit::mValue(resultObj), json_spirit::pretty_print).c_str());
} else {
JSONDoc doc(jsonObject);
int64_t id;
std::string prefix;
doc.get("id", id);
doc.get("prefix", prefix);
printf(" id: %" PRId64 "\n", id);
printf(" prefix: %s\n", printable(prefix).c_str());
}
printf(" id: %" PRId64 "\n", id);
printf(" prefix: %s\n", printable(prefix).c_str());
return true; return true;
} catch (Error& e) { } catch (Error& e) {
state Error err(e); try {
if (e.code() == error_code_special_keys_api_failure) { wait(safeThreadFutureToFuture(tr->onError(e)));
std::string errorMsgStr = wait(fdb_cli::getSpecialKeysFailureErrorMessage(tr)); } catch (Error& finalErr) {
fprintf(stderr, "ERROR: %s\n", errorMsgStr.c_str()); state std::string errorStr;
return false; if (finalErr.code() == error_code_special_keys_api_failure) {
std::string str = wait(getSpecialKeysFailureErrorMessage(tr));
errorStr = str;
} else if (useJson) {
errorStr = finalErr.what();
} else {
throw finalErr;
}
if (useJson) {
json_spirit::mObject resultObj;
resultObj["type"] = "error";
resultObj["error"] = errorStr;
printf(
"%s\n",
json_spirit::write_string(json_spirit::mValue(resultObj), json_spirit::pretty_print).c_str());
} else {
fprintf(stderr, "ERROR: %s\n", errorStr.c_str());
}
} }
wait(safeThreadFutureToFuture(tr->onError(err)));
} }
} }
} }
CommandFactory getTenantFactory("gettenant", CommandFactory getTenantFactory(
CommandHelp("gettenant <TENANT_NAME>", "gettenant",
"prints the metadata for a tenant", CommandHelp("gettenant <TENANT_NAME> [JSON]",
"Prints the metadata for a tenant.")); "prints the metadata for a tenant",
"Prints the metadata for a tenant. If JSON is specified, then the output will be in JSON format."));
} // namespace fdb_cli } // namespace fdb_cli