use crate::types::{
AllocationStatus, AppCatalogApp, AppCatalogAppConfiguration, Application, ApplicationSecret, ApplicationVolumes, Bucket, Empty, HealthCheck, HealthCheckProtocol,
LimitValueCertificateCount, LimitValueCertificateCountName, LimitValueConsumerRate, LimitValueConsumerRateName, LimitValueCpu, LimitValueCpuName, LimitValueKafkaAclGroupCount,
LimitValueKafkaAclGroupCountName, LimitValueMem, LimitValueMemName, LimitValuePartitionCount, LimitValuePartitionCountName, LimitValueProducerRate, LimitValueProducerRateName,
LimitValueRequestRate, LimitValueRequestRateName, LimitValueSecretCount, LimitValueSecretCountName, LimitValueTopicCount, LimitValueTopicCountName, ManagedStreamId,
ManagedTenant, ManagedTenantServices, ManagedTenantServicesName, Metrics, Notification, PathSpec, Secret, Vhost, Volume,
};
use itertools::Itertools;
use std::collections::HashMap;
use std::fmt::Display;
use std::num::NonZero;
use std::str::FromStr;
impl AllocationStatus {
pub fn new<T>(derived_from: T, provisioned: bool) -> Self
where
T: Into<String>,
{
Self { derived_from: Some(derived_from.into()), provisioned, ..Self::default() }
}
}
impl AppCatalogApp {
pub fn new<S, T>(name: S, manifest_urn: T) -> Self
where
S: Into<String>,
T: Into<String>,
{
Self { name: name.into(), manifest_urn: manifest_urn.into(), ..Self::default() }
}
}
impl AppCatalogAppConfiguration {
pub fn new<S, T>(name: S, manifest_urn: T, stopped: bool) -> Self
where
S: Into<String>,
T: Into<String>,
{
Self { name: name.into(), manifest_urn: manifest_urn.into(), stopped, ..Self::default() }
}
}
impl Application {
pub fn new<T>(image: T, cpus: f64, mem: u64, instances: u64) -> Self
where
T: Into<String>,
{
Self { image: image.into(), cpus, mem, instances, ..Self::default() }
}
}
impl ApplicationSecret {
pub fn new<S, T>(name: S, injections: &[T]) -> Self
where
S: Into<String>,
T: ToString,
{
Self {
name: name.into(),
injections: injections
.iter()
.map(|injection| HashMap::from([("env".to_string(), injection.to_string())]))
.collect_vec(),
}
}
}
impl ApplicationVolumes {
pub fn new<T>(name: T) -> Self
where
T: Into<String>,
{
Self { name: name.into() }
}
}
impl Bucket {
pub fn new(encrypted: bool, versioned: bool) -> Self {
Self { encrypted, versioned }
}
}
impl Empty {
pub fn new() -> Self {
Self::default()
}
}
impl HealthCheck {
pub fn new<T>(path: T, port: u64) -> Self
where
T: Into<String>,
{
Self { path: path.into(), port, protocol: Some(HealthCheckProtocol::Https) }
}
}
impl LimitValueCertificateCount {
pub fn new(certificate_count: u64) -> Self {
Self { name: LimitValueCertificateCountName::CertificateCount, value: NonZero::new(certificate_count).expect("certificate count must be greater than zero") }
}
}
impl LimitValueConsumerRate {
pub fn new(consumer_rate: i64) -> Self {
Self { name: LimitValueConsumerRateName::ConsumerRate, value: consumer_rate }
}
}
impl LimitValueCpu {
pub fn new(cpus: f64) -> Self {
Self { name: LimitValueCpuName::Cpu, value: cpus }
}
}
impl LimitValueKafkaAclGroupCount {
pub fn new(kafka_acl_group_count: i64) -> Self {
Self { name: LimitValueKafkaAclGroupCountName::KafkaAclGroupCount, value: kafka_acl_group_count }
}
}
impl LimitValueMem {
pub fn new(mem: u64) -> Self {
Self { name: LimitValueMemName::Mem, value: NonZero::new(mem).expect("memory size must be greater than zero") }
}
}
impl LimitValuePartitionCount {
pub fn new(partition_count: u64) -> Self {
Self { name: LimitValuePartitionCountName::PartitionCount, value: NonZero::new(partition_count).expect("partition count must be greater than zero") }
}
}
impl LimitValueProducerRate {
pub fn new(producer_rate: i64) -> Self {
Self { name: LimitValueProducerRateName::ProducerRate, value: producer_rate }
}
}
impl LimitValueRequestRate {
pub fn new(request_rate: u64) -> Self {
Self { name: LimitValueRequestRateName::RequestRate, value: NonZero::new(request_rate).expect("request rate must be greater than zero") }
}
}
impl LimitValueSecretCount {
pub fn new(secret_count: u64) -> Self {
Self { name: LimitValueSecretCountName::SecretCount, value: NonZero::new(secret_count).expect("secret count must be greater than zero") }
}
}
impl LimitValueTopicCount {
pub fn new(topic_count: u64) -> Self {
Self { name: LimitValueTopicCountName::TopicCount, value: NonZero::new(topic_count).expect("topic count must be greater than zero") }
}
}
impl ManagedStreamId {
pub fn new<S, T>(manager: S, stream_id: T) -> Self
where
S: Display,
T: Display,
{
let managed_stream_id = format!("{}---{}", manager, stream_id);
Self::from_str(&managed_stream_id).unwrap_or_else(|_| panic!("{} is not a valid managed stream id", managed_stream_id))
}
}
impl ManagedTenant {
pub fn new<S, T>(manager: S, tenant_name: T) -> Self
where
S: Into<String>,
T: Into<String>,
{
Self {
manager: manager.into(),
name: tenant_name.into(),
services: vec![
ManagedTenantServices { enabled: true, name: ManagedTenantServicesName::Monitoring },
ManagedTenantServices { enabled: false, name: ManagedTenantServicesName::Tracing },
ManagedTenantServices { enabled: false, name: ManagedTenantServicesName::Vpn },
],
}
}
}
impl Metrics {
pub fn new<T>(path: T, port: u64) -> Self
where
T: Into<String>,
{
Self { path: path.into(), port }
}
}
impl Notification {
pub fn new<T>(remove: bool, message: T, args: HashMap<String, String>) -> Self
where
T: Into<String>,
{
Self { args, message: message.into(), remove }
}
}
impl PathSpec {
pub fn new<T>(prefix: T) -> Self
where
T: Into<String>,
{
Self { prefix: prefix.into() }
}
}
impl Secret {
pub fn new<S, T>(name: S, value: T) -> Self
where
S: Into<String>,
T: Into<String>,
{
Self { name: name.into(), value: value.into() }
}
}
impl Vhost {
pub fn new<T>(value: T) -> Self
where
T: Into<String>,
{
Self { value: value.into() }
}
}
impl Volume {
pub fn new(size_gi_b: i64) -> Self {
Self { size_gi_b }
}
}