Importing fotoobo
Although fotoobo is meant to be used as a CLI application you may also import it into your own Python modules. With this you can write your own business logic without changing the fotoobo code itself.
First install fotoobo as documented in Getting Started:
pip install fotoobo
After successful installation you may import the desired module into your code. Refer to the Fortinet Classes to get a list of available module parameters.
Examples
FortiGate
from fotoobo import FortiGate
fgt = FortiGate("<HOSTNAME>", "<TOKEN>", https_port=8443, ssl_verify=False)
print(fgt.get_version())
FortiManager
from fotoobo import FortiManager
fmg = FortiManager("<HOSTNAME>", "<USERNAME>", "<PASSWORD>", ssl_verify=False)
print(fmg.get_version())
fmg.logout()
FortiAnalyzer
from fotoobo import FortiAnalyzer
faz = FortiAnalyzer("<HOSTNAME>", "<USERNAME>", "<PASSWORD>", ssl_verify=False)
print(faz.get_version())
faz.logout()
FortiClient EMS
from fotoobo import FortiClientEMS
ems = FortiClientEMS("<HOSTNAME>", "<USERNAME>", "<PASSWORD>", ssl_verify=False)
ems.login()
print(ems.get_version())
ems.logout()
Using an inventory file
You can maintain an inventory file to load the assets from. This helps not having API tokens or username/passwords directly in the source code.
Imagine you created the inventory file ~/.config/inventory.yaml:
fmg-test:
hostname: myfortimanager.local
username: the_fortimanager_username
password: the_fortimanager_password
type: fortimanager
With this inventory file you can use the asset `fmg-test in your code:
from fotoobo.inventory import Inventory
inventory = Inventory(Path("~/.config/inventory.yaml"))
fmg = inventory.get_item("fmg-test")
print(fmg.get_version())
Using a configuration file
You can read your fotoobo configuration file. This is useful if you wish to load and change settings like inventory file, logging options or a vault configuration.
Use the inventory file ~/.config/inventory.yaml with the string VAULT as the token or password for your assets.
fmg-test:
hostname: myfortimanager.local
username: the_fortimanager_username
password: VAULT
type: fortimanager
Then you have to specify this inventory file and the vault configuration in the fotoobo configuration file ~/.config/fotoobo.yaml.
inventory: ~/.config/inventory.yaml
vault:
url: https://vault.local
namespace: vault_namespace
data_path: /v1/kv/data/fotoobo
role_id: ...
secret_id: ...
token_file: ~/.cache/token.key
With these preparations you can use the fotoobo configuration to access your assets.
from fotoobo.helpers.config import config
from fotoobo.inventory import Inventory
config.load_configuration()
config.vault["ssl_verify"] = False
inventory = Inventory(config.inventory_file)
fmg = inventory.get_item("fmg-test")
print(fmg.get_version())