diff --git a/src/catalog/backends/sql/endpoint.rs b/src/catalog/backends/sql/endpoint.rs index 09781808..a4b54550 100644 --- a/src/catalog/backends/sql/endpoint.rs +++ b/src/catalog/backends/sql/endpoint.rs @@ -12,59 +12,18 @@ // // SPDX-License-Identifier: Apache-2.0 -use sea_orm::DatabaseConnection; -use sea_orm::entity::*; -use sea_orm::query::*; use serde_json::Value; use tracing::error; -use crate::catalog::backends::error::{CatalogDatabaseError, db_err}; +use crate::catalog::backends::error::CatalogDatabaseError; use crate::catalog::types::*; -use crate::config::Config; -use crate::db::entity::{endpoint as db_endpoint, prelude::Endpoint as DbEndpoint}; +use crate::db::entity::endpoint as db_endpoint; -pub async fn get>( - _conf: &Config, - db: &DatabaseConnection, - id: I, -) -> Result, CatalogDatabaseError> { - let select = DbEndpoint::find_by_id(id.as_ref()); +mod get; +mod list; - let entry: Option = select - .one(db) - .await - .map_err(|err| db_err(err, "fetching service endpoint by id"))?; - entry.map(TryInto::try_into).transpose() -} - -pub async fn list( - _conf: &Config, - db: &DatabaseConnection, - params: &EndpointListParameters, -) -> Result, CatalogDatabaseError> { - let mut select = DbEndpoint::find(); - - if let Some(val) = ¶ms.interface { - select = select.filter(db_endpoint::Column::Interface.eq(val)); - } - if let Some(val) = ¶ms.service_id { - select = select.filter(db_endpoint::Column::ServiceId.eq(val)); - } - if let Some(val) = ¶ms.region_id { - select = select.filter(db_endpoint::Column::RegionId.eq(val)); - } - - let db_entities: Vec = select - .all(db) - .await - .map_err(|err| db_err(err, "fetching endpoints"))?; - let results: Result, _> = db_entities - .into_iter() - .map(TryInto::::try_into) - .collect(); - - results -} +pub use get::get; +pub use list::list; impl TryFrom for Endpoint { type Error = CatalogDatabaseError; @@ -92,14 +51,9 @@ impl TryFrom for Endpoint { #[cfg(test)] mod tests { - use sea_orm::{DatabaseBackend, MockDatabase, Transaction}; - - use crate::config::Config; use crate::db::entity::endpoint; - use super::*; - - fn get_endpoint_mock(id: String) -> endpoint::Model { + pub(super) fn get_endpoint_mock(id: String) -> endpoint::Model { endpoint::Model { id: id.clone(), interface: "public".into(), @@ -111,92 +65,4 @@ mod tests { ..Default::default() } } - - #[tokio::test] - async fn test_get() { - // Create MockDatabase with mock query results - let db = MockDatabase::new(DatabaseBackend::Postgres) - .append_query_results([ - // First query result - select user itself - vec![get_endpoint_mock("1".into())], - ]) - .into_connection(); - let config = Config::default(); - assert_eq!( - get(&config, &db, "1").await.unwrap().unwrap(), - Endpoint { - id: "1".into(), - interface: "public".into(), - service_id: "srv_id".into(), - region_id: Some("region".into()), - enabled: true, - url: "http://localhost".into(), - extra: None - } - ); - - // Checking transaction log - assert_eq!( - db.into_transaction_log(), - [Transaction::from_sql_and_values( - DatabaseBackend::Postgres, - r#"SELECT "endpoint"."id", "endpoint"."legacy_endpoint_id", "endpoint"."interface", "endpoint"."service_id", "endpoint"."url", "endpoint"."extra", "endpoint"."enabled", "endpoint"."region_id" FROM "endpoint" WHERE "endpoint"."id" = $1 LIMIT $2"#, - ["1".into(), 1u64.into()] - ),] - ); - } - - #[tokio::test] - async fn test_list() { - // Create MockDatabase with mock query results - let db = MockDatabase::new(DatabaseBackend::Postgres) - .append_query_results([vec![get_endpoint_mock("1".into())]]) - .append_query_results([vec![get_endpoint_mock("1".into())]]) - .into_connection(); - let config = Config::default(); - assert!( - list(&config, &db, &EndpointListParameters::default()) - .await - .is_ok() - ); - assert_eq!( - list( - &config, - &db, - &EndpointListParameters { - interface: Some("public".into()), - service_id: Some("service_id".into()), - region_id: Some("region_id".into()) - } - ) - .await - .unwrap(), - vec![Endpoint { - id: "1".into(), - interface: "public".into(), - service_id: "srv_id".into(), - region_id: Some("region".into()), - enabled: true, - url: "http://localhost".into(), - extra: None - }] - ); - - // Checking transaction log - assert_eq!( - db.into_transaction_log(), - [ - Transaction::from_sql_and_values( - DatabaseBackend::Postgres, - r#"SELECT "endpoint"."id", "endpoint"."legacy_endpoint_id", "endpoint"."interface", "endpoint"."service_id", "endpoint"."url", "endpoint"."extra", "endpoint"."enabled", "endpoint"."region_id" FROM "endpoint""#, - [] - ), - Transaction::from_sql_and_values( - DatabaseBackend::Postgres, - r#"SELECT "endpoint"."id", "endpoint"."legacy_endpoint_id", "endpoint"."interface", "endpoint"."service_id", "endpoint"."url", "endpoint"."extra", "endpoint"."enabled", "endpoint"."region_id" FROM "endpoint" WHERE "endpoint"."interface" = $1 AND "endpoint"."service_id" = $2 AND "endpoint"."region_id" = $3"#, - ["public".into(), "service_id".into(), "region_id".into()] - ), - ] - ); - } } diff --git a/src/catalog/backends/sql/endpoint/get.rs b/src/catalog/backends/sql/endpoint/get.rs new file mode 100644 index 00000000..3e62fa0c --- /dev/null +++ b/src/catalog/backends/sql/endpoint/get.rs @@ -0,0 +1,79 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: Apache-2.0 + +use sea_orm::DatabaseConnection; +use sea_orm::entity::*; + +use crate::catalog::backends::error::{CatalogDatabaseError, db_err}; +use crate::catalog::types::*; +use crate::config::Config; +use crate::db::entity::{endpoint as db_endpoint, prelude::Endpoint as DbEndpoint}; + +pub async fn get>( + _conf: &Config, + db: &DatabaseConnection, + id: I, +) -> Result, CatalogDatabaseError> { + let select = DbEndpoint::find_by_id(id.as_ref()); + + let entry: Option = select + .one(db) + .await + .map_err(|err| db_err(err, "fetching service endpoint by id"))?; + entry.map(TryInto::try_into).transpose() +} + +#[cfg(test)] +mod tests { + use sea_orm::{DatabaseBackend, MockDatabase, Transaction}; + + use crate::config::Config; + + use super::super::tests::get_endpoint_mock; + use super::*; + + #[tokio::test] + async fn test_get() { + // Create MockDatabase with mock query results + let db = MockDatabase::new(DatabaseBackend::Postgres) + .append_query_results([ + // First query result - select user itself + vec![get_endpoint_mock("1".into())], + ]) + .into_connection(); + let config = Config::default(); + assert_eq!( + get(&config, &db, "1").await.unwrap().unwrap(), + Endpoint { + id: "1".into(), + interface: "public".into(), + service_id: "srv_id".into(), + region_id: Some("region".into()), + enabled: true, + url: "http://localhost".into(), + extra: None + } + ); + + // Checking transaction log + assert_eq!( + db.into_transaction_log(), + [Transaction::from_sql_and_values( + DatabaseBackend::Postgres, + r#"SELECT "endpoint"."id", "endpoint"."legacy_endpoint_id", "endpoint"."interface", "endpoint"."service_id", "endpoint"."url", "endpoint"."extra", "endpoint"."enabled", "endpoint"."region_id" FROM "endpoint" WHERE "endpoint"."id" = $1 LIMIT $2"#, + ["1".into(), 1u64.into()] + ),] + ); + } +} diff --git a/src/catalog/backends/sql/endpoint/list.rs b/src/catalog/backends/sql/endpoint/list.rs new file mode 100644 index 00000000..babee5ac --- /dev/null +++ b/src/catalog/backends/sql/endpoint/list.rs @@ -0,0 +1,115 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: Apache-2.0 + +use sea_orm::DatabaseConnection; +use sea_orm::entity::*; +use sea_orm::query::*; + +use crate::catalog::backends::error::{CatalogDatabaseError, db_err}; +use crate::catalog::types::*; +use crate::config::Config; +use crate::db::entity::{endpoint as db_endpoint, prelude::Endpoint as DbEndpoint}; + +pub async fn list( + _conf: &Config, + db: &DatabaseConnection, + params: &EndpointListParameters, +) -> Result, CatalogDatabaseError> { + let mut select = DbEndpoint::find(); + + if let Some(val) = ¶ms.interface { + select = select.filter(db_endpoint::Column::Interface.eq(val)); + } + if let Some(val) = ¶ms.service_id { + select = select.filter(db_endpoint::Column::ServiceId.eq(val)); + } + if let Some(val) = ¶ms.region_id { + select = select.filter(db_endpoint::Column::RegionId.eq(val)); + } + + let db_entities: Vec = select + .all(db) + .await + .map_err(|err| db_err(err, "fetching endpoints"))?; + let results: Result, _> = db_entities + .into_iter() + .map(TryInto::::try_into) + .collect(); + + results +} + +#[cfg(test)] +mod tests { + use sea_orm::{DatabaseBackend, MockDatabase, Transaction}; + + use crate::config::Config; + + use super::super::tests::get_endpoint_mock; + use super::*; + + #[tokio::test] + async fn test_list() { + // Create MockDatabase with mock query results + let db = MockDatabase::new(DatabaseBackend::Postgres) + .append_query_results([vec![get_endpoint_mock("1".into())]]) + .append_query_results([vec![get_endpoint_mock("1".into())]]) + .into_connection(); + let config = Config::default(); + assert!( + list(&config, &db, &EndpointListParameters::default()) + .await + .is_ok() + ); + assert_eq!( + list( + &config, + &db, + &EndpointListParameters { + interface: Some("public".into()), + service_id: Some("service_id".into()), + region_id: Some("region_id".into()) + } + ) + .await + .unwrap(), + vec![Endpoint { + id: "1".into(), + interface: "public".into(), + service_id: "srv_id".into(), + region_id: Some("region".into()), + enabled: true, + url: "http://localhost".into(), + extra: None + }] + ); + + // Checking transaction log + assert_eq!( + db.into_transaction_log(), + [ + Transaction::from_sql_and_values( + DatabaseBackend::Postgres, + r#"SELECT "endpoint"."id", "endpoint"."legacy_endpoint_id", "endpoint"."interface", "endpoint"."service_id", "endpoint"."url", "endpoint"."extra", "endpoint"."enabled", "endpoint"."region_id" FROM "endpoint""#, + [] + ), + Transaction::from_sql_and_values( + DatabaseBackend::Postgres, + r#"SELECT "endpoint"."id", "endpoint"."legacy_endpoint_id", "endpoint"."interface", "endpoint"."service_id", "endpoint"."url", "endpoint"."extra", "endpoint"."enabled", "endpoint"."region_id" FROM "endpoint" WHERE "endpoint"."interface" = $1 AND "endpoint"."service_id" = $2 AND "endpoint"."region_id" = $3"#, + ["public".into(), "service_id".into(), "region_id".into()] + ), + ] + ); + } +}