diff --git a/maxscale-system-test/maxrest.cc b/maxscale-system-test/maxrest.cc index 9db0cac9d..20be858c2 100644 --- a/maxscale-system-test/maxrest.cc +++ b/maxscale-system-test/maxrest.cc @@ -29,14 +29,56 @@ MaxRest::MaxRest(TestConnections* pTest) { } -unique_ptr MaxRest::servers() const +unique_ptr MaxRest::v1_servers(const string& id) const { - return curl("servers"); + string path("servers"); + path += "/"; + path += id; + + return curl_get(path); +} + +unique_ptr MaxRest::v1_servers() const +{ + return curl_get("servers"); +} + +void MaxRest::v1_maxscale_modules(const string& module, + const string& command, + const string& instance, + const std::vector& params) const +{ + string path("maxscale/modules"); + + path += "/"; + path += module; + path += "/"; + path += command; + path += "?"; + path += instance; + + if (!params.empty()) + { + for (const auto& param : params) + { + path += "\\&"; + path += param; + } + } + + curl_post(path); +} + +MaxRest::Server MaxRest::show_server(const std::string& id) const +{ + unique_ptr sObject = v1_servers(id); + json_t* pData = get_object(sObject.get(), "data", Presence::MANDATORY); + return Server(*this, pData); } vector MaxRest::list_servers() const { - return get_array(servers().get(), "data", Presence::MANDATORY); + return get_array(v1_servers().get(), "data", Presence::MANDATORY); } json_t* MaxRest::get_object(json_t* pObject, const string& key, Presence presence) const @@ -84,19 +126,49 @@ unique_ptr MaxRest::parse(const string& json) const return sRoot; } -unique_ptr MaxRest::curl(const string& path) const +unique_ptr MaxRest::curl_get(const string& path) const +{ + return curl(GET, path); +} + +unique_ptr MaxRest::curl_post(const string& path) const +{ + return curl(POST, path); +} + +unique_ptr MaxRest::curl(Command command, const string& path) const { string url = "http://127.0.0.1:8989/v1/" + path; - string command = "curl -u admin:mariadb " + url; + string curl_command = "curl -u admin:mariadb "; - auto result = m_test.maxscales->ssh_output(command.c_str(), 0, false); + switch (command) + { + case GET: + curl_command += "-X GET "; + break; + + case POST: + curl_command += "-X POST "; + break; + } + + curl_command += url; + + auto result = m_test.maxscales->ssh_output(curl_command.c_str(), 0, false); if (result.first != 0) { raise("Invocation of curl failed: " + to_string(result.first)); } - return parse(result.second); + unique_ptr sRv; + + if (!result.second.empty()) + { + sRv = parse(result.second); + } + + return sRv; } void MaxRest::raise(const std::string& message) const diff --git a/maxscale-system-test/maxrest.hh b/maxscale-system-test/maxrest.hh index 0c3304a9b..308560993 100644 --- a/maxscale-system-test/maxrest.hh +++ b/maxscale-system-test/maxrest.hh @@ -57,18 +57,59 @@ public: return m_test; } + /** + * @return The JSON object corresponding to /v1/servers/:id: + */ + std::unique_ptr v1_servers(const std::string& id) const; + /** * @return The JSON object corresponding to /v1/servers. */ - std::unique_ptr servers() const; + std::unique_ptr v1_servers() const; + + /** + * POST request to /v1/maxscale/modules/:module:/:command:?instance[¶m...] + * + * @param module Module name. + * @param command The command. + * @param instance The object instance to execute it on. + * @param params Optional arguments. + */ + void v1_maxscale_modules(const std::string& module, + const std::string& command, + const std::string& instance, + const std::vector& params = std::vector()) const; + + /** + * Call a module command. + * + * @param module Module name. + * @param command The command. + * @param instance The object instance to execute it on. + * @param params Optional arguments. + */ + void call_command(const std::string& module, + const std::string& command, + const std::string& instance, + const std::vector& params = std::vector()) const + { + return v1_maxscale_modules(module, command, instance, params); + } /** * The equivalent of 'maxctrl list servers' * - * @return The JSON resrouce /v1/servers as a vector of Server objects. + * @return The JSON resource /v1/servers as a vector of Server objects. */ std::vector list_servers() const; + /** + * The equivalent of 'maxctrl show server' + * + * @return The JSON resource /v1/servers/:id: as a Server object. + */ + Server show_server(const std::string& id) const; + enum class Presence { OPTIONAL, @@ -158,7 +199,7 @@ public: std::unique_ptr parse(const std::string& json) const; /** - * Issue a curl request to the REST-API endpoint of the MaxScale running on + * Issue a curl GET to the REST-API endpoint of the MaxScale running on * the maxscale 0 VM instance. * * The path will be appended to "http://127.0.0.1:8989/v1/". @@ -167,10 +208,31 @@ public: * * @return The corresponding json_t object. */ - std::unique_ptr curl(const std::string& path) const; + std::unique_ptr curl_get(const std::string& path) const; + + /** + * Issue a curl POST to the REST-API endpoint of the MaxScale running on + * the maxscale 0 VM instance. + * + * The path will be appended to "http://127.0.0.1:8989/v1/". + * + * @param path The path of the resource. + * + * @return The corresponding json_t object. + */ + std::unique_ptr curl_post(const std::string& path) const; void raise(const std::string& message) const; +private: + enum Command + { + GET, + POST + }; + + std::unique_ptr curl(Command command, const std::string& path) const; + private: TestConnections& m_test; };