| 20 | |
| 21 | #[allow(clippy::too_many_arguments)] |
| 22 | pub fn build_policy_update_plan( |
| 23 | add_endpoints: &[String], |
| 24 | remove_endpoints: &[String], |
| 25 | add_deny: &[String], |
| 26 | add_allow: &[String], |
| 27 | remove_rules: &[String], |
| 28 | binaries: &[String], |
| 29 | rule_name: Option<&str>, |
| 30 | ) -> Result<PolicyUpdatePlan> { |
| 31 | if binaries.iter().any(|binary| binary.trim().is_empty()) { |
| 32 | return Err(miette!("--binary values must not be empty")); |
| 33 | } |
| 34 | if !binaries.is_empty() && add_endpoints.is_empty() { |
| 35 | return Err(miette!("--binary can only be used with --add-endpoint")); |
| 36 | } |
| 37 | if rule_name.is_some() && add_endpoints.is_empty() { |
| 38 | return Err(miette!("--rule-name can only be used with --add-endpoint")); |
| 39 | } |
| 40 | if rule_name.is_some() && add_endpoints.len() > 1 { |
| 41 | return Err(miette!( |
| 42 | "--rule-name is only supported when exactly one --add-endpoint is provided" |
| 43 | )); |
| 44 | } |
| 45 | let mut merge_operations = Vec::new(); |
| 46 | let mut preview_operations = Vec::new(); |
| 47 | |
| 48 | let deduped_binaries = dedup_strings(binaries); |
| 49 | for spec in add_endpoints { |
| 50 | let endpoint = parse_add_endpoint_spec(spec)?; |
| 51 | let target_rule_name = rule_name |
| 52 | .map(str::trim) |
| 53 | .filter(|name| !name.is_empty()) |
| 54 | .map_or_else( |
| 55 | || generated_rule_name(&endpoint.host, endpoint.port), |
| 56 | ToString::to_string, |
| 57 | ); |
| 58 | let rule = NetworkPolicyRule { |
| 59 | name: target_rule_name.clone(), |
| 60 | endpoints: vec![endpoint.clone()], |
| 61 | binaries: deduped_binaries |
| 62 | .iter() |
| 63 | .map(|path| NetworkBinary { |
| 64 | path: path.clone(), |
| 65 | ..Default::default() |
| 66 | }) |
| 67 | .collect(), |
| 68 | }; |
| 69 | merge_operations.push(PolicyMergeOperation { |
| 70 | operation: Some(policy_merge_operation::Operation::AddRule(AddNetworkRule { |
| 71 | rule_name: target_rule_name.clone(), |
| 72 | rule: Some(rule.clone()), |
| 73 | })), |
| 74 | }); |
| 75 | preview_operations.push(PolicyMergeOp::AddRule { |
| 76 | rule_name: target_rule_name, |
| 77 | rule, |
| 78 | }); |
| 79 | } |