|
| 1 | +#include <ydb-cpp-sdk/client/query/client.h> |
| 2 | +#include <ydb-cpp-sdk/client/iam/iam.h> |
| 3 | +#include <ydb-cpp-sdk/client/iam_private/iam.h> |
| 4 | + |
| 5 | +#include <library/cpp/getopt/last_getopt.h> |
| 6 | + |
| 7 | + |
| 8 | +int main(int argc, char** argv) { |
| 9 | + std::string endpoint; |
| 10 | + std::string database; |
| 11 | + std::string serviceId; |
| 12 | + std::string microserviceId; |
| 13 | + std::string targetServiceAccountId; |
| 14 | + std::string resourceId; |
| 15 | + std::string resourceType; |
| 16 | + std::string iamEndpoint; |
| 17 | + bool useSsl = false; |
| 18 | + NLastGetopt::TOpts opts = NLastGetopt::TOpts::Default(); |
| 19 | + |
| 20 | + opts.AddLongOption('e', "endpoint", "YDB endpoint").Required().RequiredArgument("HOST:PORT").StoreResult(&endpoint); |
| 21 | + opts.AddLongOption('d', "database", "YDB database").Required().RequiredArgument("PATH").StoreResult(&database); |
| 22 | + |
| 23 | + opts.AddLongOption("ssl", "Use SSL").NoArgument().SetFlag(&useSsl); |
| 24 | + |
| 25 | + opts.AddLongOption("target-service-account-id", "Target service account id") |
| 26 | + .Required() |
| 27 | + .RequiredArgument("ID") |
| 28 | + .StoreResult(&targetServiceAccountId); |
| 29 | + |
| 30 | + opts.AddLongOption("service-id", "Service id") |
| 31 | + .RequiredArgument("ID") |
| 32 | + .DefaultValue("ydb") |
| 33 | + .StoreResult(&serviceId); |
| 34 | + |
| 35 | + opts.AddLongOption("microservice-id", "Microservice id") |
| 36 | + .RequiredArgument("ID") |
| 37 | + .DefaultValue("control-plane") |
| 38 | + .StoreResult(µserviceId); |
| 39 | + |
| 40 | + opts.AddLongOption("resource-id", "Resource id") |
| 41 | + .Required() |
| 42 | + .RequiredArgument("ID") |
| 43 | + .StoreResult(&resourceId); |
| 44 | + |
| 45 | + opts.AddLongOption("iam-endpoint", "IAM endpoint") |
| 46 | + .RequiredArgument("HOST") |
| 47 | + .DefaultValue("iam.api.cloud.yandex.net") |
| 48 | + .StoreResult(&iamEndpoint); |
| 49 | + |
| 50 | + opts.AddLongOption("resource-type", "Resource type") |
| 51 | + .RequiredArgument("STRING") |
| 52 | + .DefaultValue("resource-manager.cloud") |
| 53 | + .StoreResult(&resourceType); |
| 54 | + |
| 55 | + opts.SetFreeArgsMin(0); |
| 56 | + |
| 57 | + NLastGetopt::TOptsParseResult optsResult(&opts, argc, argv); |
| 58 | + |
| 59 | + NYdb::TIamServiceParams iamParams{ |
| 60 | + .SystemServiceAccountCredentials = NYdb::CreateIamCredentialsProviderFactory(), |
| 61 | + .ServiceId = serviceId, |
| 62 | + .MicroserviceId = microserviceId, |
| 63 | + .ResourceId = resourceId, |
| 64 | + .ResourceType = resourceType, |
| 65 | + .TargetServiceAccountId = targetServiceAccountId, |
| 66 | + }; |
| 67 | + |
| 68 | + iamParams.Endpoint = iamEndpoint; |
| 69 | + |
| 70 | + auto config = NYdb::TDriverConfig() |
| 71 | + .SetEndpoint(endpoint) |
| 72 | + .SetDatabase(database) |
| 73 | + .SetCredentialsProviderFactory(NYdb::CreateIamServiceCredentialsProviderFactory(iamParams)); |
| 74 | + |
| 75 | + if (useSsl) { |
| 76 | + config.UseSecureConnection(); |
| 77 | + } |
| 78 | + |
| 79 | + NYdb::TDriver driver(config); |
| 80 | + NYdb::NQuery::TQueryClient client(driver); |
| 81 | + |
| 82 | + auto result = client.ExecuteQuery("SELECT 1", NYdb::NQuery::TTxControl::NoTx()).ExtractValueSync(); |
| 83 | + if (!result.IsSuccess()) { |
| 84 | + std::cerr << ToString(static_cast<NYdb::TStatus>(result)) << std::endl; |
| 85 | + return 1; |
| 86 | + } |
| 87 | + |
| 88 | + auto parser = result.GetResultSetParser(0); |
| 89 | + while (parser.TryNextRow()) { |
| 90 | + std::cout << parser.ColumnParser(0).GetInt32() << std::endl; |
| 91 | + } |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
0 commit comments