2929def _format_admin_state (state ):
3030 return 'UP' if state else 'DOWN'
3131
32-
3332_formatters = {
3433 'admin_state_up' : _format_admin_state ,
3534 'is_admin_state_up' : _format_admin_state ,
@@ -45,6 +44,40 @@ def _get_network_columns(item):
4544 return sdk_utils .get_osc_show_columns_for_sdk_resource (item , column_map )
4645
4746
47+ class AddNetworkToAgent (command .Command ):
48+ _description = _ ("Add network to an agent" )
49+
50+ def get_parser (self , prog_name ):
51+ parser = super (AddNetworkToAgent , self ).get_parser (prog_name )
52+ parser .add_argument (
53+ '--dhcp' ,
54+ action = 'store_true' ,
55+ help = _ ('Add network to a DHCP agent' ))
56+ parser .add_argument (
57+ 'agent_id' ,
58+ metavar = '<agent-id>' ,
59+ help = _ ('Agent to which a network is added. (ID only)' ))
60+ parser .add_argument (
61+ 'network' ,
62+ metavar = '<network>' ,
63+ help = _ ('Network to be added to an agent. (ID or name)' ))
64+
65+ return parser
66+
67+ def take_action (self , parsed_args ):
68+ client = self .app .client_manager .network
69+ agent = client .get_agent (parsed_args .agent_id )
70+ if parsed_args .dhcp :
71+ network = client .find_network (
72+ parsed_args .network , ignore_missing = False )
73+ try :
74+ client .add_dhcp_agent_to_network (agent , network )
75+ except Exception :
76+ msg = 'Failed to add {} to {}' .format (
77+ network .name , agent .agent_type )
78+ exceptions .CommandError (msg )
79+
80+
4881class DeleteNetworkAgent (command .Command ):
4982 _description = _ ("Delete network agent(s)" )
5083
@@ -101,6 +134,11 @@ def get_parser(self, prog_name):
101134 metavar = '<host>' ,
102135 help = _ ("List only agents running on the specified host" )
103136 )
137+ parser .add_argument (
138+ '--network' ,
139+ metavar = '<network>' ,
140+ help = _ ('List agents hosting a network (name or ID)' )
141+ )
104142 return parser
105143
106144 def take_action (self , parsed_args ):
@@ -138,16 +176,72 @@ def take_action(self, parsed_args):
138176 }
139177
140178 filters = {}
141- if parsed_args .agent_type is not None :
142- filters ['agent_type' ] = key_value [parsed_args .agent_type ]
143- if parsed_args .host is not None :
144- filters ['host' ] = parsed_args .host
145-
146- data = client .agents (** filters )
147- return (column_headers ,
148- (utils .get_item_properties (
149- s , columns , formatters = _formatters ,
150- ) for s in data ))
179+ if parsed_args .network is not None :
180+ columns = (
181+ 'id' ,
182+ 'host' ,
183+ 'is_admin_state_up' ,
184+ 'is_alive' ,
185+ )
186+ column_headers = (
187+ 'ID' ,
188+ 'Host' ,
189+ 'Admin State Up' ,
190+ 'Alive' ,
191+ )
192+ network = client .find_network (
193+ parsed_args .network , ignore_missing = False )
194+ data = client .network_hosting_dhcp_agents (network )
195+
196+ return (column_headers ,
197+ (utils .get_item_properties (
198+ s , columns ,
199+ formatters = _formatters ,
200+ ) for s in data ))
201+ else :
202+ if parsed_args .agent_type is not None :
203+ filters ['agent_type' ] = key_value [parsed_args .agent_type ]
204+ if parsed_args .host is not None :
205+ filters ['host' ] = parsed_args .host
206+
207+ data = client .agents (** filters )
208+ return (column_headers ,
209+ (utils .get_item_properties (
210+ s , columns , formatters = _formatters ,
211+ ) for s in data ))
212+
213+
214+ class RemoveNetworkFromAgent (command .Command ):
215+ _description = _ ("Remove network from an agent." )
216+
217+ def get_parser (self , prog_name ):
218+ parser = super (RemoveNetworkFromAgent , self ).get_parser (prog_name )
219+ parser .add_argument (
220+ '--dhcp' ,
221+ action = 'store_true' ,
222+ help = _ ('Remove network from DHCP agent' ))
223+ parser .add_argument (
224+ 'agent_id' ,
225+ metavar = '<agent-id>' ,
226+ help = _ ('Agent to which a network is removed. (ID only)' ))
227+ parser .add_argument (
228+ 'network' ,
229+ metavar = '<network>' ,
230+ help = _ ('Network to be removed from an agent. (ID or name)' ))
231+ return parser
232+
233+ def take_action (self , parsed_args ):
234+ client = self .app .client_manager .network
235+ agent = client .get_agent (parsed_args .agent_id )
236+ if parsed_args .dhcp :
237+ network = client .find_network (
238+ parsed_args .network , ignore_missing = False )
239+ try :
240+ client .remove_dhcp_agent_from_network (agent , network )
241+ except Exception :
242+ msg = 'Failed to remove {} to {}' .format (
243+ network .name , agent .agent_type )
244+ exceptions .CommandError (msg )
151245
152246
153247# TODO(huanxuan): Use the SDK resource mapped attribute names once the
0 commit comments