Skip to content
This repository has been archived by the owner on Jun 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #41 from MetisMachine/jt-create-project-org
Browse files Browse the repository at this point in the history
Create a project under an organization
  • Loading branch information
Caroline Twigg authored Aug 29, 2018
2 parents 903860d + 1ac69a9 commit ca552c5
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 27 deletions.
20 changes: 13 additions & 7 deletions src/dispatch/dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ struct FunctionCaller {
};

struct command setup_cmd = {"setup", {}, false, true};
struct command init_cmd = {"init", {"--template", "--master"}, true, true};
struct command init_cmd = {"init", {"--org", "--template", "--master"}, true, true};
struct command auth_cmd = {"auth", {}, false, false};
struct command templates_cmd = {"templates", {"--update", "--search"}, true, true};
struct command env_cmd = {"env", {"--set"}, true, true};
struct command create_cmd = {"create", {"--project"}, true, true};
struct command logs_cmd = {"logs", {"-n", "--tail"}, true, true};
struct command fetch_cmd = {"fetch", {"--table"}, true, true};
struct command kill_deployment_cmd = {"kill", {"--deployments", "--job_ids"}, true, true};
struct command remote_cmd = {"remote", {"--default"}, true, true};
struct command organizations_cmd = {"orgs", {}, true, true};
struct command remote_cmd = {"remote", {}, true, true};
struct command organizations_cmd = {"orgs", {"--set-default"}, true, true};

