-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathip_pool.rs
203 lines (186 loc) · 6.62 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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/.
//! IP Pools, collections of external IP addresses for guest instances
use crate::authz;
use crate::db;
use crate::db::lookup;
use crate::db::lookup::LookupPath;
use crate::db::model::Name;
use crate::external_api::params;
use crate::external_api::shared::IpRange;
use ipnetwork::IpNetwork;
use nexus_db_queries::context::OpContext;
use omicron_common::api::external::http_pagination::PaginatedBy;
use omicron_common::api::external::CreateResult;
use omicron_common::api::external::DataPageParams;
use omicron_common::api::external::DeleteResult;
use omicron_common::api::external::Error;
use omicron_common::api::external::ListResultVec;
use omicron_common::api::external::LookupResult;
use omicron_common::api::external::NameOrId;
use omicron_common::api::external::ResourceType;
use omicron_common::api::external::UpdateResult;
use ref_cast::RefCast;
impl super::Nexus {
pub fn ip_pool_lookup<'a>(
&'a self,
opctx: &'a OpContext,
pool: &'a NameOrId,
) -> LookupResult<lookup::IpPool<'a>> {
match pool {
NameOrId::Name(name) => {
let pool = LookupPath::new(opctx, &self.db_datastore)
.ip_pool_name(Name::ref_cast(name));
Ok(pool)
}
NameOrId::Id(id) => {
let pool =
LookupPath::new(opctx, &self.db_datastore).ip_pool_id(*id);
Ok(pool)
}
}
}
pub async fn ip_pool_create(
&self,
opctx: &OpContext,
new_pool: ¶ms::IpPoolCreate,
) -> CreateResult<db::model::IpPool> {
self.db_datastore
.ip_pool_create(opctx, new_pool, /* internal= */ false)
.await
}
pub async fn ip_pool_services_create(
&self,
opctx: &OpContext,
new_pool: ¶ms::IpPoolCreate,
) -> CreateResult<db::model::IpPool> {
self.db_datastore
.ip_pool_create(opctx, new_pool, /* internal= */ true)
.await
}
pub async fn ip_pools_list(
&self,
opctx: &OpContext,
pagparams: &PaginatedBy<'_>,
) -> ListResultVec<db::model::IpPool> {
self.db_datastore.ip_pools_list(opctx, pagparams).await
}
pub async fn ip_pool_delete(
&self,
opctx: &OpContext,
pool_lookup: &lookup::IpPool<'_>,
) -> DeleteResult {
let (.., authz_pool, db_pool) =
pool_lookup.fetch_for(authz::Action::Delete).await?;
self.db_datastore.ip_pool_delete(opctx, &authz_pool, &db_pool).await
}
pub async fn ip_pool_update(
&self,
opctx: &OpContext,
pool_lookup: &lookup::IpPool<'_>,
updates: ¶ms::IpPoolUpdate,
) -> UpdateResult<db::model::IpPool> {
let (.., authz_pool) =
pool_lookup.lookup_for(authz::Action::Modify).await?;
self.db_datastore
.ip_pool_update(opctx, &authz_pool, updates.clone().into())
.await
}
pub async fn ip_pool_list_ranges(
&self,
opctx: &OpContext,
pool_lookup: &lookup::IpPool<'_>,
pagparams: &DataPageParams<'_, IpNetwork>,
) -> ListResultVec<db::model::IpPoolRange> {
let (.., authz_pool, db_pool) =
pool_lookup.fetch_for(authz::Action::ListChildren).await?;
if db_pool.internal {
return Err(Error::not_found_by_name(
ResourceType::IpPool,
&db_pool.identity.name,
));
}
self.db_datastore
.ip_pool_list_ranges(opctx, &authz_pool, pagparams)
.await
}
pub async fn ip_pool_add_range(
&self,
opctx: &OpContext,
pool_lookup: &lookup::IpPool<'_>,
range: &IpRange,
) -> UpdateResult<db::model::IpPoolRange> {
let (.., authz_pool, db_pool) =
pool_lookup.fetch_for(authz::Action::Modify).await?;
if db_pool.internal {
return Err(Error::not_found_by_name(
ResourceType::IpPool,
&db_pool.identity.name,
));
}
self.db_datastore.ip_pool_add_range(opctx, &authz_pool, range).await
}
pub async fn ip_pool_delete_range(
&self,
opctx: &OpContext,
pool_lookup: &lookup::IpPool<'_>,
range: &IpRange,
) -> DeleteResult {
let (.., authz_pool, db_pool) =
pool_lookup.fetch_for(authz::Action::Modify).await?;
if db_pool.internal {
return Err(Error::not_found_by_name(
ResourceType::IpPool,
&db_pool.identity.name,
));
}
self.db_datastore.ip_pool_delete_range(opctx, &authz_pool, range).await
}
// The "ip_pool_service_..." functions look up IP pools for Oxide service usage,
// rather than for VMs.
//
// TODO(https://github.com/oxidecomputer/omicron/issues/1276): Should be
// accessed via AZ UUID, probably.
pub async fn ip_pool_service_fetch(
&self,
opctx: &OpContext,
) -> LookupResult<db::model::IpPool> {
let (authz_pool, db_pool) =
self.db_datastore.ip_pools_service_lookup(opctx).await?;
opctx.authorize(authz::Action::Read, &authz_pool).await?;
Ok(db_pool)
}
pub async fn ip_pool_service_list_ranges(
&self,
opctx: &OpContext,
pagparams: &DataPageParams<'_, IpNetwork>,
) -> ListResultVec<db::model::IpPoolRange> {
let (authz_pool, ..) =
self.db_datastore.ip_pools_service_lookup(opctx).await?;
opctx.authorize(authz::Action::Read, &authz_pool).await?;
self.db_datastore
.ip_pool_list_ranges(opctx, &authz_pool, pagparams)
.await
}
pub async fn ip_pool_service_add_range(
&self,
opctx: &OpContext,
range: &IpRange,
) -> UpdateResult<db::model::IpPoolRange> {
let (authz_pool, ..) =
self.db_datastore.ip_pools_service_lookup(opctx).await?;
opctx.authorize(authz::Action::Modify, &authz_pool).await?;
self.db_datastore.ip_pool_add_range(opctx, &authz_pool, range).await
}
pub async fn ip_pool_service_delete_range(
&self,
opctx: &OpContext,
range: &IpRange,
) -> DeleteResult {
let (authz_pool, ..) =
self.db_datastore.ip_pools_service_lookup(opctx).await?;
opctx.authorize(authz::Action::Modify, &authz_pool).await?;
self.db_datastore.ip_pool_delete_range(opctx, &authz_pool, range).await
}
}