Skip to content

Commit

Permalink
refactor(pubsub): move get topic, update topic, and list topic samples (
Browse files Browse the repository at this point in the history
#13527)

* refactor(pubsub): move get topic, update topic, and list topic samples

* fix tags

* add unused functions

* remove function that was moved

* add unused func

* fix docs

* remove sample reference

* remove docs

* fix

* remove

* remove admin
  • Loading branch information
alevenberg committed Feb 2, 2024
1 parent 3ec226c commit 73698cf
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 123 deletions.
124 changes: 24 additions & 100 deletions google/cloud/pubsub/samples/samples.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,61 +145,6 @@ void CreateTopic(google::cloud::pubsub::TopicAdminClient client,
}(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]
Expand Down Expand Up @@ -236,9 +181,28 @@ void DetachSubscription(google::cloud::pubsub::TopicAdminClient client,
(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]
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]
(std::move(client), argv.at(0));
}

void ListTopicSubscriptions(google::cloud::pubsub::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [START pubsub_list_topic_subscriptions] [list-topic-subscriptions]
//! [START pubsub_list_topic_subscriptions]
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string const& project_id,
std::string const& topic_id) {
Expand All @@ -249,25 +213,7 @@ void ListTopicSubscriptions(google::cloud::pubsub::TopicAdminClient client,
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]
//! [END pubsub_list_topic_subscriptions]
(std::move(client), argv.at(0), argv.at(1));
}

