Skip to content

Commit

Permalink
WIP: adding installation_id to manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
korvyashka committed Apr 4, 2024
1 parent 7a58eb6 commit 60865d5
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 49 deletions.
72 changes: 35 additions & 37 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ pub enum EdgeAppVersionCommands {
#[arg(short, long)]
app_id: Option<String>,

/// 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<String>,

#[arg(long, action = clap::ArgAction::SetTrue, conflicts_with = "revision", default_value="false")]
latest: bool,

Expand All @@ -409,6 +413,10 @@ pub enum EdgeAppSettingsCommands {
#[arg(short, long)]
app_id: Option<String>,

/// 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<String>,

/// Enables JSON output.
#[arg(short, long, action = clap::ArgAction::SetTrue)]
json: Option<bool>,
Expand All @@ -423,6 +431,10 @@ pub enum EdgeAppSettingsCommands {
#[arg(short, long)]
app_id: Option<String>,

/// 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<String>,

/// Path to the directory with the manifest. If not specified CLI will use the current working directory.
#[arg(short, long)]
path: Option<String>,
Expand Down Expand Up @@ -453,6 +465,10 @@ pub enum EdgeAppSecretsCommands {
#[arg(short, long)]
app_id: Option<String>,

/// 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<String>,

/// Path to the directory with the manifest. If not specified CLI will use the current working directory.
#[arg(short, long)]
path: Option<String>,
Expand Down Expand Up @@ -510,29 +526,6 @@ pub fn get_screen_name(
Err(CommandError::MissingField)
}

fn get_actual_app_id(
app_id: &Option<String>,
path: &Option<String>,
) -> Result<String, CommandError> {
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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.");
}
Expand All @@ -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.");
Expand Down Expand Up @@ -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.");
}
Expand Down
88 changes: 76 additions & 12 deletions src/commands/edge_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ impl EdgeAppCommand {

let json_response = serde_json::from_value::<Vec<EdgeAppCreationResponse>>(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 {
Expand Down Expand Up @@ -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(())
Expand All @@ -186,8 +194,8 @@ impl EdgeAppCommand {
)?))
}

pub fn list_settings(&self, app_id: &str) -> Result<EdgeAppSettings, CommandError> {
let installation_id = self.get_or_create_installation(app_id)?;
pub fn list_settings(&self, app_id: &str, installation_id: &str) -> Result<EdgeAppSettings, CommandError> {

let response = commands::get(
&self.authentication,
&format!(
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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> {
Expand All @@ -337,7 +346,6 @@ impl EdgeAppCommand {
}
)
} else {
let installation_id = self.get_or_create_installation(app_id)?;
json!(
{
"installation_id": installation_id,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Vec<String>, CommandError> {
let installation_id = self.get_or_create_installation(app_id)?;

fn get_undefined_settings(&self, app_id: &str, installation_id: &str) -> Result<Vec<String>, CommandError> {
let undefined_settings_response = commands::get(
&self.authentication,
&format!(
Expand Down Expand Up @@ -782,7 +787,12 @@ impl EdgeAppCommand {
Ok(())
}

fn get_or_create_installation(&self, manifest: &mut EdgeAppManifest, app_id: &str, path: &Path) -> Result<String, CommandError> {
pub fn get_or_create_installation(&self, app_id: &str, path: Option<String>) -> Result<String, CommandError> {
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());
Expand All @@ -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)
}
Expand Down Expand Up @@ -1101,6 +1111,59 @@ impl EdgeAppCommand {
None => Ok(false),
}
}

fn get_actual_app_id(
&self,
app_id: &Option<String>,
path: &Option<String>,
) -> Result<String, CommandError> {
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<String>, installation_id: Option<String>, path: Option<String>) -> 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)]
Expand All @@ -1118,6 +1181,7 @@ mod tests {
fn create_edge_app_manifest_for_test(settings: Vec<Setting>) -> 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()),
Expand Down
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand Down

0 comments on commit 60865d5

Please sign in to comment.