Skip to content

Commit

Permalink
[π˜€π—½π—Ώ] initial version
Browse files Browse the repository at this point in the history
Created using spr 1.3.5
  • Loading branch information
sunshowers committed Feb 8, 2024
1 parent a3d9d37 commit c71d28d
Show file tree
Hide file tree
Showing 17 changed files with 689 additions and 152 deletions.
6 changes: 4 additions & 2 deletions nexus/db-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ mod silo_user;
mod silo_user_password_hash;
mod sled;
mod sled_instance;
mod sled_provision_state;
mod sled_policy;
mod sled_resource;
mod sled_resource_kind;
mod sled_state;
mod sled_underlay_subnet_allocation;
mod snapshot;
mod ssh_key;
Expand Down Expand Up @@ -160,9 +161,10 @@ pub use silo_user::*;
pub use silo_user_password_hash::*;
pub use sled::*;
pub use sled_instance::*;
pub use sled_provision_state::*;
pub use sled_policy::to_db_sled_policy; // Do not expose DbSledPolicy
pub use sled_resource::*;
pub use sled_resource_kind::*;
pub use sled_state::*;
pub use sled_underlay_subnet_allocation::*;
pub use snapshot::*;
pub use ssh_key::*;
Expand Down
3 changes: 2 additions & 1 deletion nexus/db-model/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,8 @@ table! {
ip -> Inet,
port -> Int4,
last_used_address -> Inet,
provision_state -> crate::SledProvisionStateEnum,
sled_policy -> crate::sled_policy::SledPolicyEnum,
sled_state -> crate::SledStateEnum,
}
}

Expand Down
35 changes: 27 additions & 8 deletions nexus/db-model/src/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use super::{ByteCount, Generation, SqlU16, SqlU32};
use super::{ByteCount, Generation, SledState, SqlU16, SqlU32};
use crate::collection::DatastoreCollectionConfig;
use crate::ipv6;
use crate::schema::{physical_disk, service, sled, zpool};
use crate::{ipv6, SledProvisionState};
use crate::sled_policy::DbSledPolicy;
use chrono::{DateTime, Utc};
use db_macros::Asset;
use nexus_types::{external_api::shared, external_api::views, identity::Asset};
Expand Down Expand Up @@ -60,7 +61,11 @@ pub struct Sled {
/// The last IP address provided to a propolis instance on this sled
pub last_used_address: ipv6::Ipv6Addr,

provision_state: SledProvisionState,
#[diesel(column_name = sled_policy)]
policy: DbSledPolicy,

#[diesel(column_name = sled_state)]
state: SledState,
}

