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 _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
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)
Example #4
0
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
import json
from pyFMG.fortimgr import FortiManager
import pytest

try:
    from ansible.modules.network.fortimanager import fmgr_ha
except ImportError:
    pytest.skip("Could not load required modules for testing",
                allow_module_level=True)

fmg_instance = FortiManager("1.1.1.1", "admin", "")


def load_fixtures():
    fixture_path = os.path.join(
        os.path.dirname(__file__), 'fixtures') + "/{filename}.json".format(
            filename=os.path.splitext(os.path.basename(__file__))[0])
    try:
        with open(fixture_path, "r") as fixture_file:
            fixture_data = json.load(fixture_file)
    except IOError:
        return []
    return [fixture_data]


@pytest.fixture(scope="function", params=load_fixtures())
Example #5
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)