-
Notifications
You must be signed in to change notification settings - Fork 40
/
ip_pool.rs
165 lines (151 loc) · 5.11 KB
/
ip_pool.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 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/.
//! Model types for IP Pools and the CIDR blocks therein.
use crate::collection::DatastoreCollectionConfig;
use crate::schema::ip_pool;
use crate::schema::ip_pool_range;
use crate::Name;
use chrono::DateTime;
use chrono::Utc;
use db_macros::Resource;
use diesel::Selectable;
use ipnetwork::IpNetwork;
use nexus_types::external_api::params;
use nexus_types::external_api::shared::IpRange;
use nexus_types::external_api::views;
use nexus_types::identity::Resource;
use omicron_common::api::external;
use std::net::IpAddr;
use uuid::Uuid;
/// An IP Pool is a collection of IP addresses external to the rack.
#[derive(Queryable, Insertable, Selectable, Clone, Debug, Resource)]
#[diesel(table_name = ip_pool)]
pub struct IpPool {
#[diesel(embed)]
pub identity: IpPoolIdentity,
/// If true, identifies that this IP pool is dedicated to "Control-Plane
/// Services", such as Nexus.
///
/// Otherwise, this IP pool is intended for usage by customer VMs.
pub internal: bool,
/// Child resource generation number, for optimistic concurrency control of
/// the contained ranges.
pub rcgen: i64,
}
impl IpPool {
pub fn new(
pool_identity: &external::IdentityMetadataCreateParams,
internal: bool,
) -> Self {
Self {
identity: IpPoolIdentity::new(
Uuid::new_v4(),
pool_identity.clone(),
),
internal,
rcgen: 0,
}
}
}
impl From<IpPool> for views::IpPool {
fn from(pool: IpPool) -> Self {
Self { identity: pool.identity() }
}
}
/// A set of updates to an IP Pool
#[derive(AsChangeset)]
#[diesel(table_name = ip_pool)]
pub struct IpPoolUpdate {
pub name: Option<Name>,
pub description: Option<String>,
pub time_modified: DateTime<Utc>,
}
impl From<params::IpPoolUpdate> for IpPoolUpdate {
fn from(params: params::IpPoolUpdate) -> Self {
Self {
name: params.identity.name.map(|n| n.into()),
description: params.identity.description,
time_modified: Utc::now(),
}
}
}
/// A range of IP addresses for an IP Pool.
#[derive(Queryable, Insertable, Selectable, Clone, Debug)]
#[diesel(table_name = ip_pool_range)]
pub struct IpPoolRange {
pub id: Uuid,
pub time_created: DateTime<Utc>,
pub time_modified: DateTime<Utc>,
pub time_deleted: Option<DateTime<Utc>>,
/// First (lowest) address in the range, inclusive.
pub first_address: IpNetwork,
/// Last (highest) address in the range, inclusive.
pub last_address: IpNetwork,
/// Foreign-key to the `ip_pool` table with the parent pool for this range
pub ip_pool_id: Uuid,
/// The child resource generation number, tracking IP addresses allocated or
/// used from this range.
pub rcgen: i64,
}
impl IpPoolRange {
pub fn new(range: &IpRange, ip_pool_id: Uuid) -> Self {
let now = Utc::now();
let first_address = range.first_address();
let last_address = range.last_address();
// `range` has already been validated to have first address no greater
// than last address.
assert!(
last_address >= first_address,
"Address ranges must be non-decreasing"
);
Self {
id: Uuid::new_v4(),
time_created: now,
time_modified: now,
time_deleted: None,
first_address: IpNetwork::from(range.first_address()),
last_address: IpNetwork::from(range.last_address()),
ip_pool_id,
rcgen: 0,
}
}
}
impl From<IpPoolRange> for views::IpPoolRange {
fn from(range: IpPoolRange) -> Self {
Self {
id: range.id,
time_created: range.time_created,
range: IpRange::from(&range),
}
}
}
impl From<&IpPoolRange> for IpRange {
fn from(range: &IpPoolRange) -> Self {
let maybe_range =
match (range.first_address.ip(), range.last_address.ip()) {
(IpAddr::V4(first), IpAddr::V4(last)) => {
IpRange::try_from((first, last))
}
(IpAddr::V6(first), IpAddr::V6(last)) => {
IpRange::try_from((first, last))
}
(first, last) => {
unreachable!(
"Expected first/last address of an IP range to \
both be of the same protocol version, but first = {:?} \
and last = {:?}",
first, last,
);
}
};
maybe_range
.expect("Retrieved an out-of-order IP range pair from the database")
}
}
impl DatastoreCollectionConfig<IpPoolRange> for IpPool {
type CollectionId = uuid::Uuid;
type GenerationNumberColumn = ip_pool::dsl::rcgen;
type CollectionTimeDeletedColumn = ip_pool::dsl::time_deleted;
type CollectionIdColumn = ip_pool_range::dsl::ip_pool_id;
}