Add state intent argument in vyos interface (#28066)

* Add state intent argument in vyos interface

*  State intent argument support
*  Integration test for supported intent arguments

* Add intent testcase

* FIx ci issue
This commit is contained in:
Ganesh Nalawade 2017-08-14 10:17:41 +05:30 committed by GitHub
parent 711a6b2326
commit eb33cc88bd
5 changed files with 215 additions and 33 deletions

View file

@ -55,19 +55,8 @@ options:
- Interface link status. - Interface link status.
default: auto default: auto
choices: ['full', 'half', 'auto'] choices: ['full', 'half', 'auto']
tx_rate:
description:
- Transmit rate.
rx_rate:
description:
- Receiver rate.
aggregate: aggregate:
description: List of Interfaces definitions. description: List of Interfaces definitions.
purge:
description:
- Purge Interfaces not defined in the aggregate parameter.
This applies only for logical interface.
default: no
state: state:
description: description:
- State of the Interface configuration, C(up) means present and - State of the Interface configuration, C(up) means present and
@ -90,12 +79,12 @@ EXAMPLES = """
- name: make interface down - name: make interface down
vyos_interface: vyos_interface:
name: eth0 name: eth0
state: down enabled: False
- name: make interface up - name: make interface up
vyos_interface: vyos_interface:
name: eth0 name: eth0
state: up enabled: True
- name: Configure interface speed, mtu, duplex - name: Configure interface speed, mtu, duplex
vyos_interface: vyos_interface:
@ -119,7 +108,12 @@ commands:
""" """
import re import re
from time import sleep
from ansible.module_utils._text import to_text
from ansible.module_utils.basic import AnsibleModule 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.vyos import load_config, get_config from ansible.module_utils.vyos import load_config, get_config
from ansible.module_utils.vyos import vyos_argument_spec, check_args from ansible.module_utils.vyos import vyos_argument_spec, check_args
@ -194,7 +188,6 @@ def map_config_to_obj(module):
match = re.search(r'%s (\S+)' % name, line, re.M) match = re.search(r'%s (\S+)' % name, line, re.M)
if match: if match:
param = match.group(1) param = match.group(1)
if param == 'description': if param == 'description':
match = re.search(r'description (\S+)', line, re.M) match = re.search(r'description (\S+)', line, re.M)
@ -234,15 +227,23 @@ def map_params_to_obj(module):
if item not in d: if item not in d:
d[item] = None d[item] = None
if d.get('description') is None:
d['description'] = DEFAULT_DESCRIPTION d['description'] = DEFAULT_DESCRIPTION
if not d.get('state'): if not d.get('state'):
d['state'] = module.params['state'] d['state'] = module.params['state']
if d['state'] in ('present', 'up'): if d.get('enabled') is None:
d['enabled'] = module.params['enabled']
if d['enabled']:
d['disable'] = False d['disable'] = False
else: else:
d['disable'] = True d['disable'] = True
if d.get('delay') is None:
d['delay'] = module.params['delay']
if d.get('speed'): if d.get('speed'):
d['speed'] = str(d['speed']) d['speed'] = str(d['speed'])
@ -254,11 +255,11 @@ def map_params_to_obj(module):
'speed': module.params['speed'], 'speed': module.params['speed'],
'mtu': module.params['mtu'], 'mtu': module.params['mtu'],
'duplex': module.params['duplex'], 'duplex': module.params['duplex'],
'delay': module.params['delay'],
'state': module.params['state'] 'state': module.params['state']
} }
state = module.params['state'] if module.params['enabled']:
if state == 'present' or state == 'up':
params.update({'disable': False}) params.update({'disable': False})
else: else:
params.update({'disable': True}) params.update({'disable': True})
@ -267,6 +268,35 @@ def map_params_to_obj(module):
return obj return obj
def check_declarative_intent_params(module, want, result):
failed_conditions = []
for w in want:
want_state = w.get('state')
want_tx_rate = w.get('tx_rate')
want_rx_rate = w.get('rx_rate')
if want_state not in ('up', 'down') and not want_tx_rate and not want_rx_rate:
continue
if result['changed']:
sleep(w['delay'])
command = 'show interfaces ethernet %s' % w['name']
rc, out, err = exec_command(module, command)
if rc != 0:
module.fail_json(msg=to_text(err, errors='surrogate_then_replace'), command=command, rc=rc)
if want_state in ('up', 'down'):
match = re.search(r'%s (\w+)' % 'state', out, re.M)
have_state = None
if match:
have_state = match.group(1)
if have_state is None or not conditional(want_state, have_state.strip().lower()):
failed_conditions.append('state ' + 'eq(%s)' % want_state)
return failed_conditions
def main(): def main():
""" main entry point for module execution """ main entry point for module execution
""" """
@ -276,10 +306,9 @@ def main():
speed=dict(), speed=dict(),
mtu=dict(type='int'), mtu=dict(type='int'),
duplex=dict(choices=['full', 'half', 'auto']), duplex=dict(choices=['full', 'half', 'auto']),
tx_rate=dict(), enabled=dict(default=True, type='bool'),
rx_rate=dict(), delay=dict(default=10, type='int'),
aggregate=dict(type='list'), aggregate=dict(type='list'),
purge=dict(default=False, type='bool'),
state=dict(default='present', state=dict(default='present',
choices=['present', 'absent', 'up', 'down']) choices=['present', 'absent', 'up', 'down'])
) )
@ -318,6 +347,11 @@ def main():
result['diff'] = {'prepared': diff} result['diff'] = {'prepared': diff}
result['changed'] = True result['changed'] = True
failed_conditions = check_declarative_intent_params(module, want, result)
if failed_conditions:
msg = 'One or more conditional statements have not been satisfied'
module.fail_json(msg=msg, failed_conditions=failed_conditions)
module.exit_json(**result) module.exit_json(**result)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -59,7 +59,7 @@
- name: Disable interface - name: Disable interface
net_interface: net_interface:
name: eth1 name: eth1
state: down enabled: False
register: result register: result
- assert: - assert:
@ -70,7 +70,7 @@
- name: Enable interface - name: Enable interface
net_interface: net_interface:
name: eth1 name: eth1
state: up enabled: True
register: result register: result
- assert: - assert:
@ -138,8 +138,8 @@
- name: Disable interface on aggregate - name: Disable interface on aggregate
net_interface: net_interface:
aggregate: aggregate:
- { name: eth1, description: test-interface-1, speed: 100, duplex: half, mtu: 512, state: down} - { name: eth1, description: test-interface-1, speed: 100, duplex: half, mtu: 512, enabled: False}
- { name: eth2, description: test-interface-2, speed: 1000, duplex: full, mtu: 256, state: down} - { name: eth2, description: test-interface-2, speed: 1000, duplex: full, mtu: 256, enabled: False}
register: result register: result
- assert: - assert:
@ -151,8 +151,8 @@
- name: Enable interface on aggregate - name: Enable interface on aggregate
net_interface: net_interface:
aggregate: aggregate:
- { name: eth1, description: test-interface-1, speed: 100, duplex: half, mtu: 512, state: present} - { name: eth1, description: test-interface-1, speed: 100, duplex: half, mtu: 512, enabled: True}
- { name: eth2, description: test-interface-2, speed: 1000, duplex: full, mtu: 256, state: present} - { name: eth2, description: test-interface-2, speed: 1000, duplex: full, mtu: 256, enabled: True}
register: result register: result
- assert: - assert:

View file

@ -0,0 +1,74 @@
---
- debug: msg="START net_interface vyos/intent.yaml"
- name: Setup (interface is up)
net_interface:
name: eth1
enabled: True
state: present
provider: "{{ cli }}"
register: result
- name: Check intent arguments
net_interface:
name: eth1
state: up
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.failed == false"
- name: Check intent arguments (failed condition)
net_interface:
name: eth1
state: down
provider: "{{ cli }}"
ignore_errors: yes
register: result
- assert:
that:
- "result.failed == true"
- "'state eq(down)' in result.failed_conditions"
- name: Config + intent
net_interface:
name: eth1
enabled: False
state: down
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.failed == false"
- name: Config + intent (fail)
net_interface:
name: eth1
enabled: False
state: up
provider: "{{ cli }}"
ignore_errors: yes
register: result
- assert:
that:
- "result.failed == true"
- "'state eq(up)' in result.failed_conditions"
- name: Aggregate config + intent (pass)
net_interface:
aggregate:
- name: eth1
enabled: True
state: up
provider: "{{ cli }}"
ignore_errors: yes
register: result
- assert:
that:
- "result.failed == false"

View file

@ -84,7 +84,7 @@
- name: Disable interface - name: Disable interface
vyos_interface: vyos_interface:
name: eth1 name: eth1
state: down enabled: False
register: result register: result
- assert: - assert:
@ -95,7 +95,7 @@
- name: Enable interface - name: Enable interface
vyos_interface: vyos_interface:
name: eth1 name: eth1
state: up enabled: True
register: result register: result
- assert: - assert:
@ -167,8 +167,8 @@
- name: Disable interface on aggregate - name: Disable interface on aggregate
vyos_interface: vyos_interface:
aggregate: aggregate:
- { name: eth1, description: test-interface-1, state: down} - { name: eth1, description: test-interface-1, enabled: False}
- { name: eth2, description: test-interface-2, state: down} - { name: eth2, description: test-interface-2, enabled: False}
register: result register: result
- assert: - assert:
@ -180,8 +180,8 @@
- name: Enable interface on aggregate - name: Enable interface on aggregate
vyos_interface: vyos_interface:
aggregate: aggregate:
- { name: eth1, description: test-interface-1, state: present} - { name: eth1, description: test-interface-1, enabled: True}
- { name: eth2, description: test-interface-2, state: present} - { name: eth2, description: test-interface-2, enabled: True}
register: result register: result
- assert: - assert:

View file

@ -0,0 +1,74 @@
---
- debug: msg="START vyos_interface cli/intent.yaml"
- name: Setup (interface is up)
vyos_interface:
name: eth1
enabled: True
state: present
provider: "{{ cli }}"
register: result
- name: Check intent arguments
vyos_interface:
name: eth1
state: up
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.failed == false"
- name: Check intent arguments (failed condition)
vyos_interface:
name: eth1
state: down
provider: "{{ cli }}"
ignore_errors: yes
register: result
- assert:
that:
- "result.failed == true"
- "'state eq(down)' in result.failed_conditions"
- name: Config + intent
vyos_interface:
name: eth1
enabled: False
state: down
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.failed == false"
- name: Config + intent (fail)
vyos_interface:
name: eth1
enabled: False
state: up
provider: "{{ cli }}"
ignore_errors: yes
register: result
- assert:
that:
- "result.failed == true"
- "'state eq(up)' in result.failed_conditions"
- name: Aggregate config + intent (pass)
vyos_interface:
aggregate:
- name: eth1
enabled: True
state: up
provider: "{{ cli }}"
ignore_errors: yes
register: result
- assert:
that:
- "result.failed == false"