Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2aa358f

Browse filesBrowse files
authored
samples(storagecontrol): add samples for anywhere cache (googleapis#15134)
* samples(storagecontrol): add samples for anywhere cache * skip anywhere cache samples tests * fix typo * avoid copies
1 parent 4055e08 commit 2aa358f
Copy full SHA for 2aa358f

File tree

Expand file treeCollapse file tree

2 files changed

+235
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+235
-0
lines changed

‎ci/cloudbuild/builds/integration-production.sh

Copy file name to clipboardExpand all lines: ci/cloudbuild/builds/integration-production.sh
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ excluded_rules=(
3535
# This sample uses HMAC keys, which are very limited in production (at most
3636
# 5 per service account). Disabled for now.
3737
"-//google/cloud/storage/examples:storage_service_account_samples"
38+
# This sample can be very long running due to creation time of AnywhereCache
39+
"-//google/cloud/storagecontrol:v2_samples_storage_control_anywhere_cache_samples"
3840
)
3941

4042
io::log_h2 "Running the integration tests against prod"
+233Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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+
#include "google/cloud/storagecontrol/v2/storage_control_client.h"
16+
#include "google/cloud/internal/getenv.h"
17+
#include "google/cloud/testing_util/example_driver.h"
18+
#include <google/storage/control/v2/storage_control.pb.h>
19+
#include <string>
20+
#include <utility>
21+
#include <vector>
22+
23+
namespace {
24+
25+
void CreateAnywhereCache(
26+
google::cloud::storagecontrol_v2::StorageControlClient client,
27+
std::vector<std::string> const& argv) {
28+
// [START storage_control_create_anywhere_cache]
29+
namespace storagecontrol = google::cloud::storagecontrol_v2;
30+
[](storagecontrol::StorageControlClient client,
31+
std::string const& bucket_name, std::string const& cache_name,
32+
std::string const& zone_name) {
33+
google::storage::control::v2::AnywhereCache cache;
34+
cache.set_name(cache_name);
35+
cache.set_zone(zone_name);
36+
37+
google::storage::control::v2::CreateAnywhereCacheRequest request;
38+
request.set_parent(std::string{"projects/_/buckets/"} + bucket_name);
39+
*request.mutable_anywhere_cache() = cache;
40+
41+
// Start a create operation and block until it completes. Real applications
42+
// may want to setup a callback, wait on a coroutine, or poll until it
43+
// completes.
44+
auto anywhere_cache = client.CreateAnywhereCache(request).get();
45+
if (!anywhere_cache) throw std::move(anywhere_cache).status();
46+
std::cout << "Created anywhere cache: " << anywhere_cache->name() << "\n";
47+
}
48+
// [END storage_control_create_anywhere_cache]
49+
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
50+
}
51+
52+
void GetAnywhereCache(
53+
google::cloud::storagecontrol_v2::StorageControlClient client,
54+
std::vector<std::string> const& argv) {
55+
// [START storage_control_get_anywhere_cache]
56+
namespace storagecontrol = google::cloud::storagecontrol_v2;
57+
[](storagecontrol::StorageControlClient client,
58+
std::string const& cache_name) {
59+
auto anywhere_cache = client.GetAnywhereCache(cache_name);
60+
if (!anywhere_cache) throw std::move(anywhere_cache).status();
61+
std::cout << "Got anywhere cache: " << anywhere_cache->name() << "\n";
62+
}
63+
// [END storage_control_get_anywhere_cache]
64+
(std::move(client), argv.at(0));
65+
}
66+
67+
void ListAnywhereCaches(
68+
google::cloud::storagecontrol_v2::StorageControlClient client,
69+
std::vector<std::string> const& argv) {
70+
// [START storage_control_list_anywhere_caches]
71+
namespace storagecontrol = google::cloud::storagecontrol_v2;
72+
[](storagecontrol::StorageControlClient client,
73+
std::string const& bucket_name) {
74+
auto const parent = std::string{"projects/_/buckets/"} + bucket_name;
75+
for (auto anywhere_cache : client.ListAnywhereCaches(parent)) {
76+
if (!anywhere_cache) throw std::move(anywhere_cache).status();
77+
std::cout << anywhere_cache->name() << "\n";
78+
}
79+
}
80+
// [END storage_control_list_anywhere_caches]
81+
(std::move(client), argv.at(0));
82+
}
83+
84+
void UpdateAnywhereCache(
85+
google::cloud::storagecontrol_v2::StorageControlClient client,
86+
std::vector<std::string> const& argv) {
87+
// [START storage_control_update_anywhere_cache]
88+
namespace storagecontrol = google::cloud::storagecontrol_v2;
89+
[](storagecontrol::StorageControlClient client, std::string const& cache_name,
90+
std::string const& admission_policy) {
91+
google::storage::control::v2::AnywhereCache cache;
92+
google::protobuf::FieldMask field_mask;
93+
field_mask.add_paths("admission_policy");
94+
cache.set_name(cache_name);
95+
cache.set_admission_policy(admission_policy);
96+
// Start an update operation and block until it completes. Real applications
97+
// may want to setup a callback, wait on a coroutine, or poll until it
98+
// completes.
99+
auto anywhere_cache = client.UpdateAnywhereCache(cache, field_mask).get();
100+
if (!anywhere_cache) throw std::move(anywhere_cache).status();
101+
std::cout << "Updated anywhere cache: " << anywhere_cache->name() << "\n";
102+
}
103+
// [END storage_control_update_anywhere_cache]
104+
(std::move(client), argv.at(0), argv.at(1));
105+
}
106+
107+
void PauseAnywhereCache(
108+
google::cloud::storagecontrol_v2::StorageControlClient client,
109+
std::vector<std::string> const& argv) {
110+
// [START storage_control_pause_anywhere_cache]
111+
namespace storagecontrol = google::cloud::storagecontrol_v2;
112+
[](storagecontrol::StorageControlClient client,
113+
std::string const& cache_name) {
114+
auto anywhere_cache = client.PauseAnywhereCache(cache_name);
115+
if (!anywhere_cache) throw std::move(anywhere_cache).status();
116+
std::cout << "Paused anywhere cache: " << anywhere_cache->name() << "\n";
117+
}
118+
// [END storage_control_pause_anywhere_cache]
119+
(std::move(client), argv.at(0));
120+
}
121+
122+
void ResumeAnywhereCache(
123+
google::cloud::storagecontrol_v2::StorageControlClient client,
124+
std::vector<std::string> const& argv) {
125+
// [START storage_control_resume_anywhere_cache]
126+
namespace storagecontrol = google::cloud::storagecontrol_v2;
127+
[](storagecontrol::StorageControlClient client,
128+
std::string const& cache_name) {
129+
auto anywhere_cache = client.ResumeAnywhereCache(cache_name);
130+
if (!anywhere_cache) throw std::move(anywhere_cache).status();
131+
std::cout << "Resumed anywhere cache: " << anywhere_cache->name() << "\n";
132+
}
133+
// [END storage_control_resume_anywhere_cache]
134+
(std::move(client), argv.at(0));
135+
}
136+
137+
void DisableAnywhereCache(
138+
google::cloud::storagecontrol_v2::StorageControlClient client,
139+
std::vector<std::string> const& argv) {
140+
// [START storage_control_disable_anywhere_cache]
141+
namespace storagecontrol = google::cloud::storagecontrol_v2;
142+
[](storagecontrol::StorageControlClient client,
143+
std::string const& cache_name) {
144+
auto anywhere_cache = client.DisableAnywhereCache(cache_name);
145+
if (!anywhere_cache) throw std::move(anywhere_cache).status();
146+
std::cout << "Disabled anywhere cache: " << anywhere_cache->name() << "\n";
147+
}
148+
// [END storage_control_disable_anywhere_cache]
149+
(std::move(client), argv.at(0));
150+
}
151+
152+
void AutoRun(std::vector<std::string> const& argv) {
153+
namespace examples = google::cloud::testing_util;
154+
namespace storagecontrol = google::cloud::storagecontrol_v2;
155+
if (!argv.empty()) throw examples::Usage{"auto"};
156+
examples::CheckEnvironmentVariablesAreSet(
157+
{"GOOGLE_CLOUD_CPP_STORAGE_TEST_BUCKET_NAME",
158+
"GOOGLE_CLOUD_CPP_TEST_ZONE"});
159+
auto const bucket_name = google::cloud::internal::GetEnv(
160+
"GOOGLE_CLOUD_CPP_STORAGE_TEST_BUCKET_NAME")
161+
.value();
162+
auto const zone_name =
163+
google::cloud::internal::GetEnv("GOOGLE_CLOUD_CPP_TEST_ZONE").value();
164+
auto client = storagecontrol::StorageControlClient(
165+
storagecontrol::MakeStorageControlConnection());
166+
167+
auto const cache_name =
168+
"projects/_/buckets/" + bucket_name + "/anywhereCaches/" + zone_name;
169+
170+
std::cout << "\nRunning CreateAnywhereCache() example" << std::endl;
171+
CreateAnywhereCache(client, {bucket_name, cache_name, zone_name});
172+
173+
std::cout << "\nRunning GetAnywhereCache() example" << std::endl;
174+
GetAnywhereCache(client, {cache_name});
175+
176+
std::cout << "\nRunning ListAnywhereCaches() example" << std::endl;
177+
ListAnywhereCaches(client, {bucket_name});
178+
179+
std::cout << "\nRunning UpdateAnywhereCache() example" << std::endl;
180+
UpdateAnywhereCache(client, {cache_name, "admit-on-second-miss"});
181+
182+
std::cout << "\nRunning PauseAnywhereCache() example" << std::endl;
183+
PauseAnywhereCache(client, {cache_name});
184+
185+
std::cout << "\nRunning ResumeAnywhereCache() example" << std::endl;
186+
ResumeAnywhereCache(client, {cache_name});
187+
188+
std::cout << "\nRunning DisableAnywhereCache() example" << std::endl;
189+
DisableAnywhereCache(client, {cache_name});
190+
}
191+
192+
} // namespace
193+
194+
int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
195+
using google::cloud::testing_util::Example;
196+
namespace storagecontrol = google::cloud::storagecontrol_v2;
197+
using ClientCommand = std::function<void(storagecontrol::StorageControlClient,
198+
std::vector<std::string> argv)>;
199+
200+
auto make_entry = [](std::string name,
201+
std::vector<std::string> const& arg_names,
202+
ClientCommand const& command) {
203+
auto adapter = [=](std::vector<std::string> argv) {
204+
if ((argv.size() == 1 && argv[0] == "--help") ||
205+
argv.size() != arg_names.size()) {
206+
std::string usage = name;
207+
for (auto const& a : arg_names) usage += " <" + a + ">";
208+
throw google::cloud::testing_util::Usage{std::move(usage)};
209+
}
210+
auto client = storagecontrol::StorageControlClient(
211+
storagecontrol::MakeStorageControlConnection());
212+
command(client, std::move(argv));
213+
};
214+
return google::cloud::testing_util::Commands::value_type(std::move(name),
215+
adapter);
216+
};
217+
218+
Example example({
219+
make_entry("create-anywhere-cache",
220+
{"bucket-name", "cache-name", "zone-name"},
221+
CreateAnywhereCache),
222+
make_entry("get-anywhere-cache", {"cache-name"}, GetAnywhereCache),
223+
make_entry("list-anywhere-caches", {"bucket-name"}, ListAnywhereCaches),
224+
make_entry("update-anywhere-cache", {"cache-name", "admission-policy"},
225+
UpdateAnywhereCache),
226+
make_entry("pause-anywhere-cache", {"cache-name"}, PauseAnywhereCache),
227+
make_entry("resume-anywhere-cache", {"cache-name"}, ResumeAnywhereCache),
228+
make_entry("disable-anywhere-cache", {"cache-name"},
229+
DisableAnywhereCache),
230+
{"auto", AutoRun},
231+
});
232+
return example.Run(argc, argv);
233+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.