Skip to content

Commit

Permalink
get projects list almost works pending one compile error
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo committed Aug 9, 2021
1 parent ad7d5e1 commit d1ef487
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 61 deletions.
1 change: 1 addition & 0 deletions omicron-common/src/api/external/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub type CreateResult<T> = Result<T, Error>;
pub type DeleteResult = Result<(), Error>;
/** Result of a list operation that returns an ObjectStream */
pub type ListResult<T> = Result<ObjectStream<T>, Error>;
pub type ListResultVec<T> = Result<Vec<T>, Error>;
/** Result of a lookup operation for the specified type */
pub type LookupResult<T> = Result<T, Error>;
/** Result of an update operation for the specified type */
Expand Down
29 changes: 0 additions & 29 deletions omicron-nexus/src/db/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ use super::sql::SqlString;
use super::sql::SqlValueSet;
use super::sql::Table;
use super::sql_operations::sql_fetch_page_by;
use super::sql_operations::sql_fetch_page_from_table;
use super::sql_operations::sql_fetch_row_by;
use super::sql_operations::sql_fetch_row_raw;
use super::sql_operations::sql_insert;
Expand Down Expand Up @@ -125,34 +124,6 @@ impl DataStore {
sql_row_value(&row, "id")
}

/// List a page of projects by id
pub async fn projects_list_by_id(
&self,
pagparams: &DataPageParams<'_, Uuid>,
) -> ListResult<db::model::Project> {
let client = self.pool.acquire().await?;
sql_fetch_page_from_table::<LookupByUniqueId, Project>(
&client,
(),
pagparams,
)
.await
}

/// List a page of projects by name
pub async fn projects_list_by_name(
&self,
pagparams: &DataPageParams<'_, Name>,
) -> ListResult<db::model::Project> {
let client = self.pool.acquire().await?;
sql_fetch_page_by::<
LookupByUniqueName,
Project,
<Project as Table>::Model,
>(&client, (), pagparams, Project::ALL_COLUMNS)
.await
}

/// Updates a project by name (clobbering update -- no etag)
pub async fn project_update(
&self,
Expand Down
20 changes: 0 additions & 20 deletions omicron-nexus/src/db/sql_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,6 @@ where
R::Model::try_from(&row)
}

/// Fetch a page of rows from a table using the specified lookup
pub async fn sql_fetch_page_from_table<'a, L, T>(
client: &'a tokio_postgres::Client,
scope_key: L::ScopeKey,
pagparams: &'a DataPageParams<'a, L::ItemKey>,
) -> ListResult<T::Model>
where
L: LookupKey<'a, T>,
L::ScopeKey: 'a,
T: Table,
{
sql_fetch_page_by::<L, T, T::Model>(
client,
scope_key,
pagparams,
T::ALL_COLUMNS,
)
.await
}

/// Like `fetch_page_from_table`, but the caller can specify which columns
/// to select and how to interpret the row
pub async fn sql_fetch_page_by<'a, L, T, R>(
Expand Down
12 changes: 6 additions & 6 deletions omicron-nexus/src/http_entrypoints_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,21 @@ async fn projects_get(
let params = ScanByNameOrId::from_query(&query)?;
let field = pagination_field_for_scan_params(params);

let project_stream = match field {
let projects = match field {
PagField::Id => {
let page_selector = data_page_params_nameid_id(&rqctx, &query)?;
nexus.projects_list_by_id(&page_selector).await?
nexus.projects_list_by_id(&page_selector)?
}

PagField::Name => {
let page_selector = data_page_params_nameid_name(&rqctx, &query)?;
nexus.projects_list_by_name(&page_selector).await?
nexus.projects_list_by_name(&page_selector)?
}
};

let view_list =
to_list::<db::model::Project, Project>(project_stream).await;
Ok(HttpResponseOk(ScanByNameOrId::results_page(&query, view_list)?))
// TODO filter out non-ok things like to_list does
let projects = projects.iter().map(|p| p.into()).collect();
Ok(HttpResponseOk(ScanByNameOrId::results_page(&query, projects)?))
}

/**
Expand Down
31 changes: 25 additions & 6 deletions omicron-nexus/src/nexus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use omicron_common::api::external::IdentityMetadataCreateParams;
use omicron_common::api::external::InstanceCreateParams;
use omicron_common::api::external::InstanceState;
use omicron_common::api::external::ListResult;
use omicron_common::api::external::ListResultVec;
use omicron_common::api::external::LookupResult;
use omicron_common::api::external::Name;
use omicron_common::api::external::ProjectCreateParams;
Expand Down Expand Up @@ -346,18 +347,36 @@ impl Nexus {
.map_err(|e| e.into())
}

pub async fn projects_list_by_name(
pub fn projects_list_by_name(
&self,
pagparams: &DataPageParams<'_, Name>,
) -> ListResult<db::model::Project> {
self.db_datastore.projects_list_by_name(pagparams).await
) -> ListResultVec<db::model::DieselProject> {
use db::diesel_schema::project::dsl::*;
let conn = self.dpool.get().unwrap();
// TODO these are incompatible arm types
// let order = match pagparams.direction {
// PaginationOrder::Ascending => name.asc(),
// PaginationOrder::Descending => name.desc(),
// };
project
.limit(pagparams.limit.get().into())
.order(name.asc())
.load::<db::model::DieselProject>(&*conn)
.map_err(|e| e.into())
}

pub async fn projects_list_by_id(
pub fn projects_list_by_id(
&self,
pagparams: &DataPageParams<'_, Uuid>,
) -> ListResult<db::model::Project> {
self.db_datastore.projects_list_by_id(pagparams).await
) -> ListResultVec<db::model::DieselProject> {
use db::diesel_schema::project::dsl::*;
let conn = self.dpool.get().unwrap();
// TODO figure out order here too
project
.limit(pagparams.limit.get().into())
.order(id.asc())
.load::<db::model::DieselProject>(&*conn)
.map_err(|e| e.into())
}

pub async fn project_delete(&self, name: &Name) -> DeleteResult {
Expand Down

0 comments on commit d1ef487

Please sign in to comment.