From 18a6f12f23596c1e1a8d34eb03a0819dc21858bb Mon Sep 17 00:00:00 2001 From: jonaro00 <54029719+jonaro00@users.noreply.github.com> Date: Tue, 20 Aug 2024 13:20:29 +0200 Subject: [PATCH] wip: cert command --- api-client/src/lib.rs | 11 +++++++++++ cargo-shuttle/src/args.rs | 31 ++++++++++++++++++++++++------- cargo-shuttle/src/lib.rs | 21 ++++++++++++++++++--- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/api-client/src/lib.rs b/api-client/src/lib.rs index cd642bdc0..96fd10af4 100644 --- a/api-client/src/lib.rs +++ b/api-client/src/lib.rs @@ -8,6 +8,7 @@ use reqwest::Response; use reqwest_middleware::{ClientWithMiddleware, RequestBuilder}; use serde::{Deserialize, Serialize}; use serde_json::Value; +use shuttle_common::certificate::AddCertificateRequest; use shuttle_common::log::{LogsRange, LogsResponseBeta}; use shuttle_common::models::deployment::{ DeploymentRequest, DeploymentRequestBeta, UploadArchiveResponseBeta, @@ -231,6 +232,16 @@ impl ShuttleApiClient { .await } + pub async fn add_certificate_beta(&self, project: &str, domain: String) -> Result<()> { + self.post( + format!("/projects/{project}/certificates"), + Some(AddCertificateRequest { domain }), + ) + .await?; + + Ok(()) + } + pub async fn create_project( &self, project: &str, diff --git a/cargo-shuttle/src/args.rs b/cargo-shuttle/src/args.rs index adcddda03..ce46edfa9 100644 --- a/cargo-shuttle/src/args.rs +++ b/cargo-shuttle/src/args.rs @@ -99,7 +99,7 @@ impl ProjectArgs { /// for more information. #[derive(Parser)] pub enum Command { - /// Create a new Shuttle project + /// Generate a Shuttle project from a template Init(InitArgs), /// Run a Shuttle service locally Run(RunArgs), @@ -110,16 +110,19 @@ pub enum Command { Deployment(DeploymentCommand), /// View the status of a Shuttle service Status, - /// Stop this Shuttle service + /// Stop a Shuttle service Stop, - /// View the logs of a deployment in this Shuttle service + /// View logs of a Shuttle service Logs(LogsArgs), - /// List or manage projects on Shuttle + /// Manage projects on Shuttle #[command(subcommand)] Project(ProjectCommand), - /// Manage resources of a Shuttle project + /// Manage resources #[command(subcommand)] Resource(ResourceCommand), + /// BETA: Manage SSL certificates for custom domains + #[command(subcommand)] + Certificate(CertificateCommand), /// Remove cargo build artifacts in the Shuttle environment Clean, /// Login to the Shuttle platform @@ -156,7 +159,7 @@ pub struct TableArgs { #[derive(Parser)] pub enum DeploymentCommand { - /// List all the deployments for a service + /// List the deployments for a service List { #[arg(long, default_value = "1")] /// Which page to display @@ -180,7 +183,7 @@ pub enum DeploymentCommand { #[derive(Parser)] pub enum ResourceCommand { - /// List all the resources for a project + /// List the resources for a project List { #[command(flatten)] table: TableArgs, @@ -200,6 +203,20 @@ pub enum ResourceCommand { }, } +#[derive(Parser)] +pub enum CertificateCommand { + /// Add an SSL certificate for a custom domain + Add { + /// Domain name + domain: String, + }, + /// List the certificates for a project + List { + #[command(flatten)] + table: TableArgs, + }, +} + #[derive(Parser)] pub enum ProjectCommand { /// Create an environment for this project on Shuttle diff --git a/cargo-shuttle/src/lib.rs b/cargo-shuttle/src/lib.rs index 8f7491d9e..826ee9af3 100644 --- a/cargo-shuttle/src/lib.rs +++ b/cargo-shuttle/src/lib.rs @@ -76,12 +76,12 @@ use tracing::{debug, error, info, trace, warn}; use uuid::Uuid; use zip::write::FileOptions; -pub use crate::args::{Command, ProjectArgs, RunArgs, ShuttleArgs}; use crate::args::{ - ConfirmationArgs, DeployArgs, DeploymentCommand, GenerateCommand, InitArgs, LoginArgs, - LogoutArgs, LogsArgs, ProjectCommand, ProjectStartArgs, ResourceCommand, TableArgs, + CertificateCommand, ConfirmationArgs, DeployArgs, DeploymentCommand, GenerateCommand, InitArgs, + LoginArgs, LogoutArgs, LogsArgs, ProjectCommand, ProjectStartArgs, ResourceCommand, TableArgs, TemplateLocation, }; +pub use crate::args::{Command, ProjectArgs, RunArgs, ShuttleArgs}; use crate::config::RequestContext; use crate::provisioner_server::beta::{ProvApiState, ProvisionerServerBeta}; use crate::provisioner_server::LocalProvisioner; @@ -271,6 +271,10 @@ impl Shuttle { confirmation: ConfirmationArgs { yes }, } => self.resource_delete(&resource_type, yes).await, }, + Command::Certificate(cmd) => match cmd { + CertificateCommand::Add { domain } => self.add_certificate(domain).await, + CertificateCommand::List { .. } => todo!(), + }, Command::Project(cmd) => match cmd { ProjectCommand::Start(ProjectStartArgs { idle_minutes }) => { if self.beta { @@ -1276,6 +1280,17 @@ impl Shuttle { Ok(CommandOutcome::Ok) } + async fn add_certificate(&self, domain: String) -> Result { + let client = self.client.as_ref().unwrap(); + client + .add_certificate_beta(self.ctx.project_name(), domain.clone()) + .await?; + + println!("Added certificate for {domain}"); + + Ok(CommandOutcome::Ok) + } + fn get_secrets(run_args: &RunArgs, service: &BuiltService) -> Result> { let secrets_file = run_args.secret_args.secrets.clone().or_else(|| { let crate_dir = service.crate_directory();