-
Notifications
You must be signed in to change notification settings - Fork 40
/
resource_helpers.rs
376 lines (353 loc) · 10.6 KB
/
resource_helpers.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// 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 crate::http_testing::RequestBuilder;
use crate::ControlPlaneTestContext;
use super::http_testing::AuthnMode;
use super::http_testing::NexusRequest;
use dropshot::test_util::ClientTestContext;
use dropshot::HttpErrorResponseBody;
use dropshot::Method;
use http::StatusCode;
use omicron_common::api::external::ByteCount;
use omicron_common::api::external::Disk;
use omicron_common::api::external::IdentityMetadataCreateParams;
use omicron_common::api::external::Instance;
use omicron_common::api::external::InstanceCpuCount;
use omicron_nexus::crucible_agent_client::types::State as RegionState;
use omicron_nexus::external_api::params;
use omicron_nexus::external_api::shared;
use omicron_nexus::external_api::shared::IdentityType;
use omicron_nexus::external_api::views::{
Organization, Project, Silo, Vpc, VpcRouter,
};
use omicron_sled_agent::sim::SledAgent;
use std::sync::Arc;
use uuid::Uuid;
pub async fn objects_list_page_authz<ItemType>(
client: &ClientTestContext,
path: &str,
) -> dropshot::ResultsPage<ItemType>
where
ItemType: serde::de::DeserializeOwned,
{
NexusRequest::object_get(client, path)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.expect("failed to make request")
.parsed_body()
.unwrap()
}
pub async fn object_create<InputType, OutputType>(
client: &ClientTestContext,
path: &str,
input: &InputType,
) -> OutputType
where
InputType: serde::Serialize,
OutputType: serde::de::DeserializeOwned,
{
NexusRequest::objects_post(client, path, input)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.expect("failed to make \"create\" request")
.parsed_body()
.unwrap()
}
pub async fn create_silo(
client: &ClientTestContext,
silo_name: &str,
discoverable: bool,
) -> Silo {
object_create(
client,
"/silos",
¶ms::SiloCreate {
identity: IdentityMetadataCreateParams {
name: silo_name.parse().unwrap(),
description: "a silo".to_string(),
},
discoverable,
},
)
.await
}
pub async fn create_organization(
client: &ClientTestContext,
organization_name: &str,
) -> Organization {
object_create(
client,
"/organizations",
¶ms::OrganizationCreate {
identity: IdentityMetadataCreateParams {
name: organization_name.parse().unwrap(),
description: "an org".to_string(),
},
},
)
.await
}
pub async fn create_project(
client: &ClientTestContext,
organization_name: &str,
project_name: &str,
) -> Project {
let url = format!("/organizations/{}/projects", &organization_name);
object_create(
client,
&url,
¶ms::ProjectCreate {
identity: IdentityMetadataCreateParams {
name: project_name.parse().unwrap(),
description: "a pier".to_string(),
},
},
)
.await
}
pub async fn create_disk(
client: &ClientTestContext,
organization_name: &str,
project_name: &str,
disk_name: &str,
) -> Disk {
let url = format!(
"/organizations/{}/projects/{}/disks",
organization_name, project_name
);
object_create(
client,
&url,
¶ms::DiskCreate {
identity: IdentityMetadataCreateParams {
name: disk_name.parse().unwrap(),
description: String::from("sells rainsticks"),
},
disk_source: params::DiskSource::Blank {
block_size: params::BlockSize::try_from(512).unwrap(),
},
size: ByteCount::from_gibibytes_u32(1),
},
)
.await
}
pub async fn create_instance(
client: &ClientTestContext,
organization_name: &str,
project_name: &str,
instance_name: &str,
) -> Instance {
create_instance_with_nics(
client,
organization_name,
project_name,
instance_name,
¶ms::InstanceNetworkInterfaceAttachment::Default,
)
.await
}
pub async fn create_instance_with_nics(
client: &ClientTestContext,
organization_name: &str,
project_name: &str,
instance_name: &str,
nics: ¶ms::InstanceNetworkInterfaceAttachment,
) -> Instance {
let url = format!(
"/organizations/{}/projects/{}/instances",
organization_name, project_name
);
object_create(
client,
&url,
¶ms::InstanceCreate {
identity: IdentityMetadataCreateParams {
name: instance_name.parse().unwrap(),
description: format!("instance {:?}", instance_name),
},
ncpus: InstanceCpuCount(4),
memory: ByteCount::from_gibibytes_u32(1),
hostname: String::from("the_host"),
user_data:
b"#cloud-config\nsystem_info:\n default_user:\n name: oxide"
.to_vec(),
network_interfaces: nics.clone(),
disks: vec![],
},
)
.await
}
pub async fn create_vpc(
client: &ClientTestContext,
organization_name: &str,
project_name: &str,
vpc_name: &str,
) -> Vpc {
object_create(
&client,
format!(
"/organizations/{}/projects/{}/vpcs",
&organization_name, &project_name
)
.as_str(),
¶ms::VpcCreate {
identity: IdentityMetadataCreateParams {
name: vpc_name.parse().unwrap(),
description: "vpc description".to_string(),
},
ipv6_prefix: None,
dns_name: "abc".parse().unwrap(),
},
)
.await
}
// TODO: probably would be cleaner to replace these helpers with something that
// just generates the create params since that's the noisiest part
pub async fn create_vpc_with_error(
client: &ClientTestContext,
organization_name: &str,
project_name: &str,
vpc_name: &str,
status: StatusCode,
) -> HttpErrorResponseBody {
NexusRequest::new(
RequestBuilder::new(
client,
Method::POST,
format!(
"/organizations/{}/projects/{}/vpcs",
&organization_name, &project_name
)
.as_str(),
)
.body(Some(¶ms::VpcCreate {
identity: IdentityMetadataCreateParams {
name: vpc_name.parse().unwrap(),
description: String::from("vpc description"),
},
ipv6_prefix: None,
dns_name: "abc".parse().unwrap(),
}))
.expect_status(Some(status)),
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.unwrap()
.parsed_body()
.unwrap()
}
pub async fn create_router(
client: &ClientTestContext,
organization_name: &str,
project_name: &str,
vpc_name: &str,
router_name: &str,
) -> VpcRouter {
NexusRequest::objects_post(
&client,
format!(
"/organizations/{}/projects/{}/vpcs/{}/routers",
&organization_name, &project_name, &vpc_name
)
.as_str(),
¶ms::VpcRouterCreate {
identity: IdentityMetadataCreateParams {
name: router_name.parse().unwrap(),
description: String::from("router description"),
},
},
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.unwrap()
.parsed_body()
.unwrap()
}
/// Grant a role on a resource to a user
///
/// * `grant_resource_url`: URL of the resource we're granting the role on
/// * `grant_role`: the role we're granting
/// * `grant_user`: the uuid of the user we're granting the role to
/// * `run_as`: the user _doing_ the granting
pub async fn grant_iam<T>(
client: &ClientTestContext,
grant_resource_url: &str,
grant_role: T,
grant_user: Uuid,
run_as: AuthnMode,
) where
T: serde::Serialize + serde::de::DeserializeOwned,
{
let policy_url = format!("{}/policy", grant_resource_url);
let existing_policy: shared::Policy<T> =
NexusRequest::object_get(client, &policy_url)
.authn_as(run_as.clone())
.execute()
.await
.expect("failed to fetch policy")
.parsed_body()
.expect("failed to parse policy");
let new_role_assignment = shared::RoleAssignment {
identity_type: IdentityType::SiloUser,
identity_id: grant_user,
role_name: grant_role,
};
let new_role_assignments = existing_policy
.role_assignments
.into_iter()
.chain(std::iter::once(new_role_assignment))
.collect();
let new_policy = shared::Policy { role_assignments: new_role_assignments };
// TODO-correctness use etag when we have it
NexusRequest::object_put(client, &policy_url, Some(&new_policy))
.authn_as(run_as)
.execute()
.await
.expect("failed to update policy");
}
pub async fn project_get(
client: &ClientTestContext,
project_url: &str,
) -> Project {
NexusRequest::object_get(client, project_url)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.expect("failed to get project")
.parsed_body()
.expect("failed to parse Project")
}
pub struct DiskTest {
pub sled_agent: Arc<SledAgent>,
pub zpool_id: Uuid,
pub zpool_size: ByteCount,
pub dataset_ids: Vec<Uuid>,
}
impl DiskTest {
// Creates fake physical storage, an organization, and a project.
pub async fn new(cptestctx: &ControlPlaneTestContext) -> Self {
let sled_agent = cptestctx.sled_agent.sled_agent.clone();
// Create a Zpool.
let zpool_id = Uuid::new_v4();
let zpool_size = ByteCount::from_gibibytes_u32(10);
sled_agent.create_zpool(zpool_id, zpool_size.to_bytes()).await;
// Create multiple Datasets within that Zpool.
let dataset_count = 3;
let dataset_ids: Vec<_> =
(0..dataset_count).map(|_| Uuid::new_v4()).collect();
for id in &dataset_ids {
sled_agent.create_crucible_dataset(zpool_id, *id).await;
// By default, regions are created immediately.
let crucible = sled_agent.get_crucible_dataset(zpool_id, *id).await;
crucible
.set_create_callback(Box::new(|_| RegionState::Created))
.await;
}
Self { sled_agent, zpool_id, zpool_size, dataset_ids }
}
}