Skip to content

Commit 1621446

Browse files
authored
impl(generator): call DetermineServiceEndpoint from stub factory function (#13176)
1 parent 29b7679 commit 1621446

File tree

301 files changed

+7338
-2730
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

301 files changed

+7338
-2730
lines changed

generator/integration_tests/golden/v1/internal/golden_kitchen_sink_stub_factory.cc

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
#include "google/cloud/common_options.h"
2626
#include "google/cloud/grpc_options.h"
2727
#include "google/cloud/internal/algorithm.h"
28+
#include "google/cloud/internal/credentials_impl.h"
29+
#include "google/cloud/internal/getenv.h"
2830
#include "google/cloud/internal/opentelemetry.h"
31+
#include "google/cloud/internal/service_endpoint.h"
2932
#include "google/cloud/log.h"
3033
#include "google/cloud/options.h"
3134
#include <generator/integration_tests/test.grpc.pb.h>
@@ -39,14 +42,27 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3942
std::shared_ptr<GoldenKitchenSinkStub>
4043
CreateDefaultGoldenKitchenSinkStub(
4144
google::cloud::CompletionQueue cq, Options const& options) {
42-
auto auth = google::cloud::internal::CreateAuthenticationStrategy(
43-
std::move(cq), options);
44-
auto channel = auth->CreateChannel(
45-
options.get<EndpointOption>(), internal::MakeChannelArguments(options));
46-
auto service_grpc_stub = google::test::admin::database::v1::GoldenKitchenSink::NewStub(channel);
47-
std::shared_ptr<GoldenKitchenSinkStub> stub =
48-
std::make_shared<DefaultGoldenKitchenSinkStub>(std::move(service_grpc_stub));
45+
auto endpoint = internal::DetermineServiceEndpoint(
46+
internal::GetEnv("GOLDEN_KITCHEN_SINK_ENDPOINT"),
47+
internal::FetchOption<EndpointOption>(options), "goldenkitchensink.googleapis.com",
48+
options);
4949

50+
std::shared_ptr<GoldenKitchenSinkStub> stub;
51+
std::shared_ptr<internal::GrpcAuthenticationStrategy> auth;
52+
if (!endpoint.ok()) {
53+
Options error_options = options;
54+
error_options.set<google::cloud::UnifiedCredentialsOption>(
55+
internal::MakeErrorCredentials(endpoint.status()));
56+
auth = internal::CreateAuthenticationStrategy(CompletionQueue{},
57+
error_options);
58+
} else {
59+
auth = internal::CreateAuthenticationStrategy(std::move(cq), options);
60+
auto channel = auth->CreateChannel(
61+
*endpoint, internal::MakeChannelArguments(options));
62+
auto service_grpc_stub = google::test::admin::database::v1::GoldenKitchenSink::NewStub(channel);
63+
stub =
64+
std::make_shared<DefaultGoldenKitchenSinkStub>(std::move(service_grpc_stub));
65+
}
5066
if (auth->RequiresConfigureContext()) {
5167
stub = std::make_shared<GoldenKitchenSinkAuth>(
5268
std::move(auth), std::move(stub));

generator/integration_tests/golden/v1/internal/golden_thing_admin_stub_factory.cc

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
#include "google/cloud/common_options.h"
2626
#include "google/cloud/grpc_options.h"
2727
#include "google/cloud/internal/algorithm.h"
28+
#include "google/cloud/internal/credentials_impl.h"
29+
#include "google/cloud/internal/getenv.h"
2830
#include "google/cloud/internal/opentelemetry.h"
31+
#include "google/cloud/internal/service_endpoint.h"
2932
#include "google/cloud/log.h"
3033
#include "google/cloud/options.h"
3134
#include <generator/integration_tests/test.grpc.pb.h>
@@ -39,16 +42,29 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3942
std::shared_ptr<GoldenThingAdminStub>
4043
CreateDefaultGoldenThingAdminStub(
4144
google::cloud::CompletionQueue cq, Options const& options) {
42-
auto auth = google::cloud::internal::CreateAuthenticationStrategy(
43-
std::move(cq), options);
44-
auto channel = auth->CreateChannel(
45-
options.get<EndpointOption>(), internal::MakeChannelArguments(options));
46-
auto service_grpc_stub = google::test::admin::database::v1::GoldenThingAdmin::NewStub(channel);
47-
std::shared_ptr<GoldenThingAdminStub> stub =
48-
std::make_shared<DefaultGoldenThingAdminStub>(
49-
std::move(service_grpc_stub),
50-
google::longrunning::Operations::NewStub(channel));
45+
auto endpoint = internal::DetermineServiceEndpoint(
46+
internal::GetEnv("GOLDEN_KITCHEN_SINK_ENDPOINT"),
47+
internal::FetchOption<EndpointOption>(options), "test.googleapis.com",
48+
options);
5149

50+
std::shared_ptr<GoldenThingAdminStub> stub;
51+
std::shared_ptr<internal::GrpcAuthenticationStrategy> auth;
52+
if (!endpoint.ok()) {
53+
Options error_options = options;
54+
error_options.set<google::cloud::UnifiedCredentialsOption>(
55+
internal::MakeErrorCredentials(endpoint.status()));
56+
auth = internal::CreateAuthenticationStrategy(CompletionQueue{},
57+
error_options);
58+
} else {
59+
auth = internal::CreateAuthenticationStrategy(std::move(cq), options);
60+
auto channel = auth->CreateChannel(
61+
*endpoint, internal::MakeChannelArguments(options));
62+
auto service_grpc_stub = google::test::admin::database::v1::GoldenThingAdmin::NewStub(channel);
63+
stub =
64+
std::make_shared<DefaultGoldenThingAdminStub>(
65+
std::move(service_grpc_stub),
66+
google::longrunning::Operations::NewStub(channel));
67+
}
5268
if (auth->RequiresConfigureContext()) {
5369
stub = std::make_shared<GoldenThingAdminAuth>(
5470
std::move(auth), std::move(stub));

generator/integration_tests/tests/golden_kitchen_sink_stub_factory_test.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "google/cloud/testing_util/opentelemetry_matchers.h"
1818
#include "google/cloud/testing_util/scoped_log.h"
1919
#include "google/cloud/testing_util/status_matchers.h"
20+
#include "google/cloud/universe_domain_options.h"
2021
#include <gmock/gmock.h>
2122
#include <memory>
2223

@@ -27,6 +28,7 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2728
namespace {
2829

2930
using ::google::cloud::testing_util::IsOk;
31+
using ::google::cloud::testing_util::StatusIs;
3032
using ::google::test::admin::database::v1::GenerateIdTokenRequest;
3133
using ::testing::Contains;
3234
using ::testing::HasSubstr;
@@ -68,6 +70,45 @@ TEST_F(GoldenKitchenSinkStubFactoryTest, DefaultStubWithAuth) {
6870
EXPECT_THAT(context.credentials(), Not(IsNull()));
6971
}
7072

73+
TEST_F(GoldenKitchenSinkStubFactoryTest, DefaultStubWithUniverseDomainOption) {
74+
Options options;
75+
options.set<EndpointOption>("localhost:1")
76+
.set<internal::UniverseDomainOption>("not empty")
77+
.set<UnifiedCredentialsOption>(MakeAccessTokenCredentials(
78+
"invalid-access-token",
79+
std::chrono::system_clock::now() + std::chrono::minutes(15)));
80+
auto default_stub =
81+
CreateDefaultGoldenKitchenSinkStub(CompletionQueue{}, options);
82+
grpc::ClientContext context;
83+
auto response =
84+
default_stub->GenerateIdToken(context, GenerateIdTokenRequest{});
85+
EXPECT_THAT(
86+
response,
87+
Not(AnyOf(IsOk(),
88+
StatusIs(StatusCode::kInvalidArgument,
89+
HasSubstr("UniverseDomainOption cannot be empty")))));
90+
EXPECT_THAT(context.credentials(), Not(IsNull()));
91+
}
92+
93+
TEST_F(GoldenKitchenSinkStubFactoryTest,
94+
DefaultStubWithEmptyUniverseDomainOption) {
95+
Options options;
96+
options.set<internal::UniverseDomainOption>("").set<UnifiedCredentialsOption>(
97+
MakeAccessTokenCredentials(
98+
"invalid-access-token",
99+
std::chrono::system_clock::now() + std::chrono::minutes(15)));
100+
auto default_stub =
101+
CreateDefaultGoldenKitchenSinkStub(CompletionQueue{}, options);
102+
103+
grpc::ClientContext context;
104+
auto response =
105+
default_stub->GenerateIdToken(context, GenerateIdTokenRequest{});
106+
EXPECT_THAT(response,
107+
StatusIs(StatusCode::kInvalidArgument,
108+
HasSubstr("UniverseDomainOption cannot be empty")));
109+
EXPECT_THAT(context.credentials(), IsNull());
110+
}
111+
71112
#ifdef GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY
72113
using ::google::cloud::testing_util::DisableTracing;
73114
using ::google::cloud::testing_util::EnableTracing;

generator/internal/stub_factory_generator.cc

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ Status StubFactoryGenerator::GenerateCc() {
8383
"google/cloud/common_options.h",
8484
"google/cloud/grpc_options.h",
8585
"google/cloud/internal/algorithm.h",
86+
"google/cloud/internal/credentials_impl.h",
87+
"google/cloud/internal/getenv.h",
8688
"google/cloud/internal/opentelemetry.h",
89+
"google/cloud/internal/service_endpoint.h",
8790
"google/cloud/options.h", "google/cloud/log.h"});
8891
CcSystemIncludes({vars("proto_grpc_header_path"), "memory"});
8992

@@ -95,24 +98,37 @@ Status StubFactoryGenerator::GenerateCc() {
9598
std::shared_ptr<$stub_class_name$>
9699
CreateDefault$stub_class_name$(
97100
google::cloud::CompletionQueue cq, Options const& options) {
98-
auto auth = google::cloud::internal::CreateAuthenticationStrategy(
99-
std::move(cq), options);
100-
auto channel = auth->CreateChannel(
101-
options.get<EndpointOption>(), internal::MakeChannelArguments(options));
102-
auto service_grpc_stub = $grpc_stub_fqn$::NewStub(channel);)""");
101+
auto endpoint = internal::DetermineServiceEndpoint(
102+
internal::GetEnv("$service_endpoint_env_var$"),
103+
internal::FetchOption<EndpointOption>(options), "$service_endpoint$",
104+
options);
105+
106+
std::shared_ptr<$stub_class_name$> stub;
107+
std::shared_ptr<internal::GrpcAuthenticationStrategy> auth;
108+
if (!endpoint.ok()) {
109+
Options error_options = options;
110+
error_options.set<google::cloud::UnifiedCredentialsOption>(
111+
internal::MakeErrorCredentials(endpoint.status()));
112+
auth = internal::CreateAuthenticationStrategy(CompletionQueue{},
113+
error_options);
114+
} else {
115+
auth = internal::CreateAuthenticationStrategy(std::move(cq), options);
116+
auto channel = auth->CreateChannel(
117+
*endpoint, internal::MakeChannelArguments(options));
118+
auto service_grpc_stub = $grpc_stub_fqn$::NewStub(channel);)""");
103119

104120
if (!HasLongrunningMethod()) {
105121
CcPrint(R"""(
106-
std::shared_ptr<$stub_class_name$> stub =
107-
std::make_shared<Default$stub_class_name$>(std::move(service_grpc_stub));
108-
)""");
122+
stub =
123+
std::make_shared<Default$stub_class_name$>(std::move(service_grpc_stub));
124+
})""");
109125
} else {
110126
CcPrint(R"""(
111-
std::shared_ptr<$stub_class_name$> stub =
112-
std::make_shared<Default$stub_class_name$>(
113-
std::move(service_grpc_stub),
114-
google::longrunning::Operations::NewStub(channel));
115-
)""");
127+
stub =
128+
std::make_shared<Default$stub_class_name$>(
129+
std::move(service_grpc_stub),
130+
google::longrunning::Operations::NewStub(channel));
131+
})""");
116132
}
117133
CcPrint(R"""(
118134
if (auth->RequiresConfigureContext()) {

google/cloud/accessapproval/v1/internal/access_approval_stub_factory.cc

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
#include "google/cloud/common_options.h"
2626
#include "google/cloud/grpc_options.h"
2727
#include "google/cloud/internal/algorithm.h"
28+
#include "google/cloud/internal/credentials_impl.h"
29+
#include "google/cloud/internal/getenv.h"
2830
#include "google/cloud/internal/opentelemetry.h"
31+
#include "google/cloud/internal/service_endpoint.h"
2932
#include "google/cloud/log.h"
3033
#include "google/cloud/options.h"
3134
#include <google/cloud/accessapproval/v1/accessapproval.grpc.pb.h>
@@ -38,15 +41,28 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3841

3942
std::shared_ptr<AccessApprovalStub> CreateDefaultAccessApprovalStub(
4043
google::cloud::CompletionQueue cq, Options const& options) {
41-
auto auth = google::cloud::internal::CreateAuthenticationStrategy(
42-
std::move(cq), options);
43-
auto channel = auth->CreateChannel(options.get<EndpointOption>(),
44-
internal::MakeChannelArguments(options));
45-
auto service_grpc_stub =
46-
google::cloud::accessapproval::v1::AccessApproval::NewStub(channel);
47-
std::shared_ptr<AccessApprovalStub> stub =
48-
std::make_shared<DefaultAccessApprovalStub>(std::move(service_grpc_stub));
44+
auto endpoint = internal::DetermineServiceEndpoint(
45+
internal::GetEnv("GOOGLE_CLOUD_CPP_ACCESS_APPROVAL_ENDPOINT"),
46+
internal::FetchOption<EndpointOption>(options),
47+
"accessapproval.googleapis.com", options);
4948

49+
std::shared_ptr<AccessApprovalStub> stub;
50+
std::shared_ptr<internal::GrpcAuthenticationStrategy> auth;
51+
if (!endpoint.ok()) {
52+
Options error_options = options;
53+
error_options.set<google::cloud::UnifiedCredentialsOption>(
54+
internal::MakeErrorCredentials(endpoint.status()));
55+
auth = internal::CreateAuthenticationStrategy(CompletionQueue{},
56+
error_options);
57+
} else {
58+
auth = internal::CreateAuthenticationStrategy(std::move(cq), options);
59+
auto channel =
60+
auth->CreateChannel(*endpoint, internal::MakeChannelArguments(options));
61+
auto service_grpc_stub =
62+
google::cloud::accessapproval::v1::AccessApproval::NewStub(channel);
63+
stub = std::make_shared<DefaultAccessApprovalStub>(
64+
std::move(service_grpc_stub));
65+
}
5066
if (auth->RequiresConfigureContext()) {
5167
stub =
5268
std::make_shared<AccessApprovalAuth>(std::move(auth), std::move(stub));

google/cloud/accesscontextmanager/v1/internal/access_context_manager_stub_factory.cc

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
#include "google/cloud/common_options.h"
2626
#include "google/cloud/grpc_options.h"
2727
#include "google/cloud/internal/algorithm.h"
28+
#include "google/cloud/internal/credentials_impl.h"
29+
#include "google/cloud/internal/getenv.h"
2830
#include "google/cloud/internal/opentelemetry.h"
31+
#include "google/cloud/internal/service_endpoint.h"
2932
#include "google/cloud/log.h"
3033
#include "google/cloud/options.h"
3134
#include <google/identity/accesscontextmanager/v1/access_context_manager.grpc.pb.h>
@@ -38,18 +41,29 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3841

3942
std::shared_ptr<AccessContextManagerStub> CreateDefaultAccessContextManagerStub(
4043
google::cloud::CompletionQueue cq, Options const& options) {
41-
auto auth = google::cloud::internal::CreateAuthenticationStrategy(
42-
std::move(cq), options);
43-
auto channel = auth->CreateChannel(options.get<EndpointOption>(),
44-
internal::MakeChannelArguments(options));
45-
auto service_grpc_stub =
46-
google::identity::accesscontextmanager::v1::AccessContextManager::NewStub(
47-
channel);
48-
std::shared_ptr<AccessContextManagerStub> stub =
49-
std::make_shared<DefaultAccessContextManagerStub>(
50-
std::move(service_grpc_stub),
51-
google::longrunning::Operations::NewStub(channel));
44+
auto endpoint = internal::DetermineServiceEndpoint(
45+
internal::GetEnv("GOOGLE_CLOUD_CPP_ACCESS_CONTEXT_MANAGER_ENDPOINT"),
46+
internal::FetchOption<EndpointOption>(options),
47+
"accesscontextmanager.googleapis.com", options);
5248

49+
std::shared_ptr<AccessContextManagerStub> stub;
50+
std::shared_ptr<internal::GrpcAuthenticationStrategy> auth;
51+
if (!endpoint.ok()) {
52+
Options error_options = options;
53+
error_options.set<google::cloud::UnifiedCredentialsOption>(
54+
internal::MakeErrorCredentials(endpoint.status()));
55+
auth = internal::CreateAuthenticationStrategy(CompletionQueue{},
56+
error_options);
57+
} else {
58+
auth = internal::CreateAuthenticationStrategy(std::move(cq), options);
59+
auto channel =
60+
auth->CreateChannel(*endpoint, internal::MakeChannelArguments(options));
61+
auto service_grpc_stub = google::identity::accesscontextmanager::v1::
62+
AccessContextManager::NewStub(channel);
63+
stub = std::make_shared<DefaultAccessContextManagerStub>(
64+
std::move(service_grpc_stub),
65+
google::longrunning::Operations::NewStub(channel));
66+
}
5367
if (auth->RequiresConfigureContext()) {
5468
stub = std::make_shared<AccessContextManagerAuth>(std::move(auth),
5569
std::move(stub));

google/cloud/advisorynotifications/v1/internal/advisory_notifications_stub_factory.cc

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
#include "google/cloud/common_options.h"
2626
#include "google/cloud/grpc_options.h"
2727
#include "google/cloud/internal/algorithm.h"
28+
#include "google/cloud/internal/credentials_impl.h"
29+
#include "google/cloud/internal/getenv.h"
2830
#include "google/cloud/internal/opentelemetry.h"
31+
#include "google/cloud/internal/service_endpoint.h"
2932
#include "google/cloud/log.h"
3033
#include "google/cloud/options.h"
3134
#include <google/cloud/advisorynotifications/v1/service.grpc.pb.h>
@@ -39,16 +42,29 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3942
std::shared_ptr<AdvisoryNotificationsServiceStub>
4043
CreateDefaultAdvisoryNotificationsServiceStub(google::cloud::CompletionQueue cq,
4144
Options const& options) {
42-
auto auth = google::cloud::internal::CreateAuthenticationStrategy(
43-
std::move(cq), options);
44-
auto channel = auth->CreateChannel(options.get<EndpointOption>(),
45-
internal::MakeChannelArguments(options));
46-
auto service_grpc_stub = google::cloud::advisorynotifications::v1::
47-
AdvisoryNotificationsService::NewStub(channel);
48-
std::shared_ptr<AdvisoryNotificationsServiceStub> stub =
49-
std::make_shared<DefaultAdvisoryNotificationsServiceStub>(
50-
std::move(service_grpc_stub));
45+
auto endpoint = internal::DetermineServiceEndpoint(
46+
internal::GetEnv(
47+
"GOOGLE_CLOUD_CPP_ADVISORY_NOTIFICATIONS_SERVICE_ENDPOINT"),
48+
internal::FetchOption<EndpointOption>(options),
49+
"advisorynotifications.googleapis.com", options);
5150

51+
std::shared_ptr<AdvisoryNotificationsServiceStub> stub;
52+
std::shared_ptr<internal::GrpcAuthenticationStrategy> auth;
53+
if (!endpoint.ok()) {
54+
Options error_options = options;
55+
error_options.set<google::cloud::UnifiedCredentialsOption>(
56+
internal::MakeErrorCredentials(endpoint.status()));
57+
auth = internal::CreateAuthenticationStrategy(CompletionQueue{},
58+
error_options);
59+
} else {
60+
auth = internal::CreateAuthenticationStrategy(std::move(cq), options);
61+
auto channel =
62+
auth->CreateChannel(*endpoint, internal::MakeChannelArguments(options));
63+
auto service_grpc_stub = google::cloud::advisorynotifications::v1::
64+
AdvisoryNotificationsService::NewStub(channel);
65+
stub = std::make_shared<DefaultAdvisoryNotificationsServiceStub>(
66+
std::move(service_grpc_stub));
67+
}
5268
if (auth->RequiresConfigureContext()) {
5369
stub = std::make_shared<AdvisoryNotificationsServiceAuth>(std::move(auth),
5470
std::move(stub));

0 commit comments

Comments
 (0)