-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolusvm-integration.cpp
More file actions
125 lines (98 loc) · 3.95 KB
/
solusvm-integration.cpp
File metadata and controls
125 lines (98 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <string>
#include <sstream>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
class SolusVM {
public:
SolusVM(std::string api_url, std::string api_key) :
api_url(api_url), api_key(api_key), api_hash(md5(api_key + std::to_string(time(nullptr))))
{}
std::string createServer(std::string plan, std::string hostname, std::string password, std::string username, std::string os, std::string template) {
std::string api_action = "vserver-create";
std::stringstream api_params;
api_params << "plan=" << plan << "&hostname=" << hostname << "&password=" << password << "&username=" << username << "&os=" << os << "&template=" << template;
json response = makeApiRequest(api_action, api_params.str());
if (response["status"] == "success") {
return response["vserverid"].get<std::string>();
} else {
throw std::runtime_error(response["statusmsg"].get<std::string>());
}
}
json getServerInfo(std::string vserverid) {
std::string api_action = "vserver-info";
std::stringstream api_params;
api_params << "vserverid=" << vserverid;
json response = makeApiRequest(api_action, api_params.str());
if (response["status"] == "success") {
return response;
} else {
throw std::runtime_error(response["statusmsg"].get<std::string>());
}
}
bool rebootServer(std::string vserverid) {
std::string api_action = "vserver-reboot";
std::stringstream api_params;
api_params << "vserverid=" << vserverid;
json response = makeApiRequest(api_action, api_params.str());
if (response["status"] == "success") {
return true;
} else {
throw std::runtime_error(response["statusmsg"].get<std::string>());
}
}
bool suspendServer(std::string vserverid) {
std::string api_action = "vserver-suspend";
std::stringstream api_params;
api_params << "vserverid=" << vserverid;
json response = makeApiRequest(api_action, api_params.str());
if (response["status"] == "success") {
return true;
} else {
throw std::runtime_error(response["statusmsg"].get<std::string>());
}
}
bool unsuspendServer(std::string vserverid) {
std::string api_action = "vserver-unsuspend";
std::stringstream api_params;
api_params << "vserverid=" << vserverid;
json response = makeApiRequest(api_action, api_params.str());
if (response["status"] == "success") {
return true;
} else {
throw std::runtime_error(response["statusmsg"].get<std::string>());
}
}
private:
std::string api_url;
std::string api_key;
std::string api_hash;
json makeApiRequest(std::string api_action, std::string api_params) {
CURL *curl;
CURLcode res;
std::string request_url = api_url + "?key=" + api_key + "&hash=" + api_hash + "&action=" + api_action + "&" + api_params;
std::string response;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, request_url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
throw std::runtime_error(curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
json result = json::parse(response);
return result;
} else {
throw std::runtime_error("Failed to initialize CURL");
}
}
};
static size_t writeCallback(char* ptr, size_t size, size_t nmemb, std::string* data) {
data->append(ptr, size * nmemb);
return size * nmemb;
}
static std::string md5(std::string input) {
// Implementation of MD5 hash function
}