|
| 1 | +// Copyright 2024 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 | +#include "google/cloud/pubsub/admin/subscription_admin_client.h" |
| 16 | +#include "google/cloud/pubsub/admin/topic_admin_client.h" |
| 17 | +#include "google/cloud/pubsub/samples/pubsub_samples_common.h" |
| 18 | +#include "google/cloud/pubsub/subscription.h" |
| 19 | +#include "google/cloud/internal/getenv.h" |
| 20 | +#include "google/cloud/internal/random.h" |
| 21 | +#include "google/cloud/testing_util/example_driver.h" |
| 22 | +#include <sstream> |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +using google::cloud::pubsub::examples::Cleanup; |
| 27 | +using SubscriptionAdminCommand = |
| 28 | + std::function<void(google::cloud::pubsub_admin::SubscriptionAdminClient, |
| 29 | + std::vector<std::string> const&)>; |
| 30 | + |
| 31 | +google::cloud::testing_util::Commands::value_type |
| 32 | +CreateSubscriptionAdminCommand(std::string const& name, |
| 33 | + std::vector<std::string> const& arg_names, |
| 34 | + SubscriptionAdminCommand const& command) { |
| 35 | + auto adapter = [=](std::vector<std::string> const& argv) { |
| 36 | + if ((argv.size() == 1 && argv[0] == "--help") || |
| 37 | + argv.size() != arg_names.size()) { |
| 38 | + std::ostringstream os; |
| 39 | + os << name; |
| 40 | + for (auto const& a : arg_names) { |
| 41 | + os << " <" << a << ">"; |
| 42 | + } |
| 43 | + throw google::cloud::testing_util::Usage{std::move(os).str()}; |
| 44 | + } |
| 45 | + google::cloud::pubsub_admin::SubscriptionAdminClient client( |
| 46 | + google::cloud::pubsub_admin::MakeSubscriptionAdminConnection()); |
| 47 | + command(std::move(client), std::move(argv)); |
| 48 | + }; |
| 49 | + return google::cloud::testing_util::Commands::value_type{name, |
| 50 | + std::move(adapter)}; |
| 51 | +} |
| 52 | + |
| 53 | +void CreateSubscription( |
| 54 | + google::cloud::pubsub_admin::SubscriptionAdminClient client, |
| 55 | + std::vector<std::string> const& argv) { |
| 56 | + namespace pubsub_admin = ::google::cloud::pubsub_admin; |
| 57 | + namespace pubsub = ::google::cloud::pubsub; |
| 58 | + [](pubsub_admin::SubscriptionAdminClient client, |
| 59 | + std::string const& project_id, std::string const& topic_id, |
| 60 | + std::string const& subscription_id) { |
| 61 | + google::pubsub::v1::Subscription request; |
| 62 | + request.set_name( |
| 63 | + pubsub::Subscription(project_id, subscription_id).FullName()); |
| 64 | + request.set_topic(pubsub::Topic(project_id, topic_id).FullName()); |
| 65 | + auto sub = client.CreateSubscription(request); |
| 66 | + if (sub.status().code() == google::cloud::StatusCode::kAlreadyExists) { |
| 67 | + std::cout << "The subscription already exists\n"; |
| 68 | + return; |
| 69 | + } |
| 70 | + if (!sub) throw std::move(sub).status(); |
| 71 | + |
| 72 | + std::cout << "The subscription was successfully created: " |
| 73 | + << sub->DebugString() << "\n"; |
| 74 | + }(std::move(client), argv.at(0), argv.at(1), argv.at(2)); |
| 75 | +} |
| 76 | + |
| 77 | +void DeleteSubscription( |
| 78 | + google::cloud::pubsub_admin::SubscriptionAdminClient client, |
| 79 | + std::vector<std::string> const& argv) { |
| 80 | + namespace pubsub_admin = ::google::cloud::pubsub_admin; |
| 81 | + namespace pubsub = ::google::cloud::pubsub; |
| 82 | + [](pubsub_admin::SubscriptionAdminClient client, |
| 83 | + std::string const& project_id, std::string const& subscription_id) { |
| 84 | + auto status = client.DeleteSubscription( |
| 85 | + pubsub::Subscription(project_id, subscription_id).FullName()); |
| 86 | + // Note that kNotFound is a possible result when the library retries. |
| 87 | + if (status.code() == google::cloud::StatusCode::kNotFound) { |
| 88 | + std::cout << "The subscription was not found\n"; |
| 89 | + return; |
| 90 | + } |
| 91 | + if (!status.ok()) throw std::runtime_error(status.message()); |
| 92 | + |
| 93 | + std::cout << "The subscription was successfully deleted\n"; |
| 94 | + }(std::move(client), argv.at(0), argv.at(1)); |
| 95 | +} |
| 96 | + |
| 97 | +void AutoRun(std::vector<std::string> const& argv) { |
| 98 | + namespace examples = ::google::cloud::testing_util; |
| 99 | + using ::google::cloud::pubsub::examples::RandomSubscriptionId; |
| 100 | + using ::google::cloud::pubsub::examples::RandomTopicId; |
| 101 | + |
| 102 | + if (!argv.empty()) throw examples::Usage{"auto"}; |
| 103 | + examples::CheckEnvironmentVariablesAreSet({"GOOGLE_CLOUD_PROJECT"}); |
| 104 | + auto project_id = |
| 105 | + google::cloud::internal::GetEnv("GOOGLE_CLOUD_PROJECT").value(); |
| 106 | + |
| 107 | + auto generator = google::cloud::internal::MakeDefaultPRNG(); |
| 108 | + auto const topic_id = RandomTopicId(generator); |
| 109 | + auto const topic = google::cloud::pubsub::Topic(project_id, topic_id); |
| 110 | + auto const subscription_id = RandomSubscriptionId(generator); |
| 111 | + auto const subscription = |
| 112 | + google::cloud::pubsub::Subscription(project_id, subscription_id); |
| 113 | + |
| 114 | + auto topic_admin_client = google::cloud::pubsub_admin::TopicAdminClient( |
| 115 | + google::cloud::pubsub_admin::MakeTopicAdminConnection()); |
| 116 | + google::cloud::pubsub_admin::SubscriptionAdminClient |
| 117 | + subscription_admin_client( |
| 118 | + google::cloud::pubsub_admin::MakeSubscriptionAdminConnection()); |
| 119 | + |
| 120 | + std::cout << "\nCreate topic (" << topic_id << ")" << std::endl; |
| 121 | + topic_admin_client.CreateTopic(topic.FullName()); |
| 122 | + Cleanup cleanup; |
| 123 | + cleanup.Defer([topic_admin_client, topic]() mutable { |
| 124 | + (void)topic_admin_client.DeleteTopic(topic.FullName()); |
| 125 | + }); |
| 126 | + |
| 127 | + std::cout << "\nRunning CreateSubscription() sample" << std::endl; |
| 128 | + CreateSubscription(subscription_admin_client, |
| 129 | + {project_id, topic_id, subscription_id}); |
| 130 | + |
| 131 | + cleanup.Defer( |
| 132 | + [subscription_admin_client, project_id, subscription_id]() mutable { |
| 133 | + std::cout << "\nRunning DeleteSubscription() sample" << std::endl; |
| 134 | + DeleteSubscription(subscription_admin_client, |
| 135 | + {project_id, subscription_id}); |
| 136 | + }); |
| 137 | + |
| 138 | + std::cout << "\nAutoRun done" << std::endl; |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace |
| 142 | + |
| 143 | +int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape) |
| 144 | + using ::google::cloud::testing_util::Example; |
| 145 | + |
| 146 | + Example example({ |
| 147 | + CreateSubscriptionAdminCommand( |
| 148 | + "create-subscription", {"project-id", "topic-id", "subscription-id"}, |
| 149 | + CreateSubscription), |
| 150 | + CreateSubscriptionAdminCommand("delete-subscription", |
| 151 | + {"project-id", "subscription-id"}, |
| 152 | + DeleteSubscription), |
| 153 | + {"auto", AutoRun}, |
| 154 | + }); |
| 155 | + |
| 156 | + return example.Run(argc, argv); |
| 157 | +} |
0 commit comments