|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +"""Network agent action implementations""" |
| 15 | + |
| 16 | +import logging |
| 17 | + |
| 18 | +from osc_lib.command import command |
| 19 | +from osc_lib import exceptions |
| 20 | +from osc_lib import utils |
| 21 | + |
| 22 | +from openstackclient.i18n import _ |
| 23 | + |
| 24 | + |
| 25 | +LOG = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +def _format_admin_state(state): |
| 29 | + return 'UP' if state else 'DOWN' |
| 30 | + |
| 31 | + |
| 32 | +_formatters = { |
| 33 | + 'admin_state_up': _format_admin_state, |
| 34 | + 'configurations': utils.format_dict, |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +class DeleteNetworkAgent(command.Command): |
| 39 | + """Delete network agent(s)""" |
| 40 | + |
| 41 | + def get_parser(self, prog_name): |
| 42 | + parser = super(DeleteNetworkAgent, self).get_parser(prog_name) |
| 43 | + parser.add_argument( |
| 44 | + 'network_agent', |
| 45 | + metavar="<network-agent>", |
| 46 | + nargs='+', |
| 47 | + help=(_("Network agent(s) to delete (ID only)")) |
| 48 | + ) |
| 49 | + return parser |
| 50 | + |
| 51 | + def take_action(self, parsed_args): |
| 52 | + client = self.app.client_manager.network |
| 53 | + result = 0 |
| 54 | + |
| 55 | + for agent in parsed_args.network_agent: |
| 56 | + try: |
| 57 | + obj = client.get_agent(agent, ignore_missing=False) |
| 58 | + client.delete_agent(obj) |
| 59 | + except Exception as e: |
| 60 | + result += 1 |
| 61 | + LOG.error(_("Failed to delete network agent with " |
| 62 | + "ID '%(agent)s': %(e)s"), |
| 63 | + {'agent': agent, 'e': e}) |
| 64 | + |
| 65 | + if result > 0: |
| 66 | + total = len(parsed_args.network_agent) |
| 67 | + msg = (_("%(result)s of %(total)s network agents failed " |
| 68 | + "to delete.") % {'result': result, 'total': total}) |
| 69 | + raise exceptions.CommandError(msg) |
| 70 | + |
| 71 | + |
| 72 | +class ListNetworkAgent(command.Lister): |
| 73 | + """List network agents""" |
| 74 | + |
| 75 | + def take_action(self, parsed_args): |
| 76 | + client = self.app.client_manager.network |
| 77 | + columns = ( |
| 78 | + 'id', |
| 79 | + 'agent_type', |
| 80 | + 'host', |
| 81 | + 'availability_zone', |
| 82 | + 'alive', |
| 83 | + 'admin_state_up', |
| 84 | + 'binary' |
| 85 | + ) |
| 86 | + column_headers = ( |
| 87 | + 'ID', |
| 88 | + 'Agent Type', |
| 89 | + 'Host', |
| 90 | + 'Availability Zone', |
| 91 | + 'Alive', |
| 92 | + 'State', |
| 93 | + 'Binary' |
| 94 | + ) |
| 95 | + data = client.agents() |
| 96 | + return (column_headers, |
| 97 | + (utils.get_item_properties( |
| 98 | + s, columns, formatters=_formatters, |
| 99 | + ) for s in data)) |
| 100 | + |
| 101 | + |
| 102 | +class ShowNetworkAgent(command.ShowOne): |
| 103 | + """Display network agent details""" |
| 104 | + |
| 105 | + def get_parser(self, prog_name): |
| 106 | + parser = super(ShowNetworkAgent, self).get_parser(prog_name) |
| 107 | + parser.add_argument( |
| 108 | + 'network_agent', |
| 109 | + metavar="<network-agent>", |
| 110 | + help=(_("Network agent to display (ID only)")) |
| 111 | + ) |
| 112 | + return parser |
| 113 | + |
| 114 | + def take_action(self, parsed_args): |
| 115 | + client = self.app.client_manager.network |
| 116 | + obj = client.get_agent(parsed_args.network_agent, ignore_missing=False) |
| 117 | + columns = tuple(sorted(list(obj.keys()))) |
| 118 | + data = utils.get_item_properties(obj, columns, formatters=_formatters,) |
| 119 | + return columns, data |
0 commit comments