Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish ydb-cli admin commands from common ones #13288

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ydb/core/driver_lib/cli_base/cli_cmds_root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ void TClientCommandRootKikimrBase::Parse(TConfig& config) {
ParseProfile();
GetProfileVariable("path", config.Path);
TClientCommandRootBase::Parse(config);
ParseCredentials(config);
ParseAddress(config);
NClient::TKikimr::DUMP_REQUESTS = DumpRequests;
}

Expand Down
26 changes: 23 additions & 3 deletions ydb/public/lib/ydb_cli/commands/ydb_admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,33 @@
namespace NYdb {
namespace NConsoleClient {

class TCommandNode : public TClientCommandTree {
public:
TCommandNode()
: TClientCommandTree("node", {}, "Node-wide administration")
{}
};

class TCommandDatabase : public TClientCommandTree {
public:
TCommandDatabase()
: TClientCommandTree("database", {}, "Database-wide administration")
{
AddCommand(std::make_unique<NDynamicConfig::TCommandConfig>());
}
};

TCommandAdmin::TCommandAdmin()
: TClientCommandTree("admin", {}, "Administrative cluster operations")
{
AddCommand(std::make_unique<NDynamicConfig::TCommandConfig>());
AddCommand(std::make_unique<NDynamicConfig::TCommandVolatileConfig>());
AddCommand(std::make_unique<NStorageConfig::TCommandStorageConfig>());
MarkDangerous();
UseOnlyExplicitProfile();
AddHiddenCommand(std::make_unique<NDynamicConfig::TCommandConfig>(false));
AddHiddenCommand(std::make_unique<NDynamicConfig::TCommandVolatileConfig>());
AddHiddenCommand(std::make_unique<NStorageConfig::TCommandStorageConfig>(false));
AddCommand(std::make_unique<NCluster::TCommandCluster>());
AddCommand(std::make_unique<TCommandNode>());
AddCommand(std::make_unique<TCommandDatabase>());
}

}
Expand Down
2 changes: 2 additions & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_cluster.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ydb_cluster.h"

#include "ydb_dynamic_config.h"
#include <ydb/public/sdk/cpp/client/ydb_bsconfig/ydb_storage_config.h>

using namespace NKikimr;
Expand All @@ -10,6 +11,7 @@ TCommandCluster::TCommandCluster()
: TClientCommandTree("cluster", {}, "Cluster-wide administration")
{
AddCommand(std::make_unique<TCommandClusterBootstrap>());
AddCommand(std::make_unique<NDynamicConfig::TCommandConfig>());
}

TCommandClusterBootstrap::TCommandClusterBootstrap()
Expand Down
16 changes: 15 additions & 1 deletion ydb/public/lib/ydb_cli/commands/ydb_dynamic_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,28 @@ TString WrapYaml(const TString& yaml) {
return out.Str();
}

TCommandConfig::TCommandConfig()
TCommandConfig::TCommandConfig(std::optional<bool> overrideOnlyExplicitProfile)
: TClientCommandTree("config", {}, "Dynamic config")
, OverrideOnlyExplicitProfile(overrideOnlyExplicitProfile)
{
AddCommand(std::make_unique<TCommandConfigFetch>());
AddCommand(std::make_unique<TCommandConfigReplace>());
AddCommand(std::make_unique<TCommandConfigResolve>());
}


void TCommandConfig::PropagateFlags(const TCommandFlags& flags) {
TClientCommand::PropagateFlags(flags);

if (OverrideOnlyExplicitProfile) {
OnlyExplicitProfile = *OverrideOnlyExplicitProfile;
}

for (auto& [_, cmd] : SubCommands) {
cmd->PropagateFlags(TCommandFlags{.Dangerous = Dangerous, .OnlyExplicitProfile = OnlyExplicitProfile});
}
}

TCommandConfigFetch::TCommandConfigFetch()
: TYdbCommand("fetch", {"get", "dump"}, "Fetch main dynamic-config")
{
Expand Down
5 changes: 4 additions & 1 deletion ydb/public/lib/ydb_cli/commands/ydb_dynamic_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ namespace NYdb::NConsoleClient::NDynamicConfig {

class TCommandConfig : public TClientCommandTree {
public:
TCommandConfig();
TCommandConfig(std::optional<bool> overrideOnlyExplicitProfile = std::nullopt);
void PropagateFlags(const TCommandFlags& flags) override;
private:
std::optional<bool> OverrideOnlyExplicitProfile;
};

class TCommandConfigReplace : public TYdbCommand {
Expand Down
22 changes: 16 additions & 6 deletions ydb/public/lib/ydb_cli/commands/ydb_root_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TClientCommandRootCommon::TClientCommandRootCommon(const TString& name, const TC
, Settings(settings)
{
ValidateSettings();
AddCommand(std::make_unique<TCommandAdmin>());
AddDangerousCommand(std::make_unique<TCommandAdmin>());
AddCommand(std::make_unique<TCommandAuth>());
AddCommand(std::make_unique<TCommandDiscovery>());
AddCommand(std::make_unique<TCommandScheme>());
Expand All @@ -54,6 +54,7 @@ TClientCommandRootCommon::TClientCommandRootCommon(const TString& name, const TC
AddCommand(std::make_unique<TCommandTopic>());
AddCommand(std::make_unique<TCommandWorkload>());
AddCommand(std::make_unique<TCommandDebug>());
PropagateFlags(TCommandFlags{.Dangerous = false, .OnlyExplicitProfile = false});
}

void TClientCommandRootCommon::ValidateSettings() {
Expand Down Expand Up @@ -328,6 +329,11 @@ void TClientCommandRootCommon::Config(TConfig& config) {
}

void TClientCommandRootCommon::Parse(TConfig& config) {
TClientCommandRootBase::Parse(config);
config.VerbosityLevel = std::min(static_cast<TConfig::EVerbosityLevel>(VerbosityLevel), TConfig::EVerbosityLevel::DEBUG);
}

void TClientCommandRootCommon::PostPrepare(TConfig& config) {
if (ProfileFile.empty()) {
config.ProfileFile = TStringBuilder() << HomeDir << '/' << Settings.YdbDir << "/config/config.yaml";
} else {
Expand All @@ -339,12 +345,12 @@ void TClientCommandRootCommon::Parse(TConfig& config) {
ProfileManager = CreateProfileManager(config.ProfileFile);
ParseProfile();

TClientCommandRootBase::Parse(config);
ParseDatabase(config);
ParseCaCerts(config);
ParseIamEndpoint(config);

config.VerbosityLevel = std::min(static_cast<TConfig::EVerbosityLevel>(VerbosityLevel), TConfig::EVerbosityLevel::DEBUG);
ParseCredentials(config);
ParseAddress(config);
}

namespace {
Expand Down Expand Up @@ -435,7 +441,7 @@ void TClientCommandRootCommon::ParseAddress(TConfig& config) {
return;
}
// Priority 3. Active profile (if --profile option is not specified)
if (TryGetParamFromProfile("endpoint", ProfileManager->GetActiveProfile(), false, getAddress)) {
if (!config.OnlyExplicitProfile && TryGetParamFromProfile("endpoint", ProfileManager->GetActiveProfile(), false, getAddress)) {
return;
}
}
Expand Down Expand Up @@ -519,7 +525,7 @@ void TClientCommandRootCommon::ParseDatabase(TConfig& config) {
return;
}
// Priority 3. Active profile (if --profile option is not specified)
if (TryGetParamFromProfile("database", ProfileManager->GetActiveProfile(), false, getDatabase)) {
if (!config.OnlyExplicitProfile && TryGetParamFromProfile("database", ProfileManager->GetActiveProfile(), false, getDatabase)) {
return;
}
}
Expand Down Expand Up @@ -574,11 +580,15 @@ void TClientCommandRootCommon::Validate(TConfig& config) {
throw TMisuseException() << errors;
}
}

// TODO: Maybe NeedToConnect doesn't always mean that we don't need to check endpoint and database
// TODO: Now we supplying only one error while it is possible to return all errors at once,
// maybe even erros from nested command's validate
if (!config.NeedToConnect) {
return;
}

if (config.Address.empty()) {
if (config.Address.empty() && config.AllowEmptyAddress) {
throw TMisuseException() << "Missing required option 'endpoint'.";
}

Expand Down
1 change: 1 addition & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_root_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TClientCommandRootCommon : public TClientCommandRootBase {
public:
TClientCommandRootCommon(const TString& name, const TClientSettings& settings);
void Config(TConfig& config) override;
void PostPrepare(TConfig& config) override;
void Parse(TConfig& config) override;
void ParseAddress(TConfig& config) override;
void ParseCredentials(TConfig& config) override;
Expand Down
24 changes: 12 additions & 12 deletions ydb/public/lib/ydb_cli/commands/ydb_service_scheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ void TCommandMakeDirectory::Config(TConfig& config) {

void TCommandMakeDirectory::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0);
}

int TCommandMakeDirectory::Run(TConfig& config) {
ParsePath(config, 0);
NScheme::TSchemeClient client(CreateDriver(config));
ThrowOnError(
client.MakeDirectory(
Expand Down Expand Up @@ -76,10 +76,10 @@ void TCommandRemoveDirectory::Config(TConfig& config) {

void TCommandRemoveDirectory::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0);
}

int TCommandRemoveDirectory::Run(TConfig& config) {
ParsePath(config, 0);
TDriver driver = CreateDriver(config);
NScheme::TSchemeClient schemeClient(driver);
const auto settings = FillSettings(NScheme::TRemoveDirectorySettings());
Expand Down Expand Up @@ -243,10 +243,10 @@ void TCommandDescribe::Parse(TConfig& config) {
TClientCommand::Parse(config);
Database = config.Database;
ParseOutputFormats();
ParsePath(config, 0);
}

int TCommandDescribe::Run(TConfig& config) {
ParsePath(config, 0);
TDriver driver = CreateDriver(config);
NScheme::TSchemeClient client(driver);
NScheme::TDescribePathResult result = client.DescribePath(
Expand Down Expand Up @@ -1048,14 +1048,14 @@ void TCommandList::Config(TConfig& config) {

void TCommandList::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0, true);
if (AdvancedMode && FromNewLine) {
// TODO: add "consider using --format shell"
throw TMisuseException() << "Options -1 and -l are incompatible";
}
}

int TCommandList::Run(TConfig& config) {
ParsePath(config, 0, true);
TDriver driver = CreateDriver(config);
ISchemePrinter::TSettings settings = {
Path,
Expand Down Expand Up @@ -1118,7 +1118,6 @@ void TCommandPermissionGrant::Config(TConfig& config) {

void TCommandPermissionGrant::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0);
Subject = config.ParseResult->GetFreeArgs()[1];
if (!Subject) {
throw TMisuseException() << "Missing required argument <subject>";
Expand All @@ -1129,6 +1128,7 @@ void TCommandPermissionGrant::Parse(TConfig& config) {
}

int TCommandPermissionGrant::Run(TConfig& config) {
ParsePath(config, 0);
NScheme::TSchemeClient client(CreateDriver(config));
ThrowOnError(
client.ModifyPermissions(
Expand Down Expand Up @@ -1159,7 +1159,6 @@ void TCommandPermissionRevoke::Config(TConfig& config) {

void TCommandPermissionRevoke::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0);
Subject = config.ParseResult->GetFreeArgs()[1];
if (!Subject) {
throw TMisuseException() << "Missing required argument <subject>";
Expand All @@ -1170,6 +1169,7 @@ void TCommandPermissionRevoke::Parse(TConfig& config) {
}

int TCommandPermissionRevoke::Run(TConfig& config) {
ParsePath(config, 0);
NScheme::TSchemeClient client(CreateDriver(config));
ThrowOnError(
client.ModifyPermissions(
Expand Down Expand Up @@ -1200,7 +1200,6 @@ void TCommandPermissionSet::Config(TConfig& config) {

void TCommandPermissionSet::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0);
Subject = config.ParseResult->GetFreeArgs()[1];
if (!Subject) {
throw TMisuseException() << "Missing required argument <subject>";
Expand All @@ -1211,6 +1210,7 @@ void TCommandPermissionSet::Parse(TConfig& config) {
}

int TCommandPermissionSet::Run(TConfig& config) {
ParsePath(config, 0);
NScheme::TSchemeClient client(CreateDriver(config));
ThrowOnError(
client.ModifyPermissions(
Expand Down Expand Up @@ -1238,14 +1238,14 @@ void TCommandChangeOwner::Config(TConfig& config) {

void TCommandChangeOwner::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0);
Owner = config.ParseResult->GetFreeArgs()[1];
if (!Owner){
throw TMisuseException() << "Missing required argument <owner>";
}
}

int TCommandChangeOwner::Run(TConfig& config) {
ParsePath(config, 0);
NScheme::TSchemeClient client(CreateDriver(config));
ThrowOnError(
client.ModifyPermissions(
Expand All @@ -1272,10 +1272,10 @@ void TCommandPermissionClear::Config(TConfig& config) {

void TCommandPermissionClear::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0);
}

int TCommandPermissionClear::Run(TConfig& config) {
ParsePath(config, 0);
NScheme::TSchemeClient client(CreateDriver(config));
ThrowOnError(
client.ModifyPermissions(
Expand All @@ -1302,10 +1302,10 @@ void TCommandPermissionSetInheritance::Config(TConfig& config) {

void TCommandPermissionSetInheritance::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0);
}

int TCommandPermissionSetInheritance::Run(TConfig& config) {
ParsePath(config, 0);
NScheme::TSchemeClient client(CreateDriver(config));
ThrowOnError(
client.ModifyPermissions(
Expand All @@ -1332,10 +1332,10 @@ void TCommandPermissionClearInheritance::Config(TConfig& config) {

void TCommandPermissionClearInheritance::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0);
}

int TCommandPermissionClearInheritance::Run(TConfig& config) {
ParsePath(config, 0);
NScheme::TSchemeClient client(CreateDriver(config));
ThrowOnError(
client.ModifyPermissions(
Expand All @@ -1362,10 +1362,10 @@ void TCommandPermissionList::Config(TConfig& config) {

void TCommandPermissionList::Parse(TConfig& config) {
TClientCommand::Parse(config);
ParsePath(config, 0);
}

int TCommandPermissionList::Run(TConfig& config) {
ParsePath(config, 0);
TDriver driver = CreateDriver(config);
NScheme::TSchemeClient client(driver);
NScheme::TDescribePathResult result = client.DescribePath(
Expand Down
15 changes: 14 additions & 1 deletion ydb/public/lib/ydb_cli/commands/ydb_storage_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,26 @@ TString WrapYaml(const TString& yaml) {
return out.Str();
}

TCommandStorageConfig::TCommandStorageConfig()
TCommandStorageConfig::TCommandStorageConfig(std::optional<bool> overrideOnlyExplicitProfile)
: TClientCommandTree("storage", {}, "Storage config")
, OverrideOnlyExplicitProfile(overrideOnlyExplicitProfile)
{
AddCommand(std::make_unique<TCommandStorageConfigFetch>());
AddCommand(std::make_unique<TCommandStorageConfigReplace>());
}

void TCommandStorageConfig::PropagateFlags(const TCommandFlags& flags) {
TClientCommand::PropagateFlags(flags);

if (OverrideOnlyExplicitProfile) {
OnlyExplicitProfile = *OverrideOnlyExplicitProfile;
}

for (auto& [_, cmd] : SubCommands) {
cmd->PropagateFlags(TCommandFlags{.Dangerous = Dangerous, .OnlyExplicitProfile = OnlyExplicitProfile});
}
}

TCommandStorageConfigFetch::TCommandStorageConfigFetch()
: TYdbCommand("fetch", {}, "Fetch storage config")
{
Expand Down
5 changes: 4 additions & 1 deletion ydb/public/lib/ydb_cli/commands/ydb_storage_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ namespace NYdb::NConsoleClient::NStorageConfig {

class TCommandStorageConfig : public TClientCommandTree {
public:
TCommandStorageConfig();
TCommandStorageConfig(std::optional<bool> overrideOnlyExplicitProfile = std::nullopt);
void PropagateFlags(const TCommandFlags& flags) override;
private:
std::optional<bool> OverrideOnlyExplicitProfile;
};

class TCommandStorageConfigReplace : public TYdbCommand {
Expand Down
Loading
Loading