forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic_admin_client.h
254 lines (237 loc) · 9.04 KB
/
topic_admin_client.h
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
// Copyright 2020 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.
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_TOPIC_ADMIN_CLIENT_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_TOPIC_ADMIN_CLIENT_H
#include "google/cloud/pubsub/snapshot.h"
#include "google/cloud/pubsub/topic_admin_connection.h"
#include "google/cloud/pubsub/topic_builder.h"
#include "google/cloud/pubsub/version.h"
#include <memory>
#include <string>
namespace google {
namespace cloud {
namespace pubsub {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
/**
* Performs topic admin operations in Cloud Pub/Sub.
*
* Applications use this class to perform operations on
* [Cloud Pub/Sub][pubsub-doc-link].
*
* @par Performance
* `TopicAdminClient` objects are cheap to create, copy, and move. However,
* each `TopicAdminClient` object must be created with a
* `std::shared_ptr<TopicAdminConnection>`, which itself is relatively
* expensive to create. Therefore, connection instances should be shared when
* possible. See the `MakeTopicAdminConnection()` function and the
* `TopicAdminConnection` interface for more details.
*
* @par Thread Safety
* Instances of this class created via copy-construction or copy-assignment
* share the underlying pool of connections. Access to these copies via multiple
* threads is guaranteed to work. Two threads operating on the same instance of
* this class is not guaranteed to work.
*
* @par Error Handling
* This class uses `StatusOr<T>` to report errors. When an operation fails to
* perform its work the returned `StatusOr<T>` contains the error details. If
* the `ok()` member function in the `StatusOr<T>` returns `true` then it
* contains the expected result. Please consult the [`StatusOr<T>`
* documentation](#google::cloud::StatusOr) for more details.
*
* [pubsub-doc-link]: https://cloud.google.com/pubsub/docs
*/
class TopicAdminClient {
public:
explicit TopicAdminClient(std::shared_ptr<TopicAdminConnection> connection,
Options opts = {});
/**
* The default constructor is deleted.
*
* Use `PublisherClient(std::shared_ptr<PublisherConnection>)`
*/
TopicAdminClient() = delete;
/**
* Creates a new topic in Cloud Pub/Sub.
*
* @par Idempotency
* This operation is idempotent, as it succeeds only once, therefore the
* library retries the call. It might return a status code of
* `kAlreadyExists` as a consequence of retrying a successful (but reported as
* failed) request.
*
* @par Example
* @snippet samples.cc create-topic
*
* @param builder the configuration for the new topic, includes the name.
* @param opts Override the class-level options, such as retry and backoff
* policies.
*/
StatusOr<google::pubsub::v1::Topic> CreateTopic(TopicBuilder builder,
Options opts = {}) {
return CreateTopic(std::move(builder).BuildCreateRequest(),
std::move(opts));
}
/// Create a new topic in Cloud Pub/Sub.
StatusOr<google::pubsub::v1::Topic> CreateTopic(
google::pubsub::v1::Topic request, Options opts = {}) {
internal::OptionsSpan span(
internal::MergeOptions(std::move(opts), options_));
return connection_->CreateTopic({std::move(request)});
}
/**
* Gets information about an existing Cloud Pub/Sub topic.
*
* @par Idempotency
* This is a read-only operation and therefore always idempotent and retried.
*
* @par Example
* @snippet samples.cc get-topic
*/
StatusOr<google::pubsub::v1::Topic> GetTopic(Topic topic, Options opts = {}) {
internal::OptionsSpan span(
internal::MergeOptions(std::move(opts), options_));
return connection_->GetTopic({std::move(topic)});
}
/**
* Updates the configuration of an existing Cloud Pub/Sub topic.
*
* @par Idempotency
* This operation is idempotent, the state of the system is the same after one
* or several calls, and therefore it is always retried.
*
* @par Example
* @snippet samples.cc update-topic
*
* @param builder the configuration for the new topic, includes the name.
* @param opts Override the class-level options, such as retry and backoff
* policies.
*/
StatusOr<google::pubsub::v1::Topic> UpdateTopic(TopicBuilder builder,
Options opts = {}) {
internal::OptionsSpan span(
internal::MergeOptions(std::move(opts), options_));
return connection_->UpdateTopic({std::move(builder).BuildUpdateRequest()});
}
/**
* Lists all the topics for a given project id.
*
* @par Idempotency
* This is a read-only operation and therefore always idempotent and retried.
*
* @par Example
* @snippet samples.cc list-topics
*/
ListTopicsRange ListTopics(std::string const& project_id, Options opts = {}) {
internal::OptionsSpan span(
internal::MergeOptions(std::move(opts), options_));
return connection_->ListTopics({"projects/" + project_id});
}
/**
* Deletes an existing topic in Cloud Pub/Sub.
*
* @par Idempotency
* This operation is idempotent, the state of the system is the same after one
* or several calls, and therefore it is always retried. It might return a
* status code of `kNotFound` as a consequence of retrying a successful
* (but reported as failed) request.
*
* @par Example
* @snippet samples.cc delete-topic
*
* @param topic the name of the topic to be deleted.
* @param opts Override the class-level options, such as retry and backoff
* policies.
*/
Status DeleteTopic(Topic topic, Options opts = {}) {
internal::OptionsSpan span(
internal::MergeOptions(std::move(opts), options_));
return connection_->DeleteTopic({std::move(topic)});
}
/**
* Detaches an existing subscription.
*
* This operation stops the subscription from receiving any further messages,
* it drops any messages still retained by the subscription, and any
* outstanding pull requests will fail with `kFailedPrecondition`.
*
* @par Idempotency
* This operation is idempotent, the state of the system is the same after one
* or several calls, and therefore it is always retried.
*
* @par Example
* @snippet samples.cc detach-subscription
*
* @param subscription the name of the subscription to detach.
* @param opts Override the class-level options, such as retry and backoff
* policies.
*/
StatusOr<google::pubsub::v1::DetachSubscriptionResponse> DetachSubscription(
Subscription subscription, Options opts = {}) {
internal::OptionsSpan span(
internal::MergeOptions(std::move(opts), options_));
return connection_->DetachSubscription({std::move(subscription)});
}
/**
* Lists all the subscription names for a given topic.
*
* @note
* The returned range contains fully qualified subscription names, e.g.,
* `"projects/my-project/subscriptions/my-subscription"`. Applications may
* need to parse these names to use with other APIs.
*
* @par Idempotency
* This is a read-only operation and therefore always idempotent and retried.
*
* @par Example
* @snippet samples.cc list-topic-subscriptions
*/
ListTopicSubscriptionsRange ListTopicSubscriptions(Topic const& topic,
Options opts = {}) {
internal::OptionsSpan span(
internal::MergeOptions(std::move(opts), options_));
return connection_->ListTopicSubscriptions({topic.FullName()});
}
/**
* Lists all the subscription names for a given topic.
*
* @note
* The returned range contains fully qualified snapshot names, e.g.,
* `"projects/my-project/snapshots/my-subscription"`. Applications may
* need to parse these names to use with other APIs.
*
* @par Idempotency
* This is a read-only operation and therefore always idempotent and retried.
*
* @par Example
* @snippet samples.cc list-topic-snapshots
*
* @see https://cloud.google.com/pubsub/docs/replay-overview for a detailed
* description of Cloud Pub/Sub's snapshots.
*/
ListTopicSnapshotsRange ListTopicSnapshots(Topic const& topic,
Options opts = {}) {
internal::OptionsSpan span(
internal::MergeOptions(std::move(opts), options_));
return connection_->ListTopicSnapshots({topic.FullName()});
}
private:
std::shared_ptr<TopicAdminConnection> connection_;
Options options_;
};
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace pubsub
} // namespace cloud
} // namespace google
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_TOPIC_ADMIN_CLIENT_H