Skip to content

Commit 01cb47c

Browse files
committed
views and params concept in nexus with Project as demo
1 parent 0389b14 commit 01cb47c

File tree

10 files changed

+73
-59
lines changed

10 files changed

+73
-59
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

omicron-common/src/api/external/mod.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -514,44 +514,6 @@ pub struct IdentityMetadataUpdateParams {
514514
* Specific API resources
515515
*/
516516

517-
/*
518-
* PROJECTS
519-
*/
520-
521-
/**
522-
* Client view of an [`Project`]
523-
*/
524-
#[derive(ObjectIdentity, Clone, Debug, Deserialize, Serialize, JsonSchema)]
525-
#[serde(rename_all = "camelCase")]
526-
pub struct Project {
527-
/*
528-
* TODO-correctness is flattening here (and in all the other types) the
529-
* intent in RFD 4?
530-
*/
531-
#[serde(flatten)]
532-
pub identity: IdentityMetadata,
533-
}
534-
535-
/**
536-
* Create-time parameters for an [`Project`]
537-
*/
538-
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
539-
#[serde(rename_all = "camelCase")]
540-
pub struct ProjectCreateParams {
541-
#[serde(flatten)]
542-
pub identity: IdentityMetadataCreateParams,
543-
}
544-
545-
/**
546-
* Updateable properties of an [`Project`]
547-
*/
548-
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
549-
#[serde(rename_all = "camelCase")]
550-
pub struct ProjectUpdateParams {
551-
#[serde(flatten)]
552-
pub identity: IdentityMetadataUpdateParams,
553-
}
554-
555517
/*
556518
* INSTANCES
557519
*/

omicron-nexus/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ structopt = "0.3"
2020
thiserror = "1.0"
2121
toml = "0.5.6"
2222

23+
[dependencies.api_identity]
24+
path = "../api_identity"
25+
2326
[dependencies.chrono]
2427
version = "0.4"
2528
features = [ "serde" ]

omicron-nexus/src/db/datastore.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ use super::sql_operations::sql_insert_unique;
6262
use super::sql_operations::sql_insert_unique_idempotent_and_fetch;
6363
use super::sql_operations::sql_update_precond;
6464
use crate::db;
65+
use crate::params;
6566

6667
pub struct DataStore {
6768
pool: Arc<Pool>,
@@ -171,7 +172,7 @@ impl DataStore {
171172
pub async fn project_update(
172173
&self,
173174
project_name: &Name,
174-
update_params: &api::external::ProjectUpdateParams,
175+
update_params: &params::ProjectUpdate,
175176
) -> UpdateResult<db::model::Project> {
176177
let client = self.pool.acquire().await?;
177178
let now = Utc::now();

omicron-nexus/src/db/model.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Structures stored to the database.
22
3+
use crate::params;
4+
use crate::views;
35
use chrono::{DateTime, Utc};
46
use omicron_common::api::external::{
57
self, ByteCount, Error, Generation, InstanceCpuCount,
@@ -143,7 +145,7 @@ pub struct Project {
143145

144146
impl Project {
145147
/// Creates a new database Project object.
146-
pub fn new(params: &external::ProjectCreateParams) -> Self {
148+
pub fn new(params: &params::ProjectCreate) -> Self {
147149
let id = Uuid::new_v4();
148150
Self { identity: IdentityMetadata::new(id, params.identity.clone()) }
149151
}
@@ -158,15 +160,15 @@ impl Project {
158160
}
159161

160162
/// Conversion to the internal API type.
161-
impl Into<external::Project> for Project {
162-
fn into(self) -> external::Project {
163-
external::Project { identity: self.identity.into() }
163+
impl Into<views::Project> for Project {
164+
fn into(self) -> views::Project {
165+
views::Project { identity: self.identity.into() }
164166
}
165167
}
166168

167169
/// Conversion from the internal API type.
168-
impl From<external::Project> for Project {
169-
fn from(project: external::Project) -> Self {
170+
impl From<views::Project> for Project {
171+
fn from(project: views::Project) -> Self {
170172
Self { identity: project.identity.into() }
171173
}
172174
}

omicron-nexus/src/http_entrypoints_external.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use super::ServerContext;
66
use crate::db;
77

8+
use crate::params;
9+
use crate::views;
810
use dropshot::endpoint;
911
use dropshot::ApiDescription;
1012
use dropshot::HttpError;
@@ -38,9 +40,6 @@ use omicron_common::api::external::Instance;
3840
use omicron_common::api::external::InstanceCreateParams;
3941
use omicron_common::api::external::Name;
4042
use omicron_common::api::external::PaginationOrder;
41-
use omicron_common::api::external::Project;
42-
use omicron_common::api::external::ProjectCreateParams;
43-
use omicron_common::api::external::ProjectUpdateParams;
4443
use omicron_common::api::external::Rack;
4544
use omicron_common::api::external::Saga;
4645
use omicron_common::api::external::Sled;
@@ -156,7 +155,7 @@ pub fn external_api() -> NexusApiDescription {
156155
async fn projects_get(
157156
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
158157
query_params: Query<PaginatedByNameOrId>,
159-
) -> Result<HttpResponseOk<ResultsPage<Project>>, HttpError> {
158+
) -> Result<HttpResponseOk<ResultsPage<views::Project>>, HttpError> {
160159
let apictx = rqctx.context();
161160
let nexus = &apictx.nexus;
162161
let query = query_params.into_inner();
@@ -176,7 +175,7 @@ async fn projects_get(
176175
};
177176

178177
let view_list =
179-
to_list::<db::model::Project, Project>(project_stream).await;
178+
to_list::<db::model::Project, views::Project>(project_stream).await;
180179
Ok(HttpResponseOk(ScanByNameOrId::results_page(&query, view_list)?))
181180
}
182181

@@ -189,8 +188,8 @@ async fn projects_get(
189188
}]
190189
async fn projects_post(
191190
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
192-
new_project: TypedBody<ProjectCreateParams>,
193-
) -> Result<HttpResponseCreated<Project>, HttpError> {
191+
new_project: TypedBody<params::ProjectCreate>,
192+
) -> Result<HttpResponseCreated<views::Project>, HttpError> {
194193
let apictx = rqctx.context();
195194
let nexus = &apictx.nexus;
196195
let project = nexus.project_create(&new_project.into_inner()).await?;
@@ -216,7 +215,7 @@ struct ProjectPathParam {
216215
async fn projects_get_project(
217216
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
218217
path_params: Path<ProjectPathParam>,
219-
) -> Result<HttpResponseOk<Project>, HttpError> {
218+
) -> Result<HttpResponseOk<views::Project>, HttpError> {
220219
let apictx = rqctx.context();
221220
let nexus = &apictx.nexus;
222221
let path = path_params.into_inner();
@@ -260,8 +259,8 @@ async fn projects_delete_project(
260259
async fn projects_put_project(
261260
rqctx: Arc<RequestContext<Arc<ServerContext>>>,
262261
path_params: Path<ProjectPathParam>,
263-
updated_project: TypedBody<ProjectUpdateParams>,
264-
) -> Result<HttpResponseOk<Project>, HttpError> {
262+
updated_project: TypedBody<params::ProjectUpdate>,
263+
) -> Result<HttpResponseOk<views::Project>, HttpError> {
265264
let apictx = rqctx.context();
266265
let nexus = &apictx.nexus;
267266
let path = path_params.into_inner();

omicron-nexus/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ pub mod db; // Public only for some documentation examples
1818
mod http_entrypoints_external;
1919
mod http_entrypoints_internal;
2020
mod nexus;
21+
mod params;
2122
mod saga_interface;
2223
mod sagas;
24+
mod views;
2325

2426
pub use config::Config;
2527
pub use context::ServerContext;

omicron-nexus/src/nexus.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
use crate::db;
6+
use crate::params;
67
use crate::saga_interface::SagaContext;
78
use crate::sagas;
89
use anyhow::Context;
@@ -27,8 +28,6 @@ use omicron_common::api::external::InstanceState;
2728
use omicron_common::api::external::ListResult;
2829
use omicron_common::api::external::LookupResult;
2930
use omicron_common::api::external::Name;
30-
use omicron_common::api::external::ProjectCreateParams;
31-
use omicron_common::api::external::ProjectUpdateParams;
3231
use omicron_common::api::external::ResourceType;
3332
use omicron_common::api::external::UpdateResult;
3433
use omicron_common::api::external::Vpc;
@@ -295,7 +294,7 @@ impl Nexus {
295294

296295
pub async fn project_create(
297296
&self,
298-
new_project: &ProjectCreateParams,
297+
new_project: &params::ProjectCreate,
299298
) -> CreateResult<db::model::Project> {
300299
// Create a project.
301300
let db_project = db::model::Project::new(new_project);
@@ -356,7 +355,7 @@ impl Nexus {
356355
pub async fn project_update(
357356
&self,
358357
name: &Name,
359-
new_params: &ProjectUpdateParams,
358+
new_params: &params::ProjectUpdate,
360359
) -> UpdateResult<db::model::Project> {
361360
self.db_datastore.project_update(name, new_params).await
362361
}

omicron-nexus/src/params.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use omicron_common::api::external::IdentityMetadataCreateParams;
2+
use omicron_common::api::external::IdentityMetadataUpdateParams;
3+
use schemars::JsonSchema;
4+
use serde::Deserialize;
5+
use serde::Serialize;
6+
7+
/**
8+
* Create-time parameters for an [`Project`]
9+
*/
10+
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
11+
#[serde(rename_all = "camelCase")]
12+
pub struct ProjectCreate {
13+
#[serde(flatten)]
14+
pub identity: IdentityMetadataCreateParams,
15+
}
16+
17+
/**
18+
* Updateable properties of an [`Project`]
19+
*/
20+
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
21+
#[serde(rename_all = "camelCase")]
22+
pub struct ProjectUpdate {
23+
#[serde(flatten)]
24+
pub identity: IdentityMetadataUpdateParams,
25+
}

omicron-nexus/src/views.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use api_identity::ObjectIdentity;
2+
use omicron_common::api::external::IdentityMetadata;
3+
use omicron_common::api::external::ObjectIdentity;
4+
use schemars::JsonSchema;
5+
use serde::Deserialize;
6+
use serde::Serialize;
7+
8+
/**
9+
* Client view of an [`Project`]
10+
*/
11+
#[derive(ObjectIdentity, Clone, Debug, Deserialize, Serialize, JsonSchema)]
12+
#[serde(rename_all = "camelCase")]
13+
pub struct Project {
14+
/*
15+
* TODO-correctness is flattening here (and in all the other types) the
16+
* intent in RFD 4?
17+
*/
18+
#[serde(flatten)]
19+
pub identity: IdentityMetadata,
20+
}

0 commit comments

Comments
 (0)