-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathsamples.cc
2567 lines (2305 loc) · 107 KB
/
samples.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "google/cloud/pubsub/samples/pubsub_samples_common.h"
#include "google/cloud/pubsub/schema_client.h"
#include "google/cloud/pubsub/subscriber.h"
#include "google/cloud/pubsub/subscription_admin_client.h"
#include "google/cloud/pubsub/subscription_builder.h"
#include "google/cloud/pubsub/topic_admin_client.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/internal/random.h"
#include "google/cloud/project.h"
#include "google/cloud/status.h"
#include "google/cloud/testing_util/example_driver.h"
#include <google/cloud/pubsub/samples/samples.pb.h>
#include <google/protobuf/text_format.h>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include <tuple>
#include <utility>
namespace {
using ::google::cloud::pubsub::examples::RandomSchemaId;
using ::google::cloud::pubsub::examples::RandomSnapshotId;
using ::google::cloud::pubsub::examples::RandomSubscriptionId;
using ::google::cloud::pubsub::examples::RandomTopicId;
using ::google::cloud::pubsub::examples::ReadFile;
using ::google::cloud::pubsub::examples::UsingEmulator;
auto constexpr kWaitTimeout = std::chrono::minutes(1);
void WaitForSession(google::cloud::future<google::cloud::Status> session,
std::string const& name) {
std::cout << "\nWaiting for session [" << name << "]... " << std::flush;
auto result = session.wait_for(kWaitTimeout);
if (result == std::future_status::timeout) {
std::cout << "TIMEOUT" << std::endl;
throw std::runtime_error("session timeout");
}
std::cout << "DONE (" << session.get() << ")" << std::endl;
}
/// A (file) singleton to track received messages.
class EventCounter {
public:
EventCounter() = default;
void Increment() {
std::lock_guard<std::mutex> lk(mu_);
++counter_;
cv_.notify_all();
}
std::int64_t Current() {
std::lock_guard<std::mutex> lk(mu_);
return counter_;
}
template <typename Predicate>
void Wait(Predicate&& predicate,
google::cloud::future<google::cloud::Status> session,
std::string const& name) {
std::unique_lock<std::mutex> lk(mu_);
cv_.wait_for(lk, kWaitTimeout,
[this, &predicate] { return predicate(counter_); });
lk.unlock();
session.cancel();
WaitForSession(std::move(session), name);
}
template <typename Predicate>
void Wait(Predicate&& predicate) {
std::unique_lock<std::mutex> lk(mu_);
cv_.wait_for(lk, kWaitTimeout,
[this, &predicate] { return predicate(counter_); });
}
static EventCounter& Instance() {
static auto* const kInstance = new EventCounter;
return *kInstance;
}
private:
std::int64_t counter_ = 0;
std::mutex mu_;
std::condition_variable cv_;
};
/**
* Count received messages to gracefully shutdown the samples.
*
* This function is used in the examples to simplify their shutdown and testing.
* Most applications probably do not need to worry about gracefully shutting
* down a subscriber. However, the examples are tested as part of our CI process
* and do need to shutdown gracefully. To do so, we count the number of events
* received in a subscriber, and once enough messages (the count depends on the
* example) are received we cancel the session (the object returned by
* `pubsub::Subscriber::Subscribe()`) and wait for the session to shutdown.
*
* This function is named as a hint to the readers, so they can ignore when
* understanding the sample code.
*/
void PleaseIgnoreThisSimplifiesTestingTheSamples() {
EventCounter::Instance().Increment();
}
void CreateTopic(google::cloud::pubsub::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_quickstart_create_topic]
//! [START pubsub_create_topic] [create-topic]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string project_id,
std::string topic_id) {
auto topic = client.CreateTopic(pubsub::TopicBuilder(
pubsub::Topic(std::move(project_id), std::move(topic_id))));
// Note that kAlreadyExists is a possible error when the library retries.
if (topic.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The topic already exists\n";
return;
}
if (!topic) throw std::move(topic).status();
std::cout << "The topic was successfully created: " << topic->DebugString()
<< "\n";
}
//! [END pubsub_create_topic] [create-topic]
//! [END pubsub_quickstart_create_topic]
(std::move(client), argv.at(0), argv.at(1));
}
void GetTopic(google::cloud::pubsub::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [get-topic]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string project_id,
std::string topic_id) {
auto topic = client.GetTopic(
pubsub::Topic(std::move(project_id), std::move(topic_id)));
if (!topic) throw std::move(topic).status();
std::cout << "The topic information was successfully retrieved: "
<< topic->DebugString() << "\n";
}
//! [get-topic]
(std::move(client), argv.at(0), argv.at(1));
}
void UpdateTopic(google::cloud::pubsub::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [update-topic]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string project_id,
std::string topic_id) {
auto topic = client.UpdateTopic(
pubsub::TopicBuilder(
pubsub::Topic(std::move(project_id), std::move(topic_id)))
.add_label("test-key", "test-value"));
if (!topic) throw std::move(topic).status();
std::cout << "The topic was successfully updated: " << topic->DebugString()
<< "\n";
}
//! [update-topic]
(std::move(client), argv.at(0), argv.at(1));
}
void ListTopics(google::cloud::pubsub::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_list_topics] [list-topics]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string const& project_id) {
int count = 0;
for (auto& topic : client.ListTopics(project_id)) {
if (!topic) throw std::move(topic).status();
std::cout << "Topic Name: " << topic->name() << "\n";
++count;
}
if (count == 0) {
std::cout << "No topics found in project " << project_id << "\n";
}
}
//! [END pubsub_list_topics] [list-topics]
(std::move(client), argv.at(0));
}
void DeleteTopic(google::cloud::pubsub::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_delete_topic] [delete-topic]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string const& project_id,
std::string const& topic_id) {
auto status = client.DeleteTopic(
pubsub::Topic(std::move(project_id), std::move(topic_id)));
// Note that kNotFound is a possible result when the library retries.
if (status.code() == google::cloud::StatusCode::kNotFound) {
std::cout << "The topic was not found\n";
return;
}
if (!status.ok()) throw std::runtime_error(status.message());
std::cout << "The topic was successfully deleted\n";
}
//! [END pubsub_delete_topic] [delete-topic]
(std::move(client), argv.at(0), argv.at(1));
}
void DetachSubscription(google::cloud::pubsub::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_detach_subscription] [detach-subscription]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string project_id,
std::string subscription_id) {
auto response = client.DetachSubscription(pubsub::Subscription(
std::move(project_id), std::move(subscription_id)));
if (!response.ok()) throw std::move(response).status();
std::cout << "The subscription was successfully detached: "
<< response->DebugString() << "\n";
}
//! [END pubsub_detach_subscription] [detach-subscription]
(std::move(client), argv.at(0), argv.at(1));
}
void ListTopicSubscriptions(google::cloud::pubsub::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_list_topic_subscriptions] [list-topic-subscriptions]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string project_id,
std::string topic_id) {
auto const topic =
pubsub::Topic(std::move(project_id), std::move(topic_id));
std::cout << "Subscription list for topic " << topic << ":\n";
for (auto& name : client.ListTopicSubscriptions(topic)) {
if (!name) throw std::move(name).status();
std::cout << " " << *name << "\n";
}
}
//! [END pubsub_list_topic_subscriptions] [list-topic-subscriptions]
(std::move(client), argv.at(0), argv.at(1));
}
void ListTopicSnapshots(google::cloud::pubsub::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [list-topic-snapshots]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string project_id,
std::string topic_id) {
auto const topic =
pubsub::Topic(std::move(project_id), std::move(topic_id));
std::cout << "Snapshot list for topic " << topic << ":\n";
for (auto& name : client.ListTopicSnapshots(topic)) {
if (!name) throw std::move(name).status();
std::cout << " " << *name << "\n";
}
}
//! [list-topic-snapshots]
(std::move(client), argv.at(0), argv.at(1));
}
void CreateSubscription(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_create_pull_subscription] [create-subscription]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& topic_id, std::string const& subscription_id) {
auto sub = client.CreateSubscription(
pubsub::Topic(project_id, std::move(topic_id)),
pubsub::Subscription(project_id, std::move(subscription_id)));
if (sub.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The subscription already exists\n";
return;
}
if (!sub) throw std::move(sub).status();
std::cout << "The subscription was successfully created: "
<< sub->DebugString() << "\n";
}
//! [END pubsub_create_pull_subscription] [create-subscription]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void CreateSubscriptionWithExactlyOnceDelivery(
google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
// [START pubsub_create_subscription_with_exactly_once_delivery]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& topic_id, std::string const& subscription_id) {
auto sub = client.CreateSubscription(
pubsub::Topic(project_id, topic_id),
pubsub::Subscription(project_id, subscription_id),
pubsub::SubscriptionBuilder{}.enable_exactly_once_delivery(true));
if (sub.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The subscription already exists\n";
return;
}
if (!sub) throw std::move(sub).status();
std::cout << "The subscription was successfully created: "
<< sub->DebugString() << "\n";
}
// [END pubsub_create_subscription_with_exactly_once_delivery]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void CreateFilteredSubscription(
google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [create-filtered-subscription]
// [START pubsub_create_subscription_with_filter]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string topic_id, std::string subscription_id) {
auto sub = client.CreateSubscription(
pubsub::Topic(project_id, std::move(topic_id)),
pubsub::Subscription(project_id, std::move(subscription_id)),
pubsub::SubscriptionBuilder{}.set_filter(
R"""(attributes.is-even = "false")"""));
if (sub.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The subscription already exists\n";
return;
}
if (!sub) throw std::move(sub).status();
std::cout << "The subscription was successfully created: "
<< sub->DebugString() << "\n";
}
// [END pubsub_create_subscription_with_filter]
//! [create-filtered-subscription]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void CreatePushSubscription(
google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_create_push_subscription] [create-push-subscription]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& topic_id, std::string const& subscription_id,
std::string const& endpoint) {
auto sub = client.CreateSubscription(
pubsub::Topic(project_id, std::move(topic_id)),
pubsub::Subscription(project_id, std::move(subscription_id)),
pubsub::SubscriptionBuilder{}.set_push_config(
pubsub::PushConfigBuilder{}.set_push_endpoint(endpoint)));
if (sub.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The subscription already exists\n";
return;
}
if (!sub) throw std::move(sub).status();
std::cout << "The subscription was successfully created: "
<< sub->DebugString() << "\n";
}
//! [END pubsub_create_push_subscription] [create-push-subscription]
(std::move(client), argv.at(0), argv.at(1), argv.at(2), argv.at(3));
}
void CreateBigQuerySubscription(
google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_create_bigquery_subscription] [create-bigquery-subscription]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& topic_id, std::string const& subscription_id,
std::string const& table_id) {
auto sub = client.CreateSubscription(
pubsub::Topic(project_id, std::move(topic_id)),
pubsub::Subscription(project_id, std::move(subscription_id)),
pubsub::SubscriptionBuilder{}.set_bigquery_config(
pubsub::BigQueryConfigBuilder{}.set_table(table_id)));
if (sub.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The subscription already exists\n";
return;
}
if (!sub) throw std::move(sub).status();
std::cout << "The subscription was successfully created: "
<< sub->DebugString() << "\n";
}
//! [END pubsub_create_bigquery_subscription] [create-bigquery-subscription]
(std::move(client), argv.at(0), argv.at(1), argv.at(2), argv.at(3));
}
void CreateOrderingSubscription(
google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_enable_subscription_ordering] [enable-subscription-ordering]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& topic_id, std::string const& subscription_id) {
auto sub = client.CreateSubscription(
pubsub::Topic(project_id, std::move(topic_id)),
pubsub::Subscription(project_id, std::move(subscription_id)),
pubsub::SubscriptionBuilder{}.enable_message_ordering(true));
if (sub.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The subscription already exists\n";
return;
}
if (!sub) throw std::move(sub).status();
std::cout << "The subscription was successfully created: "
<< sub->DebugString() << "\n";
}
//! [END pubsub_enable_subscription_ordering] [enable-subscription-ordering]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void CreateDeadLetterSubscription(
google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [dead-letter-create-subscription]
// [START pubsub_dead_letter_create_subscription]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& topic_id, std::string const& subscription_id,
std::string const& dead_letter_topic_id,
int dead_letter_delivery_attempts) {
auto sub = client.CreateSubscription(
pubsub::Topic(project_id, std::move(topic_id)),
pubsub::Subscription(project_id, std::move(subscription_id)),
pubsub::SubscriptionBuilder{}.set_dead_letter_policy(
pubsub::SubscriptionBuilder::MakeDeadLetterPolicy(
pubsub::Topic(project_id, dead_letter_topic_id),
dead_letter_delivery_attempts)));
if (sub.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The subscription already exists\n";
return;
}
if (!sub) throw std::move(sub).status();
std::cout << "The subscription was successfully created: "
<< sub->DebugString() << "\n";
std::cout << "It will forward dead letter messages to: "
<< sub->dead_letter_policy().dead_letter_topic() << "\n";
std::cout << "After " << sub->dead_letter_policy().max_delivery_attempts()
<< " delivery attempts.\n";
}
// [END pubsub_dead_letter_create_subscription]
//! [dead-letter-create-subscription]
(std::move(client), argv.at(0), argv.at(1), argv.at(2), argv.at(3),
std::stoi(argv.at(4)));
}
void UpdateDeadLetterSubscription(
google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [dead-letter-update-subscription]
// [START pubsub_dead_letter_update_subscription]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& subscription_id,
std::string const& dead_letter_topic_id,
int dead_letter_delivery_attempts) {
auto sub = client.UpdateSubscription(
pubsub::Subscription(project_id, std::move(subscription_id)),
pubsub::SubscriptionBuilder{}.set_dead_letter_policy(
pubsub::SubscriptionBuilder::MakeDeadLetterPolicy(
pubsub::Topic(project_id, dead_letter_topic_id),
dead_letter_delivery_attempts)));
if (!sub) throw std::move(sub).status();
std::cout << "The subscription has been updated to: " << sub->DebugString()
<< "\n";
std::cout << "It will forward dead letter messages to: "
<< sub->dead_letter_policy().dead_letter_topic() << "\n";
std::cout << "After " << sub->dead_letter_policy().max_delivery_attempts()
<< " delivery attempts.\n";
}
// [END pubsub_dead_letter_update_subscription]
//! [dead-letter-update-subscription]
(std::move(client), argv.at(0), argv.at(1), argv.at(2),
std::stoi(argv.at(3)));
}
void ReceiveDeadLetterDeliveryAttempt(
google::cloud::pubsub::Subscriber subscriber,
std::vector<std::string> const&) {
auto const initial = EventCounter::Instance().Current();
//! [dead-letter-delivery-attempt]
// [START pubsub_dead_letter_delivery_attempt]
namespace pubsub = ::google::cloud::pubsub;
auto sample = [](pubsub::Subscriber subscriber) {
return subscriber.Subscribe(
[&](pubsub::Message const& m, pubsub::AckHandler h) {
std::cout << "Received message " << m << "\n";
std::cout << "Delivery attempt: " << h.delivery_attempt() << "\n";
std::move(h).ack();
PleaseIgnoreThisSimplifiesTestingTheSamples();
});
};
// [END pubsub_dead_letter_delivery_attempt]
//! [dead-letter-delivery-attempt]
EventCounter::Instance().Wait(
[initial](std::int64_t count) { return count > initial; },
sample(std::move(subscriber)), __func__);
}
void RemoveDeadLetterPolicy(
google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_dead_letter_remove] [dead-letter-remove]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& subscription_id) {
auto sub = client.UpdateSubscription(
pubsub::Subscription(project_id, std::move(subscription_id)),
pubsub::SubscriptionBuilder{}.clear_dead_letter_policy());
if (!sub) throw std::move(sub).status();
std::cout << "The subscription has been updated to: " << sub->DebugString()
<< "\n";
}
//! [END pubsub_dead_letter_remove] [dead-letter-remove]
(std::move(client), argv.at(0), argv.at(1));
}
void GetSubscription(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [get-subscription]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& subscription_id) {
auto sub = client.GetSubscription(
pubsub::Subscription(project_id, std::move(subscription_id)));
if (!sub) throw std::move(sub).status();
std::cout << "The subscription exists and its metadata is: "
<< sub->DebugString() << "\n";
}
//! [get-subscription]
(std::move(client), argv.at(0), argv.at(1));
}
void UpdateSubscription(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [update-subscription]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& subscription_id) {
auto s = client.UpdateSubscription(
pubsub::Subscription(project_id, std::move(subscription_id)),
pubsub::SubscriptionBuilder{}.set_ack_deadline(
std::chrono::seconds(60)));
if (!s) throw std::move(s).status();
std::cout << "The subscription has been updated to: " << s->DebugString()
<< "\n";
}
//! [update-subscription]
(std::move(client), argv.at(0), argv.at(1));
}
void ListSubscriptions(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_list_subscriptions] [list-subscriptions]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id) {
int count = 0;
for (auto& subscription : client.ListSubscriptions(project_id)) {
if (!subscription) throw std::move(subscription).status();
std::cout << "Subscription Name: " << subscription->name() << "\n";
++count;
}
if (count == 0) {
std::cout << "No subscriptions found in project " << project_id << "\n";
}
}
//! [END pubsub_list_subscriptions] [list-subscriptions]
(std::move(client), argv.at(0));
}
void DeleteSubscription(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_delete_subscription] [delete-subscription]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& subscription_id) {
auto status = client.DeleteSubscription(pubsub::Subscription(
std::move(project_id), std::move(subscription_id)));
// Note that kNotFound is a possible result when the library retries.
if (status.code() == google::cloud::StatusCode::kNotFound) {
std::cout << "The subscription was not found\n";
return;
}
if (!status.ok()) throw std::runtime_error(status.message());
std::cout << "The subscription was successfully deleted\n";
}
//! [END pubsub_delete_subscription] [delete-subscription]
(std::move(client), argv.at(0), argv.at(1));
}
void ModifyPushConfig(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_update_push_configuration] [modify-push-config]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& subscription_id, std::string const& endpoint) {
auto status = client.ModifyPushSubscription(
pubsub::Subscription(project_id, std::move(subscription_id)),
pubsub::PushConfigBuilder{}.set_push_endpoint(endpoint));
if (!status.ok()) throw std::runtime_error(status.message());
std::cout << "The subscription push configuration was successfully"
<< " modified\n";
}
//! [END pubsub_update_push_configuration] [modify-push-config]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void CreateSnapshot(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [create-snapshot-with-name]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& subscription_id, std::string const& snapshot_id) {
auto snapshot = client.CreateSnapshot(
pubsub::Subscription(project_id, subscription_id),
pubsub::Snapshot(project_id, std::move(snapshot_id)));
if (snapshot.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The snapshot already exists\n";
return;
}
if (!snapshot.ok()) throw std::move(snapshot).status();
std::cout << "The snapshot was successfully created: "
<< snapshot->DebugString() << "\n";
}
//! [create-snapshot-with-name]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void GetSnapshot(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [get-snapshot]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& snapshot_id) {
auto response = client.GetSnapshot(
pubsub::Snapshot(std::move(project_id), std::move(snapshot_id)));
if (!response.ok()) throw std::move(response).status();
std::cout << "The snapshot details are: " << response->DebugString()
<< "\n";
}
//! [get-snapshot]
(std::move(client), argv.at(0), argv.at(1));
}
void UpdateSnapshot(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [update-snapshot]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string snapshot_id) {
auto snap = client.UpdateSnapshot(
pubsub::Snapshot(project_id, std::move(snapshot_id)),
pubsub::SnapshotBuilder{}.add_label("samples-cpp", "gcp"));
if (!snap.ok()) throw std::move(snap).status();
std::cout << "The snapshot was successfully updated: "
<< snap->DebugString() << "\n";
}
//! [update-snapshot]
(std::move(client), argv.at(0), argv.at(1));
}
void ListSnapshots(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [list-snapshots]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id) {
std::cout << "Snapshot list for project " << project_id << ":\n";
for (auto& snapshot : client.ListSnapshots(project_id)) {
if (!snapshot) throw std::move(snapshot).status();
std::cout << "Snapshot Name: " << snapshot->name() << "\n";
}
}
//! [list-snapshots]
(std::move(client), argv.at(0));
}
void DeleteSnapshot(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [delete-snapshot]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& snapshot_id) {
auto status = client.DeleteSnapshot(
pubsub::Snapshot(std::move(project_id), std::move(snapshot_id)));
// Note that kNotFound is a possible result when the library retries.
if (status.code() == google::cloud::StatusCode::kNotFound) {
std::cout << "The snapshot was not found\n";
return;
}
if (!status.ok()) throw std::runtime_error(status.message());
std::cout << "The snapshot was successfully deleted\n";
}
//! [delete-snapshot]
(std::move(client), argv.at(0), argv.at(1));
}
void SeekWithSnapshot(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [seek-with-snapshot]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& subscription_id, std::string const& snapshot_id) {
auto response =
client.Seek(pubsub::Subscription(project_id, subscription_id),
pubsub::Snapshot(project_id, snapshot_id));
if (!response.ok()) throw std::move(response).status();
std::cout << "The subscription seek was successful: "
<< response->DebugString() << "\n";
}
//! [seek-with-snapshot]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void SeekWithTimestamp(google::cloud::pubsub::SubscriptionAdminClient client,
std::vector<std::string> const& argv) {
//! [seek-with-timestamp]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SubscriptionAdminClient client, std::string const& project_id,
std::string const& subscription_id, std::string const& seconds) {
auto response =
client.Seek(pubsub::Subscription(project_id, subscription_id),
std::chrono::system_clock::now() -
std::chrono::seconds(std::stoi(seconds)));
if (!response.ok()) throw std::move(response).status();
std::cout << "The subscription seek was successful: "
<< response->DebugString() << "\n";
}
//! [seek-with-timestamp]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void ExampleStatusOr(google::cloud::pubsub::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [example-status-or]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string const& project_id) {
// The actual type of `topic` is
// google::cloud::StatusOr<google::pubsub::v1::Topic>, but
// we expect it'll most often be declared with auto like this.
for (auto& topic : client.ListTopics(project_id)) {
// Use `topic` like a smart pointer; check it before de-referencing
if (!topic) {
// `topic` doesn't contain a value, so `.status()` will contain error
// info
std::cerr << topic.status() << "\n";
break;
}
std::cout << topic->DebugString() << "\n";
}
}
//! [example-status-or]
(std::move(client), argv.at(0));
}
void CreateAvroSchema(google::cloud::pubsub::SchemaServiceClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_create_avro_schema] [create-avro-schema]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
std::string const& schema_id, std::string const& schema_definition_file) {
std::string const definition = ReadFile(schema_definition_file);
google::pubsub::v1::CreateSchemaRequest request;
request.set_parent(google::cloud::Project(project_id).FullName());
request.set_schema_id(schema_id);
request.mutable_schema()->set_type(google::pubsub::v1::Schema::AVRO);
request.mutable_schema()->set_definition(definition);
auto schema = client.CreateSchema(request);
if (schema.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The schema already exists\n";
return;
}
if (!schema) throw std::move(schema).status();
std::cout << "Schema successfully created: " << schema->DebugString()
<< "\n";
}
//! [END pubsub_create_avro_schema] [create-avro-schema]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void CreateProtobufSchema(google::cloud::pubsub::SchemaServiceClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_create_proto_schema] [create-protobuf-schema]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
std::string const& schema_id, std::string const& schema_definition_file) {
std::string const definition = ReadFile(schema_definition_file);
google::pubsub::v1::CreateSchemaRequest request;
request.set_parent(google::cloud::Project(project_id).FullName());
request.set_schema_id(schema_id);
request.mutable_schema()->set_type(
google::pubsub::v1::Schema::PROTOCOL_BUFFER);
request.mutable_schema()->set_definition(definition);
auto schema = client.CreateSchema(request);
if (schema.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The schema already exists\n";
return;
}
if (!schema) throw std::move(schema).status();
std::cout << "Schema successfully created: " << schema->DebugString()
<< "\n";
}
//! [END pubsub_create_proto_schema] [create-protobuf-schema]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void GetSchema(google::cloud::pubsub::SchemaServiceClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_get_schema] [get-schema]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
std::string const& schema_id) {
google::pubsub::v1::GetSchemaRequest request;
request.set_name(pubsub::Schema(project_id, schema_id).FullName());
request.set_view(google::pubsub::v1::FULL);
auto schema = client.GetSchema(request);
if (!schema) throw std::move(schema).status();
std::cout << "The schema exists and its metadata is: "
<< schema->DebugString() << "\n";
}
//! [END pubsub_get_schema] [get-schema]
(std::move(client), argv.at(0), argv.at(1));
}
void ListSchemas(google::cloud::pubsub::SchemaServiceClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_list_schemas] [list-schemas]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id) {
auto const parent = google::cloud::Project(project_id).FullName();
for (auto& s : client.ListSchemas(parent)) {
if (!s) throw std::move(s).status();
std::cout << "Schema: " << s->DebugString() << "\n";
}
}
//! [END pubsub_list_schemas] [list-schemas]
(std::move(client), argv.at(0));
}
void DeleteSchema(google::cloud::pubsub::SchemaServiceClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_delete_schema] [delete-schema]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
std::string const& schema_id) {
google::pubsub::v1::DeleteSchemaRequest request;
request.set_name(pubsub::Schema(project_id, schema_id).FullName());
auto status = client.DeleteSchema(request);
// Note that kNotFound is a possible result when the library retries.
if (status.code() == google::cloud::StatusCode::kNotFound) {
std::cout << "The schema was not found\n";
return;
}
if (!status.ok()) throw std::runtime_error(status.message());
std::cout << "Schema successfully deleted\n";
}
//! [END pubsub_delete_schema] [delete-schema]
(std::move(client), argv.at(0), argv.at(1));
}
void ValidateAvroSchema(google::cloud::pubsub::SchemaServiceClient client,
std::vector<std::string> const& argv) {
//! [validate-avro-schema]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
std::string const& schema_definition_file) {
std::string const definition = ReadFile(schema_definition_file);
google::pubsub::v1::ValidateSchemaRequest request;
request.set_parent(google::cloud::Project(project_id).FullName());
request.mutable_schema()->set_type(google::pubsub::v1::Schema::AVRO);
request.mutable_schema()->set_definition(definition);
auto schema = client.ValidateSchema(request);
if (!schema) throw std::move(schema).status();
std::cout << "Schema is valid\n";
}
//! [validate-avro-schema]
(std::move(client), argv.at(0), argv.at(1));
}
void ValidateProtobufSchema(google::cloud::pubsub::SchemaServiceClient client,
std::vector<std::string> const& argv) {
//! [validate-protobuf-schema]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
std::string const& schema_definition_file) {
std::string const definition = ReadFile(schema_definition_file);
google::pubsub::v1::ValidateSchemaRequest request;
request.set_parent(google::cloud::Project(project_id).FullName());
request.mutable_schema()->set_type(
google::pubsub::v1::Schema::PROTOCOL_BUFFER);
request.mutable_schema()->set_definition(definition);
auto schema = client.ValidateSchema(request);
if (!schema) throw std::move(schema).status();
std::cout << "Schema is valid\n";
}
//! [validate-protobuf-schema]
(std::move(client), argv.at(0), argv.at(1));
}
void ValidateMessageAvro(google::cloud::pubsub::SchemaServiceClient client,
std::vector<std::string> const& argv) {
//! [validate-message-avro]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
std::string const& schema_definition_file,
std::string const& message_file) {
std::string const definition = ReadFile(schema_definition_file);
auto message = ReadFile(message_file);
google::pubsub::v1::ValidateMessageRequest request;
request.set_parent(google::cloud::Project(project_id).FullName());
request.mutable_schema()->set_type(google::pubsub::v1::Schema::AVRO);
request.mutable_schema()->set_definition(definition);
request.set_message(message);
request.set_encoding(google::pubsub::v1::JSON);
auto response = client.ValidateMessage(request);
if (!response) throw std::move(response).status();
std::cout << "Message is valid\n";
}
//! [validate-message-avro]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void ValidateMessageProtobuf(google::cloud::pubsub::SchemaServiceClient client,
std::vector<std::string> const& argv) {
//! [validate-message-protobuf]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
std::string const& schema_definition_file,
std::string const& message_file) {
std::string const definition = ReadFile(schema_definition_file);
std::string const message = ReadFile(message_file);
google::pubsub::v1::ValidateMessageRequest request;
request.set_parent(google::cloud::Project(project_id).FullName());
request.mutable_schema()->set_type(
google::pubsub::v1::Schema::PROTOCOL_BUFFER);
request.mutable_schema()->set_definition(definition);
request.set_message(message);
request.set_encoding(google::pubsub::v1::BINARY);
auto response = client.ValidateMessage(request);
if (!response) throw std::move(response).status();
std::cout << "Message is valid\n";
}
//! [validate-message-protobuf]
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}
void ValidateMessageNamedSchema(
google::cloud::pubsub::SchemaServiceClient client,
std::vector<std::string> const& argv) {
//! [validate-message-named-schema]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id,
std::string const& schema_id, std::string const& message_file) {
std::string message = ReadFile(message_file);
auto const schema = pubsub::Schema(project_id, schema_id);
google::pubsub::v1::ValidateMessageRequest request;
request.set_parent(google::cloud::Project(project_id).FullName());
request.set_name(schema.FullName());
request.set_message(message);
request.set_encoding(google::pubsub::v1::BINARY);
auto response = client.ValidateMessage(request);
if (!response) throw std::move(response).status();
std::cout << "Message is valid for schema " << schema << "\n";