impl Sled {
Expand All @@ -84,8 +89,15 @@ impl Sled {
&self.serial_number
}

pub fn provision_state(&self) -> SledProvisionState {
self.provision_state
/// The policy here is the `views::SledPolicy` because we expect external
/// users to always use that.
pub fn policy(&self) -> views::SledPolicy {
self.policy.into()
}

/// Returns the sled's state.
pub fn state(&self) -> SledState {
self.state
}
}

Expand All @@ -99,7 +111,9 @@ impl From<Sled> for views::Sled {
part: sled.part_number,
revision: sled.revision,
},
provision_state: sled.provision_state.into(),
policy: sled.policy.into(),
provision_policy: sled.policy.to_provision_policy(),
state: sled.state.into(),
usable_hardware_threads: sled.usable_hardware_threads.0,
usable_physical_ram: *sled.usable_physical_ram,
}
Expand Down Expand Up @@ -197,8 +211,13 @@ impl SledUpdate {
serial_number: self.serial_number,
part_number: self.part_number,
revision: self.revision,
// By default, sleds start as provisionable.
provision_state: SledProvisionState::Provisionable,
// By default, sleds start in-service.
policy: DbSledPolicy::InService,
// By default, sleds start in the "active" state.
//
// XXX In the future there probably needs to be an "uninitialized"
// state here.
state: SledState::Active,
usable_hardware_threads: self.usable_hardware_threads,
usable_physical_ram: self.usable_physical_ram,
reservoir_size: self.reservoir_size,
Expand Down
66 changes: 66 additions & 0 deletions nexus/db-model/src/sled_policy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use super::impl_enum_type;
use nexus_types::external_api::views::{SledPolicy, SledProvisionPolicy};
use serde::{Deserialize, Serialize};

impl_enum_type!(
#[derive(Clone, SqlType, Debug, QueryId)]
#[diesel(postgres_type(name = "sled_policy", schema = "public"))]
pub struct SledPolicyEnum;

/// This type is not actually public, because [`SledPolicy`] has a somewhat
/// different, friendlier shape while being equivalent -- external code
/// should always use [`SledPolicy`].
#[derive(Clone, Copy, Debug, AsExpression, FromSqlRow, Serialize, Deserialize, PartialEq)]
#[diesel(sql_type = SledPolicyEnum)]
pub enum DbSledPolicy;

// Enum values
InService => b"in_service"
InServiceNoProvision => b"in_service_no_provision"
Expunged => b"expunged"
);

/// Converts a [`SledPolicy`] to a version that can be inserted into a
/// database.
pub fn to_db_sled_policy(policy: SledPolicy) -> DbSledPolicy {
match policy {
SledPolicy::InService {
provision_policy: SledProvisionPolicy::Provisionable,
} => DbSledPolicy::InService,
SledPolicy::InService {
provision_policy: SledProvisionPolicy::NonProvisionable,
} => DbSledPolicy::InServiceNoProvision,
SledPolicy::Expunged => DbSledPolicy::Expunged,
}
}

impl DbSledPolicy {
/// Converts self into the appropriate provision policy, in a lossy manner.
pub fn to_provision_policy(self) -> SledProvisionPolicy {
match self {
DbSledPolicy::InService => SledProvisionPolicy::Provisionable,
DbSledPolicy::InServiceNoProvision => {
SledProvisionPolicy::NonProvisionable
}
DbSledPolicy::Expunged => SledProvisionPolicy::NonProvisionable,
}
}
}

impl From<DbSledPolicy> for SledPolicy {
fn from(policy: DbSledPolicy) -> Self {
match policy {
DbSledPolicy::InService => SledPolicy::InService {
provision_policy: SledProvisionPolicy::Provisionable,
},
DbSledPolicy::InServiceNoProvision => SledPolicy::InService {
provision_policy: SledProvisionPolicy::NonProvisionable,
},
DbSledPolicy::Expunged => SledPolicy::Expunged,
}
}
}
53 changes: 0 additions & 53 deletions nexus/db-model/src/sled_provision_state.rs

This file was deleted.

39 changes: 39 additions & 0 deletions nexus/db-model/src/sled_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use super::impl_enum_type;
use nexus_types::external_api::views;
use serde::{Deserialize, Serialize};

impl_enum_type!(
#[derive(Clone, SqlType, Debug, QueryId)]
#[diesel(postgres_type(name = "sled_state", schema = "public"))]
pub struct SledStateEnum;

#[derive(Clone, Copy, Debug, AsExpression, FromSqlRow, Serialize, Deserialize, PartialEq)]
#[diesel(sql_type = SledStateEnum)]
pub enum SledState;

// Enum values
Active => b"active"
Decommissioned => b"decommissioned"
);

impl From<SledState> for views::SledState {
fn from(state: SledState) -> Self {
match state {
SledState::Active => views::SledState::Active,
SledState::Decommissioned => views::SledState::Decommissioned,
}
}
}

impl From<views::SledState> for SledState {
fn from(state: views::SledState) -> Self {
match state {
views::SledState::Active => SledState::Active,
views::SledState::Decommissioned => SledState::Decommissioned,
}
}
}
11 changes: 6 additions & 5 deletions nexus/db-queries/src/db/datastore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ mod test {
use crate::db::model::{
BlockSize, ConsoleSession, Dataset, DatasetKind, ExternalIp,
PhysicalDisk, PhysicalDiskKind, Project, Rack, Region, Service,
ServiceKind, SiloUser, SledBaseboard, SledProvisionState,
SledSystemHardware, SledUpdate, SshKey, VpcSubnet, Zpool,
ServiceKind, SiloUser, SledBaseboard, SledSystemHardware, SledUpdate,
SshKey, VpcSubnet, Zpool,
};
use crate::db::queries::vpc_subnet::FilterConflictingVpcSubnetRangesQuery;
use chrono::{Duration, Utc};
Expand All @@ -411,6 +411,7 @@ mod test {
use nexus_db_model::IpAttachState;
use nexus_test_utils::db::test_setup_database;
use nexus_types::external_api::params;
use nexus_types::external_api::views::SledProvisionPolicy;
use omicron_common::api::external::DataPageParams;
use omicron_common::api::external::{
ByteCount, Error, IdentityMetadataCreateParams, LookupType, Name,
Expand Down Expand Up @@ -652,10 +653,10 @@ mod test {
.unwrap();
println!("sled: {:?}", sled);
let old_state = datastore
.sled_set_provision_state(
.sled_set_provision_policy(
&opctx,
&authz_sled,
SledProvisionState::NonProvisionable,
SledProvisionPolicy::NonProvisionable,
)
.await
.unwrap_or_else(|error| {
Expand All @@ -665,7 +666,7 @@ mod test {
});
// The old state should always be provisionable since that's where we
// start.
assert_eq!(old_state, SledProvisionState::Provisionable);
assert_eq!(old_state, SledProvisionPolicy::Provisionable);
}

fn test_zpool_size() -> ByteCount {
Expand Down
Loading

0 comments on commit c71d28d

Please sign in to comment.