struct command command_list[11] = {
setup_cmd,
Expand Down Expand Up @@ -122,6 +122,7 @@ void init(int argc, char **argv, int cmd_index) {
map<string, int> init_flags = find_flags(argc, argv, cmd_index);
string name = ".";
string tpl = "base";
string org_name = "";
bool master = false;

if(argc > 2) {
Expand All @@ -130,6 +131,11 @@ void init(int argc, char **argv, int cmd_index) {
}
}

if(init_flags.find("--org")->second != -1) {
int idx = init_flags.find("--org")->second;
org_name = argv[idx + 1];
}

if(init_flags.find("--template")->second != -1) {
int tpl_index = init_flags.find("--template")->second;

Expand All @@ -142,7 +148,7 @@ void init(int argc, char **argv, int cmd_index) {
master = true;
}

Project::init(name, tpl, master);
Project::init(name, org_name, tpl, master);
}

void auth() {
Expand Down Expand Up @@ -357,15 +363,15 @@ void remote(int argc, char **argv, int cmd_index){
void organizations(int argc, char **argv, int cmd_index) {
switch(argc) {
case 4:
if (string(argv[3]) == "--default") {
if (string(argv[3]) == "--set-default") {
std::string name = std::string(argv[2]);
Organization::set_default(name);
} else {
console::error("You must supply an organization name and the --default argument to switch default organizations.");
console::error("You must supply an organization name and the --set-default argument to switch default organizations.");
}
break;
case 3:
console::error("You must supply an organization name and the --default argument to switch default organizations.");
console::error("You must supply an organization name and the --set-default argument to switch default organizations.");
break;
case 2:
console::info("All your organizations:\n");
Expand Down
18 changes: 18 additions & 0 deletions src/env/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ bool Env::load_credentials() {
return false;
}

bool Env::load_defaults() {
if(FileManager::file_exists(paths.defaults)) {
string defaults = FileManager::read(paths.defaults);
string err;
Json json = Json::parse(defaults, err);

if(err.length() > 0) {
console::error("Unable to load or read defaults: " + err);
return false;
}

string org_name = json["org_name"].string_value();
this->set(METIS_DEFAULT_ORG, org_name);
return true;
}
return false;
}

void Env::write_credentials(Json object) {
FileManager::create_path(0755, paths.home, paths.env);

Expand Down
2 changes: 2 additions & 0 deletions src/env/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

const std::string METIS_API_TOKEN = "METIS_API_TOKEN";
const std::string METIS_AUTH_TOKEN = "METIS_OAUTH";
const std::string METIS_DEFAULT_ORG = "METIS_DEFAULT_ORG";
const std::string METIS_CREDENTIALS = "credentials.json";
const std::string METIS_DEFAULTS = "defaults.json";
const std::string METIS_TEMPLATE_DIR = "templates";
Expand All @@ -37,6 +38,7 @@ class Env {
void set(std::string key, std::string val);
void setup();
bool load_credentials();
bool load_defaults();
void write_credentials(json11::Json object);
void write_default_org(std::string org_name);
void verify_auth();
Expand Down
53 changes: 38 additions & 15 deletions src/project/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ using namespace json11;
FileManager::resolve_path(name) : \
FileManager::cwd() + "/" + name

void Project::init(string name, string tpl, bool master) {
void Project::init(string name, string org_name, string tpl, bool master) {
string directory = project_directory(name);
Env::instance()->load_defaults();
string default_org_name = Env::instance()->get(METIS_DEFAULT_ORG);

if(org_name.size() == 0) {
org_name = default_org_name;
}

string project_name = directory.substr(directory.find_last_of("/"));

Expand All @@ -30,9 +36,9 @@ void Project::init(string name, string tpl, bool master) {
exit(EXIT_FAILURE);
}

existing_init(name, master);
existing_init(name, org_name, master);
} else {
template_init(name, tpl, master);
template_init(name, org_name, tpl, master);
}
}

Expand All @@ -58,7 +64,7 @@ void Project::create_job(string name, string project_token){
}
}

void Project::template_init(string name, string tpl, bool master) {
void Project::template_init(string name, string org_name, string tpl, bool master) {
Template::update();

string directory = project_directory(name);
Expand Down Expand Up @@ -99,21 +105,31 @@ void Project::template_init(string name, string tpl, bool master) {

string err;
string proj = replace(project_name, "/", "");
Json json = Json::parse(Request::create_project(proj).body, err);
string token = json["token"].string_value();
string job_id = json["jobs"][0]["id"].string_value();
string job_name = json["jobs"][0]["name"].string_value();
Json json;
if (org_name.size() == 0) {
json = Json::parse(Request::create_project(proj).body, err);
} else {
console::info("Creating project " + project_name + " under organization " + org_name);
json = Json::parse(Request::create_project(proj, org_name).body, err);
}

config_template.setValue("token", token);
config_template.setValue("name", proj);
config_template.setValue("job_id", job_id);
config_template.setValue("job_name", job_name);
if(json["error"].string_value().size() > 0) {
console::error("Unable to create your project: " + json["error"].string_value());
} else {
string token = json["token"].string_value();
string job_id = json["jobs"][0]["id"].string_value();
string job_name = json["jobs"][0]["name"].string_value();

FileManager::write(template_path, config_template.render());
config_template.setValue("token", token);
config_template.setValue("name", proj);
config_template.setValue("job_id", job_id);
config_template.setValue("job_name", job_name);

FileManager::write(template_path, config_template.render());
}
}

void Project::existing_init(string name, bool master) {
void Project::existing_init(string name, string org_name, bool master) {
const string source = "project_token: {{token}}\n"
"name: {{name}}\n"
"jobs:\n"
Expand All @@ -127,7 +143,14 @@ void Project::existing_init(string name, bool master) {
string config_path = directory + "/metis.config.yml";
string project_name = directory.substr(directory.find_last_of("/"));
string proj = replace(project_name, "/", "");
Json json = Json::parse(Request::create_project(proj).body, err);
Json json;
if (org_name.size() == 0) {
json = Json::parse(Request::create_project(proj).body, err);
} else {
console::info("Creating project " + project_name + " under organization " + org_name);
json = Json::parse(Request::create_project(proj, org_name).body, err);
}

string token = json["token"].string_value();
string job_id = json["jobs"][0]["id"].string_value();
string job_name = json["jobs"][0]["name"].string_value();
Expand Down
6 changes: 3 additions & 3 deletions src/project/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Project {
public:
static void init(std::string name, std::string tpl, bool master);
static void init(std::string name, std::string org_name, std::string tpl, bool master);
static void create_job(std::string name, std::string project_token);
static void kill(std::string project_token);
static void kill(std::string jobs, std::string deployments);
Expand All @@ -18,8 +18,8 @@ class Project {
private:
static bool exists(std::string directory);

static void template_init(std::string name, std::string tpl, bool master);
static void existing_init(std::string name, bool master);
static void template_init(std::string name, std::string org_name, std::string tpl, bool master);
static void existing_init(std::string name, std::string org_name, bool master);
};

#endif
16 changes: 16 additions & 0 deletions src/request/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ RestClient::Response Request::_create_project(string name) {
return this->connection->post(PROJECT_URL, body.dump());
}

RestClient::Response Request::_create_project(string name, string org_name) {
API_HEADERS();

Json body = Json::object{
{"name", name}
};

auto uri = ORGANIZATIONS_URL + "/" + org_name + "/projects";
console::debug("Organizations url: " + uri);
return this->connection->post(uri, body.dump());
}

RestClient::Response Request::_create_job(string name, string project_token="") {
API_HEADERS();

Expand Down Expand Up @@ -392,6 +404,10 @@ RestClient::Response Request::create_project(string name) {
return instance()->_create_project(name);
}

RestClient::Response Request::create_project(string name, string org_name) {
return instance()->_create_project(name, org_name);
}

RestClient::Response Request::create_job(string name, string project_token) {
return instance()->_create_job(name, project_token);
}
Expand Down
2 changes: 2 additions & 0 deletions src/request/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Request {
static RestClient::Response tokens();
static RestClient::Response generate_token();
static RestClient::Response create_project(std::string name);
static RestClient::Response create_project(std::string name, std::string org_name);
static RestClient::Response create_job(std::string name, std::string project_token);
static RestClient::Response env_vars(std::string project_id);
static RestClient::Response env_var(std::string project_id, std::string key);
Expand Down Expand Up @@ -49,6 +50,7 @@ class Request {
RestClient::Response _tokens();
RestClient::Response _generate_token();
RestClient::Response _create_project(std::string name);
RestClient::Response _create_project(std::string name, std::string org_name);
RestClient::Response _create_job(std::string name, std::string project_token);
RestClient::Response _env_vars(std::string project_id);
RestClient::Response _env_var(std::string project_id, std::string key);
Expand Down
5 changes: 3 additions & 2 deletions src/usage.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ static const char USAGE[] =
R"(
Usage:
skafos (setup|init|auth)...
skafos init [<name>] [--template <template_name>] [--master]
skafos init [<name>] [--org <org_name>] [--template <template_name>] [--master]
skafos templates [--update] [--search <search_term>]
skafos env [<key>] [--set <value>] [--delete]
skafos create [<kind>] [<name>] [--project <token>]
skafos logs [<project_token>] [-n <num>] [--tail]
skafos fetch --table <table_name>
skafos kill [<project_token>] [--deployments <deployment_ids>] [--job_ids <job_ids>]
skafos remote info [<project_token>]
skafos orgs [<name>] [--default]
skafos orgs [<name>] [--set-default]
skafos -h | --help
skafos -v | --version
Commands:
Expand All @@ -27,6 +27,7 @@ R"(
fetch Fetch results from a given table.
kill Kill an entire project or specific jobs/deployments.
remote info Print command to add a new remote.
orgs List or set your default organization.
Options:
-v --version Shows version.

Expand Down

0 comments on commit ca552c5

Please sign in to comment.