Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tenant): use tenant id type #6643

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/common_utils/src/id_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod payment;
mod profile;
mod refunds;
mod routing;
mod tenant;

#[cfg(feature = "v2")]
mod global_id;
Expand Down Expand Up @@ -40,6 +41,7 @@ pub use profile::ProfileId;
pub use refunds::RefundReferenceId;
pub use routing::RoutingId;
use serde::{Deserialize, Serialize};
pub use tenant::TenantId;
use thiserror::Error;

use crate::{fp_utils::when, generate_id_with_default_len};
Expand Down
2 changes: 1 addition & 1 deletion crates/common_utils/src/id_type/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ crate::impl_to_sql_from_sql_id_type!(OrganizationId);

impl OrganizationId {
/// Get an organization id from String
pub fn wrap(org_id: String) -> CustomResult<Self, ValidationError> {
pub fn try_from_string(org_id: String) -> CustomResult<Self, ValidationError> {
Self::try_from(std::borrow::Cow::from(org_id))
}
}
22 changes: 22 additions & 0 deletions crates/common_utils/src/id_type/tenant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::errors::{CustomResult, ValidationError};

crate::id_type!(
TenantId,
"A type for tenant_id that can be used for unique identifier for a tenant"
);
crate::impl_id_type_methods!(TenantId, "tenant_id");

// This is to display the `TenantId` as TenantId(abcd)
crate::impl_debug_id_type!(TenantId);
crate::impl_try_from_cow_str_id_type!(TenantId, "tenant_id");

crate::impl_serializable_secret_id_type!(TenantId);
crate::impl_queryable_id_type!(TenantId);
crate::impl_to_sql_from_sql_id_type!(TenantId);

impl TenantId {
/// Get tenant id from String
pub fn try_from_string(tenant_id: String) -> CustomResult<Self, ValidationError> {
Self::try_from(std::borrow::Cow::from(tenant_id))
}
}
12 changes: 6 additions & 6 deletions crates/common_utils/src/types/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ pub enum ThemeLineage {
// },
/// Org lineage variant
Organization {
/// tenant_id: String
tenant_id: String,
/// tenant_id: TenantId
tenant_id: id_type::TenantId,
/// org_id: OrganizationId
org_id: id_type::OrganizationId,
},
/// Merchant lineage variant
Merchant {
/// tenant_id: String
tenant_id: String,
/// tenant_id: TenantId
tenant_id: id_type::TenantId,
/// org_id: OrganizationId
org_id: id_type::OrganizationId,
/// merchant_id: MerchantId
merchant_id: id_type::MerchantId,
},
/// Profile lineage variant
Profile {
/// tenant_id: String
tenant_id: String,
/// tenant_id: TenantId
tenant_id: id_type::TenantId,
/// org_id: OrganizationId
org_id: id_type::OrganizationId,
/// merchant_id: MerchantId
Expand Down
4 changes: 2 additions & 2 deletions crates/diesel_models/src/user/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::schema::themes;
#[diesel(table_name = themes, primary_key(theme_id), check_for_backend(diesel::pg::Pg))]
pub struct Theme {
pub theme_id: String,
pub tenant_id: String,
pub tenant_id: id_type::TenantId,
pub org_id: Option<id_type::OrganizationId>,
pub merchant_id: Option<id_type::MerchantId>,
pub profile_id: Option<id_type::ProfileId>,
Expand All @@ -23,7 +23,7 @@ pub struct Theme {
#[diesel(table_name = themes)]
pub struct ThemeNew {
pub theme_id: String,
pub tenant_id: String,
pub tenant_id: id_type::TenantId,
pub org_id: Option<id_type::OrganizationId>,
pub merchant_id: Option<id_type::MerchantId>,
pub profile_id: Option<id_type::ProfileId>,
Expand Down
4 changes: 2 additions & 2 deletions crates/diesel_models/src/user_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct UserRole {
pub entity_id: Option<String>,
pub entity_type: Option<EntityType>,
pub version: enums::UserRoleVersion,
pub tenant_id: String,
pub tenant_id: id_type::TenantId,
}

impl UserRole {
Expand Down Expand Up @@ -88,7 +88,7 @@ pub struct UserRoleNew {
pub entity_id: Option<String>,
pub entity_type: Option<EntityType>,
pub version: enums::UserRoleVersion,
pub tenant_id: String,
pub tenant_id: id_type::TenantId,
}

#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)]
Expand Down
8 changes: 6 additions & 2 deletions crates/drainer/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
sync::{atomic, Arc},
};

use common_utils::id_type;
use router_env::tracing::Instrument;
use tokio::{
sync::{mpsc, oneshot},
Expand Down Expand Up @@ -34,12 +35,15 @@ pub struct HandlerInner {
loop_interval: Duration,
active_tasks: Arc<atomic::AtomicU64>,
conf: DrainerSettings,
stores: HashMap<String, Arc<Store>>,
stores: HashMap<id_type::TenantId, Arc<Store>>,
running: Arc<atomic::AtomicBool>,
}

impl Handler {
pub fn from_conf(conf: DrainerSettings, stores: HashMap<String, Arc<Store>>) -> Self {
pub fn from_conf(
conf: DrainerSettings,
stores: HashMap<id_type::TenantId, Arc<Store>>,
) -> Self {
let shutdown_interval = Duration::from_millis(conf.shutdown_interval.into());
let loop_interval = Duration::from_millis(conf.loop_interval.into());

Expand Down
4 changes: 2 additions & 2 deletions crates/drainer/src/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, sync::Arc};

use actix_web::{web, Scope};
use async_bb8_diesel::{AsyncConnection, AsyncRunQueryDsl};
use common_utils::errors::CustomResult;
use common_utils::{errors::CustomResult, id_type};
use diesel_models::{Config, ConfigNew};
use error_stack::ResultExt;
use router_env::{instrument, logger, tracing};
Expand All @@ -20,7 +20,7 @@ pub const TEST_STREAM_DATA: &[(&str, &str)] = &[("data", "sample_data")];
pub struct Health;

impl Health {
pub fn server(conf: Settings, stores: HashMap<String, Arc<Store>>) -> Scope {
pub fn server(conf: Settings, stores: HashMap<id_type::TenantId, Arc<Store>>) -> Scope {
web::scope("health")
.app_data(web::Data::new(conf))
.app_data(web::Data::new(stores))
Expand Down
6 changes: 3 additions & 3 deletions crates/drainer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{collections::HashMap, sync::Arc};
mod secrets_transformers;

use actix_web::dev::Server;
use common_utils::signals::get_allowed_signals;
use common_utils::{id_type, signals::get_allowed_signals};
use diesel_models::kv;
use error_stack::ResultExt;
use hyperswitch_interfaces::secrets_interface::secret_state::RawSecret;
Expand All @@ -31,7 +31,7 @@ use crate::{
};

pub async fn start_drainer(
stores: HashMap<String, Arc<Store>>,
stores: HashMap<id_type::TenantId, Arc<Store>>,
conf: DrainerSettings,
) -> errors::DrainerResult<()> {
let drainer_handler = handler::Handler::from_conf(conf, stores);
Expand Down Expand Up @@ -62,7 +62,7 @@ pub async fn start_drainer(

pub async fn start_web_server(
conf: Settings,
stores: HashMap<String, Arc<Store>>,
stores: HashMap<id_type::TenantId, Arc<Store>>,
) -> Result<Server, errors::DrainerError> {
let server = conf.server.clone();
let web_server = actix_web::HttpServer::new(move || {
Expand Down
16 changes: 8 additions & 8 deletions crates/drainer/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, path::PathBuf, sync::Arc};

use common_utils::{ext_traits::ConfigExt, DbConnectionParams};
use common_utils::{ext_traits::ConfigExt, id_type, DbConnectionParams};
use config::{Environment, File};
use external_services::managers::{
encryption_management::EncryptionManagementConfig, secrets_management::SecretsManagementConfig,
Expand Down Expand Up @@ -122,23 +122,23 @@ pub struct Multitenancy {
pub tenants: TenantConfig,
}
impl Multitenancy {
pub fn get_tenants(&self) -> &HashMap<String, Tenant> {
pub fn get_tenants(&self) -> &HashMap<id_type::TenantId, Tenant> {
&self.tenants.0
}
pub fn get_tenant_ids(&self) -> Vec<String> {
pub fn get_tenant_ids(&self) -> Vec<id_type::TenantId> {
self.tenants
.0
.values()
.map(|tenant| tenant.tenant_id.clone())
.collect()
}
pub fn get_tenant(&self, tenant_id: &str) -> Option<&Tenant> {
pub fn get_tenant(&self, tenant_id: &id_type::TenantId) -> Option<&Tenant> {
self.tenants.0.get(tenant_id)
}
}

#[derive(Debug, Clone, Default)]
pub struct TenantConfig(pub HashMap<String, Tenant>);
pub struct TenantConfig(pub HashMap<id_type::TenantId, Tenant>);

impl<'de> Deserialize<'de> for TenantConfig {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Expand All @@ -150,7 +150,7 @@ impl<'de> Deserialize<'de> for TenantConfig {
clickhouse_database: String,
}

let hashmap = <HashMap<String, Inner>>::deserialize(deserializer)?;
let hashmap = <HashMap<id_type::TenantId, Inner>>::deserialize(deserializer)?;

Ok(Self(
hashmap
Expand All @@ -172,9 +172,9 @@ impl<'de> Deserialize<'de> for TenantConfig {
}
}

#[derive(Debug, Deserialize, Clone, Default)]
#[derive(Debug, Deserialize, Clone)]
pub struct Tenant {
pub tenant_id: String,
pub tenant_id: id_type::TenantId,
pub base_url: String,
pub schema: String,
pub redis_key_prefix: String,
Expand Down
25 changes: 12 additions & 13 deletions crates/router/src/configs/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
#[cfg(feature = "olap")]
use analytics::{opensearch::OpenSearchConfig, ReportConfig};
use api_models::{enums, payment_methods::RequiredFieldInfo};
use common_utils::ext_traits::ConfigExt;
use common_utils::{ext_traits::ConfigExt, id_type};
use config::{Environment, File};
use error_stack::ResultExt;
#[cfg(feature = "email")]
Expand Down Expand Up @@ -138,17 +138,17 @@ pub struct Multitenancy {
}

impl Multitenancy {
pub fn get_tenants(&self) -> &HashMap<String, Tenant> {
pub fn get_tenants(&self) -> &HashMap<id_type::TenantId, Tenant> {
&self.tenants.0
}
pub fn get_tenant_ids(&self) -> Vec<String> {
pub fn get_tenant_ids(&self) -> Vec<id_type::TenantId> {
self.tenants
.0
.values()
.map(|tenant| tenant.tenant_id.clone())
.collect()
}
pub fn get_tenant(&self, tenant_id: &str) -> Option<&Tenant> {
pub fn get_tenant(&self, tenant_id: &id_type::TenantId) -> Option<&Tenant> {
self.tenants.0.get(tenant_id)
}
}
Expand All @@ -159,11 +159,11 @@ pub struct DecisionConfig {
}

#[derive(Debug, Clone, Default)]
pub struct TenantConfig(pub HashMap<String, Tenant>);
pub struct TenantConfig(pub HashMap<id_type::TenantId, Tenant>);

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct Tenant {
pub tenant_id: String,
pub tenant_id: id_type::TenantId,
pub base_url: String,
pub schema: String,
pub redis_key_prefix: String,
Expand Down Expand Up @@ -743,8 +743,7 @@ pub struct LockerBasedRecipientConnectorList {

#[derive(Debug, Deserialize, Clone, Default)]
pub struct ConnectorRequestReferenceIdConfig {
pub merchant_ids_send_payment_id_as_connector_request_id:
HashSet<common_utils::id_type::MerchantId>,
pub merchant_ids_send_payment_id_as_connector_request_id: HashSet<id_type::MerchantId>,
}

#[derive(Debug, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -970,7 +969,7 @@ pub struct ServerTls {
#[cfg(feature = "v2")]
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct CellInformation {
pub id: common_utils::id_type::CellId,
pub id: id_type::CellId,
}

#[cfg(feature = "v2")]
Expand All @@ -981,8 +980,8 @@ impl Default for CellInformation {
// around the time of deserializing application settings.
// And a panic at application startup is considered acceptable.
#[allow(clippy::expect_used)]
let cell_id = common_utils::id_type::CellId::from_string("defid")
.expect("Failed to create a default for Cell Id");
let cell_id =
id_type::CellId::from_string("defid").expect("Failed to create a default for Cell Id");
Self { id: cell_id }
}
}
Expand Down Expand Up @@ -1120,7 +1119,7 @@ impl<'de> Deserialize<'de> for TenantConfig {
clickhouse_database: String,
}

let hashmap = <HashMap<String, Inner>>::deserialize(deserializer)?;
let hashmap = <HashMap<id_type::TenantId, Inner>>::deserialize(deserializer)?;

Ok(Self(
hashmap
Expand Down
Loading
Loading