use crate::{Error, Result};
use serde::Serialize;
#[derive(Clone, Debug)]
pub struct ListParams {
pub label_selector: Option<String>,
pub field_selector: Option<String>,
pub timeout: Option<u32>,
pub bookmarks: bool,
pub limit: Option<u32>,
pub continue_token: Option<String>,
}
impl Default for ListParams {
fn default() -> Self {
Self {
bookmarks: true,
label_selector: None,
field_selector: None,
timeout: None,
limit: None,
continue_token: None,
}
}
}
impl ListParams {
pub(crate) fn validate(&self) -> Result<()> {
if let Some(to) = &self.timeout {
if *to >= 295 {
return Err(Error::RequestValidation(
"ListParams::timeout must be < 295s".into(),
));
}
}
Ok(())
}
}
impl ListParams {
pub fn timeout(mut self, timeout_secs: u32) -> Self {
self.timeout = Some(timeout_secs);
self
}
pub fn fields(mut self, field_selector: &str) -> Self {
self.field_selector = Some(field_selector.to_string());
self
}
pub fn labels(mut self, label_selector: &str) -> Self {
self.label_selector = Some(label_selector.to_string());
self
}
pub fn disable_bookmarks(mut self) -> Self {
self.bookmarks = false;
self
}
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
pub fn continue_token(mut self, token: &str) -> Self {
self.continue_token = Some(token.to_string());
self
}
}
#[derive(Default, Clone, Debug)]
pub struct PostParams {
pub dry_run: bool,
pub field_manager: Option<String>,
}
impl PostParams {
pub(crate) fn validate(&self) -> Result<()> {
if let Some(field_manager) = &self.field_manager {
if field_manager.len() > 128 {
return Err(Error::RequestValidation(
"Failed to validate PostParams::field_manager!".into(),
));
}
}
Ok(())
}
}
#[non_exhaustive]
#[derive(Debug)]
pub enum Patch<T: Serialize> {
Apply(T),
#[cfg(feature = "jsonpatch")]
#[cfg_attr(docsrs, doc(cfg(feature = "jsonpatch")))]
Json(json_patch::Patch),
Merge(T),
Strategic(T),
}
impl<T: Serialize> Patch<T> {
pub(crate) fn is_apply(&self) -> bool {
matches!(self, Patch::Apply(_))
}
pub(crate) fn content_type(&self) -> &'static str {
match &self {
Self::Apply(_) => "application/apply-patch+yaml",
#[cfg(feature = "jsonpatch")]
#[cfg_attr(docsrs, doc(cfg(feature = "jsonpatch")))]
Self::Json(_) => "application/json-patch+json",
Self::Merge(_) => "application/merge-patch+json",
Self::Strategic(_) => "application/strategic-merge-patch+json",
}
}
}
impl<T: Serialize> Patch<T> {
pub(crate) fn serialize(&self) -> Result<Vec<u8>> {
match self {
Self::Apply(p) => serde_json::to_vec(p),
#[cfg(feature = "jsonpatch")]
#[cfg_attr(docsrs, doc(cfg(feature = "jsonpatch")))]
Self::Json(p) => serde_json::to_vec(p),
Self::Strategic(p) => serde_json::to_vec(p),
Self::Merge(p) => serde_json::to_vec(p),
}
.map_err(Into::into)
}
}
#[derive(Default, Clone, Debug)]
pub struct PatchParams {
pub dry_run: bool,
pub force: bool,
pub field_manager: Option<String>,
}
impl PatchParams {
pub(crate) fn validate<P: Serialize>(&self, patch: &Patch<P>) -> Result<()> {
if let Some(field_manager) = &self.field_manager {
if field_manager.len() > 128 {
return Err(Error::RequestValidation(
"Failed to validate PatchParams::field_manager!".into(),
));
}
}
if self.force && !patch.is_apply() {
return Err(Error::RequestValidation(
"PatchParams::force only works with Patch::Apply".into(),
));
}
Ok(())
}
pub(crate) fn populate_qp(&self, qp: &mut form_urlencoded::Serializer<String>) {
if self.dry_run {
qp.append_pair("dryRun", "All");
}
if self.force {
qp.append_pair("force", "true");
}
if let Some(ref fm) = self.field_manager {
qp.append_pair("fieldManager", fm);
}
}
pub fn apply(manager: &str) -> Self {
Self {
field_manager: Some(manager.into()),
..Self::default()
}
}
pub fn force(mut self) -> Self {
self.force = true;
self
}
pub fn dry_run(mut self) -> Self {
self.dry_run = true;
self
}
}
#[derive(Default, Clone, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DeleteParams {
#[serde(
serialize_with = "dry_run_all_ser",
skip_serializing_if = "std::ops::Not::not"
)]
pub dry_run: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub grace_period_seconds: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub propagation_policy: Option<PropagationPolicy>,
#[serde(skip_serializing_if = "Option::is_none")]
pub preconditions: Option<Preconditions>,
}
fn dry_run_all_ser<S>(t: &bool, s: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
use serde::ser::SerializeTuple;
match t {
true => {
let mut map = s.serialize_tuple(1)?;
map.serialize_element("All")?;
map.end()
}
false => s.serialize_none(),
}
}
#[cfg(test)]
mod test {
use super::DeleteParams;
#[test]
fn delete_param_serialize() {
let mut dp = DeleteParams::default();
let emptyser = serde_json::to_string(&dp).unwrap();
assert_eq!(emptyser, "{}");
dp.dry_run = true;
let ser = serde_json::to_string(&dp).unwrap();
assert_eq!(ser, "{\"dryRun\":[\"All\"]}");
}
}
#[derive(Default, Clone, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Preconditions {
#[serde(skip_serializing_if = "Option::is_none")]
pub resource_version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub uid: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
pub enum PropagationPolicy {
Orphan,
Background,
Foreground,
}