Commit e1a16dd7 by Fukász Rómeó Ervin

made setup.py pass flake8

seperated the header only lib to delaration and implementation
parent d29bcc7f
// Copyright 2017 Budapest University of Technology and Economics (BME IK)
//
// This file is part of CIRCLE Cloud.
//
// CIRCLE is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
#include <string>
#include "json.hpp"
#include "resource.h"
#include "template.h"
#include "compute.h"
using std::string;
using nlohmann::json;
using namespace OcciClient;
void Compute::parseJsonInstance(json instance) {
Resource::parseJsonInstance(instance);
this->state = getAttributeFromRendering<string>(instance, "occi.compute.state");
this->stateMessage = getAttributeFromRendering<string>(instance, "occi.compute.state.message");
this->memory = getAttributeFromRendering<double>(instance, "occi.compute.memory");
this->cpuShare = getAttributeFromRendering<int>(instance, "occi.compute.share");
this->cores = getAttributeFromRendering<int>(instance, "occi.compute.cores");
this->hostname = getAttributeFromRendering<string>(instance, "occi.compute.hostname");
this->architecture = getAttributeFromRendering<string>(instance, "occi.compute.architecture");
this->credentialProtocol = getAttributeFromRendering<string>(instance, "org.circlecloud.occi.credentials.protocol");
this->credentialPort = getAttributeFromRendering<int>(instance, "org.circlecloud.occi.credentials.port");
this->credentialHost = getAttributeFromRendering<string>(instance, "org.circlecloud.occi.credentials.host");
this->credentialUsername = getAttributeFromRendering<string>(instance, "org.circlecloud.occi.credentials.username");
this->credentialPassword = getAttributeFromRendering<string>(instance, "org.circlecloud.occi.credentials.password");
this->credentialCommand = getAttributeFromRendering<string>(instance, "org.circlecloud.occi.credentials.command");
this->rendering = instance;
}
Compute::Compute(OcciSession* session, json instance) : Resource(session, "compute", instance) {
parseJsonInstance(instance);
}
void Compute::invokeAction(json actionData) {
auto inst = Resource::invokeAction(actionData);
parseJsonInstance(inst);
}
public:
static shared_ptr<Compute> Compute::getComputeInstance(OcciSession* session, string id) {
auto uri = "compute/" + id + "/";
auto instance = session->get(uri);
return make_shared<Compute>(Compute(session, instance));
}
static shared_ptr<Compute> Compute::createComputeInstance(OcciSession* session, Template t) {
auto uri = "compute/1/";
json data = json::parse("{\"mixins\": [\"" + t + "\"]}");
auto instance = session->put(uri, data);
return make_shared<Compute>(Compute(session, instance));
}
string Compute::getState(){
return this->state;
}
string Compute::getStateMessage(){
return this->stateMessage;
}
double Compute::getMemory(){
return this->memory;
}
int Compute::getCpuShare(){
return this->cpuShare;
}
int Compute::getCores(){
return this->cores;
}
string Compute::getHostname(){
return this->hostname;
}
string Compute::getArchitecture(){
return this->architecture;
}
string Compute::getCredentialProtocol(){
return this->credentialProtocol;
}
int Compute::getCredentialPort(){
return this->credentialPort;
}
string Compute::getCredentialHost(){
return this->credentialHost;
}
string Compute::getCredentialPassword(){
return this->credentialPassword;
}
string Compute::getCredentialUsername(){
return this->credentialUsername;
}
string Compute::getCredentialCommand(){
return this->credentialCommand;
}
void Compute::start() {
json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#start"}
};
invokeAction(actionData);
}
void Compute::wakeup() {
start();
}
void Compute::sleep() {
json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#suspend"},
{"attributes", {
{"method", "suspend"}
}}
};
invokeAction(actionData);
}
void Compute::reboot() {
json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#restart"},
{"attributes", {
{"method", "warm"}
}}
};
invokeAction(actionData);
}
void Compute::reset() {
json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#restart"},
{"attributes", {
{"method", "cold"}
}}
};
invokeAction(actionData);
}
void Compute::shutdown() {
json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"},
{"attributes", {
{"method", "acpioff"}
}}
};
invokeAction(actionData);
}
void Compute::shutoff() {
json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"},
{"attributes", {
{"method", "poweroff"}
}}
};
invokeAction(actionData);
}
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
namespace OcciClient { namespace OcciClient {
class Compute : public Resource { class Compute : public Resource {
private: private:
std::string state; std::string state;
...@@ -44,161 +45,57 @@ namespace OcciClient { ...@@ -44,161 +45,57 @@ namespace OcciClient {
std::string credentialUsername; std::string credentialUsername;
std::string credentialCommand; std::string credentialCommand;
void parseJsonInstance(nlohmann::json instance) { void parseJsonInstance(nlohmann::json instance);
Resource::parseJsonInstance(instance);
this->state = getAttributeFromRendering<std::string>(instance, "occi.compute.state"); Compute(OcciSession* session, nlohmann::json instance);
this->stateMessage = getAttributeFromRendering<std::string>(instance, "occi.compute.state.message");
this->memory = getAttributeFromRendering<double>(instance, "occi.compute.memory"); void invokeAction(nlohmann::json actionData);
this->cpuShare = getAttributeFromRendering<int>(instance, "occi.compute.share");
this->cores = getAttributeFromRendering<int>(instance, "occi.compute.cores");
this->hostname = getAttributeFromRendering<std::string>(instance, "occi.compute.hostname");
this->architecture = getAttributeFromRendering<std::string>(instance, "occi.compute.architecture");
this->credentialProtocol = getAttributeFromRendering<std::string>(instance, "org.circlecloud.occi.credentials.protocol");
this->credentialPort = getAttributeFromRendering<int>(instance, "org.circlecloud.occi.credentials.port");
this->credentialHost = getAttributeFromRendering<std::string>(instance, "org.circlecloud.occi.credentials.host");
this->credentialUsername = getAttributeFromRendering<std::string>(instance, "org.circlecloud.occi.credentials.username");
this->credentialPassword = getAttributeFromRendering<std::string>(instance, "org.circlecloud.occi.credentials.password");
this->credentialCommand = getAttributeFromRendering<std::string>(instance, "org.circlecloud.occi.credentials.command");
this->rendering = instance;
}
Compute(OcciSession* session, nlohmann::json instance)
: Resource(session, "compute", instance) {
parseJsonInstance(instance);
}
void invokeAction(nlohmann::json actionData) {
auto inst = Resource::invokeAction(actionData);
parseJsonInstance(inst);
}
public: public:
static std::shared_ptr<Compute> getComputeInstance(OcciSession* session, std::string id) { static std::shared_ptr<Compute> getComputeInstance(OcciSession* session, std::string id);
auto uri = "compute/" + id + "/";
auto instance = session->get(uri); static std::shared_ptr<Compute> createComputeInstance(OcciSession* session, Template t);
return std::make_shared<Compute>(Compute(session, instance));
} std::string getState();
static std::shared_ptr<Compute> createComputeInstance(OcciSession* session, Template t) { std::string getStateMessage();
auto uri = "compute/1/";
nlohmann::json data = nlohmann::json::parse("{\"mixins\": [\"" + t + "\"]}"); double getMemory();
auto instance = session->put(uri, data);
return std::make_shared<Compute>(Compute(session, instance)); int getCpuShare();
}
int getCores();
std::string getState(){
return this->state; std::string getHostname();
}
std::string getArchitecture();
std::string getStateMessage(){
return this->stateMessage; std::string getCredentialProtocol();
}
int getCredentialPort();
double getMemory(){
return this->memory; std::string getCredentialHost();
}
std::string getCredentialPassword();
int getCpuShare(){
return this->cpuShare; std::string getCredentialUsername();
}
std::string getCredentialCommand();
int getCores(){
return this->cores; void start();
}
void wakeup();
std::string getHostname(){
return this->hostname; void sleep();
}
void reboot();
std::string getArchitecture(){
return this->architecture; void reset();
}
void shutdown();
std::string getCredentialProtocol(){
return this->credentialProtocol; void shutoff();
}
int getCredentialPort(){
return this->credentialPort;
}
std::string getCredentialHost(){
return this->credentialHost;
}
std::string getCredentialPassword(){
return this->credentialPassword;
}
std::string getCredentialUsername(){
return this->credentialUsername;
}
std::string getCredentialCommand(){
return this->credentialCommand;
}
void start() {
nlohmann::json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#start"}
};
invokeAction(actionData);
}
void wakeup() {
start();
}
void sleep() {
nlohmann::json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#suspend"},
{"attributes", {
{"method", "suspend"}
}}
};
invokeAction(actionData);
}
void reboot() {
nlohmann::json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#restart"},
{"attributes", {
{"method", "warm"}
}}
};
invokeAction(actionData);
}
void reset() {
nlohmann::json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#restart"},
{"attributes", {
{"method", "cold"}
}}
};
invokeAction(actionData);
}
void shutdown() {
nlohmann::json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"},
{"attributes", {
{"method", "acpioff"}
}}
};
invokeAction(actionData);
}
void shutoff() {
nlohmann::json actionData = {
{"action", "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"},
{"attributes", {
{"method", "poweroff"}
}}
};
invokeAction(actionData);
}
}; };
} }
......
// Copyright 2017 Budapest University of Technology and Economics (BME IK)
//
// This file is part of CIRCLE Cloud.
//
// CIRCLE is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
#include <string>
#include "json.hpp"
#include "resource.h"
#include "network.h"
using std::string;
using nlohmann::json;
using namespace OcciClient;
void Network::parseJsonInstance(json instance) {
Resource::parseJsonInstance(instance);
this->state = getAttributeFromRendering<string>(instance, "occi.network.state");
this->stateMessage = getAttributeFromRendering<string>(instance, "occi.network.state.message");
this->vlan = getAttributeFromRendering<int>(instance, "occi.network.vlan");
this->address = getAttributeFromRendering<string>(instance, "occi.network.address");
this->gateway = getAttributeFromRendering<string>(instance, "occi.network.gateway");
this->allocation = getAttributeFromRendering<string>(instance, "occi.network.allocation");
this->rendering = instance;
}
Network::Network(OcciSession* session, json instance)
: Resource(session, "network", instance) {
parseJsonInstance(instance);
}
void Network::invokeAction(json actionData) {
auto inst = Resource::invokeAction(actionData);
parseJsonInstance(inst);
}
shared_ptr<Network> Network::getNetworkInstance(OcciSession* session, string id) {
auto uri = "network/" + id + "/";
auto instance = session->get(uri);
return make_shared<Network>(Network(session, instance));
}
string Network::getState(){
return this->state;
}
string Network::getStateMessage(){
return this->stateMessage;
}
int Network::getVlan(){
return this->vlan;
}
string Network::getAddress(){
return this->address;
}
string Network::getGateway(){
return this->gateway;
}
string Network::getAllocation(){
return this->allocation;
}
...@@ -36,58 +36,27 @@ namespace OcciClient { ...@@ -36,58 +36,27 @@ namespace OcciClient {
std::string gateway; std::string gateway;
std::string allocation; std::string allocation;
void parseJsonInstance(nlohmann::json instance) { void parseJsonInstance(nlohmann::json instance);
Resource::parseJsonInstance(instance);
this->state = getAttributeFromRendering<std::string>(instance, "occi.network.state"); Network(OcciSession* session, nlohmann::json instance);
this->stateMessage = getAttributeFromRendering<std::string>(instance, "occi.network.state.message");
this->vlan = getAttributeFromRendering<int>(instance, "occi.network.vlan"); void invokeAction(nlohmann::json actionData);
this->address = getAttributeFromRendering<std::string>(instance, "occi.network.address");
this->gateway = getAttributeFromRendering<std::string>(instance, "occi.network.gateway");
this->allocation = getAttributeFromRendering<std::string>(instance, "occi.network.allocation");
this->rendering = instance;
}
Network(OcciSession* session, nlohmann::json instance)
: Resource(session, "network", instance) {
parseJsonInstance(instance);
}
void invokeAction(nlohmann::json actionData) {
auto inst = Resource::invokeAction(actionData);
parseJsonInstance(inst);
}
public: public:
static std::shared_ptr<Network> getNetworkInstance(OcciSession* session, std::string id) { static std::shared_ptr<Network> getNetworkInstance(OcciSession* session, std::string id);
auto uri = "network/" + id + "/";
auto instance = session->get(uri);
return std::make_shared<Network>(Network(session, instance));
}
std::string getState(){ std::string getState();
return this->state;
}
std::string getStateMessage(){ std::string getStateMessage();
return this->stateMessage;
}
int getVlan(){ int getVlan();
return this->vlan;
}
std::string getAddress(){ std::string getAddress();
return this->address;
}
std::string getGateway(){ std::string getGateway();
return this->gateway;
}
std::string getAllocation(){ std::string getAllocation();
return this->allocation;
}
}; };
} }
......
#include "occilib.h"
// Copyright 2017 Budapest University of Technology and Economics (BME IK)
//
// This file is part of CIRCLE Cloud.
//
// CIRCLE is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
#include <string>
#include <exception>
#include <stdexcept>
#include "restclient-cpp/connection.h"
#include "restclient-cpp/restclient.h"
#include "json.hpp"
#include "occisession.h"
using std::string;
using nlohmann::json;
using namespace OcciClient;
void OcciSession::setCsrfTokenHeader(){
auto resp = this->connection->getLastResponse();
string csrftoken;
try {
csrftoken = resp.cookies.at("csrftoken");
}
catch (const std::out_of_range& e) {
return;
}
this->connection->AppendHeader("X-CSRFToken", csrftoken);
}
OcciSession::OcciSession(const char* url, bool insecure, bool csrf){
RestClient::init();
this->connection = new RestClient::Connection(url);
if (insecure) {
this->connection->SetHostVerify(false);
this->connection->SetPeerVerify(false);
}
this->csrftokenRequired = csrf;
RestClient::HeaderFields headers;
headers["Accept"] = "application/json";
headers["Content-type"] = "application/json";
headers["Referer"] = url;
this->connection->SetHeaders(headers);
}
OcciSession::~OcciSession(){
RestClient::disable();
}
json OcciSession::doRequest(string uri, RequestType type, json body){
RestClient::Response r;
if (this->csrftokenRequired)
this->setCsrfTokenHeader();
if (type == RequestType::Post)
r = connection->post(uri, body.dump());
else if (type == RequestType::Put)
r = connection->put(uri, body.dump());
else if (type == RequestType::Delete)
r = connection->del(uri);
else
r = connection->get(uri);
json result;
try {
result = json::parse(r.body);
}
catch (std::invalid_argument& e) {
result = "{}"_json;
throw std::domain_error("Didn't get a json response from the OCCI server.");
}
try {
throw std::logic_error(result["error"].get<string>());
}
catch (std::domain_error e) {
return result;
}
}
json OcciSession::get(string uri) {
return doRequest(uri, RequestType::Get);
}
json OcciSession::post(string uri, json body) {
return doRequest(uri, RequestType::Post, body);
}
json OcciSession::put(string uri, json body) {
return doRequest(uri, RequestType::Put, body);
}
json OcciSession::del(string uri) {
return doRequest(uri, RequestType::Delete);
}
void OcciSession::circleOcciLogin(string username, string password){
get("login/");
string body = "{\"username\": \"" + username + "\", \"password\": \"" + password + "\"}";
post("login/", json::parse(body));
}
json OcciSession::queryInterface(){
return get("-/");
}
...@@ -20,119 +20,46 @@ ...@@ -20,119 +20,46 @@
#define OCCILIB_SESSION_H_ #define OCCILIB_SESSION_H_
#include <string> #include <string>
#include <exception>
#include <stdexcept>
#include "restclient-cpp/connection.h" #include "restclient-cpp/connection.h"
#include "restclient-cpp/restclient.h" #include "restclient-cpp/restclient.h"
#include "json.hpp" #include "json.hpp"
namespace OcciClient { namespace OcciClient {
using std::string;
enum class RequestType{Get, Post, Delete, Put}; enum class RequestType{Get, Post, Delete, Put};
class OcciSession { class OcciSession {
private: private:
RestClient::Connection* connection = nullptr; RestClient::Connection* connection = nullptr;
bool csrftokenRequired = false; bool csrftokenRequired = false;
string csrfuri; std::string csrfuri;
void setCsrfTokenHeader(){
auto resp = this->connection->getLastResponse();
string csrftoken;
try {
csrftoken = resp.cookies.at("csrftoken");
}
catch (const std::out_of_range& e) {
return;
}
this->connection->AppendHeader("X-CSRFToken", csrftoken);
}
void setCsrfTokenHeader();
public: public:
OcciSession(const char* url, bool insecure = false, bool csrf = false){ OcciSession(const char* url, bool insecure = false, bool csrf = false);
RestClient::init();
this->connection = new RestClient::Connection(url);
if (insecure) {
this->connection->SetHostVerify(false);
this->connection->SetPeerVerify(false);
}
this->csrftokenRequired = csrf;
RestClient::HeaderFields headers;
headers["Accept"] = "application/json";
headers["Content-type"] = "application/json";
headers["Referer"] = url;
this->connection->SetHeaders(headers);
}
~OcciSession(){
RestClient::disable();
}
nlohmann::json doRequest(string uri, RequestType type, nlohmann::json body = nullptr){
RestClient::Response r;
if (this->csrftokenRequired)
this->setCsrfTokenHeader();
if (type == RequestType::Post)
r = connection->post(uri, body.dump());
else if (type == RequestType::Put)
r = connection->put(uri, body.dump());
else if (type == RequestType::Delete)
r = connection->del(uri);
else
r = connection->get(uri);
nlohmann::json result;
try {
result = nlohmann::json::parse(r.body);
}
catch (std::invalid_argument& e) {
result = "{}"_json;
throw std::domain_error("Didn't get a nlohmann::json response from the OCCI server.");
}
try {
throw std::logic_error(result["error"].get<string>());
}
catch (std::domain_error e) {
return result;
}
}
nlohmann::json get(string uri) {
return doRequest(uri, RequestType::Get);
}
nlohmann::json post(string uri, nlohmann::json body = nullptr) {
return doRequest(uri, RequestType::Post, body);
}
nlohmann::json put(string uri, nlohmann::json body = nullptr) {
return doRequest(uri, RequestType::Put, body);
}
nlohmann::json del(string uri) {
return doRequest(uri, RequestType::Delete);
}
void circleOcciLogin(string username, string password){
get("login/");
string body = "{\"username\": \"" + username + "\", \"password\": \"" + password + "\"}";
post("login/", nlohmann::json::parse(body));
}
nlohmann::json queryInterface(){
return get("-/");
}
}; ~OcciSession();
nlohmann::json doRequest(std::string uri, RequestType type, nlohmann::json body = nullptr);
nlohmann::json get(std::string uri);
nlohmann::json post(std::string uri, nlohmann::json body = nullptr);
nlohmann::json put(std::string uri, nlohmann::json body = nullptr);
nlohmann::json del(std::string uri);
void circleOcciLogin(std::string username, std::string password);
nlohmann::json queryInterface();
};
} }
#endif // OCCILIB_SESSION_H_ #endif // OCCILIB_SESSION_H_
// Copyright 2017 Budapest University of Technology and Economics (BME IK)
//
// This file is part of CIRCLE Cloud.
//
// CIRCLE is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
#include <string>
#include "json.hpp"
#include "occisession.h"
#include "resource.h"
using std::string;
using nlohmann::json;
using namespace OcciClient;
void Resource::parseJsonInstance(json instance) {
this->id = getID(instance);
if (instance.find("title") != instance.end()) {
this->title = instance["title"].get<string>();
}
this->rendering = instance;
}
Resource::Resource(OcciSession* session, string kind, json instance) {
this->session = session;
this->kind = kind;
parseJsonInstance(instance);
}
string Resource::getID(json& rendering) {
try {
return rendering["id"];
}
catch (std::domain_error) {
return "";
}
}
template <typename T> T Resource::getAttributeFromRendering(json& rendering, const string which, const T defaultValue) {
try {
return rendering["attributes"][which].get<T>();
}
catch (std::exception e){
return defaultValue;
}
}
string Resource::getId(){
return this->id;
}
string Resource::getTitle(){
return this->title;
}
json Resource::invokeAction(OcciSession* session, string kind, string id, json actionData) {
auto uri = kind + "/" + id + "/";
return session->post(uri, actionData);
}
json Resource::invokeAction(json actionData){
return invokeAction(this->session, this->kind, this->id, actionData);
}
json Resource::getRendering(){
return this->rendering;
}
void Resource::destroyInstance() {
auto uri = this->kind + "/" + this->id + "/";
session->del(uri);
}
...@@ -37,68 +37,28 @@ namespace OcciClient{ ...@@ -37,68 +37,28 @@ namespace OcciClient{
nlohmann::json rendering; nlohmann::json rendering;
string kind; string kind;
void parseJsonInstance(nlohmann::json instance) { void parseJsonInstance(nlohmann::json instance);
this->id = getID(instance);
if (instance.find("title") != instance.end()) {
this->title = instance["title"].get<string>();
}
this->rendering = instance;
}
Resource(OcciSession* session, string kind, nlohmann::json instance) {
this->session = session;
this->kind = kind;
parseJsonInstance(instance);
}
string getID(nlohmann::json& rendering) {
try {
return rendering["id"];
}
catch (std::domain_error) {
return "";
}
}
template <typename T> T getAttributeFromRendering(nlohmann::json& rendering, const string which, const T defaultValue = T()) {
try {
return rendering["attributes"][which].get<T>();
}
catch (std::exception e){
return defaultValue;
}
}
public: Resource(OcciSession* session, string kind, nlohmann::json instance);
string getId(){ string getID(nlohmann::json& rendering);
return this->id;
}
string getTitle(){ template <typename T> T getAttributeFromRendering(nlohmann::json& rendering, const string which, const T defaultValue = T());
return this->title;
}
static nlohmann::json invokeAction(OcciSession* session, string kind, string id, nlohmann::json actionData) { public:
auto uri = kind + "/" + id + "/";
return session->post(uri, actionData);
}
nlohmann::json invokeAction(nlohmann::json actionData){ string getId();
return invokeAction(this->session, this->kind, this->id, actionData);
}
nlohmann::json getRendering(){ string getTitle();
return this->rendering;
}
void destroyInstance() { static nlohmann::json invokeAction(OcciSession* session, string kind, string id, nlohmann::json actionData);
auto uri = this->kind + "/" + this->id + "/";
session->del(uri);
}
}; nlohmann::json invokeAction(nlohmann::json actionData);
nlohmann::json getRendering();
void destroyInstance();
};
} }
#endif // OCCILIB_RESOURCE_H_ #endif // OCCILIB_RESOURCE_H_
...@@ -6,22 +6,23 @@ https://github.com/pypa/sampleproject ...@@ -6,22 +6,23 @@ https://github.com/pypa/sampleproject
""" """
# Always prefer setuptools over distutils # Always prefer setuptools over distutils
from setuptools import setup, find_packages, Extension from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext from setuptools.command.build_ext import build_ext
from subprocess import check_call from subprocess import check_call
import sys
_occi = Extension('_occi', _occi = Extension('_occi',
sources=['occilib.i'], sources=['occilib.i'],
include_dirs=['restclient-cpp/include'], include_dirs=['restclient-cpp/include'],
library_dirs=['.'], library_dirs=['.'],
swig_opts=['-c++'], swig_opts=['-c++'],
extra_compile_args=['-std=c++11', '-Ofast'], extra_compile_args=['-std=c++11', '-Ofast'],
libraries=['restclient-cpp', 'curl'] libraries=['restclient-cpp', 'curl']
) )
class Build(build_ext): class Build(build_ext):
def run(self): def run(self):
check_call(['make', 'deps']) check_call(['make', 'deps'])
build_ext.run(self) build_ext.run(self)
......
// Copyright 2017 Budapest University of Technology and Economics (BME IK)
//
// This file is part of CIRCLE Cloud.
//
// CIRCLE is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
#include <string>
#include "json.hpp"
#include "resource.h"
#include "storage.h"
using std::string;
using nlohmann::json;
using namespace OcciClient;
void Storage::parseJsonInstance(json instance) {
Resource::parseJsonInstance(instance);
this->state = getAttributeFromRendering<string>(instance, "occi.storage.state");
this->stateMessage = getAttributeFromRendering<string>(instance, "occi.storage.state.message");
this->size = getAttributeFromRendering<double>(instance, "occi.storage.size");
this->rendering = instance;
}
Storage::Storage(OcciSession* session, json instance)
: Resource(session, "storage", instance) {
parseJsonInstance(instance);
}
void Storage::invokeAction(json actionData) {
auto inst = Resource::invokeAction(actionData);
parseJsonInstance(inst);
}
shared_ptr<Storage> Storage::getStorageInstance(OcciSession* session, string id) {
auto uri = "storage/" + id + "/";
auto instance = session->get(uri);
return make_shared<Storage>(Storage(session, instance));
}
string Storage::getState(){
return this->state;
}
string Storage::getStateMessage(){
return this->stateMessage;
}
double Storage::getSize(){
return this->size;
}
...@@ -33,44 +33,21 @@ namespace OcciClient { ...@@ -33,44 +33,21 @@ namespace OcciClient {
std::string state; std::string state;
std::string stateMessage; std::string stateMessage;
void parseJsonInstance(nlohmann::json instance) { void parseJsonInstance(nlohmann::json instance);
Resource::parseJsonInstance(instance);
this->state = getAttributeFromRendering<std::string>(instance, "occi.storage.state");
this->stateMessage = getAttributeFromRendering<std::string>(instance, "occi.storage.state.message");
this->size = getAttributeFromRendering<double>(instance, "occi.storage.size");
this->rendering = instance;
}
Storage(OcciSession* session, nlohmann::json instance)
: Resource(session, "storage", instance) {
parseJsonInstance(instance);
}
void invokeAction(nlohmann::json actionData) {
auto inst = Resource::invokeAction(actionData);
parseJsonInstance(inst);
}
public: Storage(OcciSession* session, nlohmann::json instance);
void invokeAction(nlohmann::json actionData);
static std::shared_ptr<Storage> getStorageInstance(OcciSession* session, std::string id) { public:
auto uri = "storage/" + id + "/";
auto instance = session->get(uri);
return std::make_shared<Storage>(Storage(session, instance));
}
std::string getState(){ static std::shared_ptr<Storage> getStorageInstance(OcciSession* session, std::string id);
return this->state;
}
std::string getStateMessage(){ std::string getState();
return this->stateMessage;
}
double getSize(){ std::string getStateMessage();
return this->size;
}
double getSize();
}; };
} }
......
// Copyright 2017 Budapest University of Technology and Economics (BME IK)
//
// This file is part of CIRCLE Cloud.
//
// CIRCLE is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
#include <string>
#include <vector>
#include "occisession.h"
#include "json.hpp"
using nlohmann::json;
using std::string;
using std::vector;
using namespace OcciClient;
vector<Template> getTemplates(OcciSession* session){
json mixins = session->queryInterface()["mixins"];
auto result = std::vector<Template>();
for (auto& mixin : mixins) {
if (mixin["depends"] == "http://schemas.ogf.org/occi/infrastructure#os_tpl") {
result.push_back(mixin["scheme"].get<string>() + mixin["term"].get<string>());
}
}
return result;
}
...@@ -23,23 +23,13 @@ ...@@ -23,23 +23,13 @@
#include <vector> #include <vector>
#include "occisession.h" #include "occisession.h"
#include "json.hpp"
typedef std::string Template; typedef std::string Template;
namespace OcciClient{
std::vector<Template> getTemplates(OcciSession* session){ namespace OcciClient{
nlohmann::json mixins = session->queryInterface()["mixins"];
auto result = std::vector<Template>();
for (auto& mixin : mixins) {
if (mixin["depends"] == "http://schemas.ogf.org/occi/infrastructure#os_tpl") {
result.push_back(mixin["scheme"].get<string>() + mixin["term"].get<string>());
}
}
return result;
}
std::vector<Template> getTemplates(OcciSession* session);
} }
#endif // OCCILIB_TEMPLATE_H #endif // OCCILIB_TEMPLATE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment