From 56cc8f76d1058bf1018943da30d91951de16da44 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Fri, 6 Aug 2021 15:45:39 -0500 Subject: [PATCH 1/7] views and params concept in nexus with Project as demo --- Cargo.lock | 1 + omicron-common/src/api/external/mod.rs | 38 ------------------- omicron-nexus/Cargo.toml | 3 ++ omicron-nexus/src/db/datastore.rs | 3 +- omicron-nexus/src/db/model.rs | 17 ++------- .../src/http_entrypoints_external.rs | 19 +++++----- omicron-nexus/src/lib.rs | 2 + omicron-nexus/src/nexus.rs | 7 ++-- omicron-nexus/src/params.rs | 25 ++++++++++++ omicron-nexus/src/views.rs | 27 +++++++++++++ 10 files changed, 75 insertions(+), 67 deletions(-) create mode 100644 omicron-nexus/src/params.rs create mode 100644 omicron-nexus/src/views.rs diff --git a/Cargo.lock b/Cargo.lock index 4d8b4d14f7..fbd795f09c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1257,6 +1257,7 @@ name = "omicron-nexus" version = "0.1.0" dependencies = [ "anyhow", + "api_identity", "async-trait", "bb8", "bb8-postgres", diff --git a/omicron-common/src/api/external/mod.rs b/omicron-common/src/api/external/mod.rs index 3d90d6399a..1593298b4f 100644 --- a/omicron-common/src/api/external/mod.rs +++ b/omicron-common/src/api/external/mod.rs @@ -514,44 +514,6 @@ pub struct IdentityMetadataUpdateParams { * Specific API resources */ -/* - * PROJECTS - */ - -/** - * Client view of an [`Project`] - */ -#[derive(ObjectIdentity, Clone, Debug, Deserialize, Serialize, JsonSchema)] -#[serde(rename_all = "camelCase")] -pub struct Project { - /* - * TODO-correctness is flattening here (and in all the other types) the - * intent in RFD 4? - */ - #[serde(flatten)] - pub identity: IdentityMetadata, -} - -/** - * Create-time parameters for an [`Project`] - */ -#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] -#[serde(rename_all = "camelCase")] -pub struct ProjectCreateParams { - #[serde(flatten)] - pub identity: IdentityMetadataCreateParams, -} - -/** - * Updateable properties of an [`Project`] - */ -#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] -#[serde(rename_all = "camelCase")] -pub struct ProjectUpdateParams { - #[serde(flatten)] - pub identity: IdentityMetadataUpdateParams, -} - /* * INSTANCES */ diff --git a/omicron-nexus/Cargo.toml b/omicron-nexus/Cargo.toml index 1b604ae329..5ddbc97388 100644 --- a/omicron-nexus/Cargo.toml +++ b/omicron-nexus/Cargo.toml @@ -20,6 +20,9 @@ structopt = "0.3" thiserror = "1.0" toml = "0.5.6" +[dependencies.api_identity] +path = "../api_identity" + [dependencies.chrono] version = "0.4" features = [ "serde" ] diff --git a/omicron-nexus/src/db/datastore.rs b/omicron-nexus/src/db/datastore.rs index fff42a4809..1e85050a2b 100644 --- a/omicron-nexus/src/db/datastore.rs +++ b/omicron-nexus/src/db/datastore.rs @@ -62,6 +62,7 @@ use super::sql_operations::sql_insert_unique; use super::sql_operations::sql_insert_unique_idempotent_and_fetch; use super::sql_operations::sql_update_precond; use crate::db; +use crate::params; pub struct DataStore { pool: Arc, @@ -171,7 +172,7 @@ impl DataStore { pub async fn project_update( &self, project_name: &Name, - update_params: &api::external::ProjectUpdateParams, + update_params: ¶ms::ProjectUpdate, ) -> UpdateResult { let client = self.pool.acquire().await?; let now = Utc::now(); diff --git a/omicron-nexus/src/db/model.rs b/omicron-nexus/src/db/model.rs index b80e96cd75..2f5d1478ad 100644 --- a/omicron-nexus/src/db/model.rs +++ b/omicron-nexus/src/db/model.rs @@ -1,5 +1,6 @@ //! Structures stored to the database. +use crate::params; use chrono::{DateTime, Utc}; use omicron_common::api::external::{ self, ByteCount, Error, Generation, InstanceCpuCount, @@ -138,12 +139,12 @@ impl TryFrom<&tokio_postgres::Row> for IdentityMetadata { /// Describes a project within the database. pub struct Project { - identity: IdentityMetadata, + pub identity: IdentityMetadata, } impl Project { /// Creates a new database Project object. - pub fn new(params: &external::ProjectCreateParams) -> Self { + pub fn new(params: ¶ms::ProjectCreate) -> Self { let id = Uuid::new_v4(); Self { identity: IdentityMetadata::new(id, params.identity.clone()) } } @@ -157,19 +158,7 @@ impl Project { } } -/// Conversion to the internal API type. -impl Into for Project { - fn into(self) -> external::Project { - external::Project { identity: self.identity.into() } - } -} - /// Conversion from the internal API type. -impl From for Project { - fn from(project: external::Project) -> Self { - Self { identity: project.identity.into() } - } -} /// Serialization to DB. impl SqlSerialize for Project { diff --git a/omicron-nexus/src/http_entrypoints_external.rs b/omicron-nexus/src/http_entrypoints_external.rs index cebc3c1e38..f35b0839fb 100644 --- a/omicron-nexus/src/http_entrypoints_external.rs +++ b/omicron-nexus/src/http_entrypoints_external.rs @@ -5,6 +5,8 @@ use super::ServerContext; use crate::db; +use crate::params; +use crate::views; use dropshot::endpoint; use dropshot::ApiDescription; use dropshot::HttpError; @@ -38,9 +40,6 @@ use omicron_common::api::external::Instance; use omicron_common::api::external::InstanceCreateParams; use omicron_common::api::external::Name; use omicron_common::api::external::PaginationOrder; -use omicron_common::api::external::Project; -use omicron_common::api::external::ProjectCreateParams; -use omicron_common::api::external::ProjectUpdateParams; use omicron_common::api::external::Rack; use omicron_common::api::external::Saga; use omicron_common::api::external::Sled; @@ -156,7 +155,7 @@ pub fn external_api() -> NexusApiDescription { async fn projects_get( rqctx: Arc>>, query_params: Query, -) -> Result>, HttpError> { +) -> Result>, HttpError> { let apictx = rqctx.context(); let nexus = &apictx.nexus; let query = query_params.into_inner(); @@ -176,7 +175,7 @@ async fn projects_get( }; let view_list = - to_list::(project_stream).await; + to_list::(project_stream).await; Ok(HttpResponseOk(ScanByNameOrId::results_page(&query, view_list)?)) } @@ -189,8 +188,8 @@ async fn projects_get( }] async fn projects_post( rqctx: Arc>>, - new_project: TypedBody, -) -> Result, HttpError> { + new_project: TypedBody, +) -> Result, HttpError> { let apictx = rqctx.context(); let nexus = &apictx.nexus; let project = nexus.project_create(&new_project.into_inner()).await?; @@ -216,7 +215,7 @@ struct ProjectPathParam { async fn projects_get_project( rqctx: Arc>>, path_params: Path, -) -> Result, HttpError> { +) -> Result, HttpError> { let apictx = rqctx.context(); let nexus = &apictx.nexus; let path = path_params.into_inner(); @@ -260,8 +259,8 @@ async fn projects_delete_project( async fn projects_put_project( rqctx: Arc>>, path_params: Path, - updated_project: TypedBody, -) -> Result, HttpError> { + updated_project: TypedBody, +) -> Result, HttpError> { let apictx = rqctx.context(); let nexus = &apictx.nexus; let path = path_params.into_inner(); diff --git a/omicron-nexus/src/lib.rs b/omicron-nexus/src/lib.rs index 238953f285..b07b836571 100644 --- a/omicron-nexus/src/lib.rs +++ b/omicron-nexus/src/lib.rs @@ -18,8 +18,10 @@ pub mod db; // Public only for some documentation examples mod http_entrypoints_external; mod http_entrypoints_internal; mod nexus; +mod params; mod saga_interface; mod sagas; +mod views; pub use config::Config; pub use context::ServerContext; diff --git a/omicron-nexus/src/nexus.rs b/omicron-nexus/src/nexus.rs index 70e0c406f2..d8056d888a 100644 --- a/omicron-nexus/src/nexus.rs +++ b/omicron-nexus/src/nexus.rs @@ -3,6 +3,7 @@ */ use crate::db; +use crate::params; use crate::saga_interface::SagaContext; use crate::sagas; use anyhow::Context; @@ -27,8 +28,6 @@ use omicron_common::api::external::InstanceState; use omicron_common::api::external::ListResult; use omicron_common::api::external::LookupResult; use omicron_common::api::external::Name; -use omicron_common::api::external::ProjectCreateParams; -use omicron_common::api::external::ProjectUpdateParams; use omicron_common::api::external::ResourceType; use omicron_common::api::external::UpdateResult; use omicron_common::api::external::Vpc; @@ -295,7 +294,7 @@ impl Nexus { pub async fn project_create( &self, - new_project: &ProjectCreateParams, + new_project: ¶ms::ProjectCreate, ) -> CreateResult { // Create a project. let db_project = db::model::Project::new(new_project); @@ -356,7 +355,7 @@ impl Nexus { pub async fn project_update( &self, name: &Name, - new_params: &ProjectUpdateParams, + new_params: ¶ms::ProjectUpdate, ) -> UpdateResult { self.db_datastore.project_update(name, new_params).await } diff --git a/omicron-nexus/src/params.rs b/omicron-nexus/src/params.rs new file mode 100644 index 0000000000..ed100898e8 --- /dev/null +++ b/omicron-nexus/src/params.rs @@ -0,0 +1,25 @@ +use omicron_common::api::external::IdentityMetadataCreateParams; +use omicron_common::api::external::IdentityMetadataUpdateParams; +use schemars::JsonSchema; +use serde::Deserialize; +use serde::Serialize; + +/** + * Create-time parameters for an [`Project`] + */ +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct ProjectCreate { + #[serde(flatten)] + pub identity: IdentityMetadataCreateParams, +} + +/** + * Updateable properties of an [`Project`] + */ +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct ProjectUpdate { + #[serde(flatten)] + pub identity: IdentityMetadataUpdateParams, +} diff --git a/omicron-nexus/src/views.rs b/omicron-nexus/src/views.rs new file mode 100644 index 0000000000..ef0a877fa2 --- /dev/null +++ b/omicron-nexus/src/views.rs @@ -0,0 +1,27 @@ +use crate::db; +use api_identity::ObjectIdentity; +use omicron_common::api::external::IdentityMetadata; +use omicron_common::api::external::ObjectIdentity; +use schemars::JsonSchema; +use serde::Deserialize; +use serde::Serialize; + +/** + * Client view of an [`Project`] + */ +#[derive(ObjectIdentity, Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(rename_all = "camelCase")] +pub struct Project { + /* + * TODO-correctness is flattening here (and in all the other types) the + * intent in RFD 4? + */ + #[serde(flatten)] + pub identity: IdentityMetadata, +} + +impl From for Project { + fn from(project: db::model::Project) -> Self { + Self { identity: project.identity.into() } + } +} From c23b32a7f2e7b4b0e7809504a79590749339f79d Mon Sep 17 00:00:00 2001 From: David Crespo Date: Fri, 6 Aug 2021 16:07:01 -0500 Subject: [PATCH 2/7] optional second step: db::model also doesn't know about params --- omicron-nexus/src/db/model.rs | 12 ++++-------- omicron-nexus/src/nexus.rs | 2 +- omicron-nexus/src/params.rs | 11 +++++++++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/omicron-nexus/src/db/model.rs b/omicron-nexus/src/db/model.rs index 2f5d1478ad..c773d1383a 100644 --- a/omicron-nexus/src/db/model.rs +++ b/omicron-nexus/src/db/model.rs @@ -1,6 +1,5 @@ //! Structures stored to the database. -use crate::params; use chrono::{DateTime, Utc}; use omicron_common::api::external::{ self, ByteCount, Error, Generation, InstanceCpuCount, @@ -59,7 +58,10 @@ pub struct IdentityMetadata { } impl IdentityMetadata { - fn new(id: Uuid, params: external::IdentityMetadataCreateParams) -> Self { + pub fn new( + id: Uuid, + params: external::IdentityMetadataCreateParams, + ) -> Self { let now = Utc::now(); Self { id, @@ -143,12 +145,6 @@ pub struct Project { } impl Project { - /// Creates a new database Project object. - pub fn new(params: ¶ms::ProjectCreate) -> Self { - let id = Uuid::new_v4(); - Self { identity: IdentityMetadata::new(id, params.identity.clone()) } - } - pub fn name(&self) -> &str { self.identity.name.as_str() } diff --git a/omicron-nexus/src/nexus.rs b/omicron-nexus/src/nexus.rs index d8056d888a..2f8f1ff0ae 100644 --- a/omicron-nexus/src/nexus.rs +++ b/omicron-nexus/src/nexus.rs @@ -297,7 +297,7 @@ impl Nexus { new_project: ¶ms::ProjectCreate, ) -> CreateResult { // Create a project. - let db_project = db::model::Project::new(new_project); + let db_project: db::model::Project = new_project.into(); let project: db::model::Project = self.db_datastore.project_create(&db_project).await?; // TODO: We probably want to have "project creation" and "default VPC diff --git a/omicron-nexus/src/params.rs b/omicron-nexus/src/params.rs index ed100898e8..75ca8e0f0f 100644 --- a/omicron-nexus/src/params.rs +++ b/omicron-nexus/src/params.rs @@ -1,8 +1,10 @@ +use crate::db::model; use omicron_common::api::external::IdentityMetadataCreateParams; use omicron_common::api::external::IdentityMetadataUpdateParams; use schemars::JsonSchema; use serde::Deserialize; use serde::Serialize; +use uuid::Uuid; /** * Create-time parameters for an [`Project`] @@ -14,6 +16,15 @@ pub struct ProjectCreate { pub identity: IdentityMetadataCreateParams, } +impl From<&ProjectCreate> for model::Project { + fn from(params: &ProjectCreate) -> Self { + let id = Uuid::new_v4(); + Self { + identity: model::IdentityMetadata::new(id, params.identity.clone()), + } + } +} + /** * Updateable properties of an [`Project`] */ From 6734208c7582c9157bc27063b3cbfc51e02398af Mon Sep 17 00:00:00 2001 From: David Crespo Date: Fri, 6 Aug 2021 16:14:01 -0500 Subject: [PATCH 3/7] ok fine make the tests pass --- omicron-nexus/src/lib.rs | 4 ++-- omicron-nexus/tests/test_basic.rs | 19 +++++++++---------- omicron-nexus/tests/test_disks.rs | 6 +++--- omicron-nexus/tests/test_instances.rs | 6 +++--- omicron-nexus/tests/test_vpcs.rs | 6 +++--- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/omicron-nexus/src/lib.rs b/omicron-nexus/src/lib.rs index b07b836571..1a6f3e257d 100644 --- a/omicron-nexus/src/lib.rs +++ b/omicron-nexus/src/lib.rs @@ -18,10 +18,10 @@ pub mod db; // Public only for some documentation examples mod http_entrypoints_external; mod http_entrypoints_internal; mod nexus; -mod params; +pub mod params; mod saga_interface; mod sagas; -mod views; +pub mod views; pub use config::Config; pub use context::ServerContext; diff --git a/omicron-nexus/tests/test_basic.rs b/omicron-nexus/tests/test_basic.rs index 03a6744370..b1f6310177 100644 --- a/omicron-nexus/tests/test_basic.rs +++ b/omicron-nexus/tests/test_basic.rs @@ -16,10 +16,9 @@ use http::StatusCode; use omicron_common::api::external::IdentityMetadataCreateParams; use omicron_common::api::external::IdentityMetadataUpdateParams; use omicron_common::api::external::Name; -use omicron_common::api::external::Project; -use omicron_common::api::external::ProjectCreateParams; -use omicron_common::api::external::ProjectUpdateParams; use omicron_common::api::external::Sled; +use omicron_nexus::params; +use omicron_nexus::views::Project; use std::convert::TryFrom; use uuid::Uuid; @@ -184,7 +183,7 @@ async fn test_projects() { let new_project_ids = { let mut project_ids: Vec = Vec::new(); for project_name in projects_to_create { - let project_create = ProjectCreateParams { + let project_create = params::ProjectCreate { identity: IdentityMetadataCreateParams { name: Name::try_from(project_name.to_string()).unwrap(), description: String::from(""), @@ -278,7 +277,7 @@ async fn test_projects() { .make_request_error_body( Method::PUT, "/projects/simproject2", - ProjectUpdateParams { + params::ProjectUpdate { identity: IdentityMetadataUpdateParams { name: None, description: None, @@ -320,7 +319,7 @@ async fn test_projects() { * Update "simproject3". We'll make sure that's reflected in the other * requests. */ - let project_update = ProjectUpdateParams { + let project_update = params::ProjectUpdate { identity: IdentityMetadataUpdateParams { name: None, description: Some("Li'l lightnin'".to_string()), @@ -351,7 +350,7 @@ async fn test_projects() { * operation under the hood. This case also exercises changes to multiple * fields in one request. */ - let project_update = ProjectUpdateParams { + let project_update = params::ProjectUpdate { identity: IdentityMetadataUpdateParams { name: Some(Name::try_from("lil-lightnin").unwrap()), description: Some("little lightning".to_string()), @@ -382,7 +381,7 @@ async fn test_projects() { /* * Try to create a project with a name that conflicts with an existing one. */ - let project_create = ProjectCreateParams { + let project_create = params::ProjectCreate { identity: IdentityMetadataCreateParams { name: Name::try_from("simproject1".to_string()).unwrap(), description: "a duplicate of simproject1".to_string(), @@ -420,7 +419,7 @@ async fn test_projects() { /* * Now, really do create another project. */ - let project_create = ProjectCreateParams { + let project_create = params::ProjectCreate { identity: IdentityMetadataCreateParams { name: Name::try_from("honor-roller").unwrap(), description: "a soapbox racer".to_string(), @@ -471,7 +470,7 @@ async fn test_projects_list() { */ let mut name = Uuid::new_v4().to_string(); name.replace_range(0..1, "a"); - let create_params = ProjectCreateParams { + let create_params = params::ProjectCreate { identity: IdentityMetadataCreateParams { name: Name::try_from(name).unwrap(), description: String::from("test suite project"), diff --git a/omicron-nexus/tests/test_disks.rs b/omicron-nexus/tests/test_disks.rs index a31e9dc406..5773afce37 100644 --- a/omicron-nexus/tests/test_disks.rs +++ b/omicron-nexus/tests/test_disks.rs @@ -14,9 +14,9 @@ use omicron_common::api::external::Instance; use omicron_common::api::external::InstanceCpuCount; use omicron_common::api::external::InstanceCreateParams; use omicron_common::api::external::Name; -use omicron_common::api::external::Project; -use omicron_common::api::external::ProjectCreateParams; use omicron_common::SledAgentTestInterfaces as _; +use omicron_nexus::params; +use omicron_nexus::views::Project; use omicron_nexus::Nexus; use omicron_nexus::TestInterfaces as _; use std::convert::TryFrom; @@ -53,7 +53,7 @@ async fn test_disks() { let project: Project = objects_post( &client, "/projects", - ProjectCreateParams { + params::ProjectCreate { identity: IdentityMetadataCreateParams { name: Name::try_from(project_name).unwrap(), description: "a pier".to_string(), diff --git a/omicron-nexus/tests/test_instances.rs b/omicron-nexus/tests/test_instances.rs index c74366bd53..7001b362d6 100644 --- a/omicron-nexus/tests/test_instances.rs +++ b/omicron-nexus/tests/test_instances.rs @@ -11,9 +11,9 @@ use omicron_common::api::external::InstanceCpuCount; use omicron_common::api::external::InstanceCreateParams; use omicron_common::api::external::InstanceState; use omicron_common::api::external::Name; -use omicron_common::api::external::Project; -use omicron_common::api::external::ProjectCreateParams; use omicron_common::SledAgentTestInterfaces as _; +use omicron_nexus::params; +use omicron_nexus::views::Project; use omicron_nexus::Nexus; use omicron_nexus::TestInterfaces as _; use std::convert::TryFrom; @@ -46,7 +46,7 @@ async fn test_instances() { let _: Project = objects_post( &client, "/projects", - ProjectCreateParams { + params::ProjectCreate { identity: IdentityMetadataCreateParams { name: Name::try_from(project_name).unwrap(), description: "a pier".to_string(), diff --git a/omicron-nexus/tests/test_vpcs.rs b/omicron-nexus/tests/test_vpcs.rs index 97a950867f..4ffe214a66 100644 --- a/omicron-nexus/tests/test_vpcs.rs +++ b/omicron-nexus/tests/test_vpcs.rs @@ -3,11 +3,11 @@ use http::StatusCode; use omicron_common::api::external::IdentityMetadataCreateParams; use omicron_common::api::external::IdentityMetadataUpdateParams; use omicron_common::api::external::Name; -use omicron_common::api::external::Project; -use omicron_common::api::external::ProjectCreateParams; use omicron_common::api::external::Vpc; use omicron_common::api::external::VpcCreateParams; use omicron_common::api::external::VpcUpdateParams; +use omicron_nexus::params; +use omicron_nexus::views::Project; use std::convert::TryFrom; use dropshot::test_util::object_get; @@ -170,7 +170,7 @@ async fn create_project( objects_post( &client, "/projects", - ProjectCreateParams { + params::ProjectCreate { identity: IdentityMetadataCreateParams { name: Name::try_from(project_name).unwrap(), description: "a pier".to_string(), From 38ca2001b005fa47e06c7ae80561506687003de4 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Fri, 6 Aug 2021 16:16:49 -0500 Subject: [PATCH 4/7] update openapi spec --- omicron-nexus/tests/output/nexus-openapi.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/omicron-nexus/tests/output/nexus-openapi.json b/omicron-nexus/tests/output/nexus-openapi.json index 099b3c14cf..7da1684e73 100644 --- a/omicron-nexus/tests/output/nexus-openapi.json +++ b/omicron-nexus/tests/output/nexus-openapi.json @@ -225,7 +225,7 @@ "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/ProjectCreateParams" + "$ref": "#/components/schemas/ProjectCreate" } } }, @@ -291,7 +291,7 @@ "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/ProjectUpdateParams" + "$ref": "#/components/schemas/ProjectUpdate" } } }, @@ -1729,7 +1729,7 @@ "timeModified" ] }, - "ProjectCreateParams": { + "ProjectCreate": { "description": "Create-time parameters for an [`Project`]", "type": "object", "properties": { @@ -1765,7 +1765,7 @@ "items" ] }, - "ProjectUpdateParams": { + "ProjectUpdate": { "description": "Updateable properties of an [`Project`]", "type": "object", "properties": { From c8bc2283fcc8fbedee2f00dae217fbf6d43d33c5 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Fri, 6 Aug 2021 16:46:48 -0500 Subject: [PATCH 5/7] inline into() call --- omicron-nexus/src/nexus.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/omicron-nexus/src/nexus.rs b/omicron-nexus/src/nexus.rs index 2f8f1ff0ae..c47f3062a9 100644 --- a/omicron-nexus/src/nexus.rs +++ b/omicron-nexus/src/nexus.rs @@ -294,12 +294,11 @@ impl Nexus { pub async fn project_create( &self, - new_project: ¶ms::ProjectCreate, + params: ¶ms::ProjectCreate, ) -> CreateResult { // Create a project. - let db_project: db::model::Project = new_project.into(); let project: db::model::Project = - self.db_datastore.project_create(&db_project).await?; + self.db_datastore.project_create(¶ms.into()).await?; // TODO: We probably want to have "project creation" and "default VPC // creation" co-located within a saga for atomicity. // @@ -355,9 +354,9 @@ impl Nexus { pub async fn project_update( &self, name: &Name, - new_params: ¶ms::ProjectUpdate, + params: ¶ms::ProjectUpdate, ) -> UpdateResult { - self.db_datastore.project_update(name, new_params).await + self.db_datastore.project_update(name, params).await } /* From 4a1267b299cc9376982967cbe3053c800070c642 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Fri, 6 Aug 2021 19:15:52 -0500 Subject: [PATCH 6/7] comment explaining views --- omicron-nexus/src/views.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/omicron-nexus/src/views.rs b/omicron-nexus/src/views.rs index ef0a877fa2..99df75a69b 100644 --- a/omicron-nexus/src/views.rs +++ b/omicron-nexus/src/views.rs @@ -1,3 +1,9 @@ +/*! + * Views are the types of API Responses. Mostly they will be straightforward + * transformations of models (which can use From), but some may be composites + * of more than one model and therefore require a special constructor. + */ + use crate::db; use api_identity::ObjectIdentity; use omicron_common::api::external::IdentityMetadata; From 38776756792079f0187c2f93c470917cecbdc009 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Fri, 6 Aug 2021 19:27:25 -0500 Subject: [PATCH 7/7] try a weird way of bringing external/internal into nexus --- omicron-nexus/src/db/datastore.rs | 2 +- .../http_entrypoints.rs} | 6 +++--- omicron-nexus/src/external/mod.rs | 3 +++ omicron-nexus/src/{ => external}/params.rs | 0 omicron-nexus/src/{ => external}/views.rs | 0 .../http_entrypoints.rs} | 2 +- omicron-nexus/src/internal/mod.rs | 1 + omicron-nexus/src/lib.rs | 10 ++++------ omicron-nexus/src/nexus.rs | 2 +- omicron-nexus/tests/test_basic.rs | 4 ++-- omicron-nexus/tests/test_disks.rs | 4 ++-- omicron-nexus/tests/test_instances.rs | 4 ++-- omicron-nexus/tests/test_vpcs.rs | 4 ++-- 13 files changed, 22 insertions(+), 20 deletions(-) rename omicron-nexus/src/{http_entrypoints_external.rs => external/http_entrypoints.rs} (99%) create mode 100644 omicron-nexus/src/external/mod.rs rename omicron-nexus/src/{ => external}/params.rs (100%) rename omicron-nexus/src/{ => external}/views.rs (100%) rename omicron-nexus/src/{http_entrypoints_internal.rs => internal/http_entrypoints.rs} (99%) create mode 100644 omicron-nexus/src/internal/mod.rs diff --git a/omicron-nexus/src/db/datastore.rs b/omicron-nexus/src/db/datastore.rs index 1e85050a2b..812ca805ea 100644 --- a/omicron-nexus/src/db/datastore.rs +++ b/omicron-nexus/src/db/datastore.rs @@ -62,7 +62,7 @@ use super::sql_operations::sql_insert_unique; use super::sql_operations::sql_insert_unique_idempotent_and_fetch; use super::sql_operations::sql_update_precond; use crate::db; -use crate::params; +use crate::external::params; pub struct DataStore { pool: Arc, diff --git a/omicron-nexus/src/http_entrypoints_external.rs b/omicron-nexus/src/external/http_entrypoints.rs similarity index 99% rename from omicron-nexus/src/http_entrypoints_external.rs rename to omicron-nexus/src/external/http_entrypoints.rs index f35b0839fb..cb89976b3c 100644 --- a/omicron-nexus/src/http_entrypoints_external.rs +++ b/omicron-nexus/src/external/http_entrypoints.rs @@ -2,11 +2,11 @@ * Handler functions (entrypoints) for external HTTP APIs */ -use super::ServerContext; +use crate::context::ServerContext; use crate::db; -use crate::params; -use crate::views; +use super::params; +use super::views; use dropshot::endpoint; use dropshot::ApiDescription; use dropshot::HttpError; diff --git a/omicron-nexus/src/external/mod.rs b/omicron-nexus/src/external/mod.rs new file mode 100644 index 0000000000..df2e9092b8 --- /dev/null +++ b/omicron-nexus/src/external/mod.rs @@ -0,0 +1,3 @@ +pub mod http_entrypoints; +pub mod params; +pub mod views; diff --git a/omicron-nexus/src/params.rs b/omicron-nexus/src/external/params.rs similarity index 100% rename from omicron-nexus/src/params.rs rename to omicron-nexus/src/external/params.rs diff --git a/omicron-nexus/src/views.rs b/omicron-nexus/src/external/views.rs similarity index 100% rename from omicron-nexus/src/views.rs rename to omicron-nexus/src/external/views.rs diff --git a/omicron-nexus/src/http_entrypoints_internal.rs b/omicron-nexus/src/internal/http_entrypoints.rs similarity index 99% rename from omicron-nexus/src/http_entrypoints_internal.rs rename to omicron-nexus/src/internal/http_entrypoints.rs index 9123b42ca0..31fdcf0232 100644 --- a/omicron-nexus/src/http_entrypoints_internal.rs +++ b/omicron-nexus/src/internal/http_entrypoints.rs @@ -1,7 +1,7 @@ /** * Handler functions (entrypoints) for HTTP APIs internal to the control plane */ -use super::ServerContext; +use super::super::ServerContext; use dropshot::endpoint; use dropshot::ApiDescription; diff --git a/omicron-nexus/src/internal/mod.rs b/omicron-nexus/src/internal/mod.rs new file mode 100644 index 0000000000..7f08fd4ac5 --- /dev/null +++ b/omicron-nexus/src/internal/mod.rs @@ -0,0 +1 @@ +pub mod http_entrypoints; diff --git a/omicron-nexus/src/lib.rs b/omicron-nexus/src/lib.rs index 1a6f3e257d..be0d2b1fee 100644 --- a/omicron-nexus/src/lib.rs +++ b/omicron-nexus/src/lib.rs @@ -15,21 +15,19 @@ mod config; mod context; pub mod db; // Public only for some documentation examples -mod http_entrypoints_external; -mod http_entrypoints_internal; +pub mod external; +mod internal; mod nexus; -pub mod params; mod saga_interface; mod sagas; -pub mod views; pub use config::Config; pub use context::ServerContext; pub use nexus::Nexus; pub use nexus::TestInterfaces; -use http_entrypoints_external::external_api; -use http_entrypoints_internal::internal_api; +use external::http_entrypoints::external_api; +use internal::http_entrypoints::internal_api; use slog::Logger; use std::sync::Arc; diff --git a/omicron-nexus/src/nexus.rs b/omicron-nexus/src/nexus.rs index c47f3062a9..30b3ff479d 100644 --- a/omicron-nexus/src/nexus.rs +++ b/omicron-nexus/src/nexus.rs @@ -3,7 +3,7 @@ */ use crate::db; -use crate::params; +use crate::external::params; use crate::saga_interface::SagaContext; use crate::sagas; use anyhow::Context; diff --git a/omicron-nexus/tests/test_basic.rs b/omicron-nexus/tests/test_basic.rs index b1f6310177..e5989a1a72 100644 --- a/omicron-nexus/tests/test_basic.rs +++ b/omicron-nexus/tests/test_basic.rs @@ -17,8 +17,8 @@ use omicron_common::api::external::IdentityMetadataCreateParams; use omicron_common::api::external::IdentityMetadataUpdateParams; use omicron_common::api::external::Name; use omicron_common::api::external::Sled; -use omicron_nexus::params; -use omicron_nexus::views::Project; +use omicron_nexus::external::params; +use omicron_nexus::external::views::Project; use std::convert::TryFrom; use uuid::Uuid; diff --git a/omicron-nexus/tests/test_disks.rs b/omicron-nexus/tests/test_disks.rs index 5773afce37..04953003f1 100644 --- a/omicron-nexus/tests/test_disks.rs +++ b/omicron-nexus/tests/test_disks.rs @@ -15,8 +15,8 @@ use omicron_common::api::external::InstanceCpuCount; use omicron_common::api::external::InstanceCreateParams; use omicron_common::api::external::Name; use omicron_common::SledAgentTestInterfaces as _; -use omicron_nexus::params; -use omicron_nexus::views::Project; +use omicron_nexus::external::params; +use omicron_nexus::external::views::Project; use omicron_nexus::Nexus; use omicron_nexus::TestInterfaces as _; use std::convert::TryFrom; diff --git a/omicron-nexus/tests/test_instances.rs b/omicron-nexus/tests/test_instances.rs index 7001b362d6..dd977d2810 100644 --- a/omicron-nexus/tests/test_instances.rs +++ b/omicron-nexus/tests/test_instances.rs @@ -12,8 +12,8 @@ use omicron_common::api::external::InstanceCreateParams; use omicron_common::api::external::InstanceState; use omicron_common::api::external::Name; use omicron_common::SledAgentTestInterfaces as _; -use omicron_nexus::params; -use omicron_nexus::views::Project; +use omicron_nexus::external::params; +use omicron_nexus::external::views::Project; use omicron_nexus::Nexus; use omicron_nexus::TestInterfaces as _; use std::convert::TryFrom; diff --git a/omicron-nexus/tests/test_vpcs.rs b/omicron-nexus/tests/test_vpcs.rs index 4ffe214a66..4c3df2fae8 100644 --- a/omicron-nexus/tests/test_vpcs.rs +++ b/omicron-nexus/tests/test_vpcs.rs @@ -6,8 +6,8 @@ use omicron_common::api::external::Name; use omicron_common::api::external::Vpc; use omicron_common::api::external::VpcCreateParams; use omicron_common::api::external::VpcUpdateParams; -use omicron_nexus::params; -use omicron_nexus::views::Project; +use omicron_nexus::external::params; +use omicron_nexus::external::views::Project; use std::convert::TryFrom; use dropshot::test_util::object_get;