-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a04f5e3
commit 1aa5cfd
Showing
17 changed files
with
755 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::schema::probe; | ||
use db_macros::Resource; | ||
use nexus_types::external_api::params; | ||
use nexus_types::identity::Resource; | ||
use omicron_common::api::external; | ||
use omicron_common::api::external::IdentityMetadataCreateParams; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use uuid::Uuid; | ||
|
||
#[derive( | ||
Queryable, | ||
Insertable, | ||
Selectable, | ||
Clone, | ||
Debug, | ||
Resource, | ||
Serialize, | ||
Deserialize, | ||
)] | ||
#[diesel(table_name = probe)] | ||
pub struct Probe { | ||
#[diesel(embed)] | ||
pub identity: ProbeIdentity, | ||
|
||
pub sled: Uuid, | ||
} | ||
|
||
impl From<¶ms::ProbeCreate> for Probe { | ||
fn from(p: ¶ms::ProbeCreate) -> Self { | ||
Self { | ||
identity: ProbeIdentity::new( | ||
Uuid::new_v4(), | ||
IdentityMetadataCreateParams { | ||
name: p.identity.name.clone(), | ||
description: p.identity.description.clone(), | ||
}, | ||
), | ||
sled: p.sled, | ||
} | ||
} | ||
} | ||
|
||
impl Into<external::Probe> for Probe { | ||
fn into(self) -> external::Probe { | ||
external::Probe { identity: self.identity().clone(), sled: self.sled } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
use crate::context::OpContext; | ||
use crate::db; | ||
use crate::db::error::public_error_from_diesel; | ||
use crate::db::error::ErrorHandler; | ||
use crate::db::model::Name; | ||
use crate::db::pagination::paginated; | ||
use async_bb8_diesel::AsyncRunQueryDsl; | ||
use chrono::Utc; | ||
use diesel::{ExpressionMethods, QueryDsl, SelectableHelper}; | ||
use nexus_db_model::Probe; | ||
use nexus_types::external_api::params; | ||
use omicron_common::api::external::http_pagination::PaginatedBy; | ||
use omicron_common::api::external::CreateResult; | ||
use omicron_common::api::external::DeleteResult; | ||
use omicron_common::api::external::ListResultVec; | ||
use omicron_common::api::external::LookupResult; | ||
use omicron_common::api::external::NameOrId; | ||
use ref_cast::RefCast; | ||
use uuid::Uuid; | ||
|
||
impl super::DataStore { | ||
pub async fn probe_list( | ||
&self, | ||
opctx: &OpContext, | ||
pagparams: &PaginatedBy<'_>, | ||
) -> ListResultVec<Probe> { | ||
use db::schema::probe::dsl; | ||
|
||
let pool = self.pool_connection_authorized(opctx).await?; | ||
|
||
match pagparams { | ||
PaginatedBy::Id(pagparams) => { | ||
paginated(dsl::probe, dsl::id, &pagparams) | ||
} | ||
PaginatedBy::Name(pagparams) => paginated( | ||
dsl::probe, | ||
dsl::name, | ||
&pagparams.map_name(|n| Name::ref_cast(n)), | ||
), | ||
} | ||
.filter(dsl::time_deleted.is_null()) | ||
.select(Probe::as_select()) | ||
.load_async(&*pool) | ||
.await | ||
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server)) | ||
} | ||
|
||
pub async fn probe_get( | ||
&self, | ||
opctx: &OpContext, | ||
name_or_id: &NameOrId, | ||
) -> LookupResult<Probe> { | ||
use db::schema::probe; | ||
use db::schema::probe::dsl; | ||
let pool = self.pool_connection_authorized(opctx).await?; | ||
|
||
let name_or_id = name_or_id.clone(); | ||
|
||
let probe = match name_or_id { | ||
NameOrId::Name(name) => dsl::probe | ||
.filter(probe::name.eq(name.to_string())) | ||
.filter(probe::time_deleted.is_null()) | ||
.select(Probe::as_select()) | ||
.limit(1) | ||
.first_async::<Probe>(&*pool) | ||
.await | ||
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server)), | ||
NameOrId::Id(id) => dsl::probe | ||
.filter(probe::id.eq(id)) | ||
.select(Probe::as_select()) | ||
.limit(1) | ||
.first_async::<Probe>(&*pool) | ||
.await | ||
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server)), | ||
}?; | ||
|
||
Ok(probe) | ||
} | ||
|
||
pub async fn probe_create( | ||
&self, | ||
opctx: &OpContext, | ||
new_probe: ¶ms::ProbeCreate, | ||
) -> CreateResult<Probe> { | ||
use db::schema::probe::dsl; | ||
let pool = self.pool_connection_authorized(opctx).await?; | ||
|
||
let probe = Probe::from(new_probe); | ||
|
||
let result = diesel::insert_into(dsl::probe) | ||
.values(probe.clone()) | ||
.returning(Probe::as_returning()) | ||
.get_result_async(&*pool) | ||
.await | ||
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?; | ||
|
||
Ok(result) | ||
} | ||
|
||
pub async fn probe_delete( | ||
&self, | ||
opctx: &OpContext, | ||
name_or_id: &NameOrId, | ||
) -> DeleteResult { | ||
use db::schema::probe; | ||
use db::schema::probe::dsl; | ||
let pool = self.pool_connection_authorized(opctx).await?; | ||
|
||
let name_or_id = name_or_id.clone(); | ||
|
||
//TODO in transaction | ||
let id = match name_or_id { | ||
NameOrId::Name(name) => dsl::probe | ||
.filter(probe::name.eq(name.to_string())) | ||
.filter(probe::time_deleted.is_null()) | ||
.select(probe::id) | ||
.limit(1) | ||
.first_async::<Uuid>(&*pool) | ||
.await | ||
.map_err(|e| { | ||
public_error_from_diesel(e, ErrorHandler::Server) | ||
})?, | ||
NameOrId::Id(id) => id, | ||
}; | ||
|
||
diesel::update(dsl::probe) | ||
.filter(dsl::id.eq(id)) | ||
.set(dsl::time_deleted.eq(Utc::now())) | ||
.execute_async(&*pool) | ||
.await | ||
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use nexus_db_model::Probe; | ||
use nexus_db_queries::context::OpContext; | ||
use nexus_types::external_api::params; | ||
use omicron_common::api::external::{ | ||
http_pagination::PaginatedBy, CreateResult, DeleteResult, ListResultVec, | ||
LookupResult, NameOrId, | ||
}; | ||
|
||
impl super::Nexus { | ||
pub(crate) async fn probe_list( | ||
&self, | ||
opctx: &OpContext, | ||
pagparams: &PaginatedBy<'_>, | ||
) -> ListResultVec<Probe> { | ||
self.db_datastore.probe_list(opctx, pagparams).await | ||
} | ||
|
||
pub(crate) async fn probe_get( | ||
&self, | ||
opctx: &OpContext, | ||
name_or_id: NameOrId, | ||
) -> LookupResult<Probe> { | ||
self.db_datastore.probe_get(opctx, &name_or_id).await | ||
} | ||
|
||
pub(crate) async fn probe_create( | ||
&self, | ||
opctx: &OpContext, | ||
new_probe_params: ¶ms::ProbeCreate, | ||
) -> CreateResult<Probe> { | ||
self.db_datastore.probe_create(opctx, new_probe_params).await | ||
} | ||
|
||
pub(crate) async fn probe_delete( | ||
&self, | ||
opctx: &OpContext, | ||
name_or_id: NameOrId, | ||
) -> DeleteResult { | ||
self.db_datastore.probe_delete(opctx, &name_or_id).await | ||
} | ||
} |
Oops, something went wrong.