Skip to content

Commit

Permalink
Merge pull request #226 from patrikspiess/Add-CLI-commands-to-get-con…
Browse files Browse the repository at this point in the history
…figuration-from-FortiGate-#225

Add cli commands to get configuration from FortiGate #225
  • Loading branch information
lucmurer authored Nov 25, 2024
2 parents a95b207 + 3ad9042 commit 5707b39
Show file tree
Hide file tree
Showing 48 changed files with 1,093 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ For unreleased changes see [WHATSNEW.md](WHATSNEW.md)

### Added

- FortiManager Methods for handling addresses and services (get and delete)
- FortiManager methods for handling addresses and services (get and delete)

### Changed

Expand Down
8 changes: 8 additions & 0 deletions WHATSNEW.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
### Added

- Add generic api_get() method in tools/fgt/get
- Add CLI command fgt get cmdb firewall address
- Add CLI command fgt get cmdb firewall addrgrp
- Add CLI command fgt get cmdb firewall service-custom
- Add CLI command fgt get cmdb firewall service-group
- Add the layers of fotoobo into the architecture documentation


### Changed

- Optimize imports in CLI module

### Removed

4 changes: 4 additions & 0 deletions fotoobo/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
Do not add too much logic into the cli commands. Just add the tools module and add the
logic there. Therefore, we can segregate the duties.
"""

from . import convert, get

__all__ = ["convert", "get"]
4 changes: 4 additions & 0 deletions fotoobo/cli/ems/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
__init__.py
"""

from . import ems, get, monitor

__all__ = ["ems", "get", "monitor"]
4 changes: 2 additions & 2 deletions fotoobo/cli/ems/main.py → fotoobo/cli/ems/ems.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

import typer

from fotoobo.cli.ems import get_commands as get
from fotoobo.cli.ems import monitor_commands as monitor
from fotoobo.helpers import cli_path

from . import get, monitor

app = typer.Typer(no_args_is_help=True, rich_markup_mode="rich")
log = logging.getLogger("fotoobo")

Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions fotoobo/cli/faz/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
__init__.py
"""

from . import faz

__all__ = ["faz"]
3 changes: 2 additions & 1 deletion fotoobo/cli/faz/main.py → fotoobo/cli/faz/faz.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import typer

from fotoobo.cli.faz import get_commands as get
from fotoobo.helpers import cli_path

from . import get

app = typer.Typer(no_args_is_help=True, rich_markup_mode="rich")
log = logging.getLogger("fotoobo")

Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions fotoobo/cli/fgt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
__init__.py
"""

from . import fgt

__all__ = ["fgt"]
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def get(
),
path: str = typer.Argument("/", help="Configuration path", metavar="[path]"),
) -> None:
"""Get configuration or parts of it from one or more FortiGate configuration files"""
"""Get configuration or parts of it from one or more FortiGate configuration files."""
result = fgt.config.get(configuration, scope, path)
result.print_raw()

Expand Down
17 changes: 9 additions & 8 deletions fotoobo/cli/fgt/main.py → fotoobo/cli/fgt/fgt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@
import typer

from fotoobo import tools
from fotoobo.cli.fgt import config_commands as config
from fotoobo.cli.fgt import get_commands as get
from fotoobo.cli.fgt import monitor_commands as monitor
from fotoobo.exceptions import GeneralWarning
from fotoobo.helpers import cli_path
from fotoobo.helpers.config import config as fotoobo_config
from fotoobo.helpers.files import create_dir, file_to_ftp, file_to_zip
from fotoobo.inventory import Inventory

from . import config, monitor
from .get import get

app = typer.Typer(no_args_is_help=True, rich_markup_mode="rich")
log = logging.getLogger("fotoobo")


# app.add_typer(get_commands.app, name="get", help="FortiGate get commands.")
app.add_typer(config.app, name="config", help="FortiGate config file commands.")
app.add_typer(get.app, name="get", help="FortiGate get commands.")
app.add_typer(monitor.app, name="monitor", help="FortiGate monitor commands.")


@app.callback()
def callback(context: typer.Context) -> None:
"""
Expand All @@ -35,11 +41,6 @@ def callback(context: typer.Context) -> None:
log.debug("About to execute command: '%s'", context.invoked_subcommand)


app.add_typer(get.app, name="get", help="FortiGate get commands.")
app.add_typer(monitor.app, name="monitor", help="FortiGate monitor commands.")
app.add_typer(config.app, name="config", help="FortiGate config file commands.")


@app.command()
def backup(
host: str = typer.Argument(
Expand Down
7 changes: 7 additions & 0 deletions fotoobo/cli/fgt/get/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
__init__.py
"""

from . import get

__all__ = ["get"]
7 changes: 7 additions & 0 deletions fotoobo/cli/fgt/get/cmdb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
__init__.py
"""

from . import cmdb

__all__ = ["cmdb"]
29 changes: 29 additions & 0 deletions fotoobo/cli/fgt/get/cmdb/cmdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
The FortiGate commands
"""

import logging

import typer

from fotoobo.helpers import cli_path

from .firewall import firewall

app = typer.Typer(no_args_is_help=True, rich_markup_mode="rich")
log = logging.getLogger("fotoobo")


app.add_typer(firewall.app, name="firewall", help="FortiGate get cmdb firewall commands.")


