Google Cloud Pub/Sub C++ Client  2.1.0
A C++ Client Library for Google Cloud Pub/Sub
subscription_builder.h
Go to the documentation of this file.
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIPTION_BUILDER_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIPTION_BUILDER_H
17 
18 #include "google/cloud/pubsub/subscription.h"
19 #include "google/cloud/pubsub/topic.h"
20 #include "google/cloud/pubsub/version.h"
21 #include "google/cloud/internal/time_utils.h"
22 #include <google/protobuf/util/field_mask_util.h>
23 #include <google/pubsub/v1/pubsub.pb.h>
24 #include <chrono>
25 #include <set>
26 #include <string>
27 #include <vector>
28 
29 namespace google {
30 namespace cloud {
31 namespace pubsub {
34 
35 /**
36  * Helper class to create google::pubsub::v1::PushConfig protos.
37  *
38  * Makes it easier to create the protobuf messages consumed by
39  * `SubscriptionAdminClient`. The main advantages are:
40  *
41  * - Use a fluent API to set multiple values when constructing complex objects.
42  * - Automatically compute the set of paths for update requests.
43  */
45  public:
46  PushConfigBuilder() = default;
47  explicit PushConfigBuilder(std::string push_endpoint) {
48  set_push_endpoint(std::move(push_endpoint));
49  }
50 
51  /// Build a protocol buffer message to update an existing push config.
52  google::pubsub::v1::ModifyPushConfigRequest BuildModifyPushConfig(
53  Subscription const& subscription) &&;
54 
55  /// @name Setters for each protocol buffer field.
56  ///@{
58  proto_.set_push_endpoint(std::move(v));
59  paths_.insert("push_endpoint");
60  return *this;
61  }
62  PushConfigBuilder&& set_push_endpoint(std::string v) && {
63  return std::move(set_push_endpoint(std::move(v)));
64  }
65 
66  PushConfigBuilder& add_attribute(std::string const& key,
67  std::string const& value) & {
68  proto_.mutable_attributes()->insert(
69  google::protobuf::Map<std::string, std::string>::value_type(key,
70  value));
71  paths_.insert("attributes");
72  return *this;
73  }
74  PushConfigBuilder&& add_attribute(std::string const& key,
75  std::string const& value) && {
76  return std::move(add_attribute(key, value));
77  }
78 
80  std::vector<std::pair<std::string, std::string>> attr) & {
81  google::protobuf::Map<std::string, std::string> attributes;
82  for (auto& kv : attr) {
83  attributes[kv.first] = std::move(kv.second);
84  }
85  proto_.mutable_attributes()->swap(attributes);
86  paths_.insert("attributes");
87  return *this;
88  }
90  std::vector<std::pair<std::string, std::string>> attr) && {
91  return std::move(set_attributes(std::move(attr)));
92  }
93 
95  proto_.mutable_attributes()->clear();
96  paths_.insert("attributes");
97  return *this;
98  }
100  return std::move(clear_attributes());
101  }
102 
103  static google::pubsub::v1::PushConfig::OidcToken MakeOidcToken(
104  std::string service_account_email) {
105  google::pubsub::v1::PushConfig::OidcToken proto;
106  proto.set_service_account_email(std::move(service_account_email));
107  return proto;
108  }
109 
110  static google::pubsub::v1::PushConfig::OidcToken MakeOidcToken(
111  std::string service_account_email, std::string audience) {
112  google::pubsub::v1::PushConfig::OidcToken proto;
113  proto.set_service_account_email(std::move(service_account_email));
114  proto.set_audience(std::move(audience));
115  return proto;
116  }
117 
119  google::pubsub::v1::PushConfig::OidcToken token) & {
120  *proto_.mutable_oidc_token() = std::move(token);
121  paths_.insert("oidc_token");
122  return *this;
123  }
125  google::pubsub::v1::PushConfig::OidcToken token) && {
126  return std::move(set_authentication(std::move(token)));
127  }
128  //@}
129 
130  private:
131  friend class SubscriptionBuilder;
132  google::pubsub::v1::PushConfig proto_;
133  std::set<std::string> paths_;
134 };
135 
136 /**
137  * A helper class to build `google::pubsub::v1::BigQueryConfig` protos.
138  *
139  * Makes it easier to create the protobuf messages consumed by
140  * `SubscriptionAdminClient`. The main advantages are:
141  *
142  * - Use a fluent API to set multiple values when constructing complex objects.
143  * - Automatically compute the set of paths for update requests.
144  */
146  public:
148 
149  /// @name Setters for each protocol buffer field.
150  ///@{
151  BigQueryConfigBuilder& set_table(std::string full_path) & {
152  *proto_.mutable_table() = std::move(full_path);
153  paths_.insert("table");
154  return *this;
155  }
156  BigQueryConfigBuilder&& set_table(std::string full_path) && {
157  return std::move(set_table(std::move(full_path)));
158  }
159  BigQueryConfigBuilder& set_table(std::string const& project_id,
160  std::string const& data_set_id,
161  std::string const& table_id) & {
162  return set_table(project_id + ':' + data_set_id + '.' + table_id);
163  }
164  BigQueryConfigBuilder&& set_table(std::string const& project_id,
165  std::string const& data_set_id,
166  std::string const& table_id) && {
167  return std::move(set_table(project_id, data_set_id, table_id));
168  }
169 
171  proto_.set_use_topic_schema(v);
172  paths_.insert("use_topic_schema");
173  return *this;
174  }
176  return std::move(set_use_topic_schema(v));
177  }
178 
180  proto_.set_write_metadata(v);
181  paths_.insert("write_metadata");
182  return *this;
183  }
185  return std::move(set_write_metadata(v));
186  }
187 
189  proto_.set_drop_unknown_fields(v);
190  paths_.insert("drop_unknown_fields");
191  return *this;
192  }
194  return std::move(set_drop_unknown_fields(v));
195  }
196  ///@}
197 
198  private:
199  friend class SubscriptionBuilder;
200  google::pubsub::v1::BigQueryConfig proto_;
201  std::set<std::string> paths_;
202 };
203 
204 /**
205  * Create a Cloud Pub/Sub subscription configuration.
206  *
207  * Makes it easier to create the protobuf messages consumed by
208  * `SubscriptionAdminClient`. The main advantages are:
209  *
210  * - Use a fluent API to set multiple values when constructing complex objects.
211  * - Automatically compute the set of paths for update requests.
212  */
214  public:
215  SubscriptionBuilder() = default;
216 
217  /// Build a protocol buffer message to update an existing subscription.
218  google::pubsub::v1::UpdateSubscriptionRequest BuildUpdateRequest(
219  Subscription const& subscription) &&;
220 
221  /// Build a protocol buffer message to create a new subscription.
222  google::pubsub::v1::Subscription BuildCreateRequest(
223  Topic const& topic, Subscription const& subscription) &&;
224 
225  /// @name Setters for each protocol buffer field.
226  ///@{
229  return std::move(set_push_config(std::move(v)));
230  }
231 
234  return std::move(set_bigquery_config(std::move(v)));
235  }
236 
237  SubscriptionBuilder& set_ack_deadline(std::chrono::seconds v) & {
238  proto_.set_ack_deadline_seconds(static_cast<std::int32_t>(v.count()));
239  paths_.insert("ack_deadline_seconds");
240  return *this;
241  }
242  SubscriptionBuilder&& set_ack_deadline(std::chrono::seconds v) && {
243  return std::move(set_ack_deadline(v));
244  }
245 
247  proto_.set_retain_acked_messages(v);
248  paths_.insert("retain_acked_messages");
249  return *this;
250  }
252  return std::move(set_retain_acked_messages(v));
253  }
254 
255  template <typename Rep, typename Period>
257  std::chrono::duration<Rep, Period> d) & {
258  *proto_.mutable_message_retention_duration() =
259  google::cloud::internal::ToDurationProto(std::move(d));
260  paths_.insert("message_retention_duration");
261  return *this;
262  }
263  template <typename Rep, typename Period>
265  std::chrono::duration<Rep, Period> d) && {
266  return std::move(set_message_retention_duration(d));
267  }
268 
269  SubscriptionBuilder& add_label(std::string const& key,
270  std::string const& value) & {
271  using value_type =
272  google::protobuf::Map<std::string, std::string>::value_type;
273  proto_.mutable_labels()->insert(value_type(key, value));
274  paths_.insert("labels");
275  return *this;
276  }
277  SubscriptionBuilder&& add_label(std::string const& key,
278  std::string const& value) && {
279  return std::move(add_label(key, value));
280  }
281 
283  std::vector<std::pair<std::string, std::string>> new_labels) & {
284  google::protobuf::Map<std::string, std::string> labels;
285  for (auto& kv : new_labels) {
286  labels[kv.first] = std::move(kv.second);
287  }
288  proto_.mutable_labels()->swap(labels);
289  paths_.insert("labels");
290  return *this;
291  }
293  std::vector<std::pair<std::string, std::string>> new_labels) && {
294  return std::move(set_labels(std::move(new_labels)));
295  }
296 
298  proto_.clear_labels();
299  paths_.insert("labels");
300  return *this;
301  }
302  SubscriptionBuilder&& clear_labels() && { return std::move(clear_labels()); }
303 
305  proto_.set_enable_message_ordering(v);
306  paths_.insert("enable_message_ordering");
307  return *this;
308  }
310  return std::move(enable_message_ordering(v));
311  }
312 
314  google::pubsub::v1::ExpirationPolicy v) & {
315  *proto_.mutable_expiration_policy() = std::move(v);
316  paths_.insert("expiration_policy");
317  return *this;
318  }
320  google::pubsub::v1::ExpirationPolicy v) && {
321  return std::move(set_expiration_policy(std::move(v)));
322  }
323 
324  SubscriptionBuilder& set_filter(std::string v) & {
325  proto_.set_filter(std::move(v));
326  paths_.insert("filter");
327  return *this;
328  }
329  SubscriptionBuilder&& set_filter(std::string v) && {
330  return std::move(set_filter(std::move(v)));
331  }
332 
334  google::pubsub::v1::DeadLetterPolicy v) & {
335  *proto_.mutable_dead_letter_policy() = std::move(v);
336  paths_.insert("dead_letter_policy");
337  return *this;
338  }
340  google::pubsub::v1::DeadLetterPolicy v) && {
341  return std::move(set_dead_letter_policy(std::move(v)));
342  }
343 
345  proto_.clear_dead_letter_policy();
346  paths_.insert("dead_letter_policy");
347  return *this;
348  }
350  return std::move(clear_dead_letter_policy());
351  }
352 
353  SubscriptionBuilder& set_retry_policy(google::pubsub::v1::RetryPolicy v) & {
354  *proto_.mutable_retry_policy() = std::move(v);
355  paths_.insert("retry_policy");
356  return *this;
357  }
358  SubscriptionBuilder&& set_retry_policy(google::pubsub::v1::RetryPolicy v) && {
359  return std::move(set_retry_policy(std::move(v)));
360  }
361 
363  proto_.clear_retry_policy();
364  paths_.insert("retry_policy");
365  return *this;
366  }
368  return std::move(clear_retry_policy());
369  }
370 
372  proto_.set_enable_exactly_once_delivery(v);
373  paths_.insert("enable_exactly_once_delivery");
374  return *this;
375  }
377  return std::move(enable_exactly_once_delivery(v));
378  }
379  ///@}
380 
381  /**
382  * Construct a `google::pubsub::v1::ExpirationPolicy` using a C++ duration.
383  *
384  * This is a convenience function to create the `set_expiration_policy()`
385  * argument.
386  */
387  template <typename Rep, typename Period>
388  static google::pubsub::v1::ExpirationPolicy MakeExpirationPolicy(
389  std::chrono::duration<Rep, Period> d) {
390  google::pubsub::v1::ExpirationPolicy result;
391  *result.mutable_ttl() =
392  google::cloud::internal::ToDurationProto(std::move(d));
393  return result;
394  }
395 
396  /**
397  * Construct a `google::pubsub::v1::DeadLetterPolicy`.
398  *
399  * This is a convenience function to create the `set_dead_letter_policy()`
400  * argument.
401  */
402  static google::pubsub::v1::DeadLetterPolicy MakeDeadLetterPolicy(
403  Topic const& dead_letter_topic, std::int32_t max_delivery_attempts = 0) {
404  google::pubsub::v1::DeadLetterPolicy result;
405  result.set_dead_letter_topic(dead_letter_topic.FullName());
406  result.set_max_delivery_attempts(max_delivery_attempts);
407  return result;
408  }
409 
410  /**
411  * Construct a `google::pubsub::v1::RetryPolicy` using C++ durations.
412  *
413  * This is a convenience function to create the `set_retry_policy()`
414  * argument.
415  */
416  template <typename Rep1, typename Period1, typename Rep2, typename Period2>
417  static google::pubsub::v1::RetryPolicy MakeRetryPolicy(
418  std::chrono::duration<Rep1, Period1> minimum_backoff,
419  std::chrono::duration<Rep2, Period2> maximum_backoff) {
420  google::pubsub::v1::RetryPolicy result;
421  *result.mutable_minimum_backoff() =
422  google::cloud::internal::ToDurationProto(minimum_backoff);
423  *result.mutable_maximum_backoff() =
424  google::cloud::internal::ToDurationProto(maximum_backoff);
425  return result;
426  }
427 
428  private:
429  google::pubsub::v1::Subscription proto_;
430  std::set<std::string> paths_;
431 };
432 
434 } // namespace pubsub
435 } // namespace cloud
436 } // namespace google
437 
438 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIPTION_BUILDER_H