|
| 1 | +// Copyright 2025 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 | +//! [all] |
| 16 | +#include "google/cloud/bigtable/admin/bigtable_instance_admin_client.h" |
| 17 | +#include "google/cloud/bigtable/admin/bigtable_instance_admin_options.h" |
| 18 | +#include "google/cloud/location.h" |
| 19 | +#include "google/cloud/universe_domain.h" |
| 20 | +#include "google/cloud/universe_domain_options.h" |
| 21 | +#include <fstream> |
| 22 | +#include <iostream> |
| 23 | + |
| 24 | +int main(int argc, char* argv[]) try { |
| 25 | + if (argc != 3 && argc != 4) { |
| 26 | + std::cerr << "Usage: " << argv[0] |
| 27 | + << " project-id location-id [sa-key-file]\n"; |
| 28 | + return 1; |
| 29 | + } |
| 30 | + namespace gc = ::google::cloud; |
| 31 | + namespace admin = ::google::cloud::bigtable_admin; |
| 32 | + auto const location = gc::Location(argv[1], argv[2]); |
| 33 | + |
| 34 | + gc::Options options; |
| 35 | + if (argc == 4) { |
| 36 | + auto is = std::ifstream(argv[3]); |
| 37 | + is.exceptions(std::ios::badbit); |
| 38 | + auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {}); |
| 39 | + options.set<google::cloud::UnifiedCredentialsOption>( |
| 40 | + google::cloud::MakeServiceAccountCredentials(contents)); |
| 41 | + } |
| 42 | + |
| 43 | + // Interrogate credentials for universe_domain and add the value to returned |
| 44 | + // options. |
| 45 | + auto ud_options = gc::AddUniverseDomainOption(gc::ExperimentalTag{}, options); |
| 46 | + if (!ud_options.ok()) throw std::move(ud_options).status(); |
| 47 | + |
| 48 | + // Override retry policy to quickly exit if there's a failure. |
| 49 | + ud_options->set<admin::BigtableInstanceAdminRetryPolicyOption>( |
| 50 | + std::make_shared< |
| 51 | + admin::BigtableInstanceAdminLimitedErrorCountRetryPolicy>(3)); |
| 52 | + |
| 53 | + auto client = admin::BigtableInstanceAdminClient( |
| 54 | + admin::MakeBigtableInstanceAdminConnection(*ud_options)); |
| 55 | + |
| 56 | + std::cout << "bigtable.ListInstances:\n"; |
| 57 | + auto response = client.ListInstances(location.project().FullName()); |
| 58 | + if (!response) throw std::move(response).status(); |
| 59 | + for (auto const& i : response->instances()) { |
| 60 | + std::string name = i.name(); |
| 61 | + std::cout << "short_instance_name: " << name.substr(name.rfind('/') + 1) |
| 62 | + << "\n"; |
| 63 | + std::cout << i.create_time().DebugString() << "\n"; |
| 64 | + } |
| 65 | + |
| 66 | + return 0; |
| 67 | +} catch (google::cloud::Status const& status) { |
| 68 | + std::cerr << "google::cloud::Status thrown: " << status << "\n"; |
| 69 | + return 1; |
| 70 | +} |
| 71 | +//! [all] |
0 commit comments