Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deploy service #7

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use reqwest::blocking::Response;

use self::types::{ListOrganizationResponse, Organization, CreateEnvironmentResponse, ListServicesResponse};
use self::types::*;

pub mod types;

Expand Down Expand Up @@ -29,12 +29,14 @@ impl APIClient {
response.json()
}

pub fn get_application(
pub fn get_service(
&self,
token: &str,
org_name: &str,
env_name: &str,
name: &str
) -> Result<ListOrganizationResponse, reqwest::Error> {
let url = format!("{}/organization", self.base_url);
) -> Result<Service, reqwest::Error> {
let url = format!("{}/orgs/{}/envs/{}/svcs/{}", self.base_url, org_name, env_name, name);
let response = self.get(&url, token)?;
response.json()
}
Expand Down Expand Up @@ -87,11 +89,20 @@ impl APIClient {
response.json()
}

pub fn initialize_application(&self) -> Result<(), reqwest::Error> {
let url = format!("{}/application", self.base_url);

let response = self.client.post(url).send()?.error_for_status()?;

pub fn deploy_service(
&self,
token: &str,
org_name: &str,
env_name: &str,
service: Service
) -> Result<Service, reqwest::Error> {
let url = format!("{}/orgs/{}/envs/{}/svcs", self.base_url, org_name, env_name);
let mut body: HashMap<&str, &str> = HashMap::new();
let port_str = &format!("{}", service.container_port);
tmlye marked this conversation as resolved.
Show resolved Hide resolved
body.insert("container_port", port_str);
body.insert("name", &service.name);
body.insert("image", &service.image);
let response = self.post(&url, token, &body)?;
response.json()
}

Expand Down
10 changes: 5 additions & 5 deletions src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tabled::Tabled;
pub struct Organization {
pub id: String,
pub name: String,
billing_email: String,
pub billing_email: String,
}

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -23,9 +23,9 @@ pub struct ListServicesResponse {
pub services: Vec<Service>
}

#[derive(Serialize, Deserialize, Debug, Tabled)]
#[derive(Serialize, Deserialize, Debug, Tabled, Clone)]
pub struct Service {
name: String,
image: String,
container_port: u16,
pub name: String,
pub image: String,
pub container_port: u16,
}
77 changes: 65 additions & 12 deletions src/commands/apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::path::Path;
use super::CommandBase;
use tabled::Table;

use crate::config::{
use crate::{config::{
application::{Build, HttpService},
scan::{scan_directory_for_type, ApplicationType},
};
}, api::types::Service};

#[derive(Parser)]
#[derive(Debug)]
Expand Down Expand Up @@ -53,27 +53,80 @@ pub enum Commands {
pub struct Deploy {
#[arg(help = "Name of the app to deploy")]
name: String,
#[arg(long, help = "The image to deploy, e.g. yourimage:v1")]
#[arg(long, help = "Environment to deploy to")]
env: String,
#[arg(short, long, help = "The image to deploy, e.g. yourimage:v1")]
image: Option<String>,
#[arg(long, help = "Skip confirmation")]
#[arg(long, help = "(not implemented) Skip confirmation")]
no_confirm: Option<bool>,
#[arg(short, long, help = "Port the application listens on")]
port: Option<u16>,
#[arg(long, help = "Organization to deploy to")]
org: Option<String>,
}

impl Deploy {
pub fn execute(&self, base: &CommandBase) -> Result<()> {
// flags: image, no-confirm
// 1. check if authenticated
let org_name = if self.org.is_some() {
self.org.clone().unwrap()
} else {
base.user_config().get_default_org().unwrap().to_string()
};
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;
// 2. get existing application from API
let response = base.api_client().get_application(

let response = base.api_client().get_service(
token,
&self.name,
)?;
// 3. show user what changed
// 4. submit change
&org_name,
&self.env,
&self.name
);

let existing_svc: Option<Service>;
if let Err(e) = response {
existing_svc = None;
if let Some(reqwest::StatusCode::NOT_FOUND) = e.status() {
// User needs to set every attribute if service does not exist yet
if self.image.is_none() || self.port.is_none() {
return Err(anyhow!("Image and port are mandatory if service does not exist"))
}
} else if let Some(reqwest::StatusCode::UNAUTHORIZED) = e.status() {
return Err(anyhow!("Unauthorized, please login first"))
} else {
return Err(anyhow!("Could not check whether service exists or not"))
}
} else {
existing_svc = Some(response.unwrap());
}

let mut new_svc: Service;
if existing_svc.is_some() {
new_svc = existing_svc.clone().unwrap();
if let Some(image) = &self.image {
new_svc.image = image.to_string()
}
if let Some(port) = self.port {
new_svc.container_port = port
}
} else {
new_svc = Service {
name: self.name.clone(),
image: self.image.clone().unwrap(),
container_port: self.port.clone().unwrap()
}
};

if let Some(false) = self.no_confirm {
// TODO: show user what changed
let existing_svc_yaml = serde_yaml::to_string(&existing_svc)?;
println!("{}", existing_svc_yaml);
}

let result = base.api_client().deploy_service(token, &org_name, &self.env, new_svc)?;
println!("{:?}", result);

Ok(())
}
}
Expand Down
Loading