Google Cloud Pub/Sub C++ Client 2.13.0
A C++ Client Library for Google Cloud Pub/Sub
Loading...
Searching...
No Matches
subscription_admin_client.h
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_ADMIN_CLIENT_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIPTION_ADMIN_CLIENT_H
17
18#include "google/cloud/pubsub/snapshot_builder.h"
19#include "google/cloud/pubsub/subscription_admin_connection.h"
20#include "google/cloud/pubsub/subscription_builder.h"
21#include "google/cloud/pubsub/version.h"
22#include <chrono>
23#include <memory>
24#include <string>
25
26namespace google {
27namespace cloud {
28namespace pubsub {
29GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
30
31/**
32 * Performs subscription administrative operations in Cloud Pub/Sub.
33 *
34 * Applications use this class to perform subscription administrative operations
35 * on [Cloud Pub/Sub][pubsub-doc-link].
36 *
37 * @par Performance
38 * `SubscriptionAdminClient` objects are relatively cheap to create, copy, and
39 * move. However, each `SubscriptionAdminClient` object must be created with a
40 * `std::shared_ptr<SubscriptionAdminConnection>`, which itself is relatively
41 * expensive to create. Therefore, connection instances should be shared when
42 * possible. See the `MakeSubscriptionAdminConnection()` function and the
43 * `SubscriptionAdminConnection` interface for more details.
44 *
45 * @par Thread Safety
46 * Instances of this class created via copy-construction or copy-assignment
47 * share the underlying pool of connections. Access to these copies via multiple
48 * threads is guaranteed to work. Two threads operating on the same instance of
49 * this class is not guaranteed to work.
50 *
51 * @par Error Handling
52 * This class uses `StatusOr<T>` to report errors. When an operation fails to
53 * perform its work the returned `StatusOr<T>` contains the error details. If
54 * the `ok()` member function in the `StatusOr<T>` returns `true` then it
55 * contains the expected result. Please consult the [`StatusOr<T>`
56 * documentation](#google::cloud::StatusOr) for more details.
57 *
58 * [pubsub-doc-link]: https://cloud.google.com/pubsub/docs
59 */
61 public:
63 std::shared_ptr<SubscriptionAdminConnection> connection,
64 Options opts = {});
65
66 /**
67 * The default constructor is deleted.
68 *
69 * Use `SubscriberClient(std::shared_ptr<SubscriberConnection>)`
70 */
71 SubscriptionAdminClient() = delete;
72
73 /**
74 * Creates a new subscription in Cloud Pub/Sub.
75 *
76 * @par Idempotency
77 * This operation is idempotent, the state of the system is the same after one
78 * or several calls, and therefore it is always retried. It might return a
79 * status code of `kAlreadyExists` as a consequence of retrying a successful
80 * (but reported as failed) request.
81 *
82 * @par Example: Create a Pull Subscription
83 * @snippet samples.cc create-subscription
84 *
85 * @par Example: Create a Push Subscription
86 * @snippet samples.cc create-push-subscription
87 *
88 * @par Example: Create a BigQuery Subscription
89 * @snippet samples.cc create-bigquery-subscription
90 *
91 * @param topic the topic that the subscription will attach to
92 * @param subscription the name for the subscription
93 * @param builder any additional configuration for the subscription
94 * @param opts Override the class-level options, such as retry and backoff
95 * policies.
96 */
97 StatusOr<google::pubsub::v1::Subscription> CreateSubscription(
98 Topic const& topic, Subscription const& subscription,
99 SubscriptionBuilder builder = {}, Options opts = {}) {
100 internal::OptionsSpan span(
101 internal::MergeOptions(std::move(opts), options_));
102 return connection_->CreateSubscription(
103 {std::move(builder).BuildCreateRequest(topic, subscription)});
104 }
105
106 /**
107 * Gets the metadata for an existing Cloud Pub/Sub subscription.
108 *
109 * @par Idempotency
110 * This is a read-only operation and therefore always idempotent and retried.
111 *
112 * @par Example
113 * @snippet samples.cc get-subscription
114 */
115 StatusOr<google::pubsub::v1::Subscription> GetSubscription(
116 Subscription subscription, Options opts = {}) {
117 internal::OptionsSpan span(
118 internal::MergeOptions(std::move(opts), options_));
119 return connection_->GetSubscription({std::move(subscription)});
120 }
121
122 /**
123 * Updates an existing subscription in Cloud Pub/Sub.
124 *
125 * @par Idempotency
126 * This operation is idempotent, the state of the system is the same after one
127 * or several calls, and therefore it is always retried.
128 *
129 * @par Example
130 * @snippet samples.cc update-subscription
131 *
132 * @param subscription the name for the subscription
133 * @param builder any additional configuration for the subscription
134 * @param opts Override the class-level options, such as retry and backoff
135 * policies.
136 */
137 StatusOr<google::pubsub::v1::Subscription> UpdateSubscription(
138 Subscription const& subscription, SubscriptionBuilder builder,
139 Options opts = {}) {
140 internal::OptionsSpan span(
141 internal::MergeOptions(std::move(opts), options_));
142 return connection_->UpdateSubscription(
143 {std::move(builder).BuildUpdateRequest(subscription)});
144 }
145
146 /**
147 * Lists all the subscriptions for a given project id.
148 *
149 * @par Idempotency
150 * This is a read-only operation and therefore always idempotent and retried.
151 *
152 * @par Example
153 * @snippet samples.cc list-subscriptions
154 */
155 ListSubscriptionsRange ListSubscriptions(std::string const& project_id,
156 Options opts = {}) {
157 internal::OptionsSpan span(
158 internal::MergeOptions(std::move(opts), options_));
159 return connection_->ListSubscriptions({"projects/" + project_id});
160 }
161
162 /**
163 * Deletes an existing subscription in Cloud Pub/Sub.
164 *
165 * @par Idempotency
166 * This operation is idempotent, the state of the system is the same after one
167 * or several calls, and therefore it is always retried. It might return a
168 * status code of `kNotFound` as a consequence of retrying a successful
169 * (but reported as failed) request.
170 *
171 * @par Example
172 * @snippet samples.cc delete-subscription
173 *
174 * @param subscription the name of the subscription to be deleted.
175 * @param opts Override the class-level options, such as retry and backoff
176 * policies.
177 */
178 Status DeleteSubscription(Subscription subscription, Options opts = {}) {
179 internal::OptionsSpan span(
180 internal::MergeOptions(std::move(opts), options_));
181 return connection_->DeleteSubscription({std::move(subscription)});
182 }
183
184 /**
185 * Modifies an existing subscription's push configuration.
186 *
187 * This can change a push subscription into a pull subscription (by setting
188 * an empty push config), change the push endpoint, or change a pull
189 * subscription into a push config.
190 *
191 * @par Idempotency
192 * This operation is idempotent, the state of the system is the same after one
193 * or several calls, and therefore it is always retried.
194 *
195 * @par Example
196 * @snippet samples.cc modify-push-config
197 *
198 * @param subscription the name of the subscription to be modified.
199 * @param builder a description of the changes to be made.
200 * @param opts Override the class-level options, such as retry and backoff
201 * policies.
202 */
203 Status ModifyPushSubscription(Subscription const& subscription,
204 PushConfigBuilder builder, Options opts = {}) {
205 internal::OptionsSpan span(
206 internal::MergeOptions(std::move(opts), options_));
207 return connection_->ModifyPushConfig(
208 {std::move(builder).BuildModifyPushConfig(subscription)});
209 }
210
211 /**
212 * Creates a new snapshot for a subscription with a server-assigned name.
213 *
214 * @par Idempotency
215 * This is not an idempotent operation, repeated calls would create multiple
216 * snapshots with different names assigned by the service, and therefore
217 * it is never retried.
218 *
219 * @param subscription the name of the subscription
220 * @param builder additional configuration for the snapshot, e.g., labels
221 * @param opts Override the class-level options, such as retry and backoff
222 * policies.
223 */
224 StatusOr<google::pubsub::v1::Snapshot> CreateSnapshot(
225 Subscription const& subscription, SnapshotBuilder builder = {},
226 Options opts = {}) {
227 internal::OptionsSpan span(
228 internal::MergeOptions(std::move(opts), options_));
229 return connection_->CreateSnapshot(
230 {std::move(builder).BuildCreateRequest(subscription)});
231 }
232
233 /**
234 * Creates a new snapshot for a subscription with a given name.
235 *
236 * @par Idempotency
237 * This operation is idempotent, the state of the system is the same after one
238 * or several calls, and therefore it is always retried.
239 *
240 * @par Example
241 * @snippet samples.cc create-snapshot-with-name
242 *
243 * @param subscription the name of the subscription
244 * @param snapshot the name of the snapshot
245 * @param builder additional configuration for the snapshot, e.g., labels
246 * @param opts Override the class-level options, such as retry and backoff
247 * policies.
248 *
249 * @see https://cloud.google.com/pubsub/docs/replay-overview for a detailed
250 * description of Cloud Pub/Sub's snapshots.
251 */
252 StatusOr<google::pubsub::v1::Snapshot> CreateSnapshot(
253 Subscription const& subscription, Snapshot const& snapshot,
254 SnapshotBuilder builder = {}, Options opts = {}) {
255 internal::OptionsSpan span(
256 internal::MergeOptions(std::move(opts), options_));
257 return connection_->CreateSnapshot(
258 {std::move(builder).BuildCreateRequest(subscription, snapshot)});
259 }
260
261 /**
262 * Gets information about an existing snapshot.
263 *
264 * @par Idempotency
265 * This is a read-only operation and therefore always idempotent and retried.
266 *
267 * @par Example
268 * @snippet samples.cc get-snapshot
269 *
270 * @param snapshot the name of the snapshot
271 * @param opts Override the class-level options, such as retry and backoff
272 * policies.
273 *
274 * @see https://cloud.google.com/pubsub/docs/replay-overview for a detailed
275 * description of Cloud Pub/Sub's snapshots.
276 */
277 StatusOr<google::pubsub::v1::Snapshot> GetSnapshot(Snapshot const& snapshot,
278 Options opts = {}) {
279 internal::OptionsSpan span(
280 internal::MergeOptions(std::move(opts), options_));
281 return connection_->GetSnapshot({snapshot});
282 }
283
284 /**
285 * Updates an existing snapshot.
286 *
287 * @par Idempotency
288 * This operation is idempotent, the state of the system is the same after one
289 * or several calls, and therefore it is always retried.
290 *
291 * @par Example
292 * @snippet samples.cc update-snapshot
293 *
294 * @param snapshot the name of the snapshot
295 * @param builder the changes applied to the snapshot
296 * @param opts Override the class-level options, such as retry and backoff
297 * policies.
298 *
299 * @see https://cloud.google.com/pubsub/docs/replay-overview for a detailed
300 * description of Cloud Pub/Sub's snapshots.
301 */
302 StatusOr<google::pubsub::v1::Snapshot> UpdateSnapshot(
303 Snapshot const& snapshot, SnapshotBuilder builder, Options opts = {}) {
304 internal::OptionsSpan span(
305 internal::MergeOptions(std::move(opts), options_));
306 return connection_->UpdateSnapshot(
307 {std::move(builder).BuildUpdateRequest(snapshot)});
308 }
309
310 /**
311 * Lists all the snapshots for a given project id.
312 *
313 * @par Idempotency
314 * This is a read-only operation and therefore always idempotent and retried.
315 *
316 * @par Example
317 * @snippet samples.cc list-snapshots
318 *
319 * @see https://cloud.google.com/pubsub/docs/replay-overview for a detailed
320 * description of Cloud Pub/Sub's snapshots.
321 */
322 ListSnapshotsRange ListSnapshots(std::string const& project_id,
323 Options opts = {}) {
324 internal::OptionsSpan span(
325 internal::MergeOptions(std::move(opts), options_));
326 return connection_->ListSnapshots({"projects/" + project_id});
327 }
328
329 /**
330 * Deletes a snapshot.
331 *
332 * @par Idempotency
333 * This operation is idempotent, the state of the system is the same after one
334 * or several calls, and therefore it is always retried.
335 *
336 * @par Example
337 * @snippet samples.cc create-snapshot-with-name
338 *
339 * @param snapshot the name of the snapshot
340 * @param opts Override the class-level options, such as retry and backoff
341 * policies.
342 *
343 * @see https://cloud.google.com/pubsub/docs/replay-overview for a detailed
344 * description of Cloud Pub/Sub's snapshots.
345 */
346 Status DeleteSnapshot(Snapshot const& snapshot, Options opts = {}) {
347 internal::OptionsSpan span(
348 internal::MergeOptions(std::move(opts), options_));
349 return connection_->DeleteSnapshot({snapshot});
350 }
351
352 /**
353 * Seeks a subscription to its state at @p timestamp.
354 *
355 * Messages retained in the subscription that were published before
356 * @p timestamp are marked as acknowledged, while messages published after
357 * @p timestamp are marked as unacknowledged.
358 *
359 * @par Idempotency
360 * This operation is idempotent, the state of the system is the same after one
361 * or several calls, and therefore it is always retried.
362 *
363 * @par Example
364 * @snippet samples.cc seek-with-timestamp
365 *
366 * @see https://cloud.google.com/pubsub/docs/replay-overview for a detailed
367 * description of Cloud Pub/Sub's `Seek()` functionality.
368 */
369 StatusOr<google::pubsub::v1::SeekResponse> Seek(
370 Subscription const& subscription,
371 std::chrono::system_clock::time_point timestamp, Options opts = {});
372
373 /**
374 * Seeks a subscription to its state at @p snapshot.
375 *
376 * @par Idempotency
377 * This operation is idempotent, the state of the system is the same after one
378 * or several calls, and therefore it is always retried.
379 *
380 * @par Example
381 * @snippet samples.cc seek-with-timestamp
382 *
383 * @see https://cloud.google.com/pubsub/docs/replay-overview for a detailed
384 * description of Cloud Pub/Sub's `Seek()` functionality.
385 */
386 StatusOr<google::pubsub::v1::SeekResponse> Seek(
387 Subscription const& subscription, Snapshot const& snapshot,
388 Options opts = {});
389
390 private:
391 std::shared_ptr<SubscriptionAdminConnection> connection_;
392 Options options_;
393};
394
395GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
396} // namespace pubsub
397} // namespace cloud
398} // namespace google
399
400#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIPTION_ADMIN_CLIENT_H
Helper class to create google::pubsub::v1::PushConfig protos.
Definition: subscription_builder.h:44
google::pubsub::v1::ModifyPushConfigRequest BuildModifyPushConfig(Subscription const &subscription) &&
Build a protocol buffer message to update an existing push config.
Build a request to create a Cloud Pub/Sub snapshot.
Definition: snapshot_builder.h:41
google::pubsub::v1::UpdateSnapshotRequest BuildUpdateRequest(Snapshot const &snapshot) &&
Build a protocol buffer message to update an existing snapshot.
google::pubsub::v1::CreateSnapshotRequest BuildCreateRequest(Subscription const &subscription, Snapshot const &snapshot) &&
Build a protocol buffer message to create snapshots with application-assigned ids.
google::pubsub::v1::CreateSnapshotRequest BuildCreateRequest(Subscription const &subscription) &&
Build a protocol buffer message to create snapshots with server-assigned ids.
Objects of this class identify a Cloud Pub/Sub snapshot.
Definition: snapshot.h:37
Performs subscription administrative operations in Cloud Pub/Sub.
Definition: subscription_admin_client.h:60
StatusOr< google::pubsub::v1::Subscription > UpdateSubscription(Subscription const &subscription, SubscriptionBuilder builder, Options opts={})
Updates an existing subscription in Cloud Pub/Sub.
Definition: subscription_admin_client.h:137
SubscriptionAdminClient(std::shared_ptr< SubscriptionAdminConnection > connection, Options opts={})
ListSubscriptionsRange ListSubscriptions(std::string const &project_id, Options opts={})
Lists all the subscriptions for a given project id.
Definition: subscription_admin_client.h:155
StatusOr< google::pubsub::v1::Subscription > GetSubscription(Subscription subscription, Options opts={})
Gets the metadata for an existing Cloud Pub/Sub subscription.
Definition: subscription_admin_client.h:115
StatusOr< google::pubsub::v1::Subscription > CreateSubscription(Topic const &topic, Subscription const &subscription, SubscriptionBuilder builder={}, Options opts={})
Creates a new subscription in Cloud Pub/Sub.
Definition: subscription_admin_client.h:97
Status ModifyPushSubscription(Subscription const &subscription, PushConfigBuilder builder, Options opts={})
Modifies an existing subscription's push configuration.
Definition: subscription_admin_client.h:203
StatusOr< google::pubsub::v1::SeekResponse > Seek(Subscription const &subscription, Snapshot const &snapshot, Options opts={})
Seeks a subscription to its state at snapshot.
Status DeleteSnapshot(Snapshot const &snapshot, Options opts={})
Deletes a snapshot.
Definition: subscription_admin_client.h:346
StatusOr< google::pubsub::v1::Snapshot > GetSnapshot(Snapshot const &snapshot, Options opts={})
Gets information about an existing snapshot.
Definition: subscription_admin_client.h:277
StatusOr< google::pubsub::v1::Snapshot > CreateSnapshot(Subscription const &subscription, SnapshotBuilder builder={}, Options opts={})
Creates a new snapshot for a subscription with a server-assigned name.
Definition: subscription_admin_client.h:224
StatusOr< google::pubsub::v1::Snapshot > UpdateSnapshot(Snapshot const &snapshot, SnapshotBuilder builder, Options opts={})
Updates an existing snapshot.
Definition: subscription_admin_client.h:302
SubscriptionAdminClient()=delete
The default constructor is deleted.
StatusOr< google::pubsub::v1::Snapshot > CreateSnapshot(Subscription const &subscription, Snapshot const &snapshot, SnapshotBuilder builder={}, Options opts={})
Creates a new snapshot for a subscription with a given name.
Definition: subscription_admin_client.h:252
Status DeleteSubscription(Subscription subscription, Options opts={})
Deletes an existing subscription in Cloud Pub/Sub.
Definition: subscription_admin_client.h:178
StatusOr< google::pubsub::v1::SeekResponse > Seek(Subscription const &subscription, std::chrono::system_clock::time_point timestamp, Options opts={})
Seeks a subscription to its state at timestamp.
ListSnapshotsRange ListSnapshots(std::string const &project_id, Options opts={})
Lists all the snapshots for a given project id.
Definition: subscription_admin_client.h:322
A connection to Cloud Pub/Sub for subscription-related administrative operations.
Definition: subscription_admin_connection.h:82
virtual StatusOr< google::pubsub::v1::Snapshot > CreateSnapshot(CreateSnapshotParams)
Defines the interface for SnapshotAdminClient::CreateSnapshot()
virtual StatusOr< google::pubsub::v1::Subscription > GetSubscription(GetSubscriptionParams)
Defines the interface for SubscriptionAdminClient::GetSubscription()
virtual StatusOr< google::pubsub::v1::Snapshot > GetSnapshot(GetSnapshotParams)
Defines the interface for SnapshotAdminClient::GetSnapshot()
virtual Status DeleteSubscription(DeleteSubscriptionParams)
Defines the interface for SubscriptionAdminClient::DeleteSubscription()
virtual Status DeleteSnapshot(DeleteSnapshotParams)
Defines the interface for SnapshotAdminClient::DeleteSnapshot()
virtual StatusOr< google::pubsub::v1::Subscription > CreateSubscription(CreateSubscriptionParams)
Defines the interface for SubscriptionAdminClient::CreateSubscription()
virtual Status ModifyPushConfig(ModifyPushConfigParams)
Defines the interface for SubscriptionAdminClient::ModifyPushConfig()
virtual StatusOr< google::pubsub::v1::Subscription > UpdateSubscription(UpdateSubscriptionParams)
Defines the interface for SubscriptionAdminClient::UpdateSubscription()
virtual ListSnapshotsRange ListSnapshots(ListSnapshotsParams)
Defines the interface for SubscriptionAdminClient::ListSnapshots()
virtual StatusOr< google::pubsub::v1::Snapshot > UpdateSnapshot(UpdateSnapshotParams)
Defines the interface for SnapshotAdminClient::UpdateSnapshot()
virtual ListSubscriptionsRange ListSubscriptions(ListSubscriptionsParams)
Defines the interface for SubscriptionAdminClient::ListSubscriptions()
Create a Cloud Pub/Sub subscription configuration.
Definition: subscription_builder.h:317
google::pubsub::v1::Subscription BuildCreateRequest(Topic const &topic, Subscription const &subscription) &&
Build a protocol buffer message to create a new subscription.
google::pubsub::v1::UpdateSubscriptionRequest BuildUpdateRequest(Subscription const &subscription) &&
Build a protocol buffer message to update an existing subscription.
Objects of this class identify a Cloud Pub/Sub subscription.
Definition: subscription.h:37
Objects of this class identify a Cloud Pub/Sub topic.
Definition: topic.h:37
Contains all the Cloud Pub/Sub C++ client types and functions.
Definition: ack_handler.h:25
The namespace Google Cloud Platform C++ client libraries.