@app.callback()
def callback(context: typer.Context) -> None:
"""
The fgt get cmdb subcommand callback
Args:
context: The context object of the typer app
"""
cli_path.append(str(context.invoked_subcommand))
log.debug("About to execute command: '%s'", context.invoked_subcommand)
7 changes: 7 additions & 0 deletions fotoobo/cli/fgt/get/cmdb/firewall/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
__init__.py
"""

from . import firewall

__all__ = ["firewall"]
187 changes: 187 additions & 0 deletions fotoobo/cli/fgt/get/cmdb/firewall/firewall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
"""
The FortiGate commands
"""

# pylint: disable=anomalous-backslash-in-string
import logging

import typer

from fotoobo.exceptions import GeneralError
from fotoobo.helpers import cli_path
from fotoobo.tools.fgt.cmdb.firewall import * # pylint: disable=wildcard-import

app = typer.Typer(no_args_is_help=True, rich_markup_mode="rich")
log = logging.getLogger("fotoobo")


@app.callback()
def callback(context: typer.Context) -> None:
"""
The fgt get cmdb get firewall subcommand callback
Args:
context: The context object of the typer app
"""
cli_path.append(str(context.invoked_subcommand))
log.debug("About to execute command: '%s'", context.invoked_subcommand)


@app.command()
def address(
host: str = typer.Argument(
"",
help="The FortiGate hostname to access (must be defined in the inventory). "
"\[default: <all>]",
show_default=False,
metavar="[host]",
),
name: str = typer.Argument(
"",
help="The firewall address object to get \[default: <all>]",
show_default=False,
metavar="[name]",
),
vdom: str = typer.Option(
"*",
"--vdom",
help="The vdom to query ('vdom1' or 'vdom1,vdom2') \[default: <all>]",
show_default=False,
metavar="[vdom]",
),
output_file: str = typer.Option(
None,
"--output",
"-o",
help="Output file (format is specified by extension)",
metavar="[file]",
show_default=False,
),
) -> None:
"""Get FortiGate cmdb firewall address."""
if name and ("*" in vdom or "," in vdom):
raise GeneralError("With name argument you have to specify one single VDOM (with --vdom)")

result = get_cmdb_firewall_address(host, name, vdom, output_file)
if not output_file:
result.print_table_raw(result.results[host], auto_header=True, title=host)


@app.command()
def addrgrp(
host: str = typer.Argument(
"",
help="The FortiGate hostname to access (must be defined in the inventory). "
"\[default: <all>]",
show_default=False,
metavar="[host]",
),
name: str = typer.Argument(
"",
help="The firewall address group object to get \[default: <all>]",
show_default=False,
metavar="[name]",
),
vdom: str = typer.Option(
"*",
"--vdom",
help="The vdom to query ('vdom1' or 'vdom1,vdom2') \[default: <all>]",
show_default=False,
metavar="[vdom]",
),
output_file: str = typer.Option(
None,
"--output",
"-o",
help="Output file (format is specified by extension)",
metavar="[file]",
show_default=False,
),
) -> None:
"""Get FortiGate cmdb firewall address group."""
if name and ("*" in vdom or "," in vdom):
raise GeneralError("With name argument you have to specify one single VDOM (with --vdom)")

result = get_cmdb_firewall_addrgrp(host, name, vdom, output_file)
if not output_file:
result.print_table_raw(result.results[host], auto_header=True, title=host)


@app.command()
def service_custom(
host: str = typer.Argument(
"",
help="The FortiGate hostname to access (must be defined in the inventory). "
"\[default: <all>]",
show_default=False,
metavar="[host]",
),
name: str = typer.Argument(
"",
help="The firewall service object to get \[default: <all>]",
show_default=False,
metavar="[name]",
),
vdom: str = typer.Option(
"*",
"--vdom",
help="The vdom to query ('vdom1' or 'vdom1,vdom2') \[default: <all>]",
show_default=False,
metavar="[vdom]",
),
output_file: str = typer.Option(
None,
"--output",
"-o",
help="Output file (format is specified by extension)",
metavar="[file]",
show_default=False,
),
) -> None:
"""Get FortiGate cmdb firewall service custom."""
if name and ("*" in vdom or "," in vdom):
raise GeneralError("With name argument you have to specify one single VDOM (with --vdom)")

result = get_cmdb_firewall_service_custom(host, name, vdom, output_file)
if not output_file:
result.print_table_raw(result.results[host], auto_header=True, title=host)


@app.command()
def service_group(
host: str = typer.Argument(
"",
help="The FortiGate hostname to access (must be defined in the inventory). "
"\[default: <all>]",
show_default=False,
metavar="[host]",
),
name: str = typer.Argument(
"",
help="The firewall service group to get \[default: <all>]",
show_default=False,
metavar="[name]",
),
vdom: str = typer.Option(
"*",
"--vdom",
help="The vdom to query ('vdom1' or 'vdom1,vdom2') \[default: <all>]",
show_default=False,
metavar="[vdom]",
),
output_file: str = typer.Option(
None,
"--output",
"-o",
help="Output file (format is specified by extension)",
metavar="[file]",
show_default=False,
),
) -> None:
"""Get FortiGate cmdb firewall service group."""
if name and ("*" in vdom or "," in vdom):
raise GeneralError("With name argument you have to specify one single VDOM (with --vdom)")

result = get_cmdb_firewall_service_group(host, name, vdom, output_file)
if not output_file:
result.print_table_raw(result.results[host], auto_header=True, title=host)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
The FortiGate get commands
The FortiGate commands
"""

# pylint: disable=anomalous-backslash-in-string
Expand All @@ -10,10 +10,15 @@
from fotoobo.helpers import cli_path
from fotoobo.tools import fgt

from .cmdb import cmdb

app = typer.Typer(no_args_is_help=True, rich_markup_mode="rich")
log = logging.getLogger("fotoobo")


app.add_typer(cmdb.app, name="cmdb", help="FortiGate get cmdb commands.")


@app.callback()
def callback(context: typer.Context) -> None:
"""
Expand Down
File renamed without changes.
Loading

0 comments on commit 5707b39

Please sign in to comment.