File tree Expand file tree Collapse file tree 4 files changed +58
-0
lines changed Expand file tree Collapse file tree 4 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ pub enum ConfigError {
22
22
DuplicateVpcVni ( u32 ) ,
23
23
#[ error( "A VPC peering with id '{0}' already exists" ) ]
24
24
DuplicateVpcPeeringId ( String ) ,
25
+ #[ error( "Peerings '{0}' and '{1}' refer to the same two VPCs" ) ]
26
+ DuplicateVpcPeerings ( String , String ) ,
25
27
#[ error( "A VPC peering object refers to non-existent VPC '{0}'" ) ]
26
28
NoSuchVpc ( String ) ,
27
29
#[ error( "'{0}' is not a valid VNI" ) ]
Original file line number Diff line number Diff line change @@ -57,6 +57,7 @@ impl Overlay {
57
57
/* collect peerings of every VPC */
58
58
self . vpc_table
59
59
. collect_peerings ( & self . peering_table , & id_map) ;
60
+ self . vpc_table . validate ( ) ?;
60
61
61
62
debug ! (
62
63
"Overlay configuration is VALID and looks as:\n {}\n {}" ,
Original file line number Diff line number Diff line change @@ -263,6 +263,41 @@ pub mod test {
263
263
) ;
264
264
}
265
265
266
+ #[ test]
267
+ fn test_overlay_duplicate_peering ( ) {
268
+ /* build VPCs */
269
+ let vpc1 = Vpc :: new ( "VPC-1" , "AAAAA" , 3000 ) . expect ( "Should succeed" ) ;
270
+ let vpc2 = Vpc :: new ( "VPC-2" , "BBBBB" , 4000 ) . expect ( "Should succeed" ) ;
271
+
272
+ /* build VPC table */
273
+ let mut vpc_table = VpcTable :: new ( ) ;
274
+ vpc_table. add ( vpc1) . expect ( "Should succeed" ) ;
275
+ vpc_table. add ( vpc2) . expect ( "Should succeed" ) ;
276
+
277
+ /* build peerings */
278
+ let peering1 = build_vpc_peering ( ) ;
279
+ let mut peering2 = build_vpc_peering ( ) ;
280
+ peering2. name = "Peering-2" . to_owned ( ) ;
281
+
282
+ let name1 = peering1. name . clone ( ) ;
283
+ let name2 = peering2. name . clone ( ) ;
284
+
285
+ assert_eq ! ( peering1. validate( ) , Ok ( ( ) ) ) ;
286
+ assert_eq ! ( peering2. validate( ) , Ok ( ( ) ) ) ;
287
+
288
+ /* build peering table */
289
+ let mut peering_table = VpcPeeringTable :: new ( ) ;
290
+ peering_table. add ( peering1) . expect ( "Should succeed" ) ;
291
+ peering_table. add ( peering2) . expect ( "Should succeed" ) ;
292
+
293
+ /* build overlay object and validate it */
294
+ let mut overlay = Overlay :: new ( vpc_table, peering_table) ;
295
+ assert_eq ! (
296
+ overlay. validate( ) ,
297
+ Err ( ConfigError :: DuplicateVpcPeerings ( name2, name1) )
298
+ ) ;
299
+ }
300
+
266
301
#[ test]
267
302
fn test_overlay ( ) {
268
303
/* build VPCs */
Original file line number Diff line number Diff line change @@ -162,4 +162,24 @@ impl VpcTable {
162
162
self . values_mut ( )
163
163
. for_each ( |vpc| vpc. collect_peerings ( peering_table, idmap) ) ;
164
164
}
165
+ /// Validate the [`VpcTable`]
166
+ pub fn validate ( & self ) -> ConfigResult {
167
+ for vpc in self . values ( ) {
168
+ // For each VPC, loop over all peerings
169
+ for ( index, peering) in vpc. peerings . iter ( ) . enumerate ( ) {
170
+ // For each peering, compare with all remaining peerings for the same (local) VPC
171
+ for other_peering in vpc. peerings . iter ( ) . skip ( index + 1 ) {
172
+ // If several peerings, for the given local VPC, refer to the same remote VPC as
173
+ // well, this is a configuration error
174
+ if peering. remote_id == other_peering. remote_id {
175
+ return Err ( ConfigError :: DuplicateVpcPeerings (
176
+ peering. name . clone ( ) ,
177
+ other_peering. name . clone ( ) ,
178
+ ) ) ;
179
+ }
180
+ }
181
+ }
182
+ }
183
+ Ok ( ( ) )
184
+ }
165
185
}
You can’t perform that action at this time.
0 commit comments