-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kdhlab
committed
Jul 6, 2024
1 parent
df0bc14
commit 115ae4c
Showing
2 changed files
with
205 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from ipaddress import ip_address | ||
|
||
from ansible.module_utils.basic import AnsibleModule | ||
|
||
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.main import \ | ||
simplify_translate | ||
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 Routing(BaseModule): | ||
FIELD_ID = 'uuid' | ||
CMDS = { | ||
'add': 'addGateway', | ||
'del': 'delGateway', | ||
'set': 'setGateway', | ||
'search': 'get', | ||
'toggle': 'toggleGateway', | ||
} | ||
API_KEY_PATH = 'routing.settings' | ||
API_MOD = 'routing' | ||
API_CONT = 'settings' | ||
API_CMD_REL = 'reconfigure' | ||
FIELDS_CHANGE = ['name', 'interface', 'ipprotocol', 'gateway', 'defaultgw', 'fargw', 'monitor_disable', 'monitor_noroute', 'monitor', 'force_down', 'priority', 'weight', 'latencylow', 'latencyhigh', 'losslow', 'losshigh', 'interval', 'time_period', 'loss_interval', 'data_length', 'description'] | ||
FIELDS_ALL = ['enabled'] | ||
FIELDS_ALL.extend(FIELDS_CHANGE) | ||
FIELDS_BOOL_INVERT = ['enabled'] | ||
FIELDS_TRANSLATE = { | ||
'description': 'descr', | ||
'enabled': 'disabled', | ||
} | ||
FIELDS_TYPING = { | ||
'bool': ['enabled', 'defaultgw', 'fargw', 'monitor_disable', 'monitor_noroute', 'force_down'], | ||
'int': ['priority', 'weight', 'latencylow', 'latencyhigh', 'losslow', 'losshigh', 'interval', 'time_period', 'loss_interval', 'data_length'], | ||
} | ||
EXIST_ATTR = 'routing' | ||
|
||
def __init__(self, module: AnsibleModule, result: dict, session: Session = None): | ||
BaseModule.__init__(self=self, m=module, r=result, s=session) | ||
self.route = {} | ||
|
||
def check(self) -> None: | ||
try: | ||
ip_address(self.p['gateway']) | ||
|
||
except ValueError: | ||
self.m.fail_json(f"Value '{self.p['gateway']}' is not a valid gateway!") | ||
|
||
self._base_check() | ||
|
||
def _simplify_existing(self, route: dict) -> dict: | ||
# makes processing easier | ||
simple = simplify_translate( | ||
existing=route, | ||
typing=self.FIELDS_TYPING, | ||
translate=self.FIELDS_TRANSLATE, | ||
bool_invert=self.FIELDS_BOOL_INVERT, | ||
) | ||
simple['gateway'] = simple['gateway'].rsplit('-', 1)[0].strip() | ||
return simple |
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,144 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright: (C) 2024, AnsibleGuy <guy@ansibleguy.net> | ||
# GNU General Public License v3.0+ (see https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# see: https://docs.opnsense.org/development/api/core/routing.html | ||
|
||
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.routing import Routing | ||
|
||
except MODULE_EXCEPTIONS: | ||
module_dependency_error() | ||
|
||
|
||
# DOCUMENTATION = 'https://opnsense.ansibleguy.net/en/latest/modules/route.html' | ||
# EXAMPLES = 'https://opnsense.ansibleguy.net/en/latest/modules/route.html' | ||
|
||
|
||
def run_module(): | ||
module_args = dict( | ||
name=dict( | ||
type='str', required=True, | ||
description='Gateway Name' | ||
), | ||
interface=dict( | ||
type='str', required=True, | ||
description='Interface for Gateway' | ||
), | ||
ipaddress=dict( | ||
type='str', required=True, | ||
description='Gateway IP' | ||
), | ||
defaultgw=dict( | ||
type='bool', required=True, | ||
description='Default Gateway', | ||
default=False | ||
), | ||
fargw=dict( | ||
type='bool', required=True, | ||
description='Far Gateway', | ||
default=False | ||
), | ||
monitor_disable=dict( | ||
type='bool', required=True, | ||
description='Disable Gateway Monitor', | ||
default=False | ||
), | ||
monitor_noroute=dict( | ||
type='bool', required=True, | ||
description='....', | ||
default=False | ||
), | ||
monitor=dict( | ||
type='str', required=False, | ||
description='Monitor IP' | ||
), | ||
force_down=dict( | ||
type='bool', required=False, | ||
description='....', | ||
default=False | ||
), | ||
priority=dict( | ||
type='int', required=False, | ||
description='....', | ||
default=255 | ||
), | ||
weight=dict( | ||
type='int', required=False, | ||
description='....', | ||
default=1 | ||
), | ||
latencylow=dict( | ||
type='int', required=False, | ||
description='....', | ||
default=100 | ||
), | ||
latencyhigh=dict( | ||
type='int', required=False, | ||
description='....', | ||
default=500 | ||
), | ||
losslow=dict( | ||
type='int', required=False, | ||
description='....', | ||
default=3 | ||
), | ||
losshigh=dict( | ||
type='int', required=False, | ||
description='....', | ||
default=10 | ||
), | ||
interval=dict( | ||
type='int', required=False, | ||
description='....', | ||
default=1 | ||
), | ||
time_period=dict( | ||
type='int', required=False, | ||
description='....', | ||
default=1 | ||
), | ||
data_length=dict( | ||
type='int', required=False, | ||
description='....', | ||
default=1 | ||
), | ||
description=dict(type='str', required=False, 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(Routing(module=module, result=result)) | ||
module.exit_json(**result) | ||
|
||
|
||
def main(): | ||
run_module() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |