Skip to content

Commit

Permalink
start on DB model
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Aug 30, 2024
1 parent 53b2337 commit b7ca828
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
13 changes: 9 additions & 4 deletions nexus/db-model/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use super::{
ByteCount, Disk, ExternalIp, Generation, InstanceCpuCount, InstanceState,
ByteCount, Disk, ExternalIp, Generation, InstanceAutoRestart,
InstanceCpuCount, InstanceState,
};
use crate::collection::DatastoreAttachTargetConfig;
use crate::schema::{disk, external_ip, instance};
Expand Down Expand Up @@ -54,8 +55,12 @@ pub struct Instance {
#[diesel(column_name = hostname)]
pub hostname: String,

#[diesel(column_name = boot_on_fault)]
pub boot_on_fault: bool,
/// The auto-restart policy for this instance.
///
/// This indicates whether the instance should be automatically restarted by
/// the control plane on failure.
#[diesel(column_name = auto_restart_policy)]
pub auto_restart_policy: InstanceAutoRestart,

#[diesel(embed)]
pub runtime_state: InstanceRuntimeState,
Expand Down Expand Up @@ -104,7 +109,7 @@ impl Instance {
ncpus: params.ncpus.into(),
memory: params.memory.into(),
hostname: params.hostname.to_string(),
boot_on_fault: false,
auto_restart_policy: InstanceAutoRestart::Never,
runtime_state,

updater_gen: Generation::new(),
Expand Down
42 changes: 42 additions & 0 deletions nexus/db-model/src/instance_auto_restart.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 serde::Deserialize;
use serde::Serialize;
use std::fmt;

impl_enum_type!(
#[derive(SqlType, Debug)]
#[diesel(postgres_type(name = "instance_auto_restart", schema = "public"))]
pub struct InstanceAutoRestartEnum;

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

// Enum values
Never => b"never"
AllFailures => b"all_failures"
);

impl InstanceAutoRestart {
pub fn label(&self) -> &'static str {
match self {
InstanceAutoRestart::Never => "never",
InstanceAutoRestart::AllFailures => "all_failures",
}
}
}

impl fmt::Display for InstanceAutoRestart {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.label())
}
}

impl diesel::query_builder::QueryId for InstanceAutoRestart {
type QueryId = ();
const HAS_STATIC_QUERY_ID: bool = false;
}
2 changes: 2 additions & 0 deletions nexus/db-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod generation;
mod identity_provider;
mod image;
mod instance;
mod instance_auto_restart;
mod instance_cpu_count;
mod instance_state;
mod inventory;
Expand Down Expand Up @@ -149,6 +150,7 @@ pub use generation::*;
pub use identity_provider::*;
pub use image::*;
pub use instance::*;
pub use instance_auto_restart::*;
pub use instance_cpu_count::*;
pub use instance_state::*;
pub use inventory::*;
Expand Down
2 changes: 1 addition & 1 deletion nexus/db-model/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ table! {
ncpus -> Int8,
memory -> Int8,
hostname -> Text,
boot_on_fault -> Bool,
auto_restart_policy -> crate::InstanceAutoRestartEnum,
time_state_updated -> Timestamptz,
state_generation -> Int8,
active_propolis_id -> Nullable<Uuid>,
Expand Down

0 comments on commit b7ca828

Please sign in to comment.