1
1
// SPDX-License-Identifier: Apache-2.0
2
2
// Copyright Open Network Fabric Authors
3
3
4
+ use :: gateway_config:: google;
4
5
use gateway_config:: config as gateway_config;
6
+
5
7
use std:: convert:: TryFrom ;
6
8
7
- use crate :: external:: overlay:: vpcpeering:: VpcExpose ;
9
+ use crate :: external:: overlay:: vpcpeering:: {
10
+ VpcExpose , VpcExposeNatConfig , VpcExposeStatefulNat , VpcExposeStatelessNat ,
11
+ } ;
8
12
use lpm:: prefix:: { Prefix , PrefixString } ;
9
13
10
14
impl TryFrom < & gateway_config:: Expose > for VpcExpose {
@@ -54,6 +58,34 @@ impl TryFrom<&gateway_config::Expose> for VpcExpose {
54
58
}
55
59
}
56
60
61
+ if !expose. r#as . is_empty ( ) {
62
+ vpc_expose = vpc_expose. make_nat ( ) ;
63
+ if let ( Some ( grpc_nat) , Some ( nat) ) = ( expose. nat . as_ref ( ) , vpc_expose. nat . as_mut ( ) ) {
64
+ #[ allow( clippy:: default_constructed_unit_structs) ]
65
+ match grpc_nat {
66
+ gateway_config:: expose:: Nat :: Stateless ( _) => {
67
+ nat. config =
68
+ VpcExposeNatConfig :: Stateless ( VpcExposeStatelessNat :: default ( ) ) ;
69
+ }
70
+ gateway_config:: expose:: Nat :: Stateful ( grpc_s) => {
71
+ if let Some ( nat) = vpc_expose. nat . as_mut ( ) {
72
+ nat. config = VpcExposeNatConfig :: Stateful ( VpcExposeStatefulNat {
73
+ idle_timeout : grpc_s
74
+ . idle_timeout
75
+ . ok_or (
76
+ "stateful nat requires idle_timeout, got None" . to_string ( ) ,
77
+ )
78
+ . and_then ( |t| {
79
+ std:: time:: Duration :: try_from ( t)
80
+ . map_err ( |e| format ! ( "Invalid duration: {e}" ) )
81
+ } ) ?,
82
+ } ) ;
83
+ }
84
+ }
85
+ }
86
+ }
87
+ }
88
+
57
89
Ok ( vpc_expose)
58
90
}
59
91
}
@@ -77,7 +109,7 @@ impl TryFrom<&VpcExpose> for gateway_config::Expose {
77
109
ips. push ( gateway_config:: PeeringIPs { rule : Some ( rule) } ) ;
78
110
}
79
111
80
- if let Some ( nat) = expose. nat . as_ref ( ) {
112
+ let nat = if let Some ( nat) = expose. nat . as_ref ( ) {
81
113
// Convert AS inclusion rules
82
114
for prefix in & nat. as_range {
83
115
let rule = gateway_config:: peering_as:: Rule :: Cidr ( prefix. to_string ( ) ) ;
@@ -89,10 +121,28 @@ impl TryFrom<&VpcExpose> for gateway_config::Expose {
89
121
let rule = gateway_config:: peering_as:: Rule :: Not ( prefix. to_string ( ) ) ;
90
122
as_rules. push ( gateway_config:: PeeringAs { rule : Some ( rule) } ) ;
91
123
}
92
- }
124
+
125
+ match & nat. config {
126
+ VpcExposeNatConfig :: Stateful ( config) => {
127
+ let idle_timeout = google:: protobuf:: Duration :: try_from ( config. idle_timeout )
128
+ . map_err ( |e| format ! ( "Unable to convert stateful nat idle timeout: {e}" ) ) ?;
129
+ Some ( gateway_config:: expose:: Nat :: Stateful (
130
+ gateway_config:: PeeringStatefulNat {
131
+ idle_timeout : Some ( idle_timeout) ,
132
+ } ,
133
+ ) )
134
+ }
135
+ VpcExposeNatConfig :: Stateless ( _) => Some ( gateway_config:: expose:: Nat :: Stateless (
136
+ gateway_config:: PeeringStatelessNat { } ,
137
+ ) ) ,
138
+ }
139
+ } else {
140
+ None
141
+ } ;
93
142
Ok ( gateway_config:: Expose {
94
143
ips,
95
144
r#as : as_rules,
145
+ nat,
96
146
} )
97
147
}
98
148
}
0 commit comments