Skip to content

Commit 83e3066

Browse files
committed
feat(mgmt): add iterators for peerings
Signed-off-by: Fredi Raspall <[email protected]>
1 parent 4045fe6 commit 83e3066

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

mgmt/src/rpc/overlay/tests.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub mod test {
3737
vpc1.add_expose(expose).expect("Should succeed");
3838
vpc1
3939
}
40+
4041
/* build sample peering between VPC-1 and VPC-2 */
4142
fn build_vpc_peering() -> VpcPeering {
4243
// build vpc manifests
@@ -140,4 +141,55 @@ pub mod test {
140141
assert_eq!(overlay.validate(), Ok(()));
141142
println!("{overlay:#?}");
142143
}
144+
145+
#[test]
146+
fn test_peering_iter() {
147+
let mut peering_table = VpcPeeringTable::new();
148+
149+
let m1 = VpcExposeManifest::new("VPC-1");
150+
let m2 = VpcExposeManifest::new("VPC-2");
151+
let mut peering = VpcPeering::new("Peering-1");
152+
peering.set_one(m1);
153+
peering.set_two(m2);
154+
peering_table.add(peering).unwrap();
155+
156+
let m1 = VpcExposeManifest::new("VPC-1");
157+
let m2 = VpcExposeManifest::new("VPC-3");
158+
let mut peering = VpcPeering::new("Peering-2");
159+
peering.set_one(m1);
160+
peering.set_two(m2);
161+
peering_table.add(peering).unwrap();
162+
163+
let m1 = VpcExposeManifest::new("VPC-2");
164+
let m2 = VpcExposeManifest::new("VPC-4");
165+
let mut peering = VpcPeering::new("Peering-3");
166+
peering.set_one(m1);
167+
peering.set_two(m2);
168+
peering_table.add(peering).unwrap();
169+
170+
let m1 = VpcExposeManifest::new("VPC-1");
171+
let m2 = VpcExposeManifest::new("VPC-4");
172+
let mut peering = VpcPeering::new("Peering-4");
173+
peering.set_one(m1);
174+
peering.set_two(m2);
175+
peering_table.add(peering).unwrap();
176+
177+
// all peerings of VPC-1
178+
let x: Vec<String> = peering_table
179+
.peerings_vpc("VPC-1")
180+
.map(|p| p.name.clone())
181+
.collect();
182+
183+
assert!(x.contains(&"Peering-1".to_owned()));
184+
assert!(x.contains(&"Peering-2".to_owned()));
185+
assert!(x.contains(&"Peering-4".to_owned()));
186+
assert!(!x.contains(&"Peering-3".to_owned()), "not there");
187+
188+
// all peerings of VPC-2 with VPC-4
189+
let x: Vec<String> = peering_table
190+
.peerings_between("VPC-2", "VPC-4")
191+
.map(|p| p.name.clone())
192+
.collect();
193+
assert!(x.contains(&"Peering-3".to_owned()));
194+
}
143195
}

mgmt/src/rpc/overlay/vpcpeering.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,31 @@ impl VpcPeeringTable {
115115
pub fn values(&self) -> impl Iterator<Item = &VpcPeering> {
116116
self.0.values()
117117
}
118+
/// Produce iterator of [`VpcPeering`]s that involve the vpc with the provided name
119+
pub fn peerings_vpc(&self, vpc: &str) -> impl Iterator<Item = &VpcPeering> {
120+
self.0.values().filter(|peering| {
121+
// VPCs are options to ease builders but should always be there
122+
let name1 = peering.vpc1.as_ref().map(|m| m.name.as_str());
123+
let name2 = peering.vpc2.as_ref().map(|m| m.name.as_str());
124+
if name1.is_none() || name2.is_none() {
125+
false
126+
} else {
127+
name1 == Some(vpc) || name2 == Some(vpc)
128+
}
129+
})
130+
}
131+
/// Produce iterator of [`VpcPeering`]s between the two provided vpcs.
132+
/// In principle there should be one peering at the most between two vpcs ?
133+
pub fn peerings_between(&self, vpc1: &str, vpc2: &str) -> impl Iterator<Item = &VpcPeering> {
134+
self.0.values().filter(|peering| {
135+
let name1 = peering.vpc1.as_ref().map(|m| m.name.as_str());
136+
let name2 = peering.vpc2.as_ref().map(|m| m.name.as_str());
137+
if name1.is_none() || name2.is_none() {
138+
false
139+
} else {
140+
name1 == Some(vpc1) && name2 == Some(vpc2)
141+
|| name1 == Some(vpc2) && name2 == Some(vpc1)
142+
}
143+
})
144+
}
118145
}

0 commit comments

Comments
 (0)