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
@@ -102,6 +135,11 @@ def get_parser(self, prog_name):
102135 metavar = '<host>' ,
103136 help = _ ("List only agents running on the specified host" )
104137 )
138+ parser .add_argument (
139+ '--network' ,
140+ metavar = '<network>' ,
141+ help = _ ('List agents hosting a network (name or ID)' )
142+ )
105143 return parser
106144
107145 def take_action (self , parsed_args ):
@@ -140,16 +178,72 @@ def take_action(self, parsed_args):
140178 }
141179
142180 filters = {}
143- if parsed_args .agent_type is not None :
144- filters ['agent_type' ] = key_value [parsed_args .agent_type ]
145- if parsed_args .host is not None :
146- filters ['host' ] = parsed_args .host
147-
148- data = client .agents (** filters )
149- return (column_headers ,
150- (utils .get_item_properties (
151- s , columns , formatters = _formatters ,
152- ) for s in data ))
181+ 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+ )
194+ network = client .find_network (
195+ parsed_args .network , ignore_missing = False )
196+ 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 ))
203+ else :
204+ if parsed_args .agent_type is not None :
205+ filters ['agent_type' ] = key_value [parsed_args .agent_type ]
206+ if parsed_args .host is not None :
207+ filters ['host' ] = parsed_args .host
208+
209+ data = client .agents (** filters )
210+ return (column_headers ,
211+ (utils .get_item_properties (
212+ s , columns , formatters = _formatters ,
213+ ) for s in data ))
214+
215+
216+ class RemoveNetworkFromAgent (command .Command ):
217+ _description = _ ("Remove network from an agent." )
218+
219+ def get_parser (self , prog_name ):
220+ parser = super (RemoveNetworkFromAgent , self ).get_parser (prog_name )
221+ parser .add_argument (
222+ '--dhcp' ,
223+ action = 'store_true' ,
224+ help = _ ('Remove network from DHCP agent' ))
225+ parser .add_argument (
226+ 'agent_id' ,
227+ metavar = '<agent-id>' ,
228+ help = _ ('Agent to which a network is removed. (ID only)' ))
229+ parser .add_argument (
230+ 'network' ,
231+ metavar = '<network>' ,
232+ help = _ ('Network to be removed from an agent. (ID or name)' ))
233+ return parser
234+
235+ def take_action (self , parsed_args ):
236+ client = self .app .client_manager .network
237+ agent = client .get_agent (parsed_args .agent_id )
238+ if parsed_args .dhcp :
239+ network = client .find_network (
240+ parsed_args .network , ignore_missing = False )
241+ try :
242+ client .remove_dhcp_agent_from_network (agent , network )
243+ except Exception :
244+ msg = 'Failed to remove {} to {}' .format (
245+ network .name , agent .agent_type )
246+ exceptions .CommandError (msg )
153247
154248
155249# TODO(huanxuan): Use the SDK resource mapped attribute names once the
0 commit comments