Skip to content

Commit d59a27c

Browse files
committed
Extract (a subset of) nexus::external_api into new nexus-types crate
1 parent d023a6d commit d59a27c

36 files changed

+463
-368
lines changed

Cargo.lock

+19-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ members = [
1515
"nexus/db-macros",
1616
"nexus/test-utils",
1717
"nexus/test-utils-macros",
18+
"nexus/types",
1819
"nexus-client",
1920
"package",
2021
"rpaths",
@@ -45,6 +46,7 @@ default-members = [
4546
"nexus",
4647
"nexus/authz-macros",
4748
"nexus/db-macros",
49+
"nexus/types",
4850
"package",
4951
"rpaths",
5052
"sled-agent",

nexus/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ usdt = "0.3.1"
6060

6161
authz-macros = { path = "authz-macros" }
6262
db-macros = { path = "db-macros" }
63-
64-
[dependencies.api_identity]
65-
path = "../api_identity"
63+
nexus-types = { path = "types" }
6664

6765
[dependencies.chrono]
6866
version = "0.4"

nexus/db-macros/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ fn build_resource_impl(
339339
self.identity.id
340340
}
341341

342-
fn name(&self) -> &crate::db::model::Name {
343-
&self.identity.name
342+
fn name(&self) -> &::omicron_common::api::external::Name {
343+
&self.identity.name.0
344344
}
345345

346346
fn description(&self) -> &str {

nexus/src/app/sagas/instance_create.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ async fn sic_create_custom_network_interfaces(
326326
//
327327
// This isn't strictly necessary, as the queries would fail below, but it's
328328
// easier to handle here.
329-
if interface_params.iter().any(|p| p.vpc_name != db_vpc.name().0) {
329+
if interface_params.iter().any(|p| &p.vpc_name != db_vpc.name()) {
330330
return Err(ActionError::action_failed(Error::invalid_request(
331331
"All interfaces must be in the same VPC",
332332
)));
@@ -806,7 +806,7 @@ async fn sic_create_instance_record(
806806
.await
807807
.map_err(ActionError::action_failed)?;
808808

809-
Ok(instance.name().clone())
809+
Ok(instance.name().clone().into())
810810
}
811811

812812
async fn sic_delete_instance_record(

nexus/src/app/silo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl super::Nexus {
234234
.ssh_key_name(ssh_key_name)
235235
.fetch()
236236
.await?;
237-
assert_eq!(ssh_key.name(), ssh_key_name);
237+
assert_eq!(ssh_key.name(), &ssh_key_name.0);
238238
Ok(ssh_key)
239239
}
240240

nexus/src/cidata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::Serialize;
66
use std::io::{self, Cursor, Write};
77
use uuid::Uuid;
88

9-
pub const MAX_USER_DATA_BYTES: usize = 32 * 1024; // 32 KiB
9+
pub use nexus_types::external_api::params::MAX_USER_DATA_BYTES;
1010

1111
impl Instance {
1212
pub fn generate_cidata(

nexus/src/db/datastore/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ mod test {
245245
ByteCount, Error, IdentityMetadataCreateParams, LookupType, Name,
246246
};
247247
use omicron_test_utils::dev;
248+
use ref_cast::RefCast;
248249
use std::collections::HashSet;
249250
use std::net::Ipv6Addr;
250251
use std::net::SocketAddrV6;
@@ -285,7 +286,9 @@ mod test {
285286

286287
let (.., organization_after_project_create) =
287288
LookupPath::new(&opctx, &datastore)
288-
.organization_name(organization.name())
289+
.organization_name(db::model::Name::ref_cast(
290+
organization.name(),
291+
))
289292
.fetch()
290293
.await
291294
.unwrap();

nexus/src/db/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ mod update_and_check;
3232
#[cfg(test)]
3333
mod test_utils;
3434

35-
pub mod identity;
3635
pub mod model;
3736
pub mod schema;
3837

@@ -42,3 +41,5 @@ pub use pool::Pool;
4241
pub use saga_recovery::{recover, RecoveryTask};
4342
pub use saga_types::SecId;
4443
pub use sec_store::CockroachDbSecStore;
44+
45+
pub use nexus_types::identity;

nexus/src/db/model/device_auth.rs

+30
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use crate::db::schema::{device_access_token, device_auth_request};
1010

1111
use chrono::{DateTime, Duration, Utc};
12+
use nexus_types::external_api::views;
1213
use rand::{distributions::Slice, rngs::StdRng, Rng, RngCore, SeedableRng};
1314
use uuid::Uuid;
1415

@@ -29,6 +30,26 @@ pub struct DeviceAuthRequest {
2930
pub time_expires: DateTime<Utc>,
3031
}
3132

33+
impl DeviceAuthRequest {
34+
// We need the host to construct absolute verification URIs.
35+
pub fn into_response(self, host: &str) -> views::DeviceAuthResponse {
36+
views::DeviceAuthResponse {
37+
// TODO-security: use HTTPS
38+
verification_uri: format!("http://{}/device/verify", host),
39+
verification_uri_complete: format!(
40+
"http://{}/device/verify?user_code={}",
41+
host, &self.user_code
42+
),
43+
user_code: self.user_code,
44+
device_code: self.device_code,
45+
expires_in: self
46+
.time_expires
47+
.signed_duration_since(self.time_created)
48+
.num_seconds() as u16,
49+
}
50+
}
51+
}
52+
3253
/// Neither the device code nor the access token is meant to be
3354
/// human-readable, so we use 20 random bytes (160 bits), hex-encoded.
3455
const TOKEN_LENGTH: usize = 20;
@@ -135,6 +156,15 @@ impl DeviceAccessToken {
135156
}
136157
}
137158

159+
impl From<DeviceAccessToken> for views::DeviceAccessTokenGrant {
160+
fn from(access_token: DeviceAccessToken) -> Self {
161+
Self {
162+
access_token: format!("oxide-token-{}", access_token.token),
163+
token_type: views::DeviceAccessTokenType::Bearer,
164+
}
165+
}
166+
}
167+
138168
#[cfg(test)]
139169
mod test {
140170
use super::*;

nexus/src/db/model/identity_provider.rs

+33
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5+
use crate::db::identity::Resource;
56
use crate::db::model::impl_enum_type;
67
use crate::db::schema::{identity_provider, saml_identity_provider};
78
use db_macros::Resource;
89

10+
use nexus_types::external_api::views;
911
use serde::{Deserialize, Serialize};
1012
use uuid::Uuid;
1113

@@ -22,6 +24,14 @@ impl_enum_type!(
2224
Saml => b"saml"
2325
);
2426

27+
impl From<IdentityProviderType> for views::IdentityProviderType {
28+
fn from(idp_type: IdentityProviderType) -> Self {
29+
match idp_type {
30+
IdentityProviderType::Saml => views::IdentityProviderType::Saml,
31+
}
32+
}
33+
}
34+
2535
#[derive(Queryable, Insertable, Clone, Debug, Selectable, Resource)]
2636
#[diesel(table_name = identity_provider)]
2737
pub struct IdentityProvider {
@@ -33,6 +43,15 @@ pub struct IdentityProvider {
3343
pub provider_type: IdentityProviderType,
3444
}
3545

46+
impl From<IdentityProvider> for views::IdentityProvider {
47+
fn from(idp: IdentityProvider) -> Self {
48+
Self {
49+
identity: idp.identity(),
50+
provider_type: idp.provider_type.into(),
51+
}
52+
}
53+
}
54+
3655
#[derive(Queryable, Insertable, Clone, Debug, Selectable, Resource)]
3756
#[diesel(table_name = saml_identity_provider)]
3857
pub struct SamlIdentityProvider {
@@ -51,3 +70,17 @@ pub struct SamlIdentityProvider {
5170
pub public_cert: Option<String>,
5271
pub private_key: Option<String>,
5372
}
73+
74+
impl From<SamlIdentityProvider> for views::SamlIdentityProvider {
75+
fn from(saml_idp: SamlIdentityProvider) -> Self {
76+
Self {
77+
identity: saml_idp.identity(),
78+
idp_entity_id: saml_idp.idp_entity_id,
79+
sp_client_id: saml_idp.sp_client_id,
80+
acs_url: saml_idp.acs_url,
81+
slo_url: saml_idp.slo_url,
82+
technical_contact_email: saml_idp.technical_contact_email,
83+
public_cert: saml_idp.public_cert,
84+
}
85+
}
86+
}

nexus/src/db/model/ip_pool.rs

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! Model types for IP Pools and the CIDR blocks therein.
66
77
use crate::db::collection_insert::DatastoreCollection;
8+
use crate::db::identity::Resource;
89
use crate::db::model::Name;
910
use crate::db::schema::ip_pool;
1011
use crate::db::schema::ip_pool_range;
@@ -15,6 +16,7 @@ use chrono::Utc;
1516
use db_macros::Resource;
1617
use diesel::Selectable;
1718
use ipnetwork::IpNetwork;
19+
use nexus_types::external_api::views;
1820
use omicron_common::api::external;
1921
use std::net::IpAddr;
2022
use uuid::Uuid;
@@ -50,6 +52,12 @@ impl IpPool {
5052
}
5153
}
5254

55+
impl From<IpPool> for views::IpPool {
56+
fn from(pool: IpPool) -> Self {
57+
Self { identity: pool.identity(), project_id: pool.project_id }
58+
}
59+
}
60+
5361
/// A set of updates to an IP Pool
5462
#[derive(AsChangeset)]
5563
#[diesel(table_name = ip_pool)]
@@ -120,6 +128,16 @@ impl IpPoolRange {
120128
}
121129
}
122130

131+
impl From<IpPoolRange> for views::IpPoolRange {
132+
fn from(range: IpPoolRange) -> Self {
133+
Self {
134+
id: range.id,
135+
time_created: range.time_created,
136+
range: IpRange::from(&range),
137+
}
138+
}
139+
}
140+
123141
impl From<&IpPoolRange> for IpRange {
124142
fn from(range: &IpPoolRange) -> Self {
125143
let maybe_range =

nexus/src/db/model/organization.rs

+8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use super::{Generation, Name, Project};
66
use crate::db::collection_insert::DatastoreCollection;
7+
use crate::db::identity::Resource;
78
use crate::db::schema::{organization, project};
89
use crate::external_api::params;
910
use chrono::{DateTime, Utc};
1011
use db_macros::Resource;
12+
use nexus_types::external_api::views;
1113
use uuid::Uuid;
1214

1315
/// Describes an organization within the database.
@@ -35,6 +37,12 @@ impl Organization {
3537
}
3638
}
3739

40+
impl From<Organization> for views::Organization {
41+
fn from(org: Organization) -> Self {
42+
Self { identity: org.identity() }
43+
}
44+
}
45+
3846
impl DatastoreCollection<Project> for Organization {
3947
type CollectionId = Uuid;
4048
type GenerationNumberColumn = organization::dsl::rcgen;

nexus/src/db/model/project.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
use super::Name;
6-
use crate::db::schema::project;
6+
use crate::db::{identity::Resource, schema::project};
77
use crate::external_api::params;
88
use chrono::{DateTime, Utc};
99
use db_macros::Resource;
10+
use nexus_types::external_api::views;
1011
use uuid::Uuid;
1112

1213
/// Describes a project within the database.
@@ -29,6 +30,15 @@ impl Project {
2930
}
3031
}
3132

33+
impl From<Project> for views::Project {
34+
fn from(project: Project) -> Self {
35+
Self {
36+
identity: project.identity(),
37+
organization_id: project.organization_id,
38+
}
39+
}
40+
}
41+
3242
/// Describes a set of updates for the [`Project`] model.
3343
#[derive(AsChangeset)]
3444
#[diesel(table_name = project)]

nexus/src/db/model/rack.rs

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use crate::db::schema::rack;
66
use db_macros::Asset;
7+
use nexus_types::external_api::views;
78
use uuid::Uuid;
89

910
/// Information about a local rack.
@@ -26,3 +27,9 @@ impl Rack {
2627
}
2728
}
2829
}
30+
31+
impl From<Rack> for views::Rack {
32+
fn from(rack: Rack) -> Self {
33+
Self { identity: views::AssetIdentityMetadata::from(&rack) }
34+
}
35+
}

0 commit comments

Comments
 (0)