|
| 1 | +# Copyright 2012 OpenStack LLC. |
| 2 | +# All Rights Reserved |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | +# |
| 16 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +from quantumclient.common import utils |
| 21 | +from quantumclient.quantum.v2_0 import CreateCommand |
| 22 | +from quantumclient.quantum.v2_0 import DeleteCommand |
| 23 | +from quantumclient.quantum.v2_0 import ListCommand |
| 24 | +from quantumclient.quantum.v2_0 import QuantumCommand |
| 25 | +from quantumclient.quantum.v2_0 import ShowCommand |
| 26 | +from quantumclient.quantum.v2_0 import UpdateCommand |
| 27 | + |
| 28 | + |
| 29 | +def _format_external_gateway_info(router): |
| 30 | + try: |
| 31 | + return utils.dumps(router['external_gateway_info']) |
| 32 | + except Exception: |
| 33 | + return '' |
| 34 | + |
| 35 | + |
| 36 | +class ListRouter(ListCommand): |
| 37 | + """List routers that belong to a given tenant.""" |
| 38 | + |
| 39 | + resource = 'router' |
| 40 | + log = logging.getLogger(__name__ + '.ListRouter') |
| 41 | + _formatters = {'external_gateway_info': _format_external_gateway_info, } |
| 42 | + |
| 43 | + |
| 44 | +class ShowRouter(ShowCommand): |
| 45 | + """Show information of a given router.""" |
| 46 | + |
| 47 | + resource = 'router' |
| 48 | + log = logging.getLogger(__name__ + '.ShowRouter') |
| 49 | + |
| 50 | + |
| 51 | +class CreateRouter(CreateCommand): |
| 52 | + """Create a router for a given tenant.""" |
| 53 | + |
| 54 | + resource = 'router' |
| 55 | + log = logging.getLogger(__name__ + '.CreateRouter') |
| 56 | + |
| 57 | + def add_known_arguments(self, parser): |
| 58 | + parser.add_argument( |
| 59 | + '--admin_state_down', |
| 60 | + default=True, action='store_false', |
| 61 | + help='Set Admin State Up to false') |
| 62 | + parser.add_argument( |
| 63 | + 'name', metavar='name', |
| 64 | + help='Name of router to create') |
| 65 | + |
| 66 | + def args2body(self, parsed_args): |
| 67 | + body = {'router': { |
| 68 | + 'name': parsed_args.name, |
| 69 | + 'admin_state_up': parsed_args.admin_state_down, }, } |
| 70 | + if parsed_args.tenant_id: |
| 71 | + body['router'].update({'tenant_id': parsed_args.tenant_id}) |
| 72 | + return body |
| 73 | + |
| 74 | + |
| 75 | +class DeleteRouter(DeleteCommand): |
| 76 | + """Delete a given router.""" |
| 77 | + |
| 78 | + log = logging.getLogger(__name__ + '.DeleteRouter') |
| 79 | + resource = 'router' |
| 80 | + |
| 81 | + |
| 82 | +class UpdateRouter(UpdateCommand): |
| 83 | + """Update router's information.""" |
| 84 | + |
| 85 | + log = logging.getLogger(__name__ + '.UpdateRouter') |
| 86 | + resource = 'router' |
| 87 | + |
| 88 | + |
| 89 | +class RouterInterfaceCommand(QuantumCommand): |
| 90 | + """Based class to Add/Remove router interface.""" |
| 91 | + |
| 92 | + api = 'network' |
| 93 | + log = logging.getLogger(__name__ + '.AddInterfaceRouter') |
| 94 | + resource = 'router' |
| 95 | + |
| 96 | + def get_parser(self, prog_name): |
| 97 | + parser = super(RouterInterfaceCommand, self).get_parser(prog_name) |
| 98 | + parser.add_argument( |
| 99 | + 'router_id', metavar='router_id', |
| 100 | + help='ID of the router') |
| 101 | + parser.add_argument( |
| 102 | + 'subnet_id', metavar='subnet_id', |
| 103 | + help='ID of the internal subnet for the interface') |
| 104 | + return parser |
| 105 | + |
| 106 | + |
| 107 | +class AddInterfaceRouter(RouterInterfaceCommand): |
| 108 | + """Add an internal network interface to a router.""" |
| 109 | + |
| 110 | + def run(self, parsed_args): |
| 111 | + self.log.debug('run(%s)' % parsed_args) |
| 112 | + quantum_client = self.get_client() |
| 113 | + quantum_client.format = parsed_args.request_format |
| 114 | + #TODO(danwent): handle passing in port-id |
| 115 | + quantum_client.add_interface_router(parsed_args.router_id, |
| 116 | + {'subnet_id': |
| 117 | + parsed_args.subnet_id}) |
| 118 | + #TODO(danwent): print port ID that is added |
| 119 | + print >>self.app.stdout, ( |
| 120 | + _('Added interface to router %s') % parsed_args.router_id) |
| 121 | + |
| 122 | + |
| 123 | +class RemoveInterfaceRouter(RouterInterfaceCommand): |
| 124 | + """Remove an internal network interface from a router.""" |
| 125 | + |
| 126 | + def run(self, parsed_args): |
| 127 | + self.log.debug('run(%s)' % parsed_args) |
| 128 | + quantum_client = self.get_client() |
| 129 | + quantum_client.format = parsed_args.request_format |
| 130 | + #TODO(danwent): handle passing in port-id |
| 131 | + quantum_client.remove_interface_router(parsed_args.router_id, |
| 132 | + {'subnet_id': |
| 133 | + parsed_args.subnet_id}) |
| 134 | + print >>self.app.stdout, ( |
| 135 | + _('Removed interface from router %s') % parsed_args.router_id) |
| 136 | + |
| 137 | + |
| 138 | +class SetGatewayRouter(QuantumCommand): |
| 139 | + """Set the external network gateway for a router.""" |
| 140 | + |
| 141 | + log = logging.getLogger(__name__ + '.SetGatewayRouter') |
| 142 | + api = 'network' |
| 143 | + resource = 'router' |
| 144 | + |
| 145 | + def get_parser(self, prog_name): |
| 146 | + parser = super(SetGatewayRouter, self).get_parser(prog_name) |
| 147 | + parser.add_argument( |
| 148 | + 'router_id', metavar='router_id', |
| 149 | + help='ID of the router') |
| 150 | + parser.add_argument( |
| 151 | + 'external_network_id', metavar='external_network_id', |
| 152 | + help='ID of the external network for the gateway') |
| 153 | + return parser |
| 154 | + |
| 155 | + def run(self, parsed_args): |
| 156 | + self.log.debug('run(%s)' % parsed_args) |
| 157 | + quantum_client = self.get_client() |
| 158 | + quantum_client.format = parsed_args.request_format |
| 159 | + quantum_client.add_gateway_router(parsed_args.router_id, |
| 160 | + {'network_id': |
| 161 | + parsed_args.external_network_id}) |
| 162 | + print >>self.app.stdout, ( |
| 163 | + _('Set gateway for router %s') % parsed_args.router_id) |
| 164 | + |
| 165 | + |
| 166 | +class RemoveGatewayRouter(QuantumCommand): |
| 167 | + """Remove an external network gateway from a router.""" |
| 168 | + |
| 169 | + log = logging.getLogger(__name__ + '.RemoveGatewayRouter') |
| 170 | + api = 'network' |
| 171 | + resource = 'router' |
| 172 | + |
| 173 | + def get_parser(self, prog_name): |
| 174 | + parser = super(RemoveGatewayRouter, self).get_parser(prog_name) |
| 175 | + parser.add_argument( |
| 176 | + 'router_id', metavar='router_id', |
| 177 | + help='ID of the router') |
| 178 | + return parser |
| 179 | + |
| 180 | + def run(self, parsed_args): |
| 181 | + self.log.debug('run(%s)' % parsed_args) |
| 182 | + quantum_client = self.get_client() |
| 183 | + quantum_client.format = parsed_args.request_format |
| 184 | + quantum_client.remove_gateway_router(parsed_args.router_id) |
| 185 | + print >>self.app.stdout, ( |
| 186 | + _('Removed gateway from router %s') % parsed_args.router_id) |
0 commit comments