-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from scsitteam/interface_lo
Feature: Add module for Loopback interfaces
- Loading branch information
Showing
7 changed files
with
250 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from ansible.module_utils.basic import AnsibleModule | ||
|
||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.api import \ | ||
Session | ||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.cls import BaseModule | ||
|
||
|
||
class Loopback(BaseModule): | ||
FIELD_ID = 'description' | ||
CMDS = { | ||
'add': 'addItem', | ||
'del': 'delItem', | ||
'set': 'setItem', | ||
'search': 'get', | ||
} | ||
API_KEY_PATH = 'loopback.loopback' | ||
API_MOD = 'interfaces' | ||
API_CONT = 'loopback_settings' | ||
API_CMD_REL = 'reconfigure' | ||
FIELDS_CHANGE = [] | ||
FIELDS_ALL = [FIELD_ID] | ||
FIELDS_TYPING = {} | ||
EXIST_ATTR = 'interface' | ||
|
||
def __init__(self, module: AnsibleModule, result: dict, session: Session = None): | ||
BaseModule.__init__(self=self, m=module, r=result, s=session) | ||
self.interface = {} | ||
|
||
def check(self) -> None: | ||
self._base_check() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright: (C) 2024, AnsibleGuy <[email protected]> | ||
# GNU General Public License v3.0+ (see https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# template to be copied to implement new modules | ||
|
||
from ansible.module_utils.basic import AnsibleModule | ||
|
||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.base.handler import \ | ||
module_dependency_error, MODULE_EXCEPTIONS | ||
|
||
try: | ||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.wrapper import module_wrapper | ||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.defaults.main import \ | ||
OPN_MOD_ARGS, STATE_MOD_ARG, RELOAD_MOD_ARG | ||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.main.interface_loopback import Loopback | ||
|
||
except MODULE_EXCEPTIONS: | ||
module_dependency_error() | ||
|
||
|
||
# DOCUMENTATION = 'https://opnsense.ansibleguy.net/en/latest/modules/interface_loopback.html' | ||
# EXAMPLES = 'https://opnsense.ansibleguy.net/en/latest/modules/interface_loopback.html' | ||
|
||
|
||
def run_module(): | ||
module_args = dict( | ||
description=dict(type='str', required=True, aliases=['desc']), | ||
**RELOAD_MOD_ARG, | ||
**STATE_MOD_ARG, | ||
**OPN_MOD_ARGS, | ||
) | ||
|
||
result = dict( | ||
changed=False, | ||
diff={ | ||
'before': {}, | ||
'after': {}, | ||
} | ||
) | ||
|
||
module = AnsibleModule( | ||
argument_spec=module_args, | ||
supports_check_mode=True, | ||
) | ||
|
||
module_wrapper(Loopback(module=module, result=result)) | ||
module.exit_json(**result) | ||
|
||
|
||
def main(): | ||
run_module() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
|
||
- name: Testing Loopback | ||
hosts: localhost | ||
gather_facts: no | ||
module_defaults: | ||
group/ansibleguy.opnsense.all: | ||
firewall: "{{ lookup('ansible.builtin.env', 'TEST_FIREWALL') }}" | ||
api_credential_file: "{{ lookup('ansible.builtin.env', 'TEST_API_KEY') }}" | ||
ssl_verify: false | ||
|
||
ansibleguy.opnsense.list: | ||
target: 'interface_loopback' | ||
|
||
tasks: | ||
- name: Listing | ||
ansibleguy.opnsense.list: | ||
register: opn_pre1 | ||
failed_when: > | ||
'data' not in opn_pre1 or | ||
opn_pre1.data | length != 0 | ||
- name: Removing - does not exist | ||
ansibleguy.opnsense.interface_loopback: | ||
description: 'DOESNOTEXIST' | ||
state: 'absent' | ||
register: opn_pre2 | ||
failed_when: > | ||
opn_pre2.failed or | ||
opn_pre2.changed | ||
- name: Adding 1 | ||
ansibleguy.opnsense.interface_loopback: | ||
description: 'ANSIBLE_TEST_1_1' | ||
register: opn1 | ||
failed_when: > | ||
opn1.failed or | ||
not opn1.changed | ||
- name: Adding 2 | ||
ansibleguy.opnsense.interface_loopback: | ||
description: 'ANSIBLE_TEST_1_2' | ||
register: opn2 | ||
failed_when: > | ||
opn2.failed or | ||
not opn2.changed | ||
- name: Adding 2 - nothing changed | ||
ansibleguy.opnsense.interface_loopback: | ||
description: 'ANSIBLE_TEST_1_2' | ||
register: opn3 | ||
failed_when: > | ||
opn3.failed or | ||
opn3.changed | ||
when: not ansible_check_mode | ||
|
||
- name: Removing 2 | ||
ansibleguy.opnsense.interface_loopback: | ||
description: 'ANSIBLE_TEST_1_2' | ||
state: 'absent' | ||
register: opn4 | ||
failed_when: > | ||
opn4.failed or | ||
not opn4.changed | ||
when: not ansible_check_mode | ||
|
||
- name: Listing | ||
ansibleguy.opnsense.list: | ||
register: opn5 | ||
failed_when: > | ||
'data' not in opn5 or | ||
opn5.data | length != 1 | ||
when: not ansible_check_mode | ||
|
||
- name: Cleanup | ||
ansibleguy.opnsense.interface_loopback: | ||
description: "{{ item }}" | ||
state: 'absent' | ||
loop: | ||
- 'ANSIBLE_TEST_1_1' | ||
- 'ANSIBLE_TEST_1_2' | ||
when: not ansible_check_mode | ||
|
||
- name: Listing | ||
ansibleguy.opnsense.list: | ||
register: opn_clean1 | ||
failed_when: > | ||
'data' not in opn_clean1 or | ||
opn_clean1.data | length != 0 | ||
when: not ansible_check_mode |