Skip to content
This repository was archived by the owner on Nov 29, 2023. It is now read-only.

Commit 38abece

Browse files
Dan WendlandtGary Kotton
authored andcommitted
initial client + CLI support for routers + floating ips
bp quantum-client-l3-floating-ip The task also does the following: 1. Fixes alignment of the --help output 2. Ensures that a show command prints a dictionary correctly Change-Id: Ib61b3e8748a7bd476ec008ab6ce20ab852e92f58
1 parent f574f77 commit 38abece

8 files changed

Lines changed: 731 additions & 2 deletions

File tree

‎quantumclient/quantum/v2_0/__init__.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ def get_data(self, parsed_args):
412412
else:
413413
value += str(_item)
414414
data[self.resource][k] = value
415+
elif isinstance(v, dict):
416+
value = utils.dumps(v)
417+
data[self.resource][k] = value
415418
elif v is None:
416419
data[self.resource][k] = ''
417420
return zip(*sorted(data[self.resource].iteritems()))
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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.quantum.v2_0 import CreateCommand
21+
from quantumclient.quantum.v2_0 import DeleteCommand
22+
from quantumclient.quantum.v2_0 import ListCommand
23+
from quantumclient.quantum.v2_0 import QuantumCommand
24+
from quantumclient.quantum.v2_0 import ShowCommand
25+
26+
27+
class ListFloatingIP(ListCommand):
28+
"""List floating ips that belong to a given tenant."""
29+
30+
resource = 'floatingip'
31+
log = logging.getLogger(__name__ + '.ListFloatingIP')
32+
_formatters = {}
33+
34+
35+
class ShowFloatingIP(ShowCommand):
36+
"""Show information of a given floating ip."""
37+
38+
resource = 'floatingip'
39+
log = logging.getLogger(__name__ + '.ShowFloatingIP')
40+
41+
42+
class CreateFloatingIP(CreateCommand):
43+
"""Create a floating ip for a given tenant."""
44+
45+
resource = 'floatingip'
46+
log = logging.getLogger(__name__ + '.CreateFloatingIP')
47+
48+
def add_known_arguments(self, parser):
49+
parser.add_argument(
50+
'floating_network_id',
51+
help='Network to allocate floating IP from')
52+
parser.add_argument(
53+
'--port_id',
54+
help='ID of the port to be associated with the floatingip')
55+
parser.add_argument(
56+
'--fixed_ip_address',
57+
help=('IP address on the port (only required if port has multiple'
58+
'IPs)'))
59+
60+
def args2body(self, parsed_args):
61+
body = {'floatingip': {
62+
'floating_network_id': parsed_args.floating_network_id}}
63+
if parsed_args.tenant_id:
64+
body['floatingip'].update({'tenant_id': parsed_args.tenant_id})
65+
return body
66+
67+
68+
class DeleteFloatingIP(DeleteCommand):
69+
"""Delete a given floating ip."""
70+
71+
log = logging.getLogger(__name__ + '.DeleteFloatingIP')
72+
resource = 'floatingip'
73+
74+
75+
class AssociateFloatingIP(QuantumCommand):
76+
"""Create a mapping between a floating ip and a fixed ip."""
77+
78+
api = 'network'
79+
log = logging.getLogger(__name__ + '.AssociateFloatingIP')
80+
resource = 'floatingip'
81+
82+
def get_parser(self, prog_name):
83+
parser = super(AssociateFloatingIP, self).get_parser(prog_name)
84+
parser.add_argument(
85+
'floatingip_id', metavar='floatingip_id',
86+
help='IP address of the floating IP to associate')
87+
parser.add_argument(
88+
'port_id',
89+
help='ID of the port to be associated with the floatingip')
90+
parser.add_argument(
91+
'--fixed_ip_address',
92+
help=('IP address on the port (only required if port has multiple'
93+
'IPs)'))
94+
return parser
95+
96+
def run(self, parsed_args):
97+
self.log.debug('run(%s)' % parsed_args)
98+
quantum_client = self.get_client()
99+
quantum_client.format = parsed_args.request_format
100+
update_dict = {}
101+
if parsed_args.port_id:
102+
update_dict['port_id'] = parsed_args.port_id
103+
if parsed_args.fixed_ip_address:
104+
update_dict['fixed_ip_address'] = parsed_args.fixed_ip_address
105+
quantum_client.update_floatingip(parsed_args.floatingip_id,
106+
{'floatingip': update_dict})
107+
print >>self.app.stdout, (
108+
_('Associated floatingip %s') % parsed_args.floatingip_id)
109+
110+
111+
class DisassociateFloatingIP(QuantumCommand):
112+
"""Remove a mapping from a floating ip to a fixed ip.
113+
"""
114+
115+
api = 'network'
116+
log = logging.getLogger(__name__ + '.DisassociateFloatingIP')
117+
resource = 'floatingip'
118+
119+
def get_parser(self, prog_name):
120+
parser = super(DisassociateFloatingIP, self).get_parser(prog_name)
121+
parser.add_argument(
122+
'floatingip_id', metavar='floatingip_id',
123+
help='IP address of the floating IP to associate')
124+
return parser
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+
quantum_client.update_floatingip(parsed_args.floatingip_id,
131+
{'floatingip': {'port_id': None}})
132+
print >>self.app.stdout, (
133+
_('Disassociated floatingip %s') % parsed_args.floatingip_id)
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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)

