#pragma once // Long-running operations (google.longrunning) and pagination, shared by // every module that creates, deletes or lists Cloud resources. #include #include #include #include #include "google/client.h" namespace molo::google { struct Operation { std::string name; // projects/.../operations/{id} bool done = false; clients::body::Json response; // Set when done and successful. clients::body::Json metadata; int error_code = 0; // google.rpc.Status, 0: no error std::string error_message; bool failed() const { return error_code != 0; } static Operation from_json(const clients::body::Json& doc); }; struct OperationCaps { static constexpr std::chrono::seconds kMaxWait{600}; static constexpr std::chrono::milliseconds kInterval{2000}; static constexpr std::size_t kMaxPolls = 400; }; using Sleeper = std::function; // Polls GET {name} on the given endpoint until done. Throws GoogleError // (status DEADLINE_EXCEEDED) at the cap and (status from the operation, or // UNKNOWN) when the operation failed. `sleeper` is injectable for tests. Operation wait_operation(GoogleClient& client, const ServiceEndpoint& endpoint, const std::string& operation_name, const Sleeper& sleeper = nullptr); // Follows nextPageToken. `path` may already hold a query string. Collects // `items_key` arrays into one Json array; stops at max_pages (default 100). clients::body::Json list_all(GoogleClient& client, const ServiceEndpoint& endpoint, const std::string& path, const std::string& items_key, std::size_t max_pages = 100); } // namespace molo::google