Expand Down Expand Up @@ -2416,22 +2362,12 @@ void AutoRun(std::vector<std::string> const& argv) {
std::cout << "\nRunning CreateTopic() sample [3]" << std::endl;
CreateTopic(topic_admin_client, {project_id, ordering_topic_id});

std::cout << "\nRunning GetTopic() sample" << std::endl;
GetTopic(topic_admin_client, {project_id, topic_id});

std::cout << "\nRunning UpdateTopic() sample" << std::endl;
ignore_emulator_failures(
[&] {
UpdateTopic(topic_admin_client, {project_id, topic_id});
},
StatusCode::kInvalidArgument);
std::cout << "\nRunning ListTopics() sample" << std::endl;
ListTopics(topic_admin_client, {project_id});

std::cout << "\nRunning the StatusOr example" << std::endl;
ExampleStatusOr(topic_admin_client, {project_id});

std::cout << "\nRunning ListTopics() sample" << std::endl;
ListTopics(topic_admin_client, {project_id});

std::cout << "\nRunning CreateSubscription() sample [1]" << std::endl;
CreateSubscription(subscription_admin_client,
{project_id, topic_id, subscription_id});
Expand Down Expand Up @@ -2543,9 +2479,6 @@ void AutoRun(std::vector<std::string> const& argv) {
CreateSnapshot(subscription_admin_client,
{project_id, subscription_id, snapshot_id});

std::cout << "\nRunning ListTopicSnapshots() sample" << std::endl;
ListTopicSnapshots(topic_admin_client, {project_id, topic_id});

std::cout << "\nRunning GetSnapshot() sample" << std::endl;
GetSnapshot(subscription_admin_client, {project_id, snapshot_id});

Expand Down Expand Up @@ -2759,19 +2692,10 @@ int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
using ::google::cloud::testing_util::Example;

Example example({
CreateTopicAdminCommand("get-topic", {"project-id", "topic-id"},
GetTopic),
CreateTopicAdminCommand("update-topic", {"project-id", "topic-id"},
UpdateTopic),
CreateTopicAdminCommand("list-topics", {"project-id"}, ListTopics),
CreateTopicAdminCommand("detach-subscription",
{"project-id", "subscription-id"},
DetachSubscription),
CreateTopicAdminCommand("list-topic-subscriptions",
{"project-id", "topic-id"},
ListTopicSubscriptions),
CreateTopicAdminCommand("list-topic-snapshots",
{"project-id", "topic-id"}, ListTopicSnapshots),

CreateSubscriptionAdminCommand(
"create-filtered-subscription",
{"project-id", "topic-id", "subscription-id"},
Expand Down
148 changes: 146 additions & 2 deletions google/cloud/pubsub/samples/topic_admin_samples.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace {

using ::google::cloud::pubsub::examples::RandomTopicId;

using ::google::cloud::pubsub::examples::UsingEmulator;
using TopicAdminCommand =
std::function<void(::google::cloud::pubsub_admin::TopicAdminClient,
std::vector<std::string> const&)>;
Expand Down Expand Up @@ -72,6 +72,104 @@ void CreateTopic(google::cloud::pubsub_admin::TopicAdminClient client,
(std::move(client), argv.at(0), argv.at(1));
}

void GetTopic(google::cloud::pubsub_admin::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [get-topic]
namespace pubsub = ::google::cloud::pubsub;
namespace pubsub_admin = ::google::cloud::pubsub_admin;
[](pubsub_admin::TopicAdminClient client, std::string project_id,
std::string topic_id) {
auto topic = client.GetTopic(
pubsub::Topic(std::move(project_id), std::move(topic_id)).FullName());
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_admin::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [update-topic]
namespace pubsub = ::google::cloud::pubsub;
namespace pubsub_admin = ::google::cloud::pubsub_admin;
[](pubsub_admin::TopicAdminClient client, std::string project_id,
std::string topic_id) {
google::pubsub::v1::UpdateTopicRequest request;
request.mutable_topic()->set_name(
pubsub::Topic(std::move(project_id), std::move(topic_id)).FullName());
request.mutable_topic()->mutable_labels()->insert(
{"test-key", "test-value"});
*request.mutable_update_mask()->add_paths() = "labels";
auto topic = client.UpdateTopic(request);
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_admin::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [list-topics]
namespace pubsub_admin = ::google::cloud::pubsub_admin;
[](pubsub_admin::TopicAdminClient client, std::string const& project_id) {
int count = 0;
for (auto& topic : client.ListTopics("projects/" + 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";
}
}
//! [list-topics]
(std::move(client), argv.at(0));
}

void ListTopicSubscriptions(
google::cloud::pubsub_admin::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [list-topic-subscriptions]
namespace pubsub_admin = ::google::cloud::pubsub_admin;
namespace pubsub = ::google::cloud::pubsub;
[](pubsub_admin::TopicAdminClient client, std::string const& project_id,
std::string const& topic_id) {
auto const topic = pubsub::Topic(project_id, topic_id);
std::cout << "Subscription list for topic " << topic << ":\n";
for (auto& name : client.ListTopicSubscriptions(topic.FullName())) {
if (!name) throw std::move(name).status();
std::cout << " " << *name << "\n";
}
}
//! [list-topic-subscriptions]
(std::move(client), argv.at(0), argv.at(1));
}

void ListTopicSnapshots(google::cloud::pubsub_admin::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [list-topic-snapshots]
namespace pubsub_admin = ::google::cloud::pubsub_admin;
namespace pubsub = ::google::cloud::pubsub;
[](pubsub_admin::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.FullName())) {
if (!name) throw std::move(name).status();
std::cout << " " << *name << "\n";
}
}
//! [list-topic-snapshots]
(std::move(client), argv.at(0), argv.at(1));
}

void DeleteTopic(google::cloud::pubsub_admin::TopicAdminClient client,
std::vector<std::string> const& argv) {
//! [delete-topic]
Expand Down Expand Up @@ -106,12 +204,48 @@ void AutoRun(std::vector<std::string> const& argv) {
auto const topic_id = RandomTopicId(generator);
auto const topic = google::cloud::pubsub::Topic(project_id, topic_id);

using ::google::cloud::StatusCode;
auto ignore_emulator_failures =
[](auto lambda, StatusCode code = StatusCode::kUnimplemented) {
try {
lambda();
} catch (google::cloud::Status const& s) {
if (UsingEmulator() && s.code() == code) return;
throw;
} catch (...) {
throw;
}
};

google::cloud::pubsub_admin::TopicAdminClient topic_admin_client(
google::cloud::pubsub_admin::MakeTopicAdminConnection());

std::cout << "\nRunning CreateTopic() sample" << std::endl;
std::cout << "\nRunning CreateTopic() sample [1]" << std::endl;
CreateTopic(topic_admin_client, {project_id, topic_id});

// Since the topic was created already, this should return kAlreadyExists.
std::cout << "\nRunning CreateTopic() sample [2]" << std::endl;
CreateTopic(topic_admin_client, {project_id, topic_id});

std::cout << "\nRunning GetTopic() sample" << std::endl;
GetTopic(topic_admin_client, {project_id, topic_id});

std::cout << "\nRunning UpdateTopic() sample" << std::endl;
ignore_emulator_failures(
[&] {
UpdateTopic(topic_admin_client, {project_id, topic_id});
},
StatusCode::kInvalidArgument);

std::cout << "\nRunning ListTopics() sample" << std::endl;
ListTopics(topic_admin_client, {project_id});

std::cout << "\nRunning ListTopicSnapshots() sample" << std::endl;
ListTopicSnapshots(topic_admin_client, {project_id, topic_id});

std::cout << "\nRunning ListTopicSubscriptions() sample" << std::endl;
ListTopicSubscriptions(topic_admin_client, {project_id, topic_id});

std::cout << "\nRunning DeleteTopic() sample" << std::endl;
DeleteTopic(topic_admin_client, {project_id, topic_id});

Expand All @@ -126,6 +260,16 @@ int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
Example example({
CreateTopicAdminCommand("create-topic", {"project-id", "topic-id"},
CreateTopic),
CreateTopicAdminCommand("get-topic", {"project-id", "topic-id"},
GetTopic),
CreateTopicAdminCommand("update-topic", {"project-id", "topic-id"},
UpdateTopic),
CreateTopicAdminCommand("list-topics", {"project-id"}, ListTopics),
CreateTopicAdminCommand("list-topic-subscriptions",
{"project-id", "topic-id"},
ListTopicSubscriptions),
CreateTopicAdminCommand("list-topic-snapshots",
{"project-id", "topic-id"}, ListTopicSnapshots),
CreateTopicAdminCommand("delete-topic", {"project-id", "topic-id"},
DeleteTopic),
{"auto", AutoRun},
Expand Down
Loading

0 comments on commit 73698cf

Please sign in to comment.