Skip to content

Commit 748e0ab

Browse files
author
Dean Troyer
committed
Begin low-level API for Network v2
api.network.APIv2 starts with network_list() support to flush out the skeleton of the Network API. list_dhcp_agent() supports the --dhcp option of 'network list' Change-Id: I9a2b90cde84eced1f2ea6a014b769e2bae668211
1 parent b02cce5 commit 748e0ab

7 files changed

Lines changed: 422 additions & 121 deletions

File tree

doc/source/command-objects/network.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ List networks
6464
6565
os network list
6666
[--external]
67-
[--dhcp]
67+
[--dhcp <dhcp-id>]
6868
[--long]
6969
7070
.. option:: --external
7171

7272
List external networks
7373

74-
.. option:: --dhcp
74+
.. option:: --dhcp <dhcp-id>
7575

76-
ID of the DHCP agent
76+
DHCP agent ID
7777

7878
.. option:: --long
7979

openstackclient/api/network_v2.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 v2 API Library"""
15+
16+
from openstackclient.api import api
17+
18+
19+
class APIv2(api.BaseAPI):
20+
"""Network v2 API"""
21+
22+
def __init__(self, **kwargs):
23+
super(APIv2, self).__init__(**kwargs)
24+
25+
def dhcp_agent_list(
26+
self,
27+
dhcp_id=None,
28+
**filter
29+
):
30+
"""List DHCP agents
31+
32+
:param string dhcp_id:
33+
DHCP Agent ID
34+
:param filter:
35+
used to create the query string filters
36+
http://docs.openstack.org/api/openstack-network/2.0/content/filtering.html
37+
"""
38+
39+
return self.list('dhcp-networks', **filter)['dhcp-networks']
40+
41+
def network_list(
42+
self,
43+
external=False,
44+
**filter
45+
):
46+
"""List external networks
47+
48+
:param string dhcp_id:
49+
DHCP agent ID
50+
:param bool external:
51+
Return external networks if True
52+
:param filter:
53+
used to create the query string filters
54+
http://docs.openstack.org/api/openstack-network/2.0/content/filtering.html
55+
"""
56+
57+
if external:
58+
filter = {'router:external': True}
59+
return self.list('networks', **filter)['networks']

openstackclient/network/client.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,54 @@
2424
API_VERSIONS = {
2525
"2": "neutronclient.v2_0.client.Client",
2626
}
27+
# Translate our API version to auth plugin version prefix
28+
API_VERSION_MAP = {
29+
'2.0': 'v2.0',
30+
'2': 'v2.0',
31+
}
32+
33+
NETWORK_API_TYPE = 'network'
34+
NETWORK_API_VERSIONS = {
35+
'2': 'openstackclient.api.network_v2.APIv2',
36+
}
2737

2838

2939
def make_client(instance):
30-
"""Returns an network service client."""
40+
"""Returns an network service client"""
3141
network_client = utils.get_client_class(
3242
API_NAME,
3343
instance._api_version[API_NAME],
3444
API_VERSIONS)
3545
LOG.debug('Instantiating network client: %s', network_client)
3646

37-
return network_client(
47+
endpoint = instance.get_endpoint_for_service_type(
48+
API_NAME,
49+
region_name=instance._region_name,
50+
)
51+
52+
client = network_client(
3853
session=instance.session,
3954
region_name=instance._region_name,
4055
)
4156

57+
network_api = utils.get_client_class(
58+
API_NAME,
59+
instance._api_version[API_NAME],
60+
NETWORK_API_VERSIONS)
61+
LOG.debug('Instantiating network api: %s', network_client)
62+
63+
# v2 is hard-coded until discovery is completed, neutron only has one atm
64+
client.api = network_api(
65+
session=instance.session,
66+
service_type=NETWORK_API_TYPE,
67+
endpoint='/'.join([
68+
endpoint,
69+
API_VERSION_MAP[instance._api_version[API_NAME]],
70+
])
71+
)
72+
73+
return client
74+
4275

4376
def build_option_parser(parser):
4477
"""Hook to add global options"""

openstackclient/network/v2/network.py

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,21 @@
2525
from openstackclient.network import common
2626

2727

28-
def filters(data):
29-
if 'subnets' in data:
30-
data['subnets'] = utils.format_list(data['subnets'])
31-
return data
28+
def _prep_network_detail(net):
29+
"""Prepare network object for output"""
30+
31+
if 'subnets' in net:
32+
net['subnets'] = utils.format_list(net['subnets'])
33+
if 'admin_state_up' in net:
34+
net['state'] = 'UP' if net['admin_state_up'] else 'DOWN'
35+
net.pop('admin_state_up')
36+
if 'router:external' in net:
37+
net['router_type'] = 'External' if net['router:external'] \
38+
else 'Internal'
39+
net.pop('router:external')
40+
if 'tenant_id' in net:
41+
net['project_id'] = net.pop('tenant_id')
42+
return net
3243

3344

3445
class CreateNetwork(show.ShowOne):
@@ -80,7 +91,7 @@ def take_action(self, parsed_args):
8091
create_method = getattr(client, "create_network")
8192
data = create_method(body)['network']
8293
if data:
83-
data = filters(data)
94+
data = _prep_network_detail(data)
8495
else:
8596
data = {'': ''}
8697
return zip(*sorted(six.iteritems(data)))
@@ -133,40 +144,63 @@ def get_parser(self, prog_name):
133144
)
134145
parser.add_argument(
135146
'--dhcp',
136-
help='ID of the DHCP agent')
147+
metavar='<dhcp-id>',
148+
help='DHCP agent ID')
137149
parser.add_argument(
138150
'--long',
139151
action='store_true',
140152
default=False,
141-
help='Long listing',
153+
help='List additional fields in output',
142154
)
143155
return parser
144156

145157
def take_action(self, parsed_args):
146158
self.log.debug('take_action(%s)' % parsed_args)
147159
client = self.app.client_manager.network
160+
148161
if parsed_args.dhcp:
149-
list_method = getattr(client, 'list_networks_on_dhcp_agent')
150-
resources = 'networks_on_dhcp_agent'
151-
report_filter = {'dhcp_agent': parsed_args.dhcp}
152-
data = list_method(**report_filter)[resources]
153-
else:
154-
list_method = getattr(client, "list_networks")
155-
report_filter = {}
156-
if parsed_args.external:
157-
report_filter = {'router:external': True}
158-
data = list_method(**report_filter)['networks']
159-
columns = len(data) > 0 and sorted(data[0].keys()) or []
160-
if parsed_args.columns:
161-
list_columns = parsed_args.columns
162+
data = client.api.dhcp_agent_list(dhcp_id=parsed_args.dhcp)
163+
164+
columns = ('ID',)
165+
column_headers = columns
162166
else:
163-
list_columns = ['id', 'name', 'subnets']
164-
if not parsed_args.long and not parsed_args.dhcp:
165-
columns = [x for x in list_columns if x in columns]
166-
formatters = {'subnets': utils.format_list}
167-
return (columns,
168-
(utils.get_dict_properties(s, columns, formatters=formatters)
169-
for s in data))
167+
data = client.api.network_list(external=parsed_args.external)
168+
169+
if parsed_args.long:
170+
columns = (
171+
'ID',
172+
'Name',
173+
'Status',
174+
'project_id',
175+
'state',
176+
'Shared',
177+
'Subnets',
178+
'provider:network_type',
179+
'router_type',
180+
)
181+
column_headers = (
182+
'ID',
183+
'Name',
184+
'Status',
185+
'Project',
186+
'State',
187+
'Shared',
188+
'Subnets',
189+
'Network Type',
190+
'Router Type',
191+
)
192+
else:
193+
columns = ('ID', 'Name', 'Subnets')
194+
column_headers = columns
195+
196+
for d in data:
197+
d = _prep_network_detail(d)
198+
199+
return (column_headers,
200+
(utils.get_dict_properties(
201+
s, columns,
202+
formatters={'subnets': utils.format_list},
203+
) for s in data))
170204

171205

172206
class SetNetwork(command.Command):
@@ -253,9 +287,9 @@ def get_parser(self, prog_name):
253287
def take_action(self, parsed_args):
254288
self.log.debug('take_action(%s)' % parsed_args)
255289
client = self.app.client_manager.network
256-
_id = common.find(client, 'network', 'networks',
257-
parsed_args.identifier)
258-
show_method = getattr(client, "show_network")
259-
data = show_method(_id)['network']
260-
data = filters(data)
290+
net = client.api.find_attr(
291+
'networks',
292+
parsed_args.identifier,
293+
)
294+
data = _prep_network_detail(net)
261295
return zip(*sorted(six.iteritems(data)))
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 v2 API Library Tests"""
15+
16+
from requests_mock.contrib import fixture
17+
18+
from keystoneclient import session
19+
from openstackclient.api import network_v2 as network
20+
from openstackclient.tests import utils
21+
22+
23+
FAKE_PROJECT = 'xyzpdq'
24+
FAKE_URL = 'http://gopher.com/v2/' + FAKE_PROJECT
25+
26+
27+
class TestNetworkAPIv2(utils.TestCase):
28+
29+
def setUp(self):
30+
super(TestNetworkAPIv2, self).setUp()
31+
sess = session.Session()
32+
self.api = network.APIv2(session=sess, endpoint=FAKE_URL)
33+
self.requests_mock = self.useFixture(fixture.Fixture())
34+
35+
36+
class TestNetwork(TestNetworkAPIv2):
37+
38+
LIST_NETWORK_RESP = [
39+
{'id': '1', 'name': 'p1', 'description': 'none', 'enabled': True},
40+
{'id': '2', 'name': 'p2', 'description': 'none', 'enabled': False},
41+
{'id': '3', 'name': 'p3', 'description': 'none', 'enabled': True},
42+
]
43+
44+
def test_network_list_no_options(self):
45+
self.requests_mock.register_uri(
46+
'GET',
47+
FAKE_URL + '/networks',
48+
json={'networks': self.LIST_NETWORK_RESP},
49+
status_code=200,
50+
)
51+
ret = self.api.network_list()
52+
self.assertEqual(self.LIST_NETWORK_RESP, ret)

openstackclient/tests/network/common.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,26 @@
1414
import argparse
1515
import mock
1616

17+
from openstackclient.api import network_v2
1718
from openstackclient.tests import utils
1819

1920

21+
class FakeNetworkClient(object):
22+
pass
23+
24+
2025
class TestNetworkBase(utils.TestCommand):
2126
def setUp(self):
2227
super(TestNetworkBase, self).setUp()
23-
self.app = mock.Mock(name='app')
24-
self.app.client_manager = mock.Mock(name='client_manager')
2528
self.namespace = argparse.Namespace()
2629

30+
self.app.client_manager.network = FakeNetworkClient()
31+
self.app.client_manager.network.api = network_v2.APIv2(
32+
session=mock.Mock(),
33+
service_type="network",
34+
)
35+
self.api = self.app.client_manager.network.api
36+
2737
given_show_options = [
2838
'-f',
2939
'shell',

0 commit comments

Comments
 (0)