|
| 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 segment action implementations""" |
| 15 | + |
| 16 | +# TODO(rtheis): Add description and name properties when support is available. |
| 17 | + |
| 18 | +from openstackclient.common import command |
| 19 | +from openstackclient.common import exceptions |
| 20 | +from openstackclient.common import utils |
| 21 | +from openstackclient.i18n import _ |
| 22 | + |
| 23 | + |
| 24 | +class ListNetworkSegment(command.Lister): |
| 25 | + """List network segments |
| 26 | +
|
| 27 | + (Caution: This is a beta command and subject to change. |
| 28 | + Use global option --enable-beta-commands to enable |
| 29 | + this command) |
| 30 | + """ |
| 31 | + |
| 32 | + def get_parser(self, prog_name): |
| 33 | + parser = super(ListNetworkSegment, self).get_parser(prog_name) |
| 34 | + parser.add_argument( |
| 35 | + '--long', |
| 36 | + action='store_true', |
| 37 | + default=False, |
| 38 | + help=_('List additional fields in output'), |
| 39 | + ) |
| 40 | + parser.add_argument( |
| 41 | + '--network', |
| 42 | + metavar='<network>', |
| 43 | + help=_('List network segments that belong to this ' |
| 44 | + 'network (name or ID)'), |
| 45 | + ) |
| 46 | + return parser |
| 47 | + |
| 48 | + def take_action(self, parsed_args): |
| 49 | + if not self.app.options.enable_beta_commands: |
| 50 | + msg = _('Caution: This is a beta command and subject to ' |
| 51 | + 'change. Use global option --enable-beta-commands ' |
| 52 | + 'to enable this command.') |
| 53 | + raise exceptions.CommandError(msg) |
| 54 | + |
| 55 | + network_client = self.app.client_manager.network |
| 56 | + |
| 57 | + filters = {} |
| 58 | + if parsed_args.network: |
| 59 | + _network = network_client.find_network( |
| 60 | + parsed_args.network, |
| 61 | + ignore_missing=False |
| 62 | + ) |
| 63 | + filters = {'network_id': _network.id} |
| 64 | + data = network_client.segments(**filters) |
| 65 | + |
| 66 | + headers = ( |
| 67 | + 'ID', |
| 68 | + 'Network', |
| 69 | + 'Network Type', |
| 70 | + 'Segment', |
| 71 | + ) |
| 72 | + columns = ( |
| 73 | + 'id', |
| 74 | + 'network_id', |
| 75 | + 'network_type', |
| 76 | + 'segmentation_id', |
| 77 | + ) |
| 78 | + if parsed_args.long: |
| 79 | + headers = headers + ( |
| 80 | + 'Physical Network', |
| 81 | + ) |
| 82 | + columns = columns + ( |
| 83 | + 'physical_network', |
| 84 | + ) |
| 85 | + |
| 86 | + return (headers, |
| 87 | + (utils.get_item_properties( |
| 88 | + s, columns, |
| 89 | + formatters={}, |
| 90 | + ) for s in data)) |
| 91 | + |
| 92 | + |
| 93 | +class ShowNetworkSegment(command.ShowOne): |
| 94 | + """Display network segment details |
| 95 | +
|
| 96 | + (Caution: This is a beta command and subject to change. |
| 97 | + Use global option --enable-beta-commands to enable |
| 98 | + this command) |
| 99 | + """ |
| 100 | + |
| 101 | + def get_parser(self, prog_name): |
| 102 | + parser = super(ShowNetworkSegment, self).get_parser(prog_name) |
| 103 | + parser.add_argument( |
| 104 | + 'network_segment', |
| 105 | + metavar='<network-segment>', |
| 106 | + help=_('Network segment to display (ID only)'), |
| 107 | + ) |
| 108 | + return parser |
| 109 | + |
| 110 | + def take_action(self, parsed_args): |
| 111 | + if not self.app.options.enable_beta_commands: |
| 112 | + msg = _('Caution: This is a beta command and subject to ' |
| 113 | + 'change. Use global option --enable-beta-commands ' |
| 114 | + 'to enable this command.') |
| 115 | + raise exceptions.CommandError(msg) |
| 116 | + |
| 117 | + client = self.app.client_manager.network |
| 118 | + obj = client.find_segment( |
| 119 | + parsed_args.network_segment, |
| 120 | + ignore_missing=False |
| 121 | + ) |
| 122 | + columns = tuple(sorted(obj.keys())) |
| 123 | + data = utils.get_item_properties(obj, columns) |
| 124 | + return (columns, data) |
0 commit comments