Skip to content

Commit 359dfa1

Browse files
author
Tang Chen
committed
Support "network create" command in nova network
This patch only provide network name and subnet setting for "network create" command. The other options, such as --project which depends on identity v2 or v3, will make the unit tests too complicated. So I prefer to implement them in other patches. Change-Id: I9ec93f0af813c8fae4170c36e16bbe8f0f53cbb6 Partial-Bug: 1543672
1 parent f9add05 commit 359dfa1

4 files changed

Lines changed: 174 additions & 23 deletions

File tree

doc/source/command-objects/network.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@ Create new network
2222
.. option:: --project <project>
2323
2424
Owner's project (name or ID)
25+
(Network v2 only)
2526
2627
.. option:: --project-domain <project-domain>
2728
2829
Domain the project belongs to (name or ID).
2930
This can be used in case collisions between project names exist.
31+
(Network v2 only)
3032
3133
.. option:: --enable
3234
3335
Enable network (default)
36+
(Network v2 only)
3437
3538
.. option:: --disable
3639
3740
Disable network
41+
(Network v2 only)
3842
3943
.. option:: --share
4044
@@ -46,8 +50,14 @@ Create new network
4650
4751
.. option:: --availability-zone-hint <availability-zone>
4852
49-
Availability Zone in which to create this network (requires the Network
50-
Availability Zone extension, this option can be repeated).
53+
Availability Zone in which to create this network (requires the Network
54+
Availability Zone extension, this option can be repeated).
55+
(Network v2 only)
56+
57+
.. option:: --subnet <subnet>
58+
59+
IPv4 subnet for fixed IPs (in CIDR notation)
60+
(Compute v2 network only)
5161
5262
.. _network_create-name:
5363
.. describe:: <name>

openstackclient/network/v2/network.py

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,27 @@ def _get_attrs(client_manager, parsed_args):
7575
return attrs
7676

7777

78-
class CreateNetwork(command.ShowOne):
78+
def _get_attrs_compute(client_manager, parsed_args):
79+
attrs = {}
80+
if parsed_args.name is not None:
81+
attrs['label'] = str(parsed_args.name)
82+
if parsed_args.shared is not None:
83+
attrs['share_address'] = parsed_args.shared
84+
if parsed_args.subnet is not None:
85+
attrs['cidr'] = parsed_args.subnet
86+
87+
return attrs
88+
89+
90+
class CreateNetwork(common.NetworkAndComputeShowOne):
7991
"""Create new network"""
8092

81-
def get_parser(self, prog_name):
82-
parser = super(CreateNetwork, self).get_parser(prog_name)
93+
def update_parser_common(self, parser):
8394
parser.add_argument(
8495
'name',
8596
metavar='<name>',
8697
help='New network name',
8798
)
88-
admin_group = parser.add_mutually_exclusive_group()
89-
admin_group.add_argument(
90-
'--enable',
91-
dest='admin_state',
92-
action='store_true',
93-
default=True,
94-
help='Enable network (default)',
95-
)
96-
admin_group.add_argument(
97-
'--disable',
98-
dest='admin_state',
99-
action='store_false',
100-
help='Disable network',
101-
)
10299
share_group = parser.add_mutually_exclusive_group()
103100
share_group.add_argument(
104101
'--share',
@@ -113,13 +110,29 @@ def get_parser(self, prog_name):
113110
action='store_false',
114111
help='Do not share the network between projects',
115112
)
113+
return parser
114+
115+
def update_parser_network(self, parser):
116+
admin_group = parser.add_mutually_exclusive_group()
117+
admin_group.add_argument(
118+
'--enable',
119+
dest='admin_state',
120+
action='store_true',
121+
default=True,
122+
help='Enable network (default)',
123+
)
124+
admin_group.add_argument(
125+
'--disable',
126+
dest='admin_state',
127+
action='store_false',
128+
help='Disable network',
129+
)
116130
parser.add_argument(
117131
'--project',
118132
metavar='<project>',
119133
help="Owner's project (name or ID)"
120134
)
121135
identity_common.add_project_domain_option_to_parser(parser)
122-
123136
parser.add_argument(
124137
'--availability-zone-hint',
125138
action='append',
@@ -131,16 +144,28 @@ def get_parser(self, prog_name):
131144
)
132145
return parser
133146

134-
def take_action(self, parsed_args):
135-
client = self.app.client_manager.network
147+
def update_parser_compute(self, parser):
148+
parser.add_argument(
149+
'--subnet',
150+
metavar='<subnet>',
151+
help="IPv4 subnet for fixed IPs (in CIDR notation)"
152+
)
153+
return parser
136154

