diff --git a/trifid-api/src/routes/v1/mod.rs b/trifid-api/src/routes/v1/mod.rs index 36a9ce5..b619472 100644 --- a/trifid-api/src/routes/v1/mod.rs +++ b/trifid-api/src/routes/v1/mod.rs @@ -1,4 +1,5 @@ pub mod auth; pub mod signup; pub mod totp_authenticators; -pub mod verify_totp_authenticators; \ No newline at end of file +pub mod verify_totp_authenticators; +pub mod networks; \ No newline at end of file diff --git a/trifid-api/src/routes/v1/networks.rs b/trifid-api/src/routes/v1/networks.rs new file mode 100644 index 0000000..834dd0a --- /dev/null +++ b/trifid-api/src/routes/v1/networks.rs @@ -0,0 +1,46 @@ +use serde::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct GetNetworksResponse { + pub data: Vec, + pub metadata: GetNetworksResponseMetadata +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct GetNetworksResponseData { + pub id: String, + pub cidr: String, + #[serde(rename = "organizationID")] + pub organization_id: String, + #[serde(rename = "signingCAID")] + pub signing_ca_id: String, + #[serde(rename = "createdAt")] + pub created_at: String, // 2023-03-22T18:55:47.009Z + pub name: String, + #[serde(rename = "lighthousesAsRelays")] + pub lighthouses_as_relays: bool +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct GetNetworksResponseMetadata { + #[serde(rename = "totalCount")] + pub total_count: i64, + #[serde(rename = "hasNextPage")] + pub has_next_page: bool, + #[serde(rename = "hasPrevPage")] + pub has_prev_page: bool, + #[serde(default, skip_serializing_if = "is_none", rename = "prevCursor")] + pub prev_cursor: Option, + #[serde(default, skip_serializing_if = "is_none", rename = "nextCursor")] + pub next_cursor: Option, + #[serde(default, skip_serializing_if = "is_none")] + pub page: Option +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct GetNetworksResponseMetadataPage { + pub count: i64, + pub start: i64 +} + +fn is_none(o: &Option) -> bool { o.is_none() } \ No newline at end of file diff --git a/trifid-api/trifid_api_entities/src/entity/network.rs b/trifid-api/trifid_api_entities/src/entity/network.rs index 93232d1..7d575e6 100644 --- a/trifid-api/trifid_api_entities/src/entity/network.rs +++ b/trifid-api/trifid_api_entities/src/entity/network.rs @@ -7,9 +7,14 @@ use sea_orm::entity::prelude::*; pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub id: String, + pub cidr: String, #[sea_orm(unique)] pub organization: String, - pub ip_block: String, + #[sea_orm(unique)] + pub signing_ca: String, + pub created_at: i64, + pub name: String, + pub lighthouses_as_relays: bool, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -22,6 +27,14 @@ pub enum Relation { on_delete = "Cascade" )] Organization, + #[sea_orm( + belongs_to = "super::signing_ca::Entity", + from = "Column::SigningCa", + to = "super::signing_ca::Column::Id", + on_update = "Cascade", + on_delete = "Cascade" + )] + SigningCa, } impl Related for Entity { @@ -30,4 +43,10 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::SigningCa.def() + } +} + impl ActiveModelBehavior for ActiveModel {} diff --git a/trifid-api/trifid_api_entities/src/entity/signing_ca.rs b/trifid-api/trifid_api_entities/src/entity/signing_ca.rs index 2caba85..d0770a9 100644 --- a/trifid-api/trifid_api_entities/src/entity/signing_ca.rs +++ b/trifid-api/trifid_api_entities/src/entity/signing_ca.rs @@ -17,6 +17,15 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm(has_one = "super::network::Entity")] + Network, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Network.def() + } +} impl ActiveModelBehavior for ActiveModel {} diff --git a/trifid-api/trifid_api_migration/src/lib.rs b/trifid-api/trifid_api_migration/src/lib.rs index 3768bfc..35bee9d 100644 --- a/trifid-api/trifid_api_migration/src/lib.rs +++ b/trifid-api/trifid_api_migration/src/lib.rs @@ -6,12 +6,12 @@ pub mod m20230402_162601_create_table_users; pub mod m20230402_183515_create_table_magic_links; pub mod m20230402_213712_create_table_session_tokens; pub mod m20230402_232316_create_table_organizations; -pub mod m20230402_232323_create_table_networks; pub mod m20230402_233043_create_table_api_keys; pub mod m20230402_233047_create_table_api_keys_scopes; -mod m20230402_234025_create_table_totp_authenticators; -mod m20230403_002256_create_table_auth_tokens; -mod m20230403_142517_create_table_signing_cas; +pub mod m20230402_234025_create_table_totp_authenticators; +pub mod m20230403_002256_create_table_auth_tokens; +pub mod m20230403_142517_create_table_signing_cas; +pub mod m20230403_173431_create_table_networks; #[async_trait::async_trait] impl MigratorTrait for Migrator { @@ -21,12 +21,12 @@ impl MigratorTrait for Migrator { Box::new(m20230402_183515_create_table_magic_links::Migration), Box::new(m20230402_213712_create_table_session_tokens::Migration), Box::new(m20230402_232316_create_table_organizations::Migration), - Box::new(m20230402_232323_create_table_networks::Migration), Box::new(m20230402_233043_create_table_api_keys::Migration), Box::new(m20230402_233047_create_table_api_keys_scopes::Migration), Box::new(m20230402_234025_create_table_totp_authenticators::Migration), Box::new(m20230403_002256_create_table_auth_tokens::Migration), Box::new(m20230403_142517_create_table_signing_cas::Migration), + Box::new(m20230403_173431_create_table_networks::Migration), ] } } diff --git a/trifid-api/trifid_api_migration/src/m20230402_232323_create_table_networks.rs b/trifid-api/trifid_api_migration/src/m20230403_173431_create_table_networks.rs similarity index 56% rename from trifid-api/trifid_api_migration/src/m20230402_232323_create_table_networks.rs rename to trifid-api/trifid_api_migration/src/m20230403_173431_create_table_networks.rs index 668fdd4..a85fcd4 100644 --- a/trifid-api/trifid_api_migration/src/m20230402_232323_create_table_networks.rs +++ b/trifid-api/trifid_api_migration/src/m20230403_173431_create_table_networks.rs @@ -1,5 +1,6 @@ use sea_orm_migration::prelude::*; use crate::m20230402_232316_create_table_organizations::Organization; +use crate::m20230403_142517_create_table_signing_cas::SigningCA; #[derive(DeriveMigrationName)] pub struct Migration; @@ -11,15 +12,27 @@ impl MigrationTrait for Migration { Table::create() .table(Network::Table) .col(ColumnDef::new(Network::Id).string().not_null().primary_key()) + .col(ColumnDef::new(Network::Cidr).string().not_null()) .col(ColumnDef::new(Network::Organization).string().not_null().unique_key()) - .col(ColumnDef::new(Network::IpBlock).string().not_null()) + .col(ColumnDef::new(Network::SigningCA).string().not_null().unique_key()) + .col(ColumnDef::new(Network::CreatedAt).big_integer().not_null()) + .col(ColumnDef::new(Network::Name).string().not_null()) + .col(ColumnDef::new(Network::LighthousesAsRelays).boolean().not_null()) .foreign_key( ForeignKey::create() .from(Network::Table, Network::Organization) .to(Organization::Table, Organization::Id) .on_delete(ForeignKeyAction::Cascade) .on_update(ForeignKeyAction::Cascade) - ).to_owned() + ) + .foreign_key( + ForeignKey::create() + .from(Network::Table, Network::SigningCA) + .to(SigningCA::Table, SigningCA::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade) + ) + .to_owned() ).await } @@ -33,6 +46,10 @@ impl MigrationTrait for Migration { pub enum Network { Table, Id, + Cidr, Organization, - IpBlock + SigningCA, + CreatedAt, + Name, + LighthousesAsRelays }