1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//!
//! # Delete object
//!
//!

use std::io::Error;
use std::io::ErrorKind;

use tracing::trace;

use dataplane::core::Encoder;
use dataplane::core::Decoder;
use dataplane::core::Version;
use dataplane::bytes::{Buf, BufMut};
use dataplane::api::Request;
use fluvio_controlplane_metadata::topic::TopicSpec;
use fluvio_controlplane_metadata::spu::CustomSpuSpec;
use fluvio_controlplane_metadata::spu::CustomSpuKey;
use fluvio_controlplane_metadata::spg::SpuGroupSpec;
use fluvio_controlplane_metadata::core::Spec;
use fluvio_controlplane_metadata::core::Removable;

use crate::Status;
use crate::AdminPublicApiKey;
use crate::AdminRequest;

pub trait DeleteSpec: Removable {
    /// convert delete key into request
    #[allow(clippy::wrong_self_convention)]
    fn into_request<K>(key: K) -> DeleteRequest
    where
        K: Into<Self::DeleteKey>;
}

// This can be auto generated by enum derive later
#[derive(Debug)]
pub enum DeleteRequest {
    Topic(String),
    CustomSpu(CustomSpuKey),
    SpuGroup(String),
}

impl Default for DeleteRequest {
    fn default() -> Self {
        DeleteRequest::CustomSpu(CustomSpuKey::Id(0))
    }
}

impl DeleteRequest {
    /// type represent as string
    fn type_string(&self) -> &'static str {
        match self {
            Self::Topic(_) => TopicSpec::LABEL,
            Self::CustomSpu(_) => CustomSpuSpec::LABEL,
            Self::SpuGroup(_) => SpuGroupSpec::LABEL,
        }
    }
}

impl AdminRequest for DeleteRequest {}

impl Request for DeleteRequest {
    const API_KEY: u16 = AdminPublicApiKey::Delete as u16;
    const DEFAULT_API_VERSION: i16 = 1;
    type Response = Status;
}

impl Encoder for DeleteRequest {
    fn write_size(&self, version: Version) -> usize {
        let type_size = self.type_string().to_owned().write_size(version);

        type_size
            + match self {
                Self::Topic(s) => s.write_size(version),
                Self::CustomSpu(s) => s.write_size(version),
                Self::SpuGroup(s) => s.write_size(version),
            }
    }

    // encode match
    fn encode<T>(&self, dest: &mut T, version: Version) -> Result<(), Error>
    where
        T: BufMut,
    {
        self.type_string().to_owned().encode(dest, version)?;

        match self {
            Self::Topic(s) => s.encode(dest, version)?,
            Self::CustomSpu(s) => s.encode(dest, version)?,
            Self::SpuGroup(s) => s.encode(dest, version)?,
        }

        Ok(())
    }
}

impl Decoder for DeleteRequest {
    fn decode<T>(&mut self, src: &mut T, version: Version) -> Result<(), Error>
    where
        T: Buf,
    {
        let mut typ = "".to_owned();
        typ.decode(src, version)?;
        trace!("decoded type: {}", typ);

        match typ.as_ref() {
            TopicSpec::LABEL => {
                let mut response = String::default();
                response.decode(src, version)?;
                *self = Self::Topic(response);
                Ok(())
            }

            CustomSpuSpec::LABEL => {
                let mut response = CustomSpuKey::default();
                response.decode(src, version)?;
                *self = Self::CustomSpu(response);
                Ok(())
            }

            SpuGroupSpec::LABEL => {
                let mut response = String::default();
                response.decode(src, version)?;
                *self = Self::SpuGroup(response);
                Ok(())
            }

            // Unexpected type
            _ => Err(Error::new(
                ErrorKind::InvalidData,
                format!("invalid spec type {}", typ),
            )),
        }
    }
}