From 60865d5977ec2df25c8f70caf6383cdf11c955bd Mon Sep 17 00:00:00 2001 From: korvyashka Date: Thu, 4 Apr 2024 12:36:24 +0400 Subject: [PATCH] WIP: adding installation_id to manifest --- src/cli.rs | 72 ++++++++++++++++---------------- src/commands/edge_app.rs | 88 ++++++++++++++++++++++++++++++++++------ src/commands/mod.rs | 2 + 3 files changed, 113 insertions(+), 49 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index aad4665..7846e46 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -389,6 +389,10 @@ pub enum EdgeAppVersionCommands { #[arg(short, long)] app_id: Option, + /// Edge App Installation id. If app_id is specified, installation_id must be also specified. If both are not specified, CLI will use the installation_id from the manifest + #[arg(short, long)] + installation_id: Option, + #[arg(long, action = clap::ArgAction::SetTrue, conflicts_with = "revision", default_value="false")] latest: bool, @@ -409,6 +413,10 @@ pub enum EdgeAppSettingsCommands { #[arg(short, long)] app_id: Option, + /// Edge App Installation id. If app_id is specified, installation_id must be also specified. If both are not specified, CLI will use the installation_id from the manifest + #[arg(short, long)] + installation_id: Option, + /// Enables JSON output. #[arg(short, long, action = clap::ArgAction::SetTrue)] json: Option, @@ -423,6 +431,10 @@ pub enum EdgeAppSettingsCommands { #[arg(short, long)] app_id: Option, + /// Edge App Installation id. If app_id is specified, installation_id must be also specified. If both are not specified, CLI will use the installation_id from the manifest + #[arg(short, long)] + installation_id: Option, + /// Path to the directory with the manifest. If not specified CLI will use the current working directory. #[arg(short, long)] path: Option, @@ -453,6 +465,10 @@ pub enum EdgeAppSecretsCommands { #[arg(short, long)] app_id: Option, + /// Edge App Installation id. If app_id is specified, installation_id must be also specified. If both are not specified, CLI will use the installation_id from the manifest + #[arg(short, long)] + installation_id: Option, + /// Path to the directory with the manifest. If not specified CLI will use the current working directory. #[arg(short, long)] path: Option, @@ -510,29 +526,6 @@ pub fn get_screen_name( Err(CommandError::MissingField) } -fn get_actual_app_id( - app_id: &Option, - path: &Option, -) -> Result { - match app_id { - Some(id) if id.is_empty() => Err(CommandError::EmptyAppId), - Some(id) => Ok(id.clone()), - None => { - let manifest_path = transform_edge_app_path_to_manifest(path); - EdgeAppManifest::ensure_manifest_is_valid(manifest_path.as_path())?; - - let manifest = EdgeAppManifest::new(manifest_path.as_path()).unwrap(); - match manifest.app_id { - Some(id) if !id.is_empty() => Ok(id), - _ => { - error!("Edge app id is not specified. Please specify it using --app-id option or add it to the manifest."); - Err(CommandError::MissingAppId) - } - } - } - } -} - pub fn get_asset_title( id: &str, asset_command: &commands::asset::AssetCommand, @@ -923,7 +916,8 @@ pub fn handle_cli_edge_app_command(command: &EdgeAppCommands) { EdgeAppCommands::List { json } => { handle_command_execution_result(edge_app_command.list(), json); } - EdgeAppCommands::Upload { path, app_id } => { + EdgeAppCommands::Upload { path, app_id} => { + match edge_app_command.upload( transform_edge_app_path_to_manifest(path).as_path(), app_id.clone(), @@ -958,11 +952,12 @@ pub fn handle_cli_edge_app_command(command: &EdgeAppCommands) { path, revision, app_id, + installation_id, channel, latest, } => { - let actual_app_id = match get_actual_app_id(app_id, path) { - Ok(id) => id, + let (actual_app_id, actual_installation_id) = match edge_app_command.ensure_app_and_installation_id(app_id.clone(), installation_id.clone(), path.clone()) { + Ok((_app_id, _installation_id)) => {(_app_id, _installation_id)}, Err(e) => { error!("Failed to promote edge app version: {}", e); std::process::exit(1); @@ -995,7 +990,7 @@ pub fn handle_cli_edge_app_command(command: &EdgeAppCommands) { } }; - match edge_app_command.promote_version(&actual_app_id, revision, channel) { + match edge_app_command.promote_version(&actual_app_id, &actual_installation_id, revision, channel) { Ok(()) => { println!("Edge app version successfully promoted."); } @@ -1007,32 +1002,34 @@ pub fn handle_cli_edge_app_command(command: &EdgeAppCommands) { } }, EdgeAppCommands::Setting(command) => match command { - EdgeAppSettingsCommands::List { path, json, app_id } => { - let actual_app_id = match get_actual_app_id(app_id, path) { - Ok(id) => id, + EdgeAppSettingsCommands::List { path, json, app_id , installation_id} => { + let (actual_app_id, actual_installation_id) = match edge_app_command.ensure_app_and_installation_id(app_id.clone(), installation_id.clone(), path.clone()) { + Ok((_app_id, _installation_id)) => {(_app_id, _installation_id)}, Err(e) => { error!("Error calling list settings: {}", e); std::process::exit(1); } }; handle_command_execution_result( - edge_app_command.list_settings(&actual_app_id), + edge_app_command.list_settings(&actual_app_id, &actual_installation_id), json, ); } EdgeAppSettingsCommands::Set { setting_pair, app_id, + installation_id, path, } => { - let actual_app_id = match get_actual_app_id(app_id, path) { - Ok(id) => id, + let (actual_app_id, actual_installation_id) = match edge_app_command.ensure_app_and_installation_id(app_id.clone(), installation_id.clone(), path.clone()) { + Ok((_app_id, _installation_id)) => {(_app_id, _installation_id)}, Err(e) => { error!("Error calling set setting: {}", e); std::process::exit(1); } }; - match edge_app_command.set_setting(&actual_app_id, &setting_pair.0, &setting_pair.1) + + match edge_app_command.set_setting(&actual_app_id, &actual_installation_id, &setting_pair.0, &setting_pair.1) { Ok(()) => { println!("Edge app setting successfully set."); @@ -1061,17 +1058,18 @@ pub fn handle_cli_edge_app_command(command: &EdgeAppCommands) { EdgeAppSecretsCommands::Set { secret_pair, app_id, + installation_id, path, } => { - let actual_app_id = match get_actual_app_id(app_id, path) { - Ok(id) => id, + let (actual_app_id, actual_installation_id) = match edge_app_command.ensure_app_and_installation_id(app_id.clone(), installation_id.clone(), path.clone()) { + Ok((_app_id, _installation_id)) => {(_app_id, _installation_id)}, Err(e) => { error!("Error calling set secrets: {}", e); std::process::exit(1); } }; - match edge_app_command.set_secret(&actual_app_id, &secret_pair.0, &secret_pair.1) { + match edge_app_command.set_secret(&actual_app_id, &actual_installation_id, &secret_pair.0, &secret_pair.1) { Ok(()) => { println!("Edge app secret successfully set."); } diff --git a/src/commands/edge_app.rs b/src/commands/edge_app.rs index 2774366..7da8f0b 100644 --- a/src/commands/edge_app.rs +++ b/src/commands/edge_app.rs @@ -90,12 +90,16 @@ impl EdgeAppCommand { let json_response = serde_json::from_value::>(response)?; let app_id = json_response[0].id.clone(); + if app_id.is_empty() { return Err(CommandError::MissingField); } + let installation_id = self.install_edge_app(&app_id, &name)?; + let manifest = EdgeAppManifest { app_id: Some(app_id), + installation_id: Some(installation_id), entrypoint: Some("index.html".to_string()), settings: vec![ Setting { @@ -163,7 +167,11 @@ impl EdgeAppCommand { return Err(CommandError::MissingField); } + let installation_id = self.install_edge_app(&app_id, &name)?; + manifest.app_id = Some(app_id); + manifest.installation_id = Some(installation_id); + EdgeAppManifest::save_to_file(&manifest, path)?; Ok(()) @@ -186,8 +194,8 @@ impl EdgeAppCommand { )?)) } - pub fn list_settings(&self, app_id: &str) -> Result { - let installation_id = self.get_or_create_installation(app_id)?; + pub fn list_settings(&self, app_id: &str, installation_id: &str) -> Result { + let response = commands::get( &self.authentication, &format!( @@ -247,6 +255,7 @@ impl EdgeAppCommand { pub fn set_setting( &self, app_id: &str, + installation_id: &str, setting_key: &str, setting_value: &str, ) -> Result<(), CommandError> { @@ -278,7 +287,6 @@ impl EdgeAppCommand { app_id, setting_key, ); } else { - let installation_id = self.get_or_create_installation(app_id)?; setting_url = format!( "v4.1/edge-apps/settings/values?select=name&installation_id=eq.{}&name=eq.{}", installation_id, setting_key, @@ -323,6 +331,7 @@ impl EdgeAppCommand { pub fn set_secret( &self, app_id: &str, + installation_id: &str, secret_key: &str, secret_value: &str, ) -> Result<(), CommandError> { @@ -337,7 +346,6 @@ impl EdgeAppCommand { } ) } else { - let installation_id = self.get_or_create_installation(app_id)?; json!( { "installation_id": installation_id, @@ -461,18 +469,17 @@ impl EdgeAppCommand { self.publish(actual_app_id, revision)?; debug!("Edge app published."); - self.get_or_create_installation(actual_app_id)?; - Ok(revision) } pub fn promote_version( &self, app_id: &str, + installation_id: &str, revision: u32, channel: &String, ) -> Result<(), CommandError> { - let secrets = self.get_undefined_settings(app_id)?; + let secrets = self.get_undefined_settings(app_id, installation_id)?; if !secrets.is_empty() { return Err(CommandError::UndefinedSettings(serde_json::to_string( &secrets, @@ -614,9 +621,7 @@ impl EdgeAppCommand { println!("Mock data for Edge App emulator was generated."); Ok(()) } - fn get_undefined_settings(&self, app_id: &str) -> Result, CommandError> { - let installation_id = self.get_or_create_installation(app_id)?; - + fn get_undefined_settings(&self, app_id: &str, installation_id: &str) -> Result, CommandError> { let undefined_settings_response = commands::get( &self.authentication, &format!( @@ -782,7 +787,12 @@ impl EdgeAppCommand { Ok(()) } - fn get_or_create_installation(&self, manifest: &mut EdgeAppManifest, app_id: &str, path: &Path) -> Result { + pub fn get_or_create_installation(&self, app_id: &str, path: Option) -> Result { + let manifest_path = transform_edge_app_path_to_manifest(path); + EdgeAppManifest::ensure_manifest_is_valid(manifest_path.as_path())?; + + let mut manifest = EdgeAppManifest::new(manifest_path.as_path()).unwrap(); + if manifest.installation_id.is_some() { // Ideally installation_id should be stored in the manifest file return Ok(manifest.installation_id.clone().unwrap()); @@ -805,7 +815,7 @@ impl EdgeAppCommand { // Anyway save installation_id to manifest manifest.installation_id = Some(installation_id.clone()); - EdgeAppManifest::save_to_file(manifest, path)?; + EdgeAppManifest::save_to_file(&manifest, manifest_path)?; Ok(installation_id) } @@ -1101,6 +1111,59 @@ impl EdgeAppCommand { None => Ok(false), } } + + fn get_actual_app_id( + &self, + app_id: &Option, + path: &Option, + ) -> Result { + match app_id { + Some(id) if id.is_empty() => Err(CommandError::EmptyAppId), + Some(id) => Ok(id.clone()), + None => { + let manifest_path = transform_edge_app_path_to_manifest(path); + EdgeAppManifest::ensure_manifest_is_valid(manifest_path.as_path())?; + + let manifest = EdgeAppManifest::new(manifest_path.as_path()).unwrap(); + match manifest.app_id { + Some(id) if !id.is_empty() => Ok(id), + _ => { + Err(CommandError::MissingAppId) + } + } + } + } + } + + pub fn ensure_app_and_installation_id(&self, app_id: Option, installation_id: Option, path: Option) -> Result<(String, String), CommandError> { + + if installation_id.is_none() && app_id.is_some() { + return Err(CommandError::EmptyInstallationId); + } + + let actual_app_id = match self.get_actual_app_id(&app_id, &path) { + Ok(id) => id, + Err(e) => { + return Err(CommandError::EmptyAppId); + } + }; + + let actual_installation_id = match installation_id { + Some(_installation_id) => { + _installation_id + }, + None => { + match manifest.installation_id { + Some(_installation_id) => {installation_id}, + None => { + self.get_or_create_installation(&actual_app_id, path)? + } + } + } + }; + + return Ok((actual_app_id, actual_installation_id)); + } } #[cfg(test)] @@ -1118,6 +1181,7 @@ mod tests { fn create_edge_app_manifest_for_test(settings: Vec) -> EdgeAppManifest { EdgeAppManifest { app_id: Some("01H2QZ6Z8WXWNDC0KQ198XCZEW".to_string()), + installation_id: Some("01H2QZ6Z8WXWNDC0KQ198XCZEB").to_string()), user_version: Some("1".to_string()), description: Some("asdf".to_string()), icon: Some("asdf".to_string()), diff --git a/src/commands/mod.rs b/src/commands/mod.rs index cdc3d9e..1c3c32c 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -116,6 +116,8 @@ pub enum CommandError { MissingAppId, #[error("App id cannot be empty. Provide it either in manifest or with --app-id.")] EmptyAppId, + #[error("Installation id cannot be empty, when app_id is provided. Provide it with --installation-id or fill both in the manifest.")] + EmptyInstallationId, #[error("Edge App Revision {0} not found")] RevisionNotFound(String), #[error("Manifest file validation failed with error: {0}")]