Skip to content

Commit 6d6ee94

Browse files
committed
external data source / tables description: convert private protobuf to public
1 parent f9dcd6e commit 6d6ee94

File tree

4 files changed

+163
-4
lines changed

4 files changed

+163
-4
lines changed

ydb/core/grpc_services/rpc_describe_external_data_source.cpp

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,92 @@
22
#include "service_table.h"
33

44
#include <ydb/core/grpc_services/base/base.h>
5+
#include <ydb/core/ydb_convert/ydb_convert.h>
56
#include <ydb/public/api/protos/ydb_table.pb.h>
67

78
namespace NKikimr::NGRpcService {
89

910
using namespace NActors;
11+
using namespace NKikimrSchemeOp;
1012
using namespace NYql;
1113

14+
namespace {
15+
16+
using TProperties = google::protobuf::Map<TProtoStringType, TProtoStringType>;
17+
18+
void Convert(const TServiceAccountAuth& in, TProperties& out) {
19+
out["service_account_id"] = in.GetId();
20+
out["service_account_secret_name"] = in.GetSecretName();
21+
}
22+
23+
void Convert(const TBasic& in, TProperties& out) {
24+
out["login"] = in.GetLogin();
25+
out["password_secret_name"] = in.GetPasswordSecretName();
26+
}
27+
28+
void Convert(const TMdbBasic& in, TProperties& out) {
29+
out["service_account_id"] = in.GetServiceAccountId();
30+
out["service_account_secret_name"] = in.GetServiceAccountSecretName();
31+
out["login"] = in.GetLogin();
32+
out["password_secret_name"] = in.GetPasswordSecretName();
33+
}
34+
35+
void Convert(const TAws& in, TProperties& out) {
36+
out["aws_access_key_id_secret_name"] = in.GetAwsAccessKeyIdSecretName();
37+
out["aws_secret_access_key_secret_name"] = in.GetAwsSecretAccessKeySecretName();
38+
out["aws_region"] = in.GetAwsRegion();
39+
}
40+
41+
void Convert(const TToken& in, TProperties& out) {
42+
out["token_secret_name"] = in.GetTokenSecretName();
43+
}
44+
45+
void Convert(const TAuth& in, TProperties& out) {
46+
auto& authMethod = out["auth_method"];
47+
48+
switch (in.GetIdentityCase()) {
49+
case TAuth::kNone:
50+
authMethod = "NONE";
51+
return;
52+
case TAuth::kServiceAccount:
53+
authMethod = "SERVICE_ACCOUNT";
54+
Convert(in.GetServiceAccount(), out);
55+
return;
56+
case TAuth::kBasic:
57+
authMethod = "BASIC";
58+
Convert(in.GetBasic(), out);
59+
return;
60+
case TAuth::kMdbBasic:
61+
authMethod = "MDB_BASIC";
62+
Convert(in.GetMdbBasic(), out);
63+
return;
64+
case TAuth::kAws:
65+
authMethod = "AWS";
66+
Convert(in.GetAws(), out);
67+
return;
68+
case TAuth::kToken:
69+
authMethod = "TOKEN";
70+
Convert(in.GetToken(), out);
71+
return;
72+
case TAuth::IDENTITY_NOT_SET:
73+
return;
74+
}
75+
}
76+
77+
Ydb::Table::DescribeExternalDataSourceResult Convert(const TDirEntry& inSelf, const TExternalDataSourceDescription& inDesc) {
78+
Ydb::Table::DescribeExternalDataSourceResult out;
79+
ConvertDirectoryEntry(inSelf, out.mutable_self(), true);
80+
81+
out.set_source_type(inDesc.GetSourceType());
82+
out.set_location(inDesc.GetLocation());
83+
auto& properties = *out.mutable_properties();
84+
properties = inDesc.GetProperties().GetProperties();
85+
Convert(inDesc.GetAuth(), properties);
86+
return out;
87+
}
88+
89+
}
90+
1291
using TEvDescribeExternalDataSourceRequest = TGrpcRequestOperationCall<
1392
Ydb::Table::DescribeExternalDataSourceRequest,
1493
Ydb::Table::DescribeExternalDataSourceResponse
@@ -64,7 +143,7 @@ class TDescribeExternalDataSourceRPC : public TRpcSchemeRequestActor<TDescribeEx
64143

65144
return ReplyWithResult(
66145
Ydb::StatusIds::SUCCESS,
67-
Ydb::Table::DescribeExternalDataSourceResult(), // to do: convert private protobuf to public
146+
Convert(pathDescription.GetSelf(), pathDescription.GetExternalDataSourceDescription()),
68147
ctx
69148
);
70149
}

ydb/core/grpc_services/rpc_describe_external_table.cpp

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,83 @@
11
#include "rpc_scheme_base.h"
22
#include "service_table.h"
33

4+
#include <ydb/core/external_sources/external_source_factory.h>
45
#include <ydb/core/grpc_services/base/base.h>
6+
#include <ydb/core/protos/external_sources.pb.h>
7+
#include <ydb/core/ydb_convert/table_description.h>
8+
#include <ydb/core/ydb_convert/ydb_convert.h>
59
#include <ydb/public/api/protos/ydb_table.pb.h>
610

