2626from CsRoute import CsRoute
2727from CsRule import CsRule
2828
29- VRRP_TYPES = ['guest' , 'public' ]
29+ VRRP_TYPES = ['guest' ]
3030
3131
3232class CsAddress (CsDataBag ):
@@ -42,38 +42,37 @@ def get_ips(self):
4242 if dev == "id" :
4343 continue
4444 for ip in self .dbag [dev ]:
45- ret .append (CsInterface (ip ))
45+ ret .append (CsInterface (ip , self . config ))
4646 return ret
4747
48- def get_guest_ip (self ):
48+ def get_guest_if (self ):
4949 """
50- Return the ip of the first guest interface
51- For use with routers not vpcrouters
50+ Return CsIp object for the first guest interface
5251 """
5352 for ip in self .get_ips ():
5453 if ip .is_guest ():
55- return ip . get_ip ()
54+ return ip
5655 return None
5756
58- def get_guest_gateway (self ):
57+ def get_guest_ip (self ):
5958 """
60- Return the gateway of the first guest interface
59+ Return the ip of the first guest interface
6160 For use with routers not vpcrouters
6261 """
63- for ip in self .get_ips ():
64- if ip . is_guest () :
65- return ip .get_gateway ()
62+ ip = self .get_guest_if ()
63+ if ip :
64+ return ip .get_ip ()
6665 return None
6766
6867 def get_guest_netmask (self ):
6968 """
70- Return the gateway of the first guest interface
69+ Return the netmask of the first guest interface
7170 For use with routers not vpcrouters
7271 """
73- for ip in self .get_ips ():
74- if ip . is_guest () :
75- return ip .get_netmask ()
76- return None
72+ ip = self .get_guest_if ()
73+ if ip :
74+ return ip .get_netmask ()
75+ return "255.255.255.0"
7776
7877 def needs_vrrp (self , o ):
7978 """
@@ -144,8 +143,9 @@ def add_netstats(self, address):
144143
145144class CsInterface :
146145 """ Hold one single ip """
147- def __init__ (self , o ):
146+ def __init__ (self , o , config ):
148147 self .address = o
148+ self .config = config
149149
150150 def get_ip (self ):
151151 return self .get_attr ("public_ip" )
@@ -154,7 +154,17 @@ def get_netmask(self):
154154 return self .get_attr ("netmask" )
155155
156156 def get_gateway (self ):
157- return self .get_attr ("gateway" )
157+ if self .config .is_vpc ():
158+ return self .get_attr ("gateway" )
159+ else :
160+ return self .config .cmdline ().get_guest_gw ()
161+
162+ def get_gateway_cidr (self ):
163+ return "%s/%s" % (self .get_gateway (), self .get_size ())
164+
165+ def get_size (self ):
166+ """ Return the network size in bits (24, 16, 8 etc) """
167+ return self .get_attr ("size" )
158168
159169 def get_device (self ):
160170 return self .get_attr ("device" )
@@ -205,7 +215,7 @@ def __init__(self, dev, config):
205215 self .tableNo = dev [3 ]
206216 self .table = "Table_%s" % dev
207217 self .fw = config .get_fw ()
208- self .cl = config .get_cmdline ()
218+ self .cl = config .cmdline ()
209219
210220 def configure_rp (self ):
211221 """
@@ -250,7 +260,7 @@ def __init__(self, dev, config):
250260 self .address = {}
251261 self .list ()
252262 self .fw = config .get_fw ()
253- self .cl = config .get_cmdline ()
263+ self .cl = config .cmdline ()
254264 self .config = config
255265
256266 def setAddress (self , address ):
@@ -284,7 +294,7 @@ def check_is_up(self):
284294 if " DOWN " in i :
285295 cmd2 = "ip link set %s up" % self .getDevice ()
286296 # Do not change the state of ips on a redundant router that are managed by vrrp or CsRedundant
287- if self .cl .is_redundant () and self .needs_vrrp ():
297+ if self .config . cmdline () .is_redundant () and self .needs_vrrp ():
288298 pass
289299 else :
290300 CsHelper .execute (cmd2 )
@@ -412,7 +422,7 @@ def post_config_change(self, method):
412422
413423 if self .get_type () == "public" and self .config .is_vpc ():
414424 if self .address ["source_nat" ]:
415- vpccidr = self .cl .get_vpccidr ()
425+ vpccidr = self .config . cmdline () .get_vpccidr ()
416426 self .fw .append (["filter" , "" , "-A FORWARD -s %s ! -d %s -j ACCEPT" % (vpccidr , vpccidr )])
417427 self .fw .append (["nat" , "" , "-A POSTROUTING -j SNAT -o %s --to-source %s" % (self .dev , self .address ['public_ip' ])])
418428 # route.flush()
0 commit comments