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
37 changes: 34 additions & 3 deletions common/src/api/internal/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

use super::nexus::HostIdentifier;
use crate::{
api::external::{self, BfdMode, ImportExportPolicy, Name, Vni},
api::external::{self, BfdMode, ImportExportPolicy, MacAddr, Name, Vni},
disk::DatasetName,
zpool_name::ZpoolName,
};
use daft::Diffable;
use omicron_uuid_kinds::DatasetUuid;
use omicron_uuid_kinds::ExternalZpoolUuid;
use omicron_uuid_kinds::{
DatasetUuid, ExternalSubnetUuid, InstanceUuid, RackUuid, SledUuid,
};
use omicron_uuid_kinds::{ExternalZpoolUuid, PropolisUuid};
use oxnet::{IpNet, Ipv4Net, Ipv6Net};
use schemars::JsonSchema;
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
Expand Down Expand Up @@ -1097,6 +1099,35 @@ impl DelegatedZvol {
}
}

#[derive(Clone, Copy, Debug)]
pub enum AttachedSubnetId {
External(ExternalSubnetUuid),
Vpc(Uuid),
}

/// All details about an attached subnet and the Instance it's attached to.
#[derive(Debug)]
pub struct AttachedSubnet {
/// ID of the rack hosting this instance.
pub rack_id: RackUuid,
/// ID of the sled hosting the instance.
pub sled_id: SledUuid,
/// Underlay IP address of the sled hosting the instance.
pub sled_ip: Ipv6Addr,
/// ID of the Propolis hypervisor managing this instance.
pub vmm_id: PropolisUuid,
/// ID of the instance
pub instance_id: InstanceUuid,
/// ID of the subnet itself.
pub subnet_id: AttachedSubnetId,
/// The IP subnet that's attached.
pub subnet: IpNet,
/// The MAC address of the primary network interface.
pub mac: MacAddr,
/// The VNI of the VPC the instance is in.
pub vni: Vni,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion nexus/db-model/src/external_subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use nexus_types::external_api::views;
use nexus_types::identity::Resource;
use omicron_common::api::external;
use omicron_common::api::external::Error;
use omicron_uuid_kinds::GenericUuid;
use omicron_uuid_kinds::GenericUuid as _;
use omicron_uuid_kinds::InstanceKind;
use omicron_uuid_kinds::SubnetPoolKind;
use omicron_uuid_kinds::SubnetPoolMemberKind;
Expand Down
14 changes: 13 additions & 1 deletion nexus/db-model/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::{
ByteCount, Disk, ExternalIp, Generation, InstanceAutoRestartPolicy,
InstanceCpuCount, InstanceCpuPlatform, InstanceState, Vmm, VmmState,
};
use crate::ExternalSubnet;
use crate::collection::DatastoreAttachTargetConfig;
use crate::serde_time_delta::optional_time_delta;
use chrono::{DateTime, TimeDelta, Utc};
Expand All @@ -15,7 +16,7 @@ use diesel::expression::{ValidGrouping, is_aggregate};
use diesel::pg;
use diesel::prelude::*;
use diesel::sql_types::{Bool, Nullable};
use nexus_db_schema::schema::{disk, external_ip, instance};
use nexus_db_schema::schema::{disk, external_ip, external_subnet, instance};
use nexus_types::external_api::params;
use omicron_uuid_kinds::{GenericUuid, InstanceUuid};
use serde::Deserialize;
Expand Down Expand Up @@ -225,6 +226,17 @@ impl DatastoreAttachTargetConfig<ExternalIp> for Instance {
type ResourceTimeDeletedColumn = external_ip::dsl::time_deleted;
}

impl DatastoreAttachTargetConfig<ExternalSubnet> for Instance {
type Id = Uuid;

type CollectionIdColumn = instance::dsl::id;
type CollectionTimeDeletedColumn = instance::dsl::time_deleted;

type ResourceIdColumn = external_subnet::dsl::id;
type ResourceCollectionIdColumn = external_subnet::dsl::instance_id;
type ResourceTimeDeletedColumn = external_subnet::dsl::time_deleted;
}

/// Runtime state of the Instance, including the actual running state and minimal
/// metadata
///
Expand Down
10 changes: 10 additions & 0 deletions nexus/db-model/src/ipnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 crate::IpVersion;
use diesel::backend::Backend;
use diesel::deserialize;
use diesel::deserialize::FromSql;
Expand Down Expand Up @@ -30,6 +31,15 @@ pub enum IpNet {
V6(crate::Ipv6Net),
}

impl IpNet {
pub fn ip_version(&self) -> IpVersion {
match self {
IpNet::V4(_) => IpVersion::V4,
IpNet::V6(_) => IpVersion::V6,
}
}
}

impl ::std::fmt::Display for IpNet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
1 change: 1 addition & 0 deletions nexus/db-queries/src/db/datastore/external_ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ impl DataStore {
.into_boxed()
.filter(nic_dsl::parent_id.eq(instance_id.into_untyped_uuid()))
.filter(nic_dsl::time_deleted.is_null())
.filter(nic_dsl::is_primary.eq(true))
.filter(nic_dsl::kind.eq(NetworkInterfaceKind::Instance));
let has_matching_ip_stack = match ip_version {
IpVersion::V4 => base_nic_query.select(nic_dsl::ip.is_not_null()),
Expand Down
Loading
Loading