use crate::client::RestClient;
use crate::error::Result;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct License {
#[serde(skip_serializing_if = "Option::is_none")]
pub key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
#[serde(rename = "type")]
pub type_: Option<String>,
pub expired: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub activation_date: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub expiration_date: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cluster_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub owner: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub shards_limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ram_shards_in_use: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ram_shards_limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flash_shards_in_use: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flash_shards_limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub node_limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub features: Option<Vec<String>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, TypedBuilder)]
pub struct LicenseUpdateRequest {
#[builder(setter(into))]
pub license: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LicenseUsage {
pub shards_used: u32,
pub shards_limit: u32,
pub nodes_used: u32,
pub nodes_limit: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub ram_used: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ram_limit: Option<u64>,
}
pub struct LicenseHandler {
client: RestClient,
}
impl LicenseHandler {
pub fn new(client: RestClient) -> Self {
LicenseHandler { client }
}
pub async fn get(&self) -> Result<License> {
self.client.get("/v1/license").await
}
pub async fn update(&self, request: LicenseUpdateRequest) -> Result<License> {
self.client.put_action("/v1/license", &request).await?;
self.get().await
}
pub async fn usage(&self) -> Result<LicenseUsage> {
self.client.get("/v1/license/usage").await
}
pub async fn validate(&self, license_key: &str) -> Result<License> {
let request = LicenseUpdateRequest {
license: license_key.to_string(),
};
self.client.post("/v1/license/validate", &request).await
}
pub async fn cluster_license(&self) -> Result<License> {
self.client.get("/v1/cluster/license").await
}
}