mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-09 01:44:03 -07:00
Handle common argument in aggregate parameter for vyos module (#28182)
* Handle common agrument in aggregate parameter for vyos module * Add supoort to set parameter in aggregate to it's respctive top level argument if value not provided in aggregate. * Aggregate argument spec validation * Documentation for aggregate * Fix unit test failure
This commit is contained in:
parent
3f9d6aa0f1
commit
38c0b581c3
18 changed files with 353 additions and 165 deletions
|
@ -93,6 +93,26 @@ EXAMPLES = """
|
|||
speed: 100
|
||||
mtu: 256
|
||||
duplex: full
|
||||
|
||||
- name: Set interface using aggregate
|
||||
vyos_interface:
|
||||
aggregate:
|
||||
- { name: eth1, description: test-interface-1, speed: 100, duplex: half, mtu: 512}
|
||||
- { name: eth2, description: test-interface-2, speed: 1000, duplex: full, mtu: 256}
|
||||
|
||||
- name: Disable interface on aggregate
|
||||
net_interface:
|
||||
aggregate:
|
||||
- name: eth1
|
||||
- name: eth2
|
||||
enabled: False
|
||||
|
||||
- name: Delete interface using aggregate
|
||||
net_interface:
|
||||
aggregate:
|
||||
- name: eth1
|
||||
- name: eth2
|
||||
state: absent
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
|
@ -108,12 +128,13 @@ commands:
|
|||
"""
|
||||
import re
|
||||
|
||||
from copy import deepcopy
|
||||
from time import sleep
|
||||
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.connection import exec_command
|
||||
from ansible.module_utils.network_common import conditional
|
||||
from ansible.module_utils.network_common import conditional, remove_default_spec
|
||||
from ansible.module_utils.vyos import load_config, get_config
|
||||
from ansible.module_utils.vyos import vyos_argument_spec, check_args
|
||||
|
||||
|
@ -213,40 +234,19 @@ def map_config_to_obj(module):
|
|||
|
||||
def map_params_to_obj(module):
|
||||
obj = []
|
||||
|
||||
params = ['speed', 'description', 'duplex', 'mtu']
|
||||
aggregate = module.params.get('aggregate')
|
||||
|
||||
if aggregate:
|
||||
for c in aggregate:
|
||||
d = c.copy()
|
||||
if 'name' not in d:
|
||||
module.fail_json(msg="missing required arguments: %s" % 'name')
|
||||
|
||||
for item in params:
|
||||
if item not in d:
|
||||
d[item] = None
|
||||
|
||||
if d.get('description') is None:
|
||||
d['description'] = DEFAULT_DESCRIPTION
|
||||
|
||||
if not d.get('state'):
|
||||
d['state'] = module.params['state']
|
||||
|
||||
if d.get('enabled') is None:
|
||||
d['enabled'] = module.params['enabled']
|
||||
for item in aggregate:
|
||||
for key in item:
|
||||
if item.get(key) is None:
|
||||
item[key] = module.params[key]
|
||||
|
||||
d = item.copy()
|
||||
if d['enabled']:
|
||||
d['disable'] = False
|
||||
else:
|
||||
d['disable'] = True
|
||||
|
||||
if d.get('delay') is None:
|
||||
d['delay'] = module.params['delay']
|
||||
|
||||
if d.get('speed'):
|
||||
d['speed'] = str(d['speed'])
|
||||
|
||||
obj.append(d)
|
||||
else:
|
||||
params = {
|
||||
|
@ -300,7 +300,7 @@ def check_declarative_intent_params(module, want, result):
|
|||
def main():
|
||||
""" main entry point for module execution
|
||||
"""
|
||||
argument_spec = dict(
|
||||
element_spec = dict(
|
||||
name=dict(),
|
||||
description=dict(default=DEFAULT_DESCRIPTION),
|
||||
speed=dict(),
|
||||
|
@ -308,11 +308,21 @@ def main():
|
|||
duplex=dict(choices=['full', 'half', 'auto']),
|
||||
enabled=dict(default=True, type='bool'),
|
||||
delay=dict(default=10, type='int'),
|
||||
aggregate=dict(type='list'),
|
||||
state=dict(default='present',
|
||||
choices=['present', 'absent', 'up', 'down'])
|
||||
)
|
||||
|
||||
aggregate_spec = deepcopy(element_spec)
|
||||
aggregate_spec['name'] = 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),
|
||||
)
|
||||
|
||||
argument_spec.update(element_spec)
|
||||
argument_spec.update(vyos_argument_spec)
|
||||
|
||||
required_one_of = [['name', 'aggregate']]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue