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
//!
//! # Create SPU Groups
//!
//! Public API to request the SC to create managed spu groups
//!
//!
use kf_protocol::api::Request;
use kf_protocol::derive::{Decode, Encode};

use crate::FlvResponseMessage;
use crate::ScApiKey;
use crate::ApiError;

// -----------------------------------
// FlvCreateSpuGroupsRequest
// -----------------------------------

#[derive(Encode, Decode, Default, Debug)]
pub struct FlvCreateSpuGroupsRequest {
    /// A list of one or more spu groups to be created.
    pub spu_groups: Vec<FlvCreateSpuGroupRequest>,
}

impl Request for FlvCreateSpuGroupsRequest {
    const API_KEY: u16 = ScApiKey::FlvCreateSpuGroups as u16;
    const DEFAULT_API_VERSION: i16 = 1;
    type Response = FlvCreateSpuGroupsResponse;
}




// quick way to convert a single group into groups requests
impl From<FlvCreateSpuGroupRequest> for FlvCreateSpuGroupsRequest {
    fn from(group: FlvCreateSpuGroupRequest) -> Self {
        let mut groups = Self::default();
        groups.spu_groups.push(group);
        groups
    }
}

#[derive(Encode, Decode, Default, Debug)]
pub struct FlvCreateSpuGroupRequest {
    /// The name of the managed spu group
    pub name: String,

    /// The number of replicas for the spu group
    pub replicas: u16,

    /// The base spu id that the spu group uses to increment the spu ids
    /// Note: Spu id is a globally unique resource and it cannot be shared
    pub min_id: Option<i32>,

    /// Configuration elements to be applied to each SPUs in the group
    pub config: FlvGroupConfig,

    /// The rack to be used for all SPUs in the group. Racks are used by
    /// replication assignment algorithm
    pub rack: Option<String>,
}

#[derive(Encode, Decode, Default, Debug)]
pub struct FlvGroupConfig {
    pub storage: Option<FlvStorageConfig>,
    pub replication: Option<FlvReplicationConfig>,
    pub env: Vec<FlvEnvVar>
}

#[derive(Encode, Decode, Default, Debug)]
pub struct FlvStorageConfig {
    pub log_dir: Option<String>,
    pub size: Option<String>,
}

#[derive(Encode, Decode, Default, Debug)]
pub struct FlvReplicationConfig {
    pub in_sync_replica_min: Option<u16>,
}

#[derive(Encode, Decode, Default, Debug)]
pub struct FlvEnvVar {
    pub name: String,
    pub value: String,
}

// -----------------------------------
// FlvCreateSpuGroupsResponse
// -----------------------------------

#[derive(Encode, Decode, Default, Debug)]
pub struct FlvCreateSpuGroupsResponse {
    /// The spu group creation result messages.
    pub results: Vec<FlvResponseMessage>,
}



impl FlvCreateSpuGroupsResponse {

    /// validate and extract a single response
    pub fn validate(self) -> Result<(),ApiError> {

        // ? what is name, so just find first item
        if let Some(item) = self.results.into_iter().find(|_| true ) {
            item.as_result()
        } else {
            Err(ApiError::NoResourceFounded("custom spu".to_owned()))
        }
        
    }
}