class AnsibleFortiManager(object):
    """
    - DEPRECATING: USING CONNECTION MANAGER NOW INSTEAD. EVENTUALLY THIS CLASS WILL DISAPPEAR. PLEASE
    - CONVERT ALL MODULES TO CONNECTION MANAGER METHOD.
    - LEGACY pyFMG HANDLER OBJECT: REQUIRES A CHECK FOR PY FMG AT TOP OF PAGE
    """

    def __init__(self, module, ip=None, username=None, passwd=None, use_ssl=True, verify_ssl=False, timeout=300):
        self.ip = ip
        self.username = username
        self.passwd = passwd
        self.use_ssl = use_ssl
        self.verify_ssl = verify_ssl
        self.timeout = timeout
        self.fmgr_instance = None

        if not HAS_PYFMGR:
            module.fail_json(msg='Could not import the python library pyFMG required by this module')

        self.module = module

    def login(self):
        if self.ip is not None:
            self.fmgr_instance = FortiManager(self.ip, self.username, self.passwd, use_ssl=self.use_ssl,
                                              verify_ssl=self.verify_ssl, timeout=self.timeout, debug=False,
                                              disable_request_warnings=True)
            return self.fmgr_instance.login()

    def logout(self):
        if self.fmgr_instance.sid is not None:
            self.fmgr_instance.logout()

    def get(self, url, data):
        return self.fmgr_instance.get(url, **data)

    def set(self, url, data):
        return self.fmgr_instance.set(url, **data)

    def update(self, url, data):
        return self.fmgr_instance.update(url, **data)

    def delete(self, url, data):
        return self.fmgr_instance.delete(url, **data)

    def add(self, url, data):
        return self.fmgr_instance.add(url, **data)

    def execute(self, url, data):
        return self.fmgr_instance.execute(url, **data)

    def move(self, url, data):
        return self.fmgr_instance.move(url, **data)

    def clone(self, url, data):
        return self.fmgr_instance.clone(url, **data)
def _send_request(call):
    logger.info('_send_request request_props:{}'.format(call))
    host = call['host']
    username = call.get('username')
    password = call.get('password')
    use_ssl = call.get('use_ssl', False)
    verify_ssl = call.get('verify_ssl', False)

    url = call.get('path')
    data = call.get('data', {})
    method = call.get('method')

    fmg_instance = FortiManager(host,
                                username,
                                password,
                                debug=False,
                                use_ssl=use_ssl,
                                verify_ssl=verify_ssl,
                                disable_request_warnings=True)

    fmg_instance.login()

    if method == "GET":
        response = fmg_instance.get(url)
        logger.debug('---> Method: {} \n response: \n {}'.format(
            method, response))

    if method == "ADD":
        response = fmg_instance.add(url, **data)
        logger.debug('---> Method: {} \n response: \n {}'.format(
            method, response))

    if method == "DELETE":
        response = fmg_instance.delete(url)
        logger.debug('---> Method: {} \n response: \n {}'.format(
            method, response))

        # if method == "UPDATE":
        # if method == "SET":
        # if method == "REPLACE":
        # if method == "CLONE":
    if method == "EXECUTE":
        response = fmg_instance.execute(url, **data)
        logger.debug('---> Method: {} \n response: \n {}'.format(
            method, response))

    fmg_instance.logout()
    return response
Beispiel #3
0
class AnsibleFortiManager(object):
    def __init__(self,
                 module,
                 ip=None,
                 username=None,
                 passwd=None,
                 use_ssl=True,
                 verify_ssl=False,
                 timeout=300):
        self.ip = ip
        self.username = username
        self.passwd = passwd
        self.use_ssl = use_ssl
        self.verify_ssl = verify_ssl
        self.timeout = timeout
        self.fmgr_instance = None

        if not HAS_PYFMGR:
            module.fail_json(
                msg=
                'Could not import the python library pyFMG required by this module'
            )

        self.module = module

    def login(self):
        if self.ip is not None:
            self.fmgr_instance = FortiManager(self.ip,
                                              self.username,
                                              self.passwd,
                                              use_ssl=self.use_ssl,
                                              verify_ssl=self.verify_ssl,
                                              timeout=self.timeout,
                                              debug=False,
                                              disable_request_warnings=True)
            return self.fmgr_instance.login()

    def logout(self):
        if self.fmgr_instance.sid is not None:
            self.fmgr_instance.logout()

    def get(self, url, data):
        return self.fmgr_instance.get(url, **data)

    def set(self, url, data):
        return self.fmgr_instance.set(url, **data)

    def update(self, url, data):
        return self.fmgr_instance.update(url, **data)

    def delete(self, url, data):
        return self.fmgr_instance.delete(url, **data)

    def add(self, url, data):
        return self.fmgr_instance.add(url, **data)

    def execute(self, url, data):
        return self.fmgr_instance.execute(url, **data)

    def move(self, url, data):
        return self.fmgr_instance.move(url, **data)

    def clone(self, url, data):
        return self.fmgr_instance.clone(url, **data)