11+
#include <library/cpp/json/json_writer.h>
12+
#include <library/cpp/json/writer/json_value.h>
13+
714
namespace NKikimr::NGRpcService {
815

916
using namespace NActors;
17+
using namespace NJson;
18+
using namespace NKikimrSchemeOp;
1019
using namespace NYql;
1120

21+
namespace {
22+
23+
bool Convert(const TColumnDescription& in, Ydb::Table::ColumnMeta& out, TIssues& issues) {
24+
try {
25+
FillColumnDescription(out, in);
26+
} catch (const std::exception& ex) {
27+
issues.AddIssue(TStringBuilder() << "Unable to fill the column description, error: " << ex.what());
28+
return false;
29+
}
30+
return true;
31+
}
32+
33+
bool ConvertContent(
34+
const TString& sourceType,
35+
const TString& in,
36+
google::protobuf::Map<TProtoStringType, TProtoStringType>& out,
37+
TIssues& issues
38+
) {
39+
const auto externalSourceFactory = NExternalSource::CreateExternalSourceFactory({});
40+
try {
41+
const auto source = externalSourceFactory->GetOrCreate(sourceType);
42+
for (const auto& [key, items] : source->GetParameters(in)) {
43+
TJsonValue json(EJsonValueType::JSON_ARRAY);
44+
for (const auto& item : items) {
45+
json.AppendValue(item);
46+
}
47+
out[key] = WriteJson(json, false);
48+
}
49+
} catch (...) {
50+
issues.AddIssue(TStringBuilder() << "Cannot unpack the content of an external table of type: " << sourceType
51+
<< ", error: " << CurrentExceptionMessage()
52+
);
53+
}
54+
return true;
55+
}
56+
57+
std::optional<Ydb::Table::DescribeExternalTableResult> Convert(
58+
const TDirEntry& inSelf,
59+
const TExternalTableDescription& inDesc,
60+
TIssues& issues
61+
) {
62+
Ydb::Table::DescribeExternalTableResult out;
63+
ConvertDirectoryEntry(inSelf, out.mutable_self(), true);
64+
65+
out.set_source_type(inDesc.GetSourceType());
66+
out.set_data_source_path(inDesc.GetDataSourcePath());
67+
out.set_location(inDesc.GetLocation());
68+
for (const auto& column : inDesc.GetColumns()) {
69+
if (!Convert(column, *out.add_columns(), issues)) {
70+
return std::nullopt;
71+
}
72+
}
73+
if (!ConvertContent(inDesc.GetSourceType(), inDesc.GetContent(), *out.mutable_content(), issues)) {
74+
return std::nullopt;
75+
}
76+
return out;
77+
}
78+
79+
}
80+
1281
using TEvDescribeExternalTableRequest = TGrpcRequestOperationCall<
1382
Ydb::Table::DescribeExternalTableRequest,
1483
Ydb::Table::DescribeExternalTableResponse
@@ -62,9 +131,15 @@ class TDescribeExternalTableRPC : public TRpcSchemeRequestActor<TDescribeExterna
62131
return Reply(Ydb::StatusIds::SCHEME_ERROR, ctx);
63132
}
64133

134+
TIssues issues;
135+
const auto description = Convert(pathDescription.GetSelf(), pathDescription.GetExternalTableDescription(), issues);
136+
if (!description) {
137+
Reply(Ydb::StatusIds::INTERNAL_ERROR, issues, ctx);
138+
return;
139+
}
65140
return ReplyWithResult(
66141
Ydb::StatusIds::SUCCESS,
67-
Ydb::Table::DescribeExternalTableResult(), // to do: convert private proto to public
142+
*description,
68143
ctx
69144
);
70145
}

ydb/core/ydb_convert/table_description.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ Ydb::Type* AddColumn<NKikimrSchemeOp::TColumnDescription>(Ydb::Table::ColumnMeta
506506
return columnType;
507507
}
508508

509+
void FillColumnDescription(Ydb::Table::ColumnMeta& out, const NKikimrSchemeOp::TColumnDescription& in) {
510+
AddColumn(&out, in);
511+
}
512+
509513
template <typename TYdbProto>
510514
void FillColumnDescriptionImpl(TYdbProto& out,
511515
NKikimrMiniKQL::TType& splitKeyType, const NKikimrSchemeOp::TTableDescription& in) {
@@ -1138,7 +1142,7 @@ void FillAttributesImpl(TOutProto& out, const TInProto& in) {
11381142
}
11391143
void FillChangefeedDescription(Ydb::Table::ChangefeedDescription& out,
11401144
const NKikimrSchemeOp::TCdcStreamDescription& in) {
1141-
1145+
11421146
out.set_name(in.GetName());
11431147
out.set_virtual_timestamps(in.GetVirtualTimestamps());
11441148
out.set_aws_region(in.GetAwsRegion());

ydb/core/ydb_convert/table_description.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ bool BuildAlterTableAddIndexRequest(const Ydb::Table::AlterTableRequest* req, NK
5151
Ydb::StatusIds::StatusCode& status, TString& error);
5252

5353
// out
54+
void FillColumnDescription(Ydb::Table::ColumnMeta& out, const NKikimrSchemeOp::TColumnDescription& in);
5455
void FillColumnDescription(Ydb::Table::DescribeTableResult& out,
5556
NKikimrMiniKQL::TType& splitKeyType, const NKikimrSchemeOp::TTableDescription& in);
5657
void FillColumnDescription(Ydb::Table::CreateTableRequest& out,
@@ -147,7 +148,7 @@ bool FillSequenceDescription(Ydb::Table::CreateTableRequest& out, const NKikimrS
147148

148149
// in
149150
bool FillSequenceDescription(
150-
NKikimrSchemeOp::TSequenceDescription& out, const Ydb::Table::SequenceDescription& in,
151+
NKikimrSchemeOp::TSequenceDescription& out, const Ydb::Table::SequenceDescription& in,
151152
Ydb::StatusIds::StatusCode& status, TString& error);
152153

153154
} // namespace NKikimr

0 commit comments

Comments
 (0)