From f990fba44de4adc62c5b83e2b6bcc8bec947fb38 Mon Sep 17 00:00:00 2001 From: "A.J. Beamon" Date: Thu, 23 Jun 2022 14:21:23 -0700 Subject: [PATCH] Add support for getting tenant metadata as a JSON document. --- .../sphinx/source/command-line-interface.rst | 23 +++++-- fdbcli/TenantCommands.actor.cpp | 65 ++++++++++++++----- 2 files changed, 66 insertions(+), 22 deletions(-) diff --git a/documentation/sphinx/source/command-line-interface.rst b/documentation/sphinx/source/command-line-interface.rst index a51e4f10da..01c651e923 100644 --- a/documentation/sphinx/source/command-line-interface.rst +++ b/documentation/sphinx/source/command-line-interface.rst @@ -234,12 +234,27 @@ Note that :ref:`characters can be escaped ` when specifying keys ( gettenant --------- -The ``gettenant`` command fetches metadata for a given tenant and displays it. Its syntax is ``gettenant ``. +The ``gettenant`` command fetches metadata for a given tenant and displays it. Its syntax is ``gettenant [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. diff --git a/fdbcli/TenantCommands.actor.cpp b/fdbcli/TenantCommands.actor.cpp index 6660893a1f..92926e690b 100644 --- a/fdbcli/TenantCommands.actor.cpp +++ b/fdbcli/TenantCommands.actor.cpp @@ -209,11 +209,12 @@ CommandFactory listTenantsFactory( // gettenant command ACTOR Future getTenantCommandActor(Reference db, std::vector tokens) { - if (tokens.size() != 2) { + if (tokens.size() < 2 || tokens.size() > 3 || (tokens.size() == 3 && tokens[2] != "JSON"_sr)) { printUsage(tokens[0]); return false; } + state bool useJson = tokens.size() == 3; state Key tenantNameKey = fdb_cli::tenantSpecialKeyRange.begin.withSuffix(tokens[1]); state Reference tr = db->createTransaction(); @@ -228,30 +229,58 @@ ACTOR Future getTenantCommandActor(Reference db, std::vectoronError(e))); + } catch (Error& finalErr) { + state std::string errorStr; + 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", - CommandHelp("gettenant ", - "prints the metadata for a tenant", - "Prints the metadata for a tenant.")); +CommandFactory getTenantFactory( + "gettenant", + CommandHelp("gettenant [JSON]", + "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