diff options
Diffstat (limited to 'openstackclient/network')
| -rw-r--r-- | openstackclient/network/common.py | 102 | ||||
| -rw-r--r-- | openstackclient/network/v2/floating_ip.py | 66 | ||||
| -rw-r--r-- | openstackclient/network/v2/network.py | 25 |
3 files changed, 184 insertions, 9 deletions
diff --git a/openstackclient/network/common.py b/openstackclient/network/common.py index ae489523..1e2c4cce 100644 --- a/openstackclient/network/common.py +++ b/openstackclient/network/common.py @@ -66,3 +66,105 @@ class NetworkAndComputeCommand(command.Command): def take_action_compute(self, client, parsed_args): """Override to do something useful.""" pass + + +@six.add_metaclass(abc.ABCMeta) +class NetworkAndComputeLister(command.Lister): + """Network and Compute Lister + + Lister class for commands that support implementation via + the network or compute endpoint. Such commands have different + implementations for take_action() and may even have different + arguments. + """ + + def take_action(self, parsed_args): + if self.app.client_manager.is_network_endpoint_enabled(): + return self.take_action_network(self.app.client_manager.network, + parsed_args) + else: + return self.take_action_compute(self.app.client_manager.compute, + parsed_args) + + def get_parser(self, prog_name): + self.log.debug('get_parser(%s)', prog_name) + parser = super(NetworkAndComputeLister, self).get_parser(prog_name) + parser = self.update_parser_common(parser) + self.log.debug('common parser: %s', parser) + if self.app.client_manager.is_network_endpoint_enabled(): + return self.update_parser_network(parser) + else: + return self.update_parser_compute(parser) + + def update_parser_common(self, parser): + """Default is no updates to parser.""" + return parser + + def update_parser_network(self, parser): + """Default is no updates to parser.""" + return parser + + def update_parser_compute(self, parser): + """Default is no updates to parser.""" + return parser + + @abc.abstractmethod + def take_action_network(self, client, parsed_args): + """Override to do something useful.""" + pass + + @abc.abstractmethod + def take_action_compute(self, client, parsed_args): + """Override to do something useful.""" + pass + + +@six.add_metaclass(abc.ABCMeta) +class NetworkAndComputeShowOne(command.ShowOne): + """Network and Compute ShowOne + + ShowOne class for commands that support implementation via + the network or compute endpoint. Such commands have different + implementations for take_action() and may even have different + arguments. + """ + + def take_action(self, parsed_args): + if self.app.client_manager.is_network_endpoint_enabled(): + return self.take_action_network(self.app.client_manager.network, + parsed_args) + else: + return self.take_action_compute(self.app.client_manager.compute, + parsed_args) + + def get_parser(self, prog_name): + self.log.debug('get_parser(%s)', prog_name) + parser = super(NetworkAndComputeShowOne, self).get_parser(prog_name) + parser = self.update_parser_common(parser) + self.log.debug('common parser: %s', parser) + if self.app.client_manager.is_network_endpoint_enabled(): + return self.update_parser_network(parser) + else: + return self.update_parser_compute(parser) + + def update_parser_common(self, parser): + """Default is no updates to parser.""" + return parser + + def update_parser_network(self, parser): + """Default is no updates to parser.""" + return parser + + def update_parser_compute(self, parser): + """Default is no updates to parser.""" + return parser + + @abc.abstractmethod + def take_action_network(self, client, parsed_args): + """Override to do something useful.""" + pass + + @abc.abstractmethod + def take_action_compute(self, client, parsed_args): + """Override to do something useful.""" + pass diff --git a/openstackclient/network/v2/floating_ip.py b/openstackclient/network/v2/floating_ip.py new file mode 100644 index 00000000..48895048 --- /dev/null +++ b/openstackclient/network/v2/floating_ip.py @@ -0,0 +1,66 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +"""IP Floating action implementations""" + +from openstackclient.common import utils +from openstackclient.network import common + + +class DeleteFloatingIP(common.NetworkAndComputeCommand): + """Delete floating IP""" + + def update_parser_common(self, parser): + parser.add_argument( + 'floating_ip', + metavar="<floating-ip>", + help=("Floating IP to delete (IP address or ID)") + ) + return parser + + def take_action_network(self, client, parsed_args): + obj = client.find_ip(parsed_args.floating_ip) + client.delete_ip(obj) + + def take_action_compute(self, client, parsed_args): + obj = utils.find_resource( + client.floating_ips, + parsed_args.floating_ip, + ) + client.floating_ips.delete(obj.id) + + +class ListFloatingIP(common.NetworkAndComputeLister): + """List floating IP(s)""" + + columns = ('ID', 'IP', 'Fixed IP', 'Instance ID', 'Pool') + column_headers = ('ID', 'Floating IP', 'Fixed IP', 'Server ID', 'Pool') + + def take_action_network(self, client, parsed_args): + query = {} + data = client.ips(**query) + + return (self.column_headers, + (utils.get_item_properties( + s, self.columns, + formatters={}, + ) for s in data)) + + def take_action_compute(self, client, parsed_args): + data = client.floating_ips.list() + + return (self.column_headers, + (utils.get_item_properties( + s, self.columns, + formatters={}, + ) for s in data)) diff --git a/openstackclient/network/v2/network.py b/openstackclient/network/v2/network.py index 61237219..636c333e 100644 --- a/openstackclient/network/v2/network.py +++ b/openstackclient/network/v2/network.py @@ -17,6 +17,7 @@ from openstackclient.common import command from openstackclient.common import exceptions from openstackclient.common import utils from openstackclient.identity import common as identity_common +from openstackclient.network import common def _format_admin_state(item): @@ -141,11 +142,10 @@ class CreateNetwork(command.ShowOne): return (columns, data) -class DeleteNetwork(command.Command): +class DeleteNetwork(common.NetworkAndComputeCommand): """Delete network(s)""" - def get_parser(self, prog_name): - parser = super(DeleteNetwork, self).get_parser(prog_name) + def update_parser_common(self, parser): parser.add_argument( 'network', metavar="<network>", @@ -154,12 +154,19 @@ class DeleteNetwork(command.Command): ) return parser - def take_action(self, parsed_args): - client = self.app.client_manager.network + def take_action_network(self, client, parsed_args): for network in parsed_args.network: obj = client.find_network(network) client.delete_network(obj) + def take_action_compute(self, client, parsed_args): + for network in parsed_args.network: + network = utils.find_resource( + client.networks, + network, + ) + client.networks.delete(network.id) + class ListNetwork(command.Lister): """List networks""" @@ -238,7 +245,7 @@ class SetNetwork(command.Command): def get_parser(self, prog_name): parser = super(SetNetwork, self).get_parser(prog_name) parser.add_argument( - 'identifier', + 'network', metavar="<network>", help=("Network to modify (name or ID)") ) @@ -279,7 +286,7 @@ class SetNetwork(command.Command): def take_action(self, parsed_args): client = self.app.client_manager.network - obj = client.find_network(parsed_args.identifier, ignore_missing=False) + obj = client.find_network(parsed_args.network, ignore_missing=False) attrs = _get_attrs(self.app.client_manager, parsed_args) if attrs == {}: @@ -296,7 +303,7 @@ class ShowNetwork(command.ShowOne): def get_parser(self, prog_name): parser = super(ShowNetwork, self).get_parser(prog_name) parser.add_argument( - 'identifier', + 'network', metavar="<network>", help=("Network to display (name or ID)") ) @@ -304,7 +311,7 @@ class ShowNetwork(command.ShowOne): def take_action(self, parsed_args): client = self.app.client_manager.network - obj = client.find_network(parsed_args.identifier, ignore_missing=False) + obj = client.find_network(parsed_args.network, ignore_missing=False) columns = _get_columns(obj) data = utils.get_item_properties(obj, columns, formatters=_formatters) return (columns, data) |
