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
136
137
138
139
140
141
142
143
144
#![allow(clippy::assign_op_pattern)]

use std::fmt::Debug;

use dataplane::derive::{Decode, Encode};
use dataplane::core::Encoder;
use dataplane::core::Decoder;
use dataplane::api::Request;

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

pub use create::AllCreatableSpec;

#[derive(Encode, Decode, Default, Debug)]
pub struct CreateRequest {
    pub name: String,
    pub dry_run: bool,
    pub spec: AllCreatableSpec,
}

impl Request for CreateRequest {
    const API_KEY: u16 = AdminPublicApiKey::Create as u16;
    const DEFAULT_API_VERSION: i16 = 0;
    type Response = Status;
}

impl AdminRequest for CreateRequest {}

#[allow(clippy::module_inception)]
mod create {

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

    use tracing::trace;

    use dataplane::core::Version;
    use dataplane::bytes::{Buf, BufMut};
    use fluvio_controlplane_metadata::topic::TopicSpec;
    use fluvio_controlplane_metadata::spu::CustomSpuSpec;
    use fluvio_controlplane_metadata::spg::SpuGroupSpec;
    use super::*;

    const TOPIC: u8 = 0;
    const CUSTOM_SPU: u8 = 1;
    const SPG: u8 = 2;

    #[derive(Debug)]
    /// enum of spec that can be created
    pub enum AllCreatableSpec {
        Topic(TopicSpec),
        CustomSpu(CustomSpuSpec),
        SpuGroup(SpuGroupSpec),
    }

    impl Default for AllCreatableSpec {
        fn default() -> Self {
            Self::Topic(TopicSpec::default())
        }
    }

    impl Encoder for AllCreatableSpec {
        fn write_size(&self, version: Version) -> usize {
            let type_size = (0u8).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,
        {
            match self {
                Self::Topic(s) => {
                    let typ: u8 = TOPIC;
                    typ.encode(dest, version)?;
                    s.encode(dest, version)?;
                }

                Self::CustomSpu(s) => {
                    let typ: u8 = CUSTOM_SPU;
                    typ.encode(dest, version)?;
                    s.encode(dest, version)?;
                }

                Self::SpuGroup(s) => {
                    let typ: u8 = SPG;
                    typ.encode(dest, version)?;
                    s.encode(dest, version)?;
                }
            }

            Ok(())
        }
    }

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

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

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

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

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