eos aggregate spec validation and doc changes (#28414)

*  Add aggregate validation for eos DI modules
*  Doc change to add aggregate examples
This commit is contained in:
Ganesh Nalawade 2017-08-19 01:41:20 +05:30 committed by GitHub
parent c74be156b1
commit 4f54d6cea1
5 changed files with 184 additions and 86 deletions

View file

@ -55,10 +55,6 @@ options:
'warnings', 'notifications', 'informational', 'debugging'] 'warnings', 'notifications', 'informational', 'debugging']
aggregate: aggregate:
description: List of logging definitions. description: List of logging definitions.
purge:
description:
- Purge logging not defined in the aggregate parameter.
default: no
state: state:
description: description:
- State of the logging configuration. - State of the logging configuration.
@ -72,24 +68,35 @@ EXAMPLES = """
dest: host dest: host
name: 172.16.0.1 name: 172.16.0.1
state: present state: present
- name: remove host logging configuration - name: remove host logging configuration
eos_logging: eos_logging:
dest: host dest: host
name: 172.16.0.1 name: 172.16.0.1
state: absent state: absent
- name: configure console logging level and facility - name: configure console logging level and facility
eos_logging: eos_logging:
dest: console dest: console
facility: local7 facility: local7
level: debugging level: debugging
state: present state: present
- name: enable logging to all - name: enable logging to all
eos_logging: eos_logging:
dest : on dest : on
- name: configure buffer size - name: configure buffer size
eos_logging: eos_logging:
dest: buffered dest: buffered
size: 5000 size: 5000
- name: Configure logging using aggregate
eos_logging:
aggregate:
- { dest: console, level: warnings }
- { dest: buffered, size: 480000 }
state: present
""" """
RETURN = """ RETURN = """
@ -104,7 +111,10 @@ commands:
import re import re
from copy import deepcopy
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network_common import remove_default_spec
from ansible.module_utils.eos import get_config, load_config from ansible.module_utils.eos import get_config, load_config
from ansible.module_utils.eos import eos_argument_spec, check_args from ansible.module_utils.eos import eos_argument_spec, check_args
@ -268,30 +278,21 @@ def parse_obj(obj, module):
return obj return obj
def map_params_to_obj(module): def map_params_to_obj(module, required_if=None):
obj = [] obj = []
aggregate = module.params.get('aggregate')
if aggregate:
for item in aggregate:
for key in item:
if item.get(key) is None:
item[key] = module.params[key]
if 'aggregate' in module.params and module.params['aggregate']: module._check_required_if(required_if, item)
args = {'dest': '', d = item.copy()
'name': '',
'size': '',
'facility': '',
'level': '',
}
for c in module.params['aggregate']:
d = c.copy()
for key in args:
if key not in d:
d[key] = None
if d['dest'] != 'host': if d['dest'] != 'host':
d['name'] = None d['name'] = None
if 'state' not in d:
d['state'] = module.params['state']
if d['dest'] == 'buffered': if d['dest'] == 'buffered':
if 'size' in d: if 'size' in d:
d['size'] = str(validate_size(d['size'], module)) d['size'] = str(validate_size(d['size'], module))
@ -323,17 +324,25 @@ def map_params_to_obj(module):
def main(): def main():
""" main entry point for module execution """ main entry point for module execution
""" """
argument_spec = dict( element_spec = dict(
dest=dict(choices=DEST_GROUP), dest=dict(choices=DEST_GROUP),
name=dict(), name=dict(),
size=dict(type='int'), size=dict(type='int'),
facility=dict(), facility=dict(),
level=dict(choices=LEVEL_GROUP), level=dict(choices=LEVEL_GROUP),
state=dict(default='present', choices=['present', 'absent']), state=dict(default='present', choices=['present', 'absent']),
aggregate=dict(type='list'),
purge=dict(default=False, type='bool')
) )
aggregate_spec = deepcopy(element_spec)
# remove default in aggregate spec, to handle common arguments
remove_default_spec(aggregate_spec)
argument_spec = dict(
aggregate=dict(type='list', elements='dict', options=aggregate_spec),
)
argument_spec.update(element_spec)
argument_spec.update(eos_argument_spec) argument_spec.update(eos_argument_spec)
required_if = [('dest', 'host', ['name'])] required_if = [('dest', 'host', ['name'])]
@ -349,7 +358,7 @@ def main():
if warnings: if warnings:
result['warnings'] = warnings result['warnings'] = warnings
want = map_params_to_obj(module) want = map_params_to_obj(module, required_if=required_if)
have = map_config_to_obj(module) have = map_config_to_obj(module)
commands = map_obj_to_commands((want, have), module) commands = map_obj_to_commands((want, have), module)

View file

@ -149,9 +149,11 @@ session_name:
import re import re
from copy import deepcopy
from functools import partial from functools import partial
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network_common import remove_default_spec
from ansible.module_utils.eos import get_config, load_config from ansible.module_utils.eos import get_config, load_config
from ansible.module_utils.six import iteritems from ansible.module_utils.six import iteritems
from ansible.module_utils.eos import eos_argument_spec, check_args from ansible.module_utils.eos import eos_argument_spec, check_args
@ -315,8 +317,7 @@ def update_objects(want, have):
def main(): def main():
""" main entry point for module execution """ main entry point for module execution
""" """
argument_spec = dict( element_spec = dict(
aggregate=dict(type='list', aliases=['collection', 'users']),
name=dict(), name=dict(),
configured_password=dict(no_log=True), configured_password=dict(no_log=True),
@ -328,10 +329,20 @@ def main():
sshkey=dict(), sshkey=dict(),
purge=dict(type='bool', default=False),
state=dict(default='present', choices=['present', 'absent']) state=dict(default='present', choices=['present', 'absent'])
) )
aggregate_spec = deepcopy(element_spec)
# remove default in aggregate spec, to handle common arguments
remove_default_spec(aggregate_spec)
argument_spec = dict(
aggregate=dict(type='list', elements='dict', options=aggregate_spec, aliases=['collection', 'users']),
purge=dict(type='bool', default=False)
)
argument_spec.update(element_spec)
argument_spec.update(eos_argument_spec) argument_spec.update(eos_argument_spec)
mutually_exclusive = [('name', 'aggregate')] mutually_exclusive = [('name', 'aggregate')]

View file

@ -62,6 +62,35 @@ options:
""" """
EXAMPLES = """ EXAMPLES = """
- name: Create vlan
eos_vlan:
vlan_id: 4000
name: vlan-4000
state: present
- name: Add interfaces to vlan
eos_vlan:
vlan_id: 4000
state: present
interfaces:
- Ethernet1
- Ethernet2
- name: Suspend vlan
eos_vlan:
vlan_id: 4000
state: suspend
- name: Unsuspend vlan
eos_vlan:
vlan_id: 4000
state: active
- name: Create aggregate of vlans
eos_vlan:
aggregate:
- vlan_id: 4000
- {vlan_id: 4001, name: vlan-4001}
""" """
RETURN = """ RETURN = """
@ -73,14 +102,16 @@ commands:
- vlan 20 - vlan 20
- name test-vlan - name test-vlan
""" """
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.eos import load_config, run_commands
from ansible.module_utils.eos import eos_argument_spec, check_args
from ansible.module_utils.six import iteritems
import re import re
import time import time
from copy import deepcopy
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network_common import remove_default_spec
from ansible.module_utils.eos import load_config, run_commands
from ansible.module_utils.eos import eos_argument_spec, check_args
def search_obj_in_list(vlan_id, lst): def search_obj_in_list(vlan_id, lst):
for o in lst: for o in lst:
@ -187,34 +218,23 @@ def map_config_to_obj(module):
def map_params_to_obj(module): def map_params_to_obj(module):
obj = [] obj = []
aggregate = module.params.get('aggregate')
if aggregate:
for item in aggregate:
for key in item:
if item.get(key) is None:
item[key] = module.params[key]
if 'aggregate' in module.params and module.params['aggregate']: d = item.copy()
for v in module.params['aggregate']:
d = v.copy()
d['vlan_id'] = str(d['vlan_id']) d['vlan_id'] = str(d['vlan_id'])
if 'state' not in d:
d['state'] = module.params['state']
if 'name' not in d:
d['name'] = None
if 'interfaces' not in d:
d['interfaces'] = []
obj.append(d) obj.append(d)
else: else:
vlan_id = str(module.params['vlan_id'])
name = module.params['name']
state = module.params['state']
interfaces = module.params['interfaces']
obj.append({ obj.append({
'vlan_id': vlan_id, 'vlan_id': str(module.params['vlan_id']),
'name': name, 'name': module.params['name'],
'state': state, 'state': module.params['state'],
'interfaces': interfaces 'interfaces': module.params['interfaces']
}) })
return obj return obj
@ -236,23 +256,35 @@ def check_declarative_intent_params(want, module):
def main(): def main():
""" main entry point for module execution """ main entry point for module execution
""" """
argument_spec = dict( element_spec = dict(
vlan_id=dict(type='int'), vlan_id=dict(type='int'),
name=dict(), name=dict(),
interfaces=dict(type='list'), interfaces=dict(type='list'),
delay=dict(default=10, type='int'), delay=dict(default=10, type='int'),
aggregate=dict(type='list'),
purge=dict(default=False, type='bool'),
state=dict(default='present', state=dict(default='present',
choices=['present', 'absent', 'active', 'suspend']) choices=['present', 'absent', 'active', 'suspend'])
) )
aggregate_spec = deepcopy(element_spec)
aggregate_spec['vlan_id'] = dict(required=True)
# remove default in aggregate spec, to handle common arguments
remove_default_spec(aggregate_spec)
argument_spec = dict(
aggregate=dict(type='list', elements='dict', options=aggregate_spec),
purge=dict(default=False, type='bool')
)
argument_spec.update(element_spec)
argument_spec.update(eos_argument_spec) argument_spec.update(eos_argument_spec)
required_one_of = [['vlan_id', 'aggregate']] required_one_of = [['vlan_id', 'aggregate']]
mutually_exclusive = [['vlan_id', 'aggregate']] mutually_exclusive = [['vlan_id', 'aggregate']]
module = AnsibleModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True) supports_check_mode=True,
required_one_of=required_one_of,
mutually_exclusive=mutually_exclusive)
warnings = list() warnings = list()
check_args(module, warnings) check_args(module, warnings)

View file

@ -59,6 +59,35 @@ options:
""" """
EXAMPLES = """ EXAMPLES = """
- name: Create vrf
eos_vrf:
name: test
rd: 1:200
interfaces:
- Ethernet2
state: present
- name: Delete VRFs
eos_vrf:
name: test
state: absent
- name: Create aggregate of VRFs with purge
eos_vrf:
aggregate:
- { name: test4, rd: "1:204" }
- { name: test5, rd: "1:205" }
state: present
purge: yes
- name: Delete aggregate of VRFs
eos_vrf:
aggregate:
- name: test2
- name: test3
- name: test4
- name: test5
state: absent
""" """
RETURN = """ RETURN = """
@ -72,14 +101,16 @@ commands:
- interface Ethernet1 - interface Ethernet1
- vrf forwarding test - vrf forwarding test
""" """
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.eos import load_config, run_commands
from ansible.module_utils.eos import eos_argument_spec, check_args
from ansible.module_utils.six import iteritems
import re import re
import time import time
from copy import deepcopy
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network_common import remove_default_spec
from ansible.module_utils.eos import load_config, run_commands
from ansible.module_utils.eos import eos_argument_spec, check_args
def search_obj_in_list(name, lst): def search_obj_in_list(name, lst):
for o in lst: for o in lst:
@ -172,25 +203,14 @@ def map_config_to_obj(module):
def map_params_to_obj(module): def map_params_to_obj(module):
obj = [] obj = []
aggregate = module.params.get('aggregate')
if 'aggregate' in module.params and module.params['aggregate']: if aggregate:
for c in module.params['aggregate']: for item in aggregate:
d = c.copy() for key in item:
if item.get(key) is None:
if 'state' not in d: item[key] = module.params[key]
d['state'] = module.params['state'] obj.append(item.copy())
if 'rd' not in d:
d['rd'] = module.params['rd']
if 'interfaces' not in d:
d['interfaces'] = module.params['interfaces']
obj.append(d)
else: else:
name = module.params['name'],
state = module.params['state'],
rd = module.params['rd'],
interfaces = module.params['interfaces']
obj.append({ obj.append({
'name': module.params['name'], 'name': module.params['name'],
'state': module.params['state'], 'state': module.params['state'],
@ -217,16 +237,25 @@ def check_declarative_intent_params(want, module):
def main(): def main():
""" main entry point for module execution """ main entry point for module execution
""" """
argument_spec = dict( element_spec = dict(
name=dict(), name=dict(),
interfaces=dict(type='list'), interfaces=dict(type='list'),
delay=dict(default=10, type='int'), delay=dict(default=10, type='int'),
rd=dict(), rd=dict(),
aggregate=dict(type='list'),
purge=dict(default=False, type='bool'),
state=dict(default='present', choices=['present', 'absent']) state=dict(default='present', choices=['present', 'absent'])
) )
aggregate_spec = deepcopy(element_spec)
# remove default in aggregate spec, to handle common arguments
remove_default_spec(aggregate_spec)
argument_spec = dict(
aggregate=dict(type='list', elements='dict', options=aggregate_spec),
purge=dict(default=False, type='bool')
)
argument_spec.update(element_spec)
argument_spec.update(eos_argument_spec) argument_spec.update(eos_argument_spec)
required_one_of = [['name', 'aggregate']] required_one_of = [['name', 'aggregate']]

View file

@ -51,6 +51,23 @@ EXAMPLES = """
net_vrf: net_vrf:
name: MANAGEMENT name: MANAGEMENT
state: absent state: absent
- name: Create aggregate of VRFs with purge
net_vrf:
aggregate:
- { name: test4, rd: "1:204" }
- { name: test5, rd: "1:205" }
state: present
purge: yes
- name: Delete aggregate of VRFs
net_vrf:
aggregate:
- name: test2
- name: test3
- name: test4
- name: test5
state: absent
""" """
RETURN = """ RETURN = """