Skip to content

Commit

Permalink
Merge pull request #55666 from Akm0d/openvswitch_internal
Browse files Browse the repository at this point in the history
Add the `internal` flag to openvswitch
  • Loading branch information
dwoz authored Dec 20, 2019
2 parents 7207df6 + 819b104 commit 0f2e289
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
5 changes: 4 additions & 1 deletion salt/modules/openvswitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def bridge_delete(br, if_exists=True):
return _retcode_to_bool(retcode)


def port_add(br, port, may_exist=False):
def port_add(br, port, may_exist=False, internal=False):
'''
Creates on bridge a new port named port.
Expand All @@ -195,6 +195,7 @@ def port_add(br, port, may_exist=False):
br: A string - bridge name
port: A string - port name
may_exist: Bool, if False - attempting to create a port that exists returns False.
internal: A boolean to create an internal interface if one does not exist.
.. versionadded:: 2016.3.0
Expand All @@ -205,6 +206,8 @@ def port_add(br, port, may_exist=False):
'''
param_may_exist = _param_may_exist(may_exist)
cmd = 'ovs-vsctl {2}add-port {0} {1}'.format(br, port, param_may_exist)
if internal:
cmd += ' -- set interface {0} type=internal'.format(port)
result = __salt__['cmd.run_all'](cmd)
retcode = result['retcode']
return _retcode_to_bool(retcode)
Expand Down
34 changes: 30 additions & 4 deletions salt/states/openvswitch_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def present(name, bridge, tunnel_type=None, id=None, remote=None, dst_port=None,
'new': 'Created port {1} on bridge {0}.'.format(bridge, name),
}
}
comments['comment_port_internal'] = 'Port {0} already exists, but interface type has been changed to internal.'.format(name)
comments['changes_port_internal'] = {'internal': {'old': False, 'new': True}}
comments['comment_port_internal_not_changed'] = 'Port {0} already exists, but the interface type could not be changed to internal.'.format(name)

if tunnel_type:
comments['comment_invalid_ip'] = 'Remote is not valid ip address.'
Expand Down Expand Up @@ -181,7 +184,13 @@ def _check_vxlan():
else:
if name in port_list:
ret['result'] = True
ret['comment'] = comments['comment_port_exists']
current_type = __salt__['openvswitch.interface_get_type'](
name)
# The interface type is returned as a single-element list.
if internal and (current_type != ['internal']):
ret['comment'] = comments['comment_port_internal']
else:
ret['comment'] = comments['comment_port_exists']
else:
ret['result'] = None
ret['comment'] = comments['comment_port_created']
Expand Down Expand Up @@ -227,10 +236,27 @@ def _check_vxlan():
ret['comment'] = comments['comment_gre_notcreated']
else:
if name in port_list:
ret['result'] = True
ret['comment'] = comments['comment_port_exists']
current_type = __salt__['openvswitch.interface_get_type'](name)
# The interface type is returned as a single-element list.
if internal and (current_type != ['internal']):
# We do not have a direct way of only setting the interface
# type to internal, so we add the port with the --may-exist
# option.
port_add = __salt__['openvswitch.port_add'](
bridge, name, may_exist=True, internal=internal)
if port_add:
ret['result'] = True
ret['comment'] = comments['comment_port_internal']
ret['changes'] = comments['changes_port_internal']
else:
ret['result'] = False
ret['comment'] = comments[
'comment_port_internal_not_changed']
else:
ret['result'] = True
ret['comment'] = comments['comment_port_exists']
else:
port_add = __salt__['openvswitch.port_add'](bridge, name)
port_add = __salt__['openvswitch.port_add'](bridge, name, internal=internal)
if port_add:
ret['result'] = True
ret['comment'] = comments['comment_port_created']
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/states/test_openvswitch_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ def test_present(self):
mock_n = MagicMock(return_value=[])

with patch.dict(openvswitch_port.__salt__, {'openvswitch.bridge_exists': mock,
'openvswitch.interface_get_type': MagicMock(return_value='""'),
'openvswitch.port_list': mock_l
}):
comt = 'Port salt already exists.'
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(openvswitch_port.present(name, bridge), ret)

with patch.dict(openvswitch_port.__salt__, {'openvswitch.bridge_exists': mock,
'openvswitch.interface_get_type': MagicMock(return_value='""'),
'openvswitch.port_list': mock_n,
'openvswitch.port_add': mock
}):
Expand Down

0 comments on commit 0f2e289

Please sign in to comment.