Skip to content

Commit

Permalink
Merge pull request #193 from patrikspiess/add-smtp-option-to-fgt-conf…
Browse files Browse the repository at this point in the history
…ig-check-#24

👔 Add option --smtp to `fgt config check`
  • Loading branch information
lucmurer committed Feb 26, 2024
2 parents 01b0f9b + 5eb696c commit 5c01700
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions WHATSNEW.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### Added

- Option `--raw` for `fmg get devices`
- Option `--smtp` for `fgt config check`

### Changed

Expand Down
23 changes: 23 additions & 0 deletions fotoobo/cli/fgt/config_commands.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""
The FortiGate get commands
"""

import logging
from pathlib import Path

import typer

from fotoobo.helpers.config import config
from fotoobo.inventory.inventory import Inventory
from fotoobo.tools import fgt

app = typer.Typer(no_args_is_help=True, rich_markup_mode="rich")
Expand All @@ -26,11 +29,31 @@ def check(
metavar="[bundles]",
show_default=False,
),
smtp_server: str = typer.Option(
None,
"--smtp",
help="The smtp configuration from the inventory.",
metavar="[server]",
show_default=False,
),
) -> None:
"""
Check one or more FortiGate configuration files.
"""
inventory = Inventory(config.inventory_file)
result = fgt.config.check(configuration, bundles)

if smtp_server:
if smtp_server in inventory.assets:
result.send_messages_as_mail(
inventory.assets[smtp_server],
count=True,
command=True,
)

else:
log.warning("SMTP server '%s' not in found in inventory.", smtp_server)

result.print_messages()


Expand Down
5 changes: 4 additions & 1 deletion fotoobo/helpers/result.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
The fotoobo Result class
"""

import json
import re
import smtplib
from pathlib import Path
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union
Expand Down Expand Up @@ -281,7 +283,8 @@ def send_messages_as_mail(
for host, messages in self.messages.items():
for message in messages:
if not levels or message["level"] in levels:
out_messages.append(f"{host}: {message['message']}")
msg = re.sub(r"\[.*?\]", "", message["message"]) # remove rich formatting
out_messages.append(f"{host}: {msg}")

if out_messages:
body = "To:" + smtp_server.recipient + "\n"
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/fgt/test_cli_fgt_config_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_cli_app_fgt_config_check_help() -> None:
assert result.exit_code == 0
arguments, options, commands = parse_help_output(result.stdout)
assert set(arguments) == {"configuration", "bundles"}
assert options == {"-h", "--help"}
assert options == {"-h", "--help", "--smtp"}
assert not commands


Expand Down

0 comments on commit 5c01700

Please sign in to comment.