Revert "Deprecate Entity, EntityCollection and use subspec in network modules (#33575)" (#33849)

This reverts commit 4349b56643.
This commit is contained in:
Ganesh Nalawade
2017-12-13 13:07:52 +05:30
committed by GitHub
parent b6528ea19f
commit ea18b9021a
15 changed files with 200 additions and 195 deletions

View File

@@ -1929,23 +1929,14 @@ class AnsibleModule(object):
self._options_context.append(k)
key = None
for (name, value) in spec.items():
# specifies how to map a single value to spec
if value.get('key'):
key = name
break
if isinstance(params[k], dict):
elements = [params[k]]
else:
elements = params[k]
for index, param in enumerate(elements):
for param in elements:
if not isinstance(param, dict):
if key is None:
self.fail_json(msg="options spec require a key argument to map it to a single value '%s'" % param)
elements[index] = param = {key: param}
self.fail_json(msg="value of %s must be of type dict or list of dict" % k)
self._set_fallbacks(spec, param)
options_aliases = self._handle_aliases(spec, param)

View File

@@ -65,10 +65,6 @@ def sort_list(val):
class Entity(object):
"""Transforms a dict to with an argument spec
This class has been deprecated as of Ansible 2.5 and will be
removed from the code in future release.
Please use the suboptions in module argument spec instead.
This class will take a dict and apply an Ansible argument spec to the
values. The resulting dict will contain all of the keys in the param
with appropriate values set.
@@ -187,12 +183,7 @@ class Entity(object):
class EntityCollection(Entity):
"""Extends ```Entity``` to handle a list of dicts
This class has been deprecated as of Ansible 2.5 and will be
removed from the code in future release.
Please use the suboptions in module argument spec instead.
"""
"""Extends ```Entity``` to handle a list of dicts """
def __call__(self, iterable, strict=True):
if iterable is None:
@@ -207,21 +198,11 @@ class EntityCollection(Entity):
# these two are for backwards compatibility and can be removed once all of the
# modules that use them are updated
class ComplexDict(Entity):
"""
This class has been deprecated as of Ansible 2.5 and will be
removed from the code in future release.
Please use the suboptions in module argument spec instead.
"""
def __init__(self, attrs, module, *args, **kwargs):
super(ComplexDict, self).__init__(module, attrs, *args, **kwargs)
class ComplexList(EntityCollection):
"""
This class has been deprecated as of Ansible 2.5 and will be
removed from the code in future release.
Please use the suboptions in module argument spec instead.
"""
def __init__(self, attrs, module, *args, **kwargs):
super(ComplexList, self).__init__(module, attrs, *args, **kwargs)

View File

@@ -33,8 +33,8 @@ import time
from ansible.module_utils._text import to_text, to_native
from ansible.module_utils.basic import env_fallback, return_values
from ansible.module_utils.connection import exec_command
from ansible.module_utils.network.common.utils import to_list
from ansible.module_utils.six import iteritems, string_types
from ansible.module_utils.network.common.utils import to_list, ComplexList
from ansible.module_utils.six import iteritems
from ansible.module_utils.urls import fetch_url
_DEVICE_CONNECTION = None
@@ -150,6 +150,7 @@ class Cli:
"""Run list of commands on remote device and return results
"""
responses = list()
for cmd in to_list(commands):
rc, out, err = self.exec_command(cmd)
out = to_text(out, errors='surrogate_then_replace')
@@ -428,6 +429,22 @@ def is_eapi(module):
return 'eapi' in (transport, provider_transport)
def to_command(module, commands):
if is_eapi(module):
default_output = 'json'
else:
default_output = 'text'
transform = ComplexList(dict(
command=dict(key=True),
output=dict(default=default_output),
prompt=dict(),
answer=dict()
), module)
return transform(to_list(commands))
def get_config(module, flags=None):
flags = None if flags is None else flags
@@ -437,15 +454,7 @@ def get_config(module, flags=None):
def run_commands(module, commands):
conn = get_connection(module)
if is_eapi(module):
default_output = 'json'
else:
default_output = 'text'
for index, cmd in enumerate(to_list(commands)):
if isinstance(cmd, string_types):
commands[index] = {'command': cmd, 'output': default_output}
return conn.run_commands(to_list(commands))
return conn.run_commands(to_command(module, commands))
def load_config(module, config, commit=False, replace=False):

View File

@@ -27,9 +27,8 @@
#
from ansible.module_utils._text import to_text
from ansible.module_utils.basic import env_fallback, return_values
from ansible.module_utils.network.common.utils import to_list, ComplexList
from ansible.module_utils.connection import exec_command
from ansible.module_utils.network.common.utils import to_list
from ansible.module_utils.six import string_types
_DEVICE_CONFIGS = {}
@@ -101,12 +100,20 @@ def get_config(module, flags=None):
return cfg
def to_commands(module, commands):
spec = {
'command': dict(key=True),
'prompt': dict(),
'answer': dict()
}
transform = ComplexList(spec, module)
return transform(commands)
def run_commands(module, commands, check_rc=True):
responses = list()
for cmd in to_list(commands):
if isinstance(cmd, string_types):
cmd = {'command': cmd}
commands = to_commands(module, to_list(commands))
for cmd in commands:
cmd = module.jsonify(cmd)
rc, out, err = exec_command(module, cmd)
if check_rc and rc != 0:

View File

@@ -79,6 +79,12 @@ iosxr_argument_spec = {
'provider': dict(type='dict', options=iosxr_provider_spec)
}
command_spec = {
'command': dict(),
'prompt': dict(default=None),
'answer': dict(default=None)
}
iosxr_top_spec = {
'host': dict(removed_in_version=2.9),
'port': dict(removed_in_version=2.9, type='int'),

View File

@@ -32,7 +32,7 @@ import collections
from ansible.module_utils._text import to_text
from ansible.module_utils.basic import env_fallback, return_values
from ansible.module_utils.network.common.utils import to_list
from ansible.module_utils.network.common.utils import to_list, ComplexList
from ansible.module_utils.connection import exec_command
from ansible.module_utils.six import iteritems, string_types
from ansible.module_utils.urls import fetch_url
@@ -399,6 +399,28 @@ def is_nxapi(module):
return 'nxapi' in (transport, provider_transport)
def to_command(module, commands):
if is_nxapi(module):
default_output = 'json'
else:
default_output = 'text'
transform = ComplexList(dict(
command=dict(key=True),
output=dict(default=default_output),
prompt=dict(),
answer=dict()
), module)
commands = transform(to_list(commands))
for item in commands:
if is_json(item['command']):
item['output'] = 'json'
return commands
def get_config(module, flags=None):
flags = [] if flags is None else flags
@@ -408,20 +430,7 @@ def get_config(module, flags=None):
def run_commands(module, commands, check_rc=True):
conn = get_connection(module)
if is_nxapi(module):
default_output = 'json'
else:
default_output = 'text'
for index, cmd in enumerate(to_list(commands)):
if isinstance(cmd, string_types):
commands[index] = cmd = {'command': cmd, 'output': default_output}
if is_json(cmd['command']):
cmd['output'] = 'json'
return conn.run_commands(to_list(commands), check_rc)
return conn.run_commands(to_command(module, commands), check_rc)
def load_config(module, config, return_error=False, opts=None):