Skip to content

Commit 6dd962f

Browse files
authored
impl(universe_domain): add demos for additional services (#15640)
1 parent 390380a commit 6dd962f

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

google/cloud/universe_domain/demo/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ licenses(["notice"]) # Apache 2.0
1616

1717
service_demos = [
1818
"bigquery",
19+
"bigtable",
1920
"compute",
2021
"kms",
2122
"pubsub",
23+
"spanner",
2224
"storage",
2325
]
2426

google/cloud/universe_domain/demo/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ else ()
2929
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
3030
endif ()
3131

32-
set(universe_domain_demos bigquery compute kms pubsub storage)
32+
set(universe_domain_demos
33+
bigquery
34+
bigtable
35+
compute
36+
kms
37+
pubsub
38+
spanner
39+
storage)
3340
foreach (library ${universe_domain_demos})
3441
add_executable(${library}_demo ${library}.cc)
3542
target_link_libraries(${library}_demo google-cloud-cpp::${library}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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/spanner/admin/instance_admin_client.h"
17+
#include "google/cloud/spanner/admin/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::spanner_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::InstanceAdminRetryPolicyOption>(
50+
std::make_shared<admin::InstanceAdminLimitedErrorCountRetryPolicy>(3));
51+
52+
auto client = admin::InstanceAdminClient(
53+
admin::MakeInstanceAdminConnection(*ud_options));
54+
55+
std::cout << "spanner.ListInstances:\n";
56+
for (auto const& i : client.ListInstances(location.project().FullName())) {
57+
if (!i) throw std::move(i).status();
58+
std::string name = i->name();
59+
std::cout << "short_instance_name: " << name.substr(name.rfind('/') + 1)
60+
<< "\n";
61+
std::cout << i->create_time().DebugString() << "\n";
62+
}
63+
64+
return 0;
65+
} catch (google::cloud::Status const& status) {
66+
std::cerr << "google::cloud::Status thrown: " << status << "\n";
67+
return 1;
68+
}
69+
//! [all]

0 commit comments

Comments
 (0)