155+
def take_action_network(self, client, parsed_args):
137156
attrs = _get_attrs(self.app.client_manager, parsed_args)
138157
obj = client.create_network(**attrs)
139158
columns = _get_columns(obj)
140-
141159
data = utils.get_item_properties(obj, columns, formatters=_formatters)
142160
return (columns, data)
143161

162+
def take_action_compute(self, client, parsed_args):
163+
attrs = _get_attrs_compute(self.app.client_manager, parsed_args)
164+
obj = client.networks.create(**attrs)
165+
columns = tuple(sorted(obj._info.keys()))
166+
data = utils.get_dict_properties(obj._info, columns)
167+
return (columns, data)
168+
144169

145170
class DeleteNetwork(common.NetworkAndComputeCommand):
146171
"""Delete network(s)"""

openstackclient/tests/network/v2/test_network.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,120 @@ def setUp(self):
576576
self.compute = self.app.client_manager.compute
577577

578578

579+
class TestCreateNetworkCompute(TestNetworkCompute):
580+
581+
# The network to create.
582+
_network = compute_fakes.FakeNetwork.create_one_network()
583+
584+
columns = (
585+
'bridge',
586+
'bridge_interface',
587+
'broadcast',
588+
'cidr',
589+
'cidr_v6',
590+
'created_at',
591+
'deleted',
592+
'deleted_at',
593+
'dhcp_server',
594+
'dhcp_start',
595+
'dns1',
596+
'dns2',
597+
'enable_dhcp',
598+
'gateway',
599+
'gateway_v6',
600+
'host',
601+
'id',
602+
'injected',
603+
'label',
604+
'mtu',
605+
'multi_host',
606+
'netmask',
607+
'netmask_v6',
608+
'priority',
609+
'project_id',
610+
'rxtx_base',
611+
'share_address',
612+
'updated_at',
613+
'vlan',
614+
'vpn_private_address',
615+
'vpn_public_address',
616+
'vpn_public_port',
617+
)
618+
619+
data = (
620+
_network.bridge,
621+
_network.bridge_interface,
622+
_network.broadcast,
623+
_network.cidr,
624+
_network.cidr_v6,
625+
_network.created_at,
626+
_network.deleted,
627+
_network.deleted_at,
628+
_network.dhcp_server,
629+
_network.dhcp_start,
630+
_network.dns1,
631+
_network.dns2,
632+
_network.enable_dhcp,
633+
_network.gateway,
634+
_network.gateway_v6,
635+
_network.host,
636+
_network.id,
637+
_network.injected,
638+
_network.label,
639+
_network.mtu,
640+
_network.multi_host,
641+
_network.netmask,
642+
_network.netmask_v6,
643+
_network.priority,
644+
_network.project_id,
645+
_network.rxtx_base,
646+
_network.share_address,
647+
_network.updated_at,
648+
_network.vlan,
649+
_network.vpn_private_address,
650+
_network.vpn_public_address,
651+
_network.vpn_public_port,
652+
)
653+
654+
def setUp(self):
655+
super(TestCreateNetworkCompute, self).setUp()
656+
657+
self.app.client_manager.network_endpoint_enabled = False
658+
659+
self.compute.networks.create.return_value = self._network
660+
661+
# Get the command object to test
662+
self.cmd = network.CreateNetwork(self.app, None)
663+
664+
def test_create_no_options(self):
665+
arglist = []
666+
verifylist = []
667+
668+
# Missing required args should raise exception here
669+
self.assertRaises(tests_utils.ParserException, self.check_parser,
670+
self.cmd, arglist, verifylist)
671+
672+
def test_create_default_options(self):
673+
arglist = [
674+
"--subnet", self._network.cidr,
675+
self._network.label,
676+
]
677+
verifylist = [
678+
('subnet', self._network.cidr),
679+
('name', self._network.label),
680+
]
681+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
682+
683+
columns, data = self.cmd.take_action(parsed_args)
684+
685+
self.compute.networks.create.assert_called_with(**{
686+
'cidr': self._network.cidr,
687+
'label': self._network.label,
688+
})
689+
self.assertEqual(self.columns, columns)
690+
self.assertEqual(self.data, data)
691+
692+
579693
class TestDeleteNetworkCompute(TestNetworkCompute):
580694

581695
# The network to delete.

releasenotes/notes/bug-1543672-bad2fc4c6c8f3125.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ features:
66
[Bug `1543672 <https://bugs.launchpad.net/python-openstackclient/+bug/1543672>`_]
77
- Command ``network show`` is now available for nova network.
88
[Bug `1543672 <https://bugs.launchpad.net/python-openstackclient/+bug/1543672>`_]
9+
- Command ``network create`` is now available for nova network.
10+
[Bug `1543672 <https://bugs.launchpad.net/python-openstackclient/+bug/1543672>`_]

0 commit comments

Comments
 (0)