2020#include " google/cloud/internal/getenv.h"
2121#include " google/cloud/internal/random.h"
2222#include " google/cloud/testing_util/example_driver.h"
23+ #include " google/cloud/universe_domain.h"
2324#include < fstream>
2425#include < string>
2526#include < vector>
@@ -79,6 +80,32 @@ void PublisherServiceAccountKey(std::vector<std::string> const& argv) {
7980 (argv.at (0 ), argv.at (1 ), argv.at (2 ));
8081}
8182
83+ void PublisherSetUniverseDomain (std::vector<std::string> const & argv) {
84+ namespace examples = ::google::cloud::testing_util;
85+ if (argv.size () != 2 ) {
86+ throw examples::Usage{
87+ " publisher-set-universe-domain <project-id> <topic-id>" };
88+ }
89+ // ! [publisher-set-universe-domain]
90+ namespace pubsub = ::google::cloud::pubsub;
91+ [](std::string const & project_id, std::string const & topic_id) {
92+ google::cloud::Options options;
93+
94+ // AddUniverseDomainOption interrogates the UnifiedCredentialsOption, if
95+ // set, in the provided Options for the Universe Domain associated with the
96+ // credentials and adds it to the set of Options.
97+ // If no UnifiedCredentialsOption is set, GoogleDefaultCredentials are used.
98+ auto ud_options =
99+ google::cloud::AddUniverseDomainOption (std::move (options));
100+
101+ if (!ud_options.ok ()) throw std::move (ud_options).status ();
102+ auto pub = pubsub::Publisher (pubsub::MakePublisherConnection (
103+ " europe-central2" , pubsub::Topic (project_id, topic_id), *ud_options));
104+ }
105+ // ! [publisher-set-universe-domain]
106+ (argv.at (0 ), argv.at (1 ));
107+ }
108+
82109void SubscriberSetEndpoint (std::vector<std::string> const & argv) {
83110 namespace examples = ::google::cloud::testing_util;
84111 if (argv.size () != 2 ) {
@@ -131,6 +158,33 @@ void SubscriberServiceAccountKey(std::vector<std::string> const& argv) {
131158 (argv.at (0 ), argv.at (1 ), argv.at (2 ));
132159}
133160
161+ void SubscriberSetUniverseDomain (std::vector<std::string> const & argv) {
162+ namespace examples = ::google::cloud::testing_util;
163+ if (argv.size () != 2 ) {
164+ throw examples::Usage{
165+ " subscriber-set-universe-domain <project-id> <subscription-id>" };
166+ }
167+ // ! [subscriber-set-universe-domain]
168+ namespace pubsub = ::google::cloud::pubsub;
169+ [](std::string const & project_id, std::string const & subscription_id) {
170+ google::cloud::Options options;
171+
172+ // AddUniverseDomainOption interrogates the UnifiedCredentialsOption, if
173+ // set, in the provided Options for the Universe Domain associated with the
174+ // credentials and adds it to the set of Options.
175+ // If no UnifiedCredentialsOption is set, GoogleDefaultCredentials are used.
176+ auto ud_options =
177+ google::cloud::AddUniverseDomainOption (std::move (options));
178+
179+ if (!ud_options.ok ()) throw std::move (ud_options).status ();
180+ auto sub = pubsub::Subscriber (pubsub::MakeSubscriberConnection (
181+ " europe-central2" , pubsub::Subscription (project_id, subscription_id),
182+ *ud_options));
183+ }
184+ // ! [subscriber-set-universe-domain]
185+ (argv.at (0 ), argv.at (1 ));
186+ }
187+
134188void BlockingPublisherSetEndpoint (std::vector<std::string> const & argv) {
135189 namespace examples = ::google::cloud::testing_util;
136190 if (!argv.empty ()) {
@@ -177,6 +231,32 @@ void BlockingPublisherServiceAccountKey(std::vector<std::string> const& argv) {
177231 (argv.at (0 ));
178232}
179233
234+ void BlockingPublisherSetUniverseDomain (std::vector<std::string> const & argv) {
235+ namespace examples = ::google::cloud::testing_util;
236+ if (!argv.empty ()) {
237+ throw examples::Usage{" blocking-publisher-set-universe-domain" };
238+ }
239+ // ! [blocking-publisher-set-universe-domain]
240+ namespace pubsub = ::google::cloud::pubsub;
241+ []() {
242+ google::cloud::Options options;
243+
244+ // AddUniverseDomainOption interrogates the UnifiedCredentialsOption, if
245+ // set, in the provided Options for the Universe Domain associated with the
246+ // credentials and adds it to the set of Options.
247+ // If no UnifiedCredentialsOption is set, GoogleDefaultCredentials are used.
248+ auto ud_options =
249+ google::cloud::AddUniverseDomainOption (std::move (options));
250+
251+ if (!ud_options.ok ()) throw std::move (ud_options).status ();
252+ auto pub =
253+ pubsub::BlockingPublisher (pubsub::MakeBlockingPublisherConnection (
254+ " europe-central2" , *ud_options));
255+ }
256+ // ! [blocking-publisher-set-universe-domain]
257+ ();
258+ }
259+
180260void AutoRun (std::vector<std::string> const & argv) {
181261 namespace examples = ::google::cloud::testing_util;
182262
@@ -199,19 +279,29 @@ void AutoRun(std::vector<std::string> const& argv) {
199279 std::cout << " \n Running PublisherServiceAccountKey() sample" << std::endl;
200280 PublisherServiceAccountKey ({project_id, topic_id, keyfile});
201281
282+ std::cout << " \n Running PublisherSetUniverseDomain() sample" << std::endl;
283+ PublisherSetUniverseDomain ({project_id, topic_id});
284+
202285 std::cout << " \n Running SubscriberSetEndpoint() sample" << std::endl;
203286 SubscriberSetEndpoint ({project_id, subscription_id});
204287
205288 std::cout << " \n Running SubscriberServiceAccountKey() sample" << std::endl;
206289 SubscriberServiceAccountKey ({project_id, subscription_id, keyfile});
207290
291+ std::cout << " \n Running SubscriberSetUniverseDomain() sample" << std::endl;
292+ SubscriberSetUniverseDomain ({project_id, subscription_id});
293+
208294 std::cout << " \n Running BlockingPublisherSetEndpoint() sample" << std::endl;
209295 BlockingPublisherSetEndpoint ({});
210296
211297 std::cout << " \n Running BlockingPublisherServiceAccountKey() sample"
212298 << std::endl;
213299 BlockingPublisherServiceAccountKey ({keyfile});
214300
301+ std::cout << " \n Running BlockingPublisherSetUniverseDomain() sample"
302+ << std::endl;
303+ BlockingPublisherSetUniverseDomain ({});
304+
215305 std::cout << " \n AutoRun done" << std::endl;
216306}
217307
@@ -221,11 +311,15 @@ int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
221311 google::cloud::testing_util::Example example ({
222312 {" publisher-set-endpoint" , PublisherSetEndpoint},
223313 {" publisher-service-account-key" , PublisherServiceAccountKey},
314+ {" publisher-set-universe-domain" , PublisherSetUniverseDomain},
224315 {" subscriber-set-endpoint" , SubscriberSetEndpoint},
225316 {" subscriber-service-account-key" , SubscriberServiceAccountKey},
317+ {" subscriber-set-universe-domain" , SubscriberSetUniverseDomain},
226318 {" blocking-publisher-set-endpoint" , BlockingPublisherSetEndpoint},
227319 {" blocking-publisher-service-account-key" ,
228320 BlockingPublisherServiceAccountKey},
321+ {" blocking-publisher-set-universe-domain" ,
322+ BlockingPublisherSetUniverseDomain},
229323 {" auto" , AutoRun},
230324 });
231325 return example.Run (argc, argv);
0 commit comments