forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_samples.cc
More file actions
209 lines (188 loc) · 8.47 KB
/
Copy pathclient_samples.cc
File metadata and controls
209 lines (188 loc) · 8.47 KB
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
// Copyright 2022 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.
#include "google/cloud/bigtable/admin/bigtable_instance_admin_client.h"
#include "google/cloud/bigtable/admin/bigtable_table_admin_client.h"
#include "google/cloud/bigtable/table.h"
#include "google/cloud/bigtable/testing/random_names.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/internal/random.h"
#include "google/cloud/testing_util/example_driver.h"
#include "google/cloud/universe_domain.h"
#include <fstream>
#include <string>
#include <vector>
namespace {
void TableSetEndpoint(std::vector<std::string> const& argv) {
namespace examples = ::google::cloud::testing_util;
if (argv.size() != 3) {
throw examples::Usage{
"table-set-endpoint <project-id> <instance-id> <table-id>"};
}
//! [table-set-endpoint]
namespace bigtable = ::google::cloud::bigtable;
[](std::string const& project_id, std::string const& instance_id,
std::string const& table_id) {
auto options = google::cloud::Options{}.set<google::cloud::EndpointOption>(
"private.googleapis.com");
auto resource = bigtable::TableResource(project_id, instance_id, table_id);
return bigtable::Table(
bigtable::MakeDataConnection(
{bigtable::InstanceResource(google::cloud::Project(project_id),
instance_id)},
std::move(options)),
resource);
}
//! [table-set-endpoint]
(argv.at(0), argv.at(1), argv.at(2));
}
void SetRetryPolicy(std::vector<std::string> const& argv) {
if (argv.size() != 3) {
throw google::cloud::testing_util::Usage{
"set-retry-policy <project-id> <instance-id> <table-id>"};
}
//! [set-retry-policy]
namespace cbt = google::cloud::bigtable;
[](std::string const& project_id, std::string const& instance_id,
std::string const& table_id) {
auto options = google::cloud::Options{}
.set<cbt::IdempotentMutationPolicyOption>(
cbt::AlwaysRetryMutationPolicy().clone())
.set<cbt::DataRetryPolicyOption>(
cbt::DataLimitedErrorCountRetryPolicy(3).clone())
.set<cbt::DataBackoffPolicyOption>(
google::cloud::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::milliseconds(200),
/*maximum_delay=*/std::chrono::seconds(45),
/*scaling=*/2.0)
.clone());
auto connection = cbt::MakeDataConnection(
{cbt::InstanceResource(google::cloud::Project(project_id),
instance_id)},
std::move(options));
auto const table_name =
cbt::TableResource(project_id, instance_id, table_id);
// c1 and c2 share the same retry policies
auto c1 = cbt::Table(connection, table_name);
auto c2 = cbt::Table(connection, table_name);
// You can override any of the policies in a new client. This new client
// will share the policies from c1 (or c2) *except* from the retry policy.
auto c3 = cbt::Table(
connection, table_name,
google::cloud::Options{}.set<cbt::DataRetryPolicyOption>(
cbt::DataLimitedTimeRetryPolicy(std::chrono::minutes(5)).clone()));
// You can also override the policies in a single call. In this case, we
// allow no retries.
auto result =
c3.ReadRow("my-key", cbt::Filter::PassAllFilter(),
google::cloud::Options{}.set<cbt::DataRetryPolicyOption>(
cbt::DataLimitedErrorCountRetryPolicy(0).clone()));
(void)result; // ignore errors in this example
}
//! [set-retry-policy]
(argv.at(0), argv.at(1), argv.at(2));
}
void TableWithServiceAccount(std::vector<std::string> const& argv) {
namespace examples = ::google::cloud::testing_util;
if (argv.size() != 4) {
throw examples::Usage{
"table-with-service-account <project-id> <instance-id> <table-id> "
"<keyfile>"};
}
//! [table-with-service-account]
namespace bigtable = ::google::cloud::bigtable;
[](std::string const& project_id, std::string const& instance_id,
std::string const& table_id, std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit);
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
auto resource = bigtable::TableResource(project_id, instance_id, table_id);
return bigtable::Table(
bigtable::MakeDataConnection(
{bigtable::InstanceResource(google::cloud::Project(project_id),
instance_id)},
std::move(options)),
resource);
}
//! [table-with-service-account]
(argv.at(0), argv.at(1), argv.at(2), argv.at(3));
}
void TableSetUniverseDomain(std::vector<std::string> const& argv) {
if (argv.size() != 3) {
throw google::cloud::testing_util::Usage{
"table-set-endpoint <project-id> <instance-id> <table-id>"};
}
//! [table-set-universe-domain]
namespace bigtable = ::google::cloud::bigtable;
[](std::string const& project_id, std::string const& instance_id,
std::string const& table_id) {
google::cloud::Options options;
// AddUniverseDomainOption interrogates the UnifiedCredentialsOption, if
// set, in the provided Options for the Universe Domain associated with the
// credentials and adds it to the set of Options.
// If no UnifiedCredentialsOption is set, GoogleDefaultCredentials are used.
auto ud_options =
google::cloud::AddUniverseDomainOption(std::move(options));
if (!ud_options.ok()) throw std::move(ud_options).status();
auto resource = bigtable::TableResource(project_id, instance_id, table_id);
return bigtable::Table(
bigtable::MakeDataConnection(
{bigtable::InstanceResource(google::cloud::Project(project_id),
instance_id)},
*std::move(ud_options)),
resource);
}
//! [table-set-universe-domain]
(argv.at(0), argv.at(1), argv.at(2));
}
void AutoRun(std::vector<std::string> const& argv) {
using ::google::cloud::internal::GetEnv;
namespace examples = ::google::cloud::testing_util;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet({
"GOOGLE_CLOUD_PROJECT",
"GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE",
});
auto const project_id = GetEnv("GOOGLE_CLOUD_PROJECT").value();
auto const keyfile =
GetEnv("GOOGLE_CLOUD_CPP_TEST_SERVICE_ACCOUNT_KEYFILE").value();
auto generator = google::cloud::internal::DefaultPRNG(std::random_device{}());
auto const instance_id =
google::cloud::bigtable::testing::RandomInstanceId(generator);
auto const table_id =
google::cloud::bigtable::testing::RandomTableId(generator);
std::cout << "\nRunning TableSetEndpoint() sample" << std::endl;
TableSetEndpoint({project_id, instance_id, table_id});
std::cout << "\nRunning TableWithServiceAccount() sample" << std::endl;
TableWithServiceAccount({project_id, instance_id, table_id, keyfile});
std::cout << "\nRunning SetRetryPolicy() sample" << std::endl;
SetRetryPolicy({project_id, instance_id, table_id});
std::cout << "\nRunning TableSetUniverseDomain() sample" << std::endl;
TableSetUniverseDomain({project_id, instance_id, table_id});
std::cout << "\nAutoRun done" << std::endl;
}
} // namespace
int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example({
{"table-set-endpoint", TableSetEndpoint},
{"set-retry-policy", SetRetryPolicy},
{"table-with-service-account", TableWithServiceAccount},
{"table-set-universe-domain", TableSetUniverseDomain},
{"auto", AutoRun},
});
return example.Run(argc, argv);
}