| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
from __future__ import absolute_import, division, print_function |
| 29 |
|
| 30 |
__metaclass__ = type |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class FMGRMethods: |
| 35 |
GET = "get" |
| 36 |
SET = "set" |
| 37 |
EXEC = "exec" |
| 38 |
EXECUTE = "exec" |
| 39 |
UPDATE = "update" |
| 40 |
ADD = "add" |
| 41 |
DELETE = "delete" |
| 42 |
REPLACE = "replace" |
| 43 |
CLONE = "clone" |
| 44 |
MOVE = "move" |
| 45 |
|
| 46 |
|
| 47 |
COMMON_SCHEMA = { |
| 48 |
'access_token': {'type': 'str', 'no_log': True}, |
| 49 |
'bypass_validation': {'type': 'bool', 'default': False}, |
| 50 |
'enable_log': {'type': 'bool', 'default': False}, |
| 51 |
'forticloud_access_token': {'type': 'str', 'no_log': True}, |
| 52 |
'rc_succeeded': {'type': 'list', 'elements': 'int'}, |
| 53 |
'rc_failed': {'type': 'list', 'elements': 'int'}, |
| 54 |
'workspace_locking_adom': {'type': 'str'}, |
| 55 |
'workspace_locking_timeout': {'type': 'int', 'default': 300} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
def get_module_arg_spec(task_type): |
| 60 |
arg_spec = {} |
| 61 |
for key in COMMON_SCHEMA: |
| 62 |
arg_spec[key] = COMMON_SCHEMA[key] |
| 63 |
if task_type != 'exec': |
| 64 |
arg_spec['proposed_method'] = { |
| 65 |
'type': 'str', |
| 66 |
'required': False, |
| 67 |
'choices': ['set', 'update', 'add'] |
| 68 |
} |
| 69 |
if task_type in ['full crud', 'object member']: |
| 70 |
arg_spec['state'] = { |
| 71 |
'type': 'str', |
| 72 |
'required': True, |
| 73 |
'choices': ['present', 'absent'] |
| 74 |
} |
| 75 |
return arg_spec |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
class FMGBaseException(Exception): |
| 80 |
"""Wrapper to catch the unexpected""" |
| 81 |
|
| 82 |
def __init__(self, msg=None, *args, **kwargs): |
| 83 |
if msg is None: |
| 84 |
msg = "An exception occurred within the fortimanager.py httpapi connection plugin." |
| 85 |
super(FMGBaseException, self).__init__(msg, *args) |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
class FMGRCommon(object): |
| 92 |
@staticmethod |
| 93 |
def format_request(method, url, *args, **kwargs): |
| 94 |
""" |
| 95 |
Formats the payload from the module, into a payload the API handler can use. |
| 96 |
|
| 97 |
:param url: Connection URL to access |
| 98 |
:type url: string |
| 99 |
:param method: The preferred API Request method (GET, ADD, POST, etc....) |
| 100 |
:type method: basestring |
| 101 |
:param kwargs: The payload dictionary from the module to be converted. |
| 102 |
|
| 103 |
:return: Properly formatted dictionary payload for API Request via Connection Plugin. |
| 104 |
:rtype: dict |
| 105 |
""" |
| 106 |
|
| 107 |
params = [{"url": url}] |
| 108 |
if args: |
| 109 |
for arg in args: |
| 110 |
params[0].update(arg) |
| 111 |
if kwargs: |
| 112 |
keylist = list(kwargs) |
| 113 |
for k in keylist: |
| 114 |
kwargs[k.replace("__", "-")] = kwargs.pop(k) |
| 115 |
if method == "get" or method == "clone": |
| 116 |
params[0].update(kwargs) |
| 117 |
else: |
| 118 |
if kwargs.get("data", False): |
| 119 |
params[0]["data"] = kwargs["data"] |
| 120 |
else: |
| 121 |
params[0]["data"] = kwargs |
| 122 |
return params |
| 123 |
|
| 124 |
@staticmethod |
| 125 |
def syslog(module, msg): |
| 126 |
try: |
| 127 |
module.log(msg=msg) |
| 128 |
except BaseException: |
| 129 |
pass |