Commit 447365fe authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Export all port methods, except port create

Refs: #4563
parent 62c6652f
......@@ -362,3 +362,80 @@ class subnet_set(_init_network, _optional_json):
def main(self, subnet_id):
super(self.__class__, self)._run()
self._run(subnet_id=subnet_id)
@command(port_cmds)
class port_info(_init_network, _optional_json):
"""Get details about a port"""
@errors.generic.all
@errors.cyclades.connection
def _run(self, port_id):
net = self.client.get_port_details(port_id)
self._print(net, self.print_dict)
def main(self, port_id):
super(self.__class__, self)._run()
self._run(port_id=port_id)
@command(port_cmds)
class port_info(_init_network, _optional_json):
"""Get details about a port"""
@errors.generic.all
@errors.cyclades.connection
def _run(self, port_id):
net = self.client.get_port_details(port_id)
self._print(net, self.print_dict)
def main(self, port_id):
super(self.__class__, self)._run()
self._run(port_id=port_id)
@command(port_cmds)
class port_delete(_init_network, _optional_output_cmd):
"""Delete a port"""
@errors.generic.all
@errors.cyclades.connection
def _run(self, port_id):
r = self.client.delete_port(port_id)
self._optional_output(r)
def main(self, port_id):
super(self.__class__, self)._run()
self._run(port_id=port_id)
@command(port_cmds)
class port_set(_init_network, _optional_json):
"""Set an attribute of a port, leave the rest untouched (update)
Only "--name" is supported for now
"""
arguments = dict(name=ValueArgument('New name of the port', '--name'))
@errors.generic.all
@errors.cyclades.connection
def _run(self, port_id):
if self['name'] in (None, ):
raise CLISyntaxError(
'Missing port attributes to update',
details=[
'At least one if the following is expected:',
' --name=<new name>'])
r = self.client.get_port_details(port_id)
r = self.client.update_port(
port_id, r['network_id'], name=self['name'])
self._print(r, self.print_dict)
def main(self, port_id):
super(self.__class__, self)._run()
self._run(port_id=port_id)
#@command(port_cmds)
#class port_create(_init_network, _optional_json):
#
......@@ -290,3 +290,33 @@ class NetworkClient(NetworkRestClient):
def delete_port(self, port_id):
r = self.ports_delete(port_id, success=204)
return r.headers
def update_port(
self, port_id, network_id,
name=None, status=None, admin_state_up=None, mac_address=None,
fixed_ips=None, security_groups=None):
"""
:param network_id: (str)
:param name: (str)
:param status: (str)
:param admin_state_up: (bool) Router administrative status (UP / DOWN)
:param mac_address: (str)
:param fixed_ips: (str)
:param security_groups: (list)
"""
port = dict(network_id=network_id)
if name:
port['name'] = name
if status:
port['status'] = status
if admin_state_up not in (None, ):
port['admin_state_up'] = bool(admin_state_up)
if mac_address:
port['mac_address'] = mac_address
if fixed_ips:
port['fixed_ips'] = fixed_ips
if security_groups:
port['security_groups'] = security_groups
r = self.ports_put(port_id, json_data=dict(port=port), success=201)
return r.json['port']
......@@ -72,26 +72,10 @@ class NetworkRestClient(Client):
return self.get(path4url('ports', port_id), **kwargs)
return self.get(path4url('ports'), **kwargs)
def ports_post(
self,
json_data=None,
name=None, mac_address=None, fixed_ips=None, security_groups=None,
**kwargs):
self.set_param('name', name, iff=name)
self.set_param('mac_address', mac_address, iff=mac_address)
self.set_param('fixed_ips', fixed_ips, iff=fixed_ips)
self.set_param('security_groups', security_groups, iff=security_groups)
def ports_post(self, json_data=None, **kwargs):
return self.post(path4url('ports'), json=json_data, **kwargs)
def ports_put(
self, port_id,
json_data=None,
name=None, mac_address=None, fixed_ips=None, security_groups=None,
**kwargs):
self.set_param('name', name, iff=name)
self.set_param('mac_address', mac_address, iff=mac_address)
self.set_param('fixed_ips', fixed_ips, iff=fixed_ips)
self.set_param('security_groups', security_groups, iff=security_groups)
def ports_put(self, port_id, json_data=None, **kwargs):
return self.put(path4url('ports', port_id), json=json_data, **kwargs)
def ports_delete(self, port_id, **kwargs):
......
......@@ -146,81 +146,31 @@ class NetworkRestClient(TestCase):
'ret val')
self._assert(get, '/ports/%s' % port_id, **kwargs)
@patch('kamaki.clients.Client.set_param')
@patch('kamaki.clients.Client.post', return_value='ret val')
def test_ports_post(self, post, set_param):
for params, kwargs in product(
[p for p in product(
(
('name', 'port name', 'port name'),
('name', None, None)),
(
('mac_address', 'max address', 'max address'),
('mac_address', None, None)),
(
('fixed_ips', 'fixed ip', 'fixed ip'),
('fixed_ips', None, None)),
(
('security_groups', 'sec groups', 'sec groups'),
('security_groups', None, None))
)],
(dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
callargs = dict()
for p in params:
callargs[p[0]] = p[2]
callargs.update(kwargs)
self.assertEqual(self.client.ports_post(**callargs), 'ret val')
self._assert(
post, '/ports', set_param, params=params, json=None, **kwargs)
def test_ports_post(self, post):
for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
self.assertEqual(self.client.ports_post(**kwargs), 'ret val')
self._assert(post, '/ports', json=None, **kwargs)
json_data = dict(id='some id', other_param='other val')
callargs['json_data'] = json_data
self.assertEqual(self.client.ports_post(**callargs), 'ret val')
self._assert(
post, '/ports', set_param, params,
json=json_data, **kwargs)
self.assertEqual(
self.client.ports_post(json_data=json_data, **kwargs),
'ret val')
self._assert(post, '/ports', json=json_data, **kwargs)
@patch('kamaki.clients.Client.set_param')
@patch('kamaki.clients.Client.put', return_value='ret val')
def test_ports_put(self, put, set_param):
def test_ports_put(self, put):
port_id = 'portid'
for params, kwargs in product(
[p for p in product(
(
('name', 'port name', 'port name'),
('name', None, None)),
(
('mac_address', 'max address', 'max address'),
('mac_address', None, None)),
(
('fixed_ips', 'fixed ip', 'fixed ip'),
('fixed_ips', None, None)),
(
('security_groups', 'sec groups', 'sec groups'),
('security_groups', None, None))
)],
(dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
callargs = dict()
for p in params:
callargs[p[0]] = p[2]
callargs.update(kwargs)
for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
self.assertEqual(
self.client.ports_put(port_id, **callargs), 'ret val')
self._assert(
put, '/ports/%s' % port_id, set_param,
params=params, json=None, **kwargs)
self.client.ports_put(port_id, **kwargs), 'ret val')
self._assert(put, '/ports/%s' % port_id, json=None, **kwargs)
json_data = dict(id='some id', other_param='other val')
callargs['json_data'] = json_data
self.assertEqual(
self.client.ports_put(port_id, **callargs), 'ret val')
self._assert(
put, '/ports/%s' % port_id, set_param, params,
json=json_data, **kwargs)
self.client.ports_put(port_id, json_data=json_data, **kwargs),
'ret val')
self._assert(put, '/ports/%s' % port_id, json=json_data, **kwargs)
class FakeObject(object):
......@@ -517,6 +467,31 @@ class NetworkClient(TestCase):
self.assertEqual(self.client.delete_port(portid), 'ret headers')
ports_delete.assert_called_once_with(portid, success=204)
@patch(
'kamaki.clients.network.NetworkClient.ports_put',
return_value=FakeObject())
def test_update_port(self, ports_put):
for (
name, status, admin_state_up, mac_address, fixed_ips,
security_groups) in product(
('name', None), ('st', None), (True, None), ('mc', None),
('fps', None), ('sg', None)):
FakeObject.json = dict(port='rv')
port_id, network_id = 'pid', 'nid'
kwargs = dict(
network_id=network_id, name=name, status=status,
admin_state_up=admin_state_up, mac_address=mac_address,
fixed_ips=fixed_ips, security_groups=security_groups)
self.assertEqual(
self.client.update_port(port_id, **kwargs), 'rv')
req = dict()
for k, v in kwargs.items():
if v:
req[k] = v
expargs = dict(json_data=dict(port=req), success=201)
self.assertEqual(
ports_put.mock_calls[-1], call(port_id, **expargs))
if __name__ == '__main__':
from sys import argv
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment