2626LOG = logging .getLogger (__name__ )
2727
2828
29+ def _format_alive (alive ):
30+ return ":-)" if alive else "XXX"
31+
32+
2933def _format_admin_state (state ):
3034 return 'UP' if state else 'DOWN'
3135
3236_formatters = {
37+ 'is_alive' : _format_alive ,
38+ 'alive' : _format_alive ,
3339 'admin_state_up' : _format_admin_state ,
3440 'is_admin_state_up' : _format_admin_state ,
3541 'configurations' : utils .format_dict ,
@@ -60,7 +66,7 @@ def get_parser(self, prog_name):
6066 parser .add_argument (
6167 'network' ,
6268 metavar = '<network>' ,
63- help = _ ('Network to be added to an agent (ID or name )' ))
69+ help = _ ('Network to be added to an agent (name or ID )' ))
6470
6571 return parser
6672
@@ -78,6 +84,37 @@ def take_action(self, parsed_args):
7884 exceptions .CommandError (msg )
7985
8086
87+ class AddRouterToAgent (command .Command ):
88+ _description = _ ("Add router to an agent" )
89+
90+ def get_parser (self , prog_name ):
91+ parser = super (AddRouterToAgent , self ).get_parser (prog_name )
92+ parser .add_argument (
93+ '--l3' ,
94+ action = 'store_true' ,
95+ help = _ ('Add router to an L3 agent' )
96+ )
97+ parser .add_argument (
98+ 'agent_id' ,
99+ metavar = '<agent-id>' ,
100+ help = _ ("Agent to which a router is added (ID only)" )
101+ )
102+ parser .add_argument (
103+ 'router' ,
104+ metavar = '<router>' ,
105+ help = _ ("Router to be added to an agent (name or ID)" )
106+ )
107+
108+ return parser
109+
110+ def take_action (self , parsed_args ):
111+ client = self .app .client_manager .network
112+ agent = client .get_agent (parsed_args .agent_id )
113+ router = client .find_router (parsed_args .router , ignore_missing = False )
114+ if parsed_args .l3 :
115+ client .add_router_to_agent (agent , router )
116+
117+
81118class DeleteNetworkAgent (command .Command ):
82119 _description = _ ("Delete network agent(s)" )
83120
@@ -135,11 +172,24 @@ def get_parser(self, prog_name):
135172 metavar = '<host>' ,
136173 help = _ ("List only agents running on the specified host" )
137174 )
138- parser .add_argument (
175+ agent_type_group = parser .add_mutually_exclusive_group ()
176+ agent_type_group .add_argument (
139177 '--network' ,
140178 metavar = '<network>' ,
141179 help = _ ('List agents hosting a network (name or ID)' )
142180 )
181+ agent_type_group .add_argument (
182+ '--router' ,
183+ metavar = '<router>' ,
184+ help = _ ('List agents hosting this router (name or ID)' )
185+ )
186+ parser .add_argument (
187+ '--long' ,
188+ action = 'store_true' ,
189+ default = False ,
190+ help = _ ("List additional fields in output" )
191+ )
192+
143193 return parser
144194
145195 def take_action (self , parsed_args ):
@@ -178,39 +228,29 @@ def take_action(self, parsed_args):
178228 }
179229
180230 filters = {}
231+
181232 if parsed_args .network is not None :
182- columns = (
183- 'id' ,
184- 'host' ,
185- 'is_admin_state_up' ,
186- 'is_alive' ,
187- )
188- column_headers = (
189- 'ID' ,
190- 'Host' ,
191- 'Admin State Up' ,
192- 'Alive' ,
193- )
194233 network = client .find_network (
195234 parsed_args .network , ignore_missing = False )
196235 data = client .network_hosting_dhcp_agents (network )
197-
198- return (column_headers ,
199- (utils .get_item_properties (
200- s , columns ,
201- formatters = _formatters ,
202- ) for s in data ))
236+ elif parsed_args .router is not None :
237+ if parsed_args .long :
238+ columns += ('ha_state' ,)
239+ column_headers += ('HA State' ,)
240+ router = client .find_router (parsed_args .router ,
241+ ignore_missing = False )
242+ data = client .routers_hosting_l3_agents (router )
203243 else :
204244 if parsed_args .agent_type is not None :
205245 filters ['agent_type' ] = key_value [parsed_args .agent_type ]
206246 if parsed_args .host is not None :
207247 filters ['host' ] = parsed_args .host
208248
209249 data = client .agents (** filters )
210- return (column_headers ,
211- (utils .get_item_properties (
212- s , columns , formatters = _formatters ,
213- ) for s in data ))
250+ return (column_headers ,
251+ (utils .get_item_properties (
252+ s , columns , formatters = _formatters ,
253+ ) for s in data ))
214254
215255
216256class RemoveNetworkFromAgent (command .Command ):
@@ -229,7 +269,7 @@ def get_parser(self, prog_name):
229269 parser .add_argument (
230270 'network' ,
231271 metavar = '<network>' ,
232- help = _ ('Network to be removed from an agent (ID or name )' ))
272+ help = _ ('Network to be removed from an agent (name or ID )' ))
233273 return parser
234274
235275 def take_action (self , parsed_args ):
@@ -246,6 +286,37 @@ def take_action(self, parsed_args):
246286 exceptions .CommandError (msg )
247287
248288
289+ class RemoveRouterFromAgent (command .Command ):
290+ _description = _ ("Remove router from an agent" )
291+
292+ def get_parser (self , prog_name ):
293+ parser = super (RemoveRouterFromAgent , self ).get_parser (prog_name )
294+ parser .add_argument (
295+ '--l3' ,
296+ action = 'store_true' ,
297+ help = _ ('Remove router from an L3 agent' )
298+ )
299+ parser .add_argument (
300+ 'agent_id' ,
301+ metavar = '<agent-id>' ,
302+ help = _ ("Agent from which router will be removed (ID only)" )
303+ )
304+ parser .add_argument (
305+ 'router' ,
306+ metavar = '<router>' ,
307+ help = _ ("Router to be removed from an agent (name or ID)" )
308+ )
309+
310+ return parser
311+
312+ def take_action (self , parsed_args ):
313+ client = self .app .client_manager .network
314+ agent = client .get_agent (parsed_args .agent_id )
315+ router = client .find_router (parsed_args .router , ignore_missing = False )
316+ if parsed_args .l3 :
317+ client .remove_router_from_agent (agent , router )
318+
319+
249320# TODO(huanxuan): Use the SDK resource mapped attribute names once the
250321# OSC minimum requirements include SDK 1.0.
251322class SetNetworkAgent (command .Command ):
0 commit comments