Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ void RemoveStaleFolders(
google::cloud::storagecontrol_v2::StorageControlClient client,
std::string const& bucket_name, std::string const& prefix,
std::chrono::system_clock::time_point created_time_limit) {
std::regex re(prefix + R"re(-[a-z]{32})re");
auto const parent = std::string{"projects/_/buckets/"} + bucket_name;
std::regex re(parent + "/folders/" + prefix +
R"re(-(recursive-)?[a-z]{32})re");
for (auto folder : client.ListFolders(parent)) {
if (!folder) throw std::move(folder).status();
if (!std::regex_match(folder->name(), re)) continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, folder->name() returns the full resource name (e.g. projects/_/buckets//folders/<folder_id>). Because std::regex_match requires the full string to match the pattern, matching against prefix + ... will fail.

Here and below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Co-authored by AI Agent

auto const create_time =
google::cloud::internal::ToChronoTimePoint(folder->create_time());
if (create_time > created_time_limit) continue;
(void)client.DeleteFolder(folder->name());
(void)client.DeleteFolderRecursive(folder->name()).get();
}
}

Expand Down Expand Up @@ -133,6 +134,27 @@ void RenameFolder(google::cloud::storagecontrol_v2::StorageControlClient client,
(std::move(client), argv.at(0), argv.at(1), argv.at(2));
}

void DeleteFolderRecursive(
google::cloud::storagecontrol_v2::StorageControlClient client,
std::vector<std::string> const& argv) {
// [START storage_control_delete_folder_recursive]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this sample consistent with other snippets in this file:

  1. Use auto for the LRO future result.
  2. Use if (!result) instead of if (!status.ok()) to match how StatusOr is checked elsewhere in this file.
  3. Add an explanatory comment about .get() blocking on the LRO, matching RenameFolder.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Co-authored by AI Agent

namespace storagecontrol = google::cloud::storagecontrol_v2;
[](storagecontrol::StorageControlClient client,
std::string const& bucket_name, std::string const& folder_id) {
auto const name = std::string{"projects/_/buckets/"} + bucket_name +
"/folders/" + folder_id;
// Start a delete folder recursive operation and block until it completes.
// Real applications may want to setup a callback, wait on a coroutine, or
// poll until it completes.
auto result = client.DeleteFolderRecursive(name).get();
if (!result) throw std::move(result).status();

std::cout << "Deleted folder recursively: " << folder_id << "\n";
}
// [END storage_control_delete_folder_recursive]
(std::move(client), argv.at(0), argv.at(1));
}

void AutoRun(std::vector<std::string> const& argv) {
namespace examples = google::cloud::testing_util;
namespace storagecontrol = google::cloud::storagecontrol_v2;
Expand Down Expand Up @@ -176,6 +198,42 @@ void AutoRun(std::vector<std::string> const& argv) {

std::cout << "\nRunning DeleteFolder() example" << std::endl;
DeleteFolder(client, {bucket_name, dest_folder_id});

auto const recursive_parent_id =
prefix + "-recursive-" +
google::cloud::internal::Sample(generator, 32,
"abcdefghijklmnopqrstuvwxyz");
auto const recursive_child_id = recursive_parent_id + "/child";

std::cout << "\nRunning CreateFolder() for recursive test parent"
<< std::endl;
CreateFolder(client, {bucket_name, recursive_parent_id});

std::cout << "\nRunning CreateFolder() for recursive test child" << std::endl;
CreateFolder(client, {bucket_name, recursive_child_id});

std::cout << "\nRunning DeleteFolderRecursive() example" << std::endl;
DeleteFolderRecursive(client, {bucket_name, recursive_parent_id});

// Verify deletion by checking that getting the parent and child folders fail
// with NOT_FOUND.
try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's verify that recursive_child_id was also deleted and returns kNotFound.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Co-authored by AI Agent

GetFolder(client, {bucket_name, recursive_parent_id});
throw std::runtime_error("Parent folder was not deleted recursively");
} catch (google::cloud::Status const& status) {
if (status.code() != google::cloud::StatusCode::kNotFound) {
throw;
}
}

try {
GetFolder(client, {bucket_name, recursive_child_id});
throw std::runtime_error("Child folder was not deleted recursively");
} catch (google::cloud::Status const& status) {
if (status.code() != google::cloud::StatusCode::kNotFound) {
throw;
}
}
}

} // namespace
Expand Down Expand Up @@ -207,6 +265,8 @@ int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
Example example({
make_entry("create-folder", {"bucket-name", "folder-id"}, CreateFolder),
make_entry("delete-folder", {"bucket-name", "folder-id"}, DeleteFolder),
make_entry("delete-folder-recursive", {"bucket-name", "folder-id"},
DeleteFolderRecursive),
make_entry("get-folder", {"bucket-name", "folder-id"}, GetFolder),
make_entry("list-folders", {"bucket-name"}, ListFolders),
make_entry("rename-folder",
Expand Down
Loading