MXS-2481 Add MaxRest functionality

- show_server
- call_command
This commit is contained in:
Johan Wikman
2019-05-23 11:05:34 +03:00
parent c35d92eccc
commit c0a0bb86a0
2 changed files with 145 additions and 11 deletions

View File

@ -29,14 +29,56 @@ MaxRest::MaxRest(TestConnections* pTest)
{
}
unique_ptr<json_t> MaxRest::servers() const
unique_ptr<json_t> MaxRest::v1_servers(const string& id) const
{
return curl("servers");
string path("servers");
path += "/";
path += id;
return curl_get(path);
}
unique_ptr<json_t> 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<string>& 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<json_t> sObject = v1_servers(id);
json_t* pData = get_object(sObject.get(), "data", Presence::MANDATORY);
return Server(*this, pData);
}
vector<MaxRest::Server> MaxRest::list_servers() const
{
return get_array<Server>(servers().get(), "data", Presence::MANDATORY);
return get_array<Server>(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<json_t> MaxRest::parse(const string& json) const
return sRoot;
}
unique_ptr<json_t> MaxRest::curl(const string& path) const
unique_ptr<json_t> MaxRest::curl_get(const string& path) const
{
return curl(GET, path);
}
unique_ptr<json_t> MaxRest::curl_post(const string& path) const
{
return curl(POST, path);
}
unique_ptr<json_t> 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<json_t> sRv;
if (!result.second.empty())
{
sRv = parse(result.second);
}
return sRv;
}
void MaxRest::raise(const std::string& message) const

View File

@ -57,18 +57,59 @@ public:
return m_test;
}
/**
* @return The JSON object corresponding to /v1/servers/:id:
*/
std::unique_ptr<json_t> v1_servers(const std::string& id) const;
/**
* @return The JSON object corresponding to /v1/servers.
*/
std::unique_ptr<json_t> servers() const;
std::unique_ptr<json_t> v1_servers() const;
/**
* POST request to /v1/maxscale/modules/:module:/:command:?instance[&param...]
*
* @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<std::string>& params = std::vector<std::string>()) 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<std::string>& params = std::vector<std::string>()) 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<Server> 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<json_t> 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<json_t> curl(const std::string& path) const;
std::unique_ptr<json_t> 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<json_t> curl_post(const std::string& path) const;
void raise(const std::string& message) const;
private:
enum Command
{
GET,
POST
};
std::unique_ptr<json_t> curl(Command command, const std::string& path) const;
private:
TestConnections& m_test;
};