| 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 |
from osc_lib.command import command |
| 15 |
from osc_lib import utils |
| 16 |
|
| 17 |
from neutronclient._i18n import _ |
| 18 |
|
| 19 |
|
| 20 |
def _format_alive_state(item): |
| 21 |
return ':-)' if item else 'XXX' |
| 22 |
|
| 23 |
|
| 24 |
_formatters = { |
| 25 |
'alive': _format_alive_state |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
def add_common_args(parser): |
| 30 |
parser.add_argument('dragent_id', |
| 31 |
metavar='<agent-id>', |
| 32 |
help=_("ID of the dynamic routing agent")) |
| 33 |
parser.add_argument('bgp_speaker', |
| 34 |
metavar='<bgp-speaker>', |
| 35 |
help=_("ID or name of the BGP speaker")) |
| 36 |
|
| 37 |
|
| 38 |
class AddBgpSpeakerToDRAgent(command.Command): |
| 39 |
"""Add a BGP speaker to a dynamic routing agent""" |
| 40 |
|
| 41 |
def get_parser(self, prog_name): |
| 42 |
parser = super(AddBgpSpeakerToDRAgent, self).get_parser(prog_name) |
| 43 |
add_common_args(parser) |
| 44 |
return parser |
| 45 |
|
| 46 |
def take_action(self, parsed_args): |
| 47 |
client = self.app.client_manager.network |
| 48 |
speaker_id = client.find_bgp_speaker(parsed_args.bgp_speaker).id |
| 49 |
client.add_bgp_speaker_to_dragent(parsed_args.dragent_id, speaker_id) |
| 50 |
|
| 51 |
|
| 52 |
class RemoveBgpSpeakerFromDRAgent(command.Command): |
| 53 |
"""Removes a BGP speaker from a dynamic routing agent""" |
| 54 |
|
| 55 |
def get_parser(self, prog_name): |
| 56 |
parser = super(RemoveBgpSpeakerFromDRAgent, self).get_parser( |
| 57 |
prog_name) |
| 58 |
add_common_args(parser) |
| 59 |
return parser |
| 60 |
|
| 61 |
def take_action(self, parsed_args): |
| 62 |
client = self.app.client_manager.network |
| 63 |
speaker_id = client.find_bgp_speaker(parsed_args.bgp_speaker).id |
| 64 |
client.remove_bgp_speaker_from_dragent(parsed_args.dragent_id, |
| 65 |
speaker_id) |
| 66 |
|
| 67 |
|
| 68 |
class ListDRAgent(command.Lister): |
| 69 |
"""List dynamic routing agents""" |
| 70 |
|
| 71 |
resource = 'agent' |
| 72 |
list_columns = ['id', 'host', 'admin_state_up', 'alive'] |
| 73 |
unknown_parts_flag = False |
| 74 |
|
| 75 |
def get_parser(self, prog_name): |
| 76 |
parser = super(ListDRAgent, |
| 77 |
self).get_parser(prog_name) |
| 78 |
parser.add_argument('--bgp-speaker', |
| 79 |
metavar='<bgp-speaker>', |
| 80 |
help=_("List dynamic routing agents hosting a " |
| 81 |
"BGP speaker (name or ID)")) |
| 82 |
return parser |
| 83 |
|
| 84 |
def take_action(self, parsed_args): |
| 85 |
client = self.app.client_manager.network |
| 86 |
if parsed_args.bgp_speaker is not None: |
| 87 |
speaker_id = client.find_bgp_speaker(parsed_args.bgp_speaker).id |
| 88 |
data = client.get_bgp_dragents_hosting_speaker(speaker_id) |
| 89 |
else: |
| 90 |
attrs = {'agent_type': 'BGP dynamic routing agent'} |
| 91 |
data = client.agents(**attrs) |
| 92 |
columns = ( |
| 93 |
'id', |
| 94 |
'agent_type', |
| 95 |
'host', |
| 96 |
'availability_zone', |
| 97 |
'is_alive', |
| 98 |
'is_admin_state_up', |
| 99 |
'binary' |
| 100 |
) |
| 101 |
column_headers = ( |
| 102 |
'ID', |
| 103 |
'Agent Type', |
| 104 |
'Host', |
| 105 |
'Availability Zone', |
| 106 |
'Alive', |
| 107 |
'State', |
| 108 |
'Binary' |
| 109 |
) |
| 110 |
return (column_headers, |
| 111 |
(utils.get_item_properties( |
| 112 |
s, columns,) for s in data)) |