Skip to content

Commit

Permalink
Merge pull request #38 from wcm-io-devops/feature/use-service-propert…
Browse files Browse the repository at this point in the history
…y-in-env-vars-patching-resilience

Add invalid variable service type for environments and pipelines
  • Loading branch information
tobias-richter authored Oct 15, 2024
2 parents d342a0c + dd02ce9 commit c62bf75
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
27 changes: 26 additions & 1 deletion src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::client::CloudManagerClient;
use crate::config::CloudManagerConfig;
use crate::encryption::{decrypt, encrypt};
use crate::logs::{download_log, tail_log};
use crate::models::{Domain, LogType, ServiceType};
use crate::models::{Domain, LogType, PipelineVariableServiceType, EnvironmentVariableServiceType, ServiceType};
use crate::variables::{
get_env_vars, get_pipeline_vars, set_env_vars_from_file, set_pipeline_vars_from_file,
};
Expand Down Expand Up @@ -95,6 +95,17 @@ pub async fn init_cli() {
.await
.unwrap();
println!("{}", serde_json::to_string_pretty(&env_vars).unwrap());
if let Some(vf) = env_vars.variables.iter().find(|vf| {
vf.service == EnvironmentVariableServiceType::Invalid
}) {
eprintln!(
"{:>8} {} '{}: {}'",
"⚠".yellow(),
"WARN, invalid service type detected for variable".yellow(),
vf.name,
vf.service
);
}
}
} else {
eprintln!("❌ You have to provide a valid Cloud Manager environment ID to run this command!");
Expand Down Expand Up @@ -283,10 +294,24 @@ pub async fn init_cli() {
get_pipeline_vars(&mut cm_client, program_id, &pipeline_id)
.await
.unwrap();

println!(
"{}",
serde_json::to_string_pretty(&pipeline_vars).unwrap()
);
if let Some(vf) = pipeline_vars
.variables
.iter()
.find(|vf| vf.service == PipelineVariableServiceType::Invalid)
{
eprintln!(
"{:>8} {} '{}: {}'",
"⚠".yellow(),
"WARN, invalid service type detected for variable".yellow(),
vf.name,
vf.service
);
}
}
} else {
eprintln!("❌ You have to provide a valid Cloud Manager pipeline ID to run this command!");
Expand Down
26 changes: 20 additions & 6 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct EnvironmentVariable {
pub variable_type: VariableType,
#[serde(
default = "EnvironmentVariableServiceType::default",
skip_serializing_if = "env_var_service_type_is_default"
skip_serializing_if = "environment_variable_skip_serializing"
)]
pub service: EnvironmentVariableServiceType,
}
Expand All @@ -76,14 +76,20 @@ pub enum EnvironmentVariableServiceType {
Author,
Publish,
Preview,
#[serde(other)]
Invalid,
}

impl fmt::Display for EnvironmentVariableServiceType {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", format!("{:?}", self).to_lowercase())
write!(
formatter,
"{}",
format!("{}", serde_json::to_string(self).unwrap().to_string())
)
}
}
fn env_var_service_type_is_default(t: &EnvironmentVariableServiceType) -> bool {
fn environment_variable_skip_serializing(t: &EnvironmentVariableServiceType) -> bool {
*t == EnvironmentVariableServiceType::All
}

Expand All @@ -108,15 +114,23 @@ pub struct PipelineVariable {

/// Possible service types that an pipeline variable can have
#[derive(Clone, Debug, Deserialize, Serialize, IntoStaticStr, EnumString, PartialEq, Eq)]
#[strum(serialize_all = "lowercase")]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "camelCase")]
#[serde(rename_all = "camelCase")]
pub enum PipelineVariableServiceType {
Build,
UiTest,
FunctionalTest,
#[serde(other)]
Invalid,
}

impl fmt::Display for PipelineVariableServiceType {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", format!("{:?}", self).to_lowercase())
write!(
formatter,
"{}",
format!("{}", serde_json::to_string(self).unwrap().to_string())
)
}
}

Expand Down
31 changes: 30 additions & 1 deletion src/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::encryption::decrypt;
use crate::environments::get_environment;
use crate::errors::throw_adobe_api_error;
use crate::models::{
EnvironmentVariable, EnvironmentVariablesList, EnvironmentVariablesResponse, PipelineVariable,
EnvironmentVariable, EnvironmentVariableServiceType, EnvironmentVariablesList,
EnvironmentVariablesResponse, PipelineVariable, PipelineVariableServiceType,
PipelineVariablesList, PipelineVariablesResponse, VariableType, YamlConfig,
};
use crate::pipelines::get_pipeline;
Expand Down Expand Up @@ -197,6 +198,20 @@ pub async fn set_env_vars_from_file(
}
}

if let Some(vf) = vars_final
.iter()
.find(|vf| vf.service == EnvironmentVariableServiceType::Invalid)
{
eprintln!(
"{:>8} {} '{}: {}'",
"❌".red(),
"ERROR, invalid service type detected for variable".red(),
vf.name,
vf.service
);
process::exit(3);
}

for vf in &vars_final {
match vf.value {
None => {
Expand Down Expand Up @@ -427,6 +442,20 @@ pub async fn set_pipeline_vars_from_file(
}
}

if let Some(vf) = vars_final
.iter()
.find(|vf| vf.service == PipelineVariableServiceType::Invalid)
{
eprintln!(
"{:>8} {} '{}: {}'",
"❌".red(),
"ERROR, invalid service type detected for variable".red(),
vf.name,
vf.service
);
process::exit(3);
}

for vf in &vars_final {
match vf.value {
None => {
Expand Down

0 comments on commit c62bf75

Please sign in to comment.