Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Porting #50622 to master #54530

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions salt/serializers/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from salt.serializers import DeserializationError, SerializationError
from salt.ext import six
from salt.utils.odict import OrderedDict
from salt.utils.thread_local_proxy import ThreadLocalProxy

__all__ = ['deserialize', 'serialize', 'available']

Expand Down Expand Up @@ -140,3 +141,7 @@ class Dumper(BaseDumper): # pylint: disable=W0232
Dumper.add_multi_representer(datetime.datetime, Dumper.represent_datetime)
Dumper.add_multi_representer(None, Dumper.represent_undefined)
Dumper.add_multi_representer(OrderedDict, Dumper.represent_dict)
Dumper.add_representer(
ThreadLocalProxy,
lambda dumper, proxy:
dumper.represent_data(ThreadLocalProxy.unproxy(proxy)))
5 changes: 5 additions & 0 deletions salt/serializers/yamlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
from salt.serializers import DeserializationError, SerializationError
from salt.utils.aggregation import aggregate, Map, Sequence
from salt.utils.odict import OrderedDict
from salt.utils.thread_local_proxy import ThreadLocalProxy

# Import 3rd-party libs
import yaml
Expand Down Expand Up @@ -418,6 +419,10 @@ def represent_odict(self, data):
Dumper.add_multi_representer(datetime.date, Dumper.represent_date)
Dumper.add_multi_representer(datetime.datetime, Dumper.represent_datetime)
Dumper.add_multi_representer(None, Dumper.represent_undefined)
Dumper.add_representer(
ThreadLocalProxy,
lambda dumper, proxy:
dumper.represent_data(ThreadLocalProxy.unproxy(proxy)))


def merge_recursive(obj_a, obj_b, level=False):
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 @@ -42,13 +42,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