Skip to content

Commit 31b64ef

Browse files
qmonnetFredi-raspall
authored andcommitted
feat(vpcpeering): Implement validation for VpcManifest
We already have validation for VpcExpose, VpcPeering, and expose or peering addition to lists and tables; we're only missing some basic checks for the VpcManifest objects. There's not much to do this time, because most of the work consists in validating the VpcExpose objects and preventing prefix overlap between them, which we have implemented in previous commits. Make sure the name is not empty, make sure the lists of VpcExpose objects are not empty, and use the resulting validation function for each VpcManifest when validating a VpcPeering object. Add relevant enum variants to ApiError. Fix some tests that would use manifests with empty VpcExpose lists. Signed-off-by: Quentin Monnet <[email protected]>
1 parent 3c58a05 commit 31b64ef

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

mgmt/src/models/external/overlay/tests.rs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,71 @@ pub mod test {
142142
fn test_peering_iter() {
143143
let mut peering_table = VpcPeeringTable::new();
144144

145-
let m1 = VpcManifest::new("VPC-1");
146-
let m2 = VpcManifest::new("VPC-2");
145+
let mut m1 = VpcManifest::new("VPC-1");
146+
m1.add_expose(
147+
VpcExpose::empty()
148+
.ip(Prefix::from(("1.1.1.0", 24)))
149+
.as_range(Prefix::from(("1.1.2.0", 24))),
150+
)
151+
.expect("Failed to add expose");
152+
let mut m2 = VpcManifest::new("VPC-2");
153+
m2.add_expose(
154+
VpcExpose::empty()
155+
.ip(Prefix::from(("1.2.1.0", 24)))
156+
.as_range(Prefix::from(("1.2.2.0", 24))),
157+
)
158+
.expect("Failed to add expose");
147159
let peering = VpcPeering::new("Peering-1", m1, m2);
148160
peering_table.add(peering).unwrap();
149161

150-
let m1 = VpcManifest::new("VPC-1");
151-
let m2 = VpcManifest::new("VPC-3");
162+
let mut m1 = VpcManifest::new("VPC-1");
163+
m1.add_expose(
164+
VpcExpose::empty()
165+
.ip(Prefix::from(("2.1.1.0", 24)))
166+
.as_range(Prefix::from(("2.2.2.0", 24))),
167+
)
168+
.expect("Failed to add expose");
169+
let mut m2 = VpcManifest::new("VPC-3");
170+
m2.add_expose(
171+
VpcExpose::empty()
172+
.ip(Prefix::from(("2.2.1.0", 24)))
173+
.as_range(Prefix::from(("2.2.2.0", 24))),
174+
)
175+
.expect("Failed to add expose");
152176
let peering = VpcPeering::new("Peering-2", m1, m2);
153177
peering_table.add(peering).unwrap();
154178

155-
let m1 = VpcManifest::new("VPC-2");
156-
let m2 = VpcManifest::new("VPC-4");
179+
let mut m1 = VpcManifest::new("VPC-2");
180+
m1.add_expose(
181+
VpcExpose::empty()
182+
.ip(Prefix::from(("3.1.1.0", 24)))
183+
.as_range(Prefix::from(("3.1.2.0", 24))),
184+
)
185+
.expect("Failed to add expose");
186+
let mut m2 = VpcManifest::new("VPC-4");
187+
m2.add_expose(
188+
VpcExpose::empty()
189+
.ip(Prefix::from(("3.2.1.0", 24)))
190+
.as_range(Prefix::from(("3.2.2.0", 24))),
191+
)
192+
.expect("Failed to add expose");
157193
let peering = VpcPeering::new("Peering-3", m1, m2);
158194
peering_table.add(peering).unwrap();
159195

160-
let m1 = VpcManifest::new("VPC-1");
161-
let m2 = VpcManifest::new("VPC-4");
196+
let mut m1 = VpcManifest::new("VPC-1");
197+
m1.add_expose(
198+
VpcExpose::empty()
199+
.ip(Prefix::from(("4.1.1.0", 24)))
200+
.as_range(Prefix::from(("4.1.2.0", 24))),
201+
)
202+
.expect("Failed to add expose");
203+
let mut m2 = VpcManifest::new("VPC-4");
204+
m2.add_expose(
205+
VpcExpose::empty()
206+
.ip(Prefix::from(("4.2.1.0", 24)))
207+
.as_range(Prefix::from(("4.2.2.0", 24))),
208+
)
209+
.expect("Failed to add expose");
162210
let peering = VpcPeering::new("Peering-4", m1, m2);
163211
peering_table.add(peering).unwrap();
164212

mgmt/src/models/external/overlay/vpcpeering.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ impl VpcExpose {
3636
self.not_as.insert(prefix);
3737
self
3838
}
39-
#[allow(unused)]
4039
fn fixup(mut self) -> Result<Self, ConfigError> {
4140
// Add a root prefix to the list of private prefixes if the list is empty
4241
if self.ips.is_empty() {
@@ -233,6 +232,15 @@ impl VpcManifest {
233232
self.exposes.push(expose);
234233
Ok(())
235234
}
235+
pub fn validate(&self) -> ConfigResult {
236+
if self.name.is_empty() {
237+
return Err(ConfigError::MissingIdentifier("Manifest name"));
238+
}
239+
if self.exposes.is_empty() {
240+
return Err(ConfigError::IncompleteConfig("Empty manifest".to_string()));
241+
}
242+
Ok(())
243+
}
236244
}
237245

238246
#[derive(Clone, Debug)]
@@ -253,6 +261,8 @@ impl VpcPeering {
253261
if self.name.is_empty() {
254262
return Err(ConfigError::MissingIdentifier("Peering name"));
255263
}
264+
self.left.validate()?;
265+
self.right.validate()?;
256266
Ok(())
257267
}
258268
/// Given a peering fetch the manifests, orderly depending on the provided vpc name
@@ -315,7 +325,7 @@ impl VpcPeeringTable {
315325

316326
// First look for an existing entry, to avoid inserting a duplicate peering
317327
if self.0.contains_key(&peering.name) {
318-
return Err(ApiError::DuplicateVpcPeeringId(peering.name.clone()));
328+
return Err(ConfigError::DuplicateVpcPeeringId(peering.name.clone()));
319329
}
320330

321331
if let Some(peering) = self.0.insert(peering.name.to_owned(), peering) {

0 commit comments

Comments
 (0)