Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 0 additions & 69 deletions omicron-common/src/collection.rs

This file was deleted.

1 change: 0 additions & 1 deletion omicron-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
pub mod api;
pub mod backoff;
pub mod cmd;
pub mod collection;
pub mod config;
pub mod dev;
pub mod http_client;
Expand Down
29 changes: 16 additions & 13 deletions omicron-common/src/sql/dbinit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ CREATE DATABASE omicron;
CREATE USER omicron;
GRANT INSERT, SELECT, UPDATE, DELETE ON DATABASE omicron to omicron;

/*
* Sleds
*/

CREATE TABLE omicron.public.Sled (
/* Identity metadata */
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,

ip INET NOT NULL,
port INT4 NOT NULL
);

/*
* Projects
*/
Expand Down Expand Up @@ -197,19 +213,6 @@ CREATE INDEX ON omicron.public.Disk (
time_deleted IS NULL AND attach_instance_id IS NOT NULL;


/*
* Sleds
*/

CREATE TABLE omicron.public.Sled (
/* Identity metadata -- abbreviated for sleds */
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,

sled_agent_ip INET
);

/*
* Oximeter collector servers.
*/
Expand Down
60 changes: 60 additions & 0 deletions omicron-nexus/src/db/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,66 @@ impl DataStore {
self.pool.pool()
}

/// Stores a new sled in the database.
pub async fn sled_upsert(
&self,
sled: db::model::Sled,
) -> CreateResult<db::model::Sled> {
use db::schema::sled::dsl;
diesel::insert_into(dsl::sled)
.values(sled.clone())
.on_conflict(dsl::id)
.do_update()
.set((
dsl::time_modified.eq(Utc::now()),
dsl::ip.eq(sled.ip),
dsl::port.eq(sled.port),
))
.get_result_async(self.pool())
.await
.map_err(|e| {
public_error_from_diesel_pool_create(
e,
ResourceType::Sled,
&sled.id.to_string(),
)
})
}

pub async fn sled_list(
&self,
pagparams: &DataPageParams<'_, Uuid>,
) -> ListResultVec<db::model::Sled> {
use db::schema::sled::dsl;
paginated(dsl::sled, dsl::id, pagparams)
.filter(dsl::time_deleted.is_null())
.load_async::<db::model::Sled>(self.pool())
.await
.map_err(|e| {
public_error_from_diesel_pool(
e,
ResourceType::Sled,
LookupType::Other("Listing All".to_string()),
)
})
}

pub async fn sled_fetch(&self, id: Uuid) -> LookupResult<db::model::Sled> {
use db::schema::sled::dsl;
dsl::sled
.filter(dsl::time_deleted.is_null())
.filter(dsl::id.eq(id))
.first_async::<db::model::Sled>(self.pool())
.await
.map_err(|e| {
public_error_from_diesel_pool(
e,
ResourceType::Sled,
LookupType::ById(id),
)
})
}

/// Create a project
pub async fn project_create(
&self,
Expand Down
60 changes: 50 additions & 10 deletions omicron-nexus/src/db/model.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Structures stored to the database.

use super::schema::{
disk, instance, metricproducer, networkinterface, oximeter, project, vpc,
vpcsubnet,
disk, instance, metricproducer, networkinterface, oximeter, project, sled,
vpc, vpcsubnet,
};
use chrono::{DateTime, Utc};
use diesel::backend::{Backend, RawValue};
Expand Down Expand Up @@ -33,20 +33,60 @@ impl Into<external::Rack> for Rack {
}
}

// NOTE: This object is not currently stored in the database.
//
// However, it likely will be in the future. At the moment,
// Nexus simply reports all the live connections it knows about.
/// Database representation of a Sled.
#[derive(Queryable, Identifiable, Insertable, Debug, Clone)]
#[table_name = "sled"]
pub struct Sled {
pub identity: IdentityMetadata,
pub service_address: SocketAddr,
// IdentityMetadata
pub id: Uuid,
pub time_created: DateTime<Utc>,
pub time_modified: DateTime<Utc>,
pub time_deleted: Option<DateTime<Utc>>,

// ServiceAddress (Sled Agent).
pub ip: ipnetwork::IpNetwork,
pub port: i32,
}

impl Sled {
pub fn new(
id: Uuid,
addr: SocketAddr,
params: external::IdentityMetadataCreateParams,
) -> Self {
let identity = IdentityMetadata::new(id, params);
Self {
id,
time_created: identity.time_created,
time_modified: identity.time_modified,
time_deleted: identity.time_deleted,
ip: addr.ip().into(),
port: addr.port().into(),
}
}

pub fn id(&self) -> &Uuid {
&self.id
}

pub fn address(&self) -> SocketAddr {
// TODO: avoid this unwrap
SocketAddr::new(self.ip.ip(), u16::try_from(self.port).unwrap())
}
}

impl Into<external::Sled> for Sled {
fn into(self) -> external::Sled {
let service_address = self.address();
external::Sled {
identity: self.identity.into(),
service_address: self.service_address,
identity: external::IdentityMetadata {
id: self.id,
name: external::Name::try_from("sled").unwrap(),
description: "sled description".to_string(),
time_created: self.time_created,
time_modified: self.time_modified,
},
service_address,
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion omicron-nexus/src/db/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ table! {
id -> Uuid,
time_created -> Timestamptz,
time_modified -> Timestamptz,
sled_agent_ip -> Nullable<Inet>,
time_deleted -> Nullable<Timestamptz>,

ip -> Inet,
port -> Int4,
}
}

Expand Down
11 changes: 7 additions & 4 deletions omicron-nexus/src/http_entrypoints_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,10 +998,13 @@ async fn hardware_sleds_get(
let apictx = rqctx.context();
let nexus = &apictx.nexus;
let query = query_params.into_inner();
let sled_stream =
nexus.sleds_list(&data_page_params_for(&rqctx, &query)?).await?;
let view_list = to_list::<db::model::Sled, Sled>(sled_stream).await;
Ok(HttpResponseOk(ScanById::results_page(&query, view_list)?))
let sleds = nexus
.sleds_list(&data_page_params_for(&rqctx, &query)?)
.await?
.into_iter()
.map(|s| s.into())
.collect();
Ok(HttpResponseOk(ScanById::results_page(&query, sleds)?))
}

/**
Expand Down
7 changes: 1 addition & 6 deletions omicron-nexus/src/http_entrypoints_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use omicron_common::api::internal::nexus::InstanceRuntimeState;
use omicron_common::api::internal::nexus::OximeterInfo;
use omicron_common::api::internal::nexus::ProducerEndpoint;
use omicron_common::api::internal::nexus::SledAgentStartupInfo;
use omicron_common::SledAgentClient;
use schemars::JsonSchema;
use serde::Deserialize;
use std::sync::Arc;
Expand Down Expand Up @@ -68,11 +67,7 @@ async fn cpapi_sled_agents_post(
let path = path_params.into_inner();
let si = sled_info.into_inner();
let sled_id = &path.sled_id;
let client_log =
apictx.log.new(o!("SledAgent" => sled_id.clone().to_string()));
let client =
Arc::new(SledAgentClient::new(&sled_id, si.sa_address, client_log));
nexus.upsert_sled_agent(client).await;
nexus.upsert_sled(*sled_id, si.sa_address).await?;
Ok(HttpResponseUpdatedNoContent())
}

Expand Down
Loading