‎quantumclient/shell.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,36 @@ def env(*_vars, **kwargs):
9595
'quantumclient.quantum.v2_0.extension.ListExt'),
9696
'ext-show': utils.import_class(
9797
'quantumclient.quantum.v2_0.extension.ShowExt'),
98+
'router-list': utils.import_class(
99+
'quantumclient.quantum.v2_0.router.ListRouter'),
100+
'router-show': utils.import_class(
101+
'quantumclient.quantum.v2_0.router.ShowRouter'),
102+
'router-create': utils.import_class(
103+
'quantumclient.quantum.v2_0.router.CreateRouter'),
104+
'router-delete': utils.import_class(
105+
'quantumclient.quantum.v2_0.router.DeleteRouter'),
106+
'router-update': utils.import_class(
107+
'quantumclient.quantum.v2_0.router.UpdateRouter'),
108+
'router-interface-add': utils.import_class(
109+
'quantumclient.quantum.v2_0.router.AddInterfaceRouter'),
110+
'router-interface-delete': utils.import_class(
111+
'quantumclient.quantum.v2_0.router.RemoveInterfaceRouter'),
112+
'router-gateway-set': utils.import_class(
113+
'quantumclient.quantum.v2_0.router.SetGatewayRouter'),
114+
'router-gateway-clear': utils.import_class(
115+
'quantumclient.quantum.v2_0.router.RemoveGatewayRouter'),
116+
'floatingip-list': utils.import_class(
117+
'quantumclient.quantum.v2_0.floatingip.ListFloatingIP'),
118+
'floatingip-show': utils.import_class(
119+
'quantumclient.quantum.v2_0.floatingip.ShowFloatingIP'),
120+
'floatingip-create': utils.import_class(
121+
'quantumclient.quantum.v2_0.floatingip.CreateFloatingIP'),
122+
'floatingip-delete': utils.import_class(
123+
'quantumclient.quantum.v2_0.floatingip.DeleteFloatingIP'),
124+
'floatingip-associate': utils.import_class(
125+
'quantumclient.quantum.v2_0.floatingip.AssociateFloatingIP'),
126+
'floatingip-disassociate': utils.import_class(
127+
'quantumclient.quantum.v2_0.floatingip.DisassociateFloatingIP'),
98128
}
99129

100130
COMMANDS = {'2.0': COMMAND_V2}
@@ -116,7 +146,7 @@ def __call__(self, parser, namespace, values, option_string=None):
116146
factory = ep.load()
117147
cmd = factory(self, None)
118148
one_liner = cmd.get_description().split('\n')[0]
119-
app.stdout.write(' %-13s %s\n' % (name, one_liner))
149+
app.stdout.write(' %-25s %s\n' % (name, one_liner))
120150
sys.exit(0)
121151

122152

0 commit comments

Comments
 (0)