use chrono::{DateTime, Utc}; use optfield::optfield; use sea_orm::ActiveValue::{Set, Unchanged}; use uuid::Uuid; use database::generated::entities::{upstream, upstream_target}; use crate::{ services::nginx::{builder::NginxConfigProvider, traits::indentable::Indentable}, set_if_some, }; #[optfield(pub UpdateUpstreamTargetInfo)] #[derive(Clone)] pub struct UpstreamTargetInfo { pub id: uuid::Uuid, pub target_host: String, pub target_port: i64, pub weight: i64, pub is_backup: bool, pub enabled: bool, pub created_at: DateTime, pub updated_at: DateTime, // pub upstream_id: uuid::Uuid, pub upstream: Option, } #[derive(Clone)] pub struct UpstreamBasicInfo { pub id: uuid::Uuid, pub name: String, pub protocol: String, // pub created_at: DateTime, pub updated_at: DateTime, } pub struct UpstreamTargetCreateInfo { pub target_host: String, pub target_port: i64, pub weight: i64, pub is_backup: bool, pub enabled: bool, // pub upstream_id: uuid::Uuid, } impl From for UpstreamTargetInfo { fn from(model: upstream_target::Model) -> Self { Self { id: model.id, target_host: model.target_host, target_port: model.target_port, weight: model.weight, is_backup: model.is_backup, enabled: model.enabled, created_at: model.created_at, updated_at: model.updated_at, upstream_id: model.upstream_id, upstream: None, } } } impl From<(upstream_target::Model, upstream::Model)> for UpstreamTargetInfo { fn from(data: (upstream_target::Model, upstream::Model)) -> Self { let (target_model, up_model) = data; Self { id: target_model.id, target_host: target_model.target_host, target_port: target_model.target_port, weight: target_model.weight, is_backup: target_model.is_backup, enabled: target_model.enabled, created_at: target_model.created_at, updated_at: target_model.updated_at, upstream_id: target_model.upstream_id, upstream: Some(UpstreamBasicInfo { id: up_model.id, name: up_model.name, protocol: up_model.protocol, created_at: up_model.created_at, updated_at: up_model.updated_at, }), } } } impl From for upstream_target::ActiveModel { fn from(val: UpstreamTargetInfo) -> Self { upstream_target::ActiveModel { id: Set(val.id), target_host: Set(val.target_host), target_port: Set(val.target_port), weight: Set(val.weight), is_backup: Set(val.is_backup), enabled: Set(val.enabled), created_at: Set(val.created_at), updated_at: Set(val.updated_at), upstream_id: Set(val.upstream_id), } } } impl From for upstream_target::ActiveModel { fn from(val: UpstreamTargetCreateInfo) -> Self { upstream_target::ActiveModel { id: Set(Uuid::new_v4()), target_host: Set(val.target_host), target_port: Set(val.target_port), weight: Set(val.weight), is_backup: Set(val.is_backup), enabled: Set(val.enabled), created_at: Set(chrono::Utc::now()), updated_at: Set(chrono::Utc::now()), upstream_id: Set(val.upstream_id), } } } impl NginxConfigProvider for UpstreamTargetInfo { fn to_nginx_config(&self, indent: Option) -> String { format!( "{}:{} weight={}{}{}", self.target_host, self.target_port, self.weight, if self.is_backup { " backup" } else { "" }, if !self.enabled { " down" } else { "" }, ) .indent(indent.unwrap_or(0)) } } impl UpdateUpstreamTargetInfo { pub fn apply_to_model( self, current_model: upstream_target::Model, ) -> upstream_target::ActiveModel { upstream_target::ActiveModel { id: Unchanged(current_model.id), target_host: set_if_some!(self.target_host), target_port: set_if_some!(self.target_port), weight: set_if_some!(self.weight), is_backup: set_if_some!(self.is_backup), enabled: set_if_some!(self.enabled), created_at: set_if_some!(self.created_at), updated_at: set_if_some!(self.updated_at), upstream_id: set_if_some!(self.upstream_id), } } }