Skip to content

Commit

Permalink
chore: handle cloud client creation (#2121)
Browse files Browse the repository at this point in the history
* chore: handle cloud client creation

* chore: fix login
  • Loading branch information
v3g42 authored Oct 4, 2023
1 parent c0d1a7b commit 39c60f2
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 48 deletions.
2 changes: 1 addition & 1 deletion dozer-cli/src/cli/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::ArgAction;
use dozer_types::grpc_types::cloud::Secret;
use std::error::Error;

#[derive(Debug, Args)]
#[derive(Debug, Args, Clone)]
#[command(args_conflicts_with_subcommands = true)]
pub struct Cloud {
#[arg(global = true, short = 't', long)]
Expand Down
9 changes: 6 additions & 3 deletions dozer-cli/src/cloud/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use dozer_types::prettytable::{row, table};
use futures::{select, FutureExt, StreamExt};
use std::io;
use std::sync::Arc;
use tokio::runtime::Runtime;
use tonic::transport::Endpoint;
use tower::ServiceBuilder;

Expand Down Expand Up @@ -354,14 +355,16 @@ impl DozerGrpcCloudClient for CloudClient {
}

fn login(
&mut self,
runtime: Arc<Runtime>,
cloud: Cloud,
organisation_slug: Option<String>,
profile: Option<String>,
client_id: Option<String>,
client_secret: Option<String>,
) -> Result<(), OrchestrationError> {
info!("Organisation and client details can be created in https://dashboard.dev.getdozer.io/login \n");
info!(
"Organisation and client details can be created in https://cloud.getdozer.io/login \n"
);
let organisation_slug = match organisation_slug {
None => {
let mut organisation_slug = String::new();
Expand All @@ -374,7 +377,7 @@ impl DozerGrpcCloudClient for CloudClient {
Some(name) => name,
};

self.runtime.block_on(async move {
runtime.block_on(async move {
let login_svc = LoginSvc::new(
organisation_slug,
cloud
Expand Down
5 changes: 5 additions & 0 deletions dozer-cli/src/cloud/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ impl CredentialInfo {
.json()
.await
.map_err(CloudCredentialError::HttpRequestError)?;
if json_response.get("error").is_some() {
return Err(CloudCredentialError::LoginError(
json_response.get("error").unwrap().to_string(),
));
}
serde_json::from_value::<TokenResponse>(json_response)
.map_err(CloudCredentialError::JsonSerializationError)
}
Expand Down
3 changes: 2 additions & 1 deletion dozer-cli/src/cloud/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod monitor;
pub mod progress_printer;
mod token_layer;
pub use client::CloudClient;
use tokio::runtime::Runtime;

pub trait DozerGrpcCloudClient {
fn deploy(
Expand All @@ -26,7 +27,7 @@ pub trait DozerGrpcCloudClient {
fn monitor(&mut self, cloud: Cloud) -> Result<(), OrchestrationError>;
fn trace_logs(&mut self, cloud: Cloud, logs: LogCommandArgs) -> Result<(), OrchestrationError>;
fn login(
&mut self,
runtime: std::sync::Arc<Runtime>,
cloud: Cloud,
organisation_slug: Option<String>,
profile: Option<String>,
Expand Down
2 changes: 2 additions & 0 deletions dozer-cli/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ pub enum CloudCredentialError {
MissingCredentialFile,
#[error("There's no profile with given name - Please try to login again")]
MissingProfile,
#[error("{0}")]
LoginError(String),
}

#[derive(Debug, Error)]
Expand Down
112 changes: 69 additions & 43 deletions dozer-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,74 @@ fn run() -> Result<(), OrchestrationError> {

let runtime = Arc::new(Runtime::new().map_err(CliError::FailedToCreateTokioRuntime)?);

let config = init_configuration(&cli, runtime.clone())?;
let (shutdown_sender, shutdown_receiver) = shutdown::new(&runtime);
set_ctrl_handler(shutdown_sender);

set_panic_hook();

// Run Cloud
#[cfg(feature = "cloud")]
if let Commands::Cloud(cloud) = &cli.cmd {
render_logo();
let cloud = cloud.clone();
let res = if let CloudCommands::Login {
organisation_slug,
profile_name,
client_id,
client_secret,
} = cloud.command.clone()
{
CloudClient::login(
runtime.clone(),
cloud,
organisation_slug,
profile_name,
client_id,
client_secret,
)
} else {
let config = init_configuration(&cli, runtime.clone())?;
let mut cloud_client = CloudClient::new(config.clone(), runtime.clone());
match cloud.command.clone() {
CloudCommands::Deploy(deploy) => {
cloud_client.deploy(cloud, deploy, cli.config_paths.clone())
}
CloudCommands::Login {
organisation_slug: _,
profile_name: _,
client_id: _,
client_secret: _,
} => unreachable!("This is handled earlier"),
CloudCommands::Secrets(command) => {
cloud_client.execute_secrets_command(cloud, command)
}
CloudCommands::Delete => cloud_client.delete(cloud),
CloudCommands::Status => cloud_client.status(cloud),
CloudCommands::Monitor => cloud_client.monitor(cloud),
CloudCommands::Logs(logs) => cloud_client.trace_logs(cloud, logs),
CloudCommands::Version(version) => cloud_client.version(cloud, version),
CloudCommands::List(list) => cloud_client.list(cloud, list),
CloudCommands::SetApp { app_id } => {
CloudAppContext::save_app_id(app_id.clone())?;
info!("Using \"{app_id}\" app");
Ok(())
}
CloudCommands::ApiRequestSamples { endpoint } => {
cloud_client.print_api_request_samples(cloud, endpoint)
}
}
};

return match res {
Ok(_) => Ok(()),
Err(e) => {
println!("{}", e);
Err(e)
}
};
}

let config = init_configuration(&cli, runtime.clone())?;
// Now we have access to telemetry configuration. Telemetry must be initialized in tokio runtime.
let app_id = config.cloud.app_id.as_deref().unwrap_or(&config.app_name);

Expand All @@ -145,47 +209,6 @@ fn run() -> Result<(), OrchestrationError> {

let _telemetry = runtime.block_on(async { Telemetry::new(Some(app_id), &telemetry_config) });

set_panic_hook();

// Run Cloud
#[cfg(feature = "cloud")]
if let Commands::Cloud(cloud) = cli.cmd {
render_logo();
let mut cloud_client = CloudClient::new(config.clone(), runtime.clone());
let res = match cloud.command.clone() {
CloudCommands::Deploy(deploy) => {
cloud_client.deploy(cloud, deploy, cli.config_paths.clone())
}
CloudCommands::Login {
organisation_slug,
profile_name,
client_id,
client_secret,
} => cloud_client.login(
cloud,
organisation_slug,
profile_name,
client_id,
client_secret,
),
CloudCommands::Secrets(command) => cloud_client.execute_secrets_command(cloud, command),
CloudCommands::Delete => cloud_client.delete(cloud),
CloudCommands::Status => cloud_client.status(cloud),
CloudCommands::Monitor => cloud_client.monitor(cloud),
CloudCommands::Logs(logs) => cloud_client.trace_logs(cloud, logs),
CloudCommands::Version(version) => cloud_client.version(cloud, version),
CloudCommands::List(list) => cloud_client.list(cloud, list),
CloudCommands::SetApp { app_id } => {
CloudAppContext::save_app_id(app_id.clone())?;
info!("Using \"{app_id}\" app");
Ok(())
}
CloudCommands::ApiRequestSamples { endpoint } => {
cloud_client.print_api_request_samples(cloud, endpoint)
}
};
return res;
}
let mut dozer = init_dozer(
runtime.clone(),
config.clone(),
Expand Down Expand Up @@ -298,7 +321,10 @@ fn init_configuration(cli: &Cli, runtime: Arc<Runtime>) -> Result<Config, CliErr
runtime.spawn(check_update());
Ok(config)
}
Err(e) => Err(e),
Err(e) => {
error!("{}", e);
Err(e)
}
}
},
)
Expand Down

0 comments on commit 39c60f2

Please sign in to comment.