forked from andreas-stuerz/opn-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New command: configbackup -> Config backup via new api endpoint (OPNs…
…ense version >=24.1)
- Loading branch information
1 parent
1c99d2c
commit 467ebcd
Showing
12 changed files
with
563 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from opnsense_cli.api.base import ApiBase | ||
|
||
|
||
class Backup(ApiBase): | ||
MODULE = "core" | ||
CONTROLLER = "backup" | ||
""" | ||
api-backup BackupController | ||
""" | ||
|
||
@ApiBase._api_call | ||
def download(self, *args): | ||
self.method = "get" | ||
self.command = "download" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import click | ||
|
||
|
||
@click.group() | ||
def configbackup(**kwargs): | ||
""" | ||
Manage api-backup operations (OPNsense version >= 24.1) | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import click | ||
from opnsense_cli.formatters.cli_output import CliOutputFormatter | ||
from opnsense_cli.callbacks.click import formatter_from_formatter_name, expand_path, available_formats | ||
from opnsense_cli.commands.core.configbackup import configbackup | ||
from opnsense_cli.api.client import ApiClient | ||
from opnsense_cli.api.core.configbackup import Backup | ||
from opnsense_cli.facades.commands.core.configbackup.backup import ApibackupBackupFacade | ||
|
||
pass_api_client = click.make_pass_decorator(ApiClient) | ||
pass_apibackup_backup_svc = click.make_pass_decorator(ApibackupBackupFacade) | ||
|
||
|
||
@configbackup.group() | ||
@pass_api_client | ||
@click.pass_context | ||
def backup(ctx, api_client: ApiClient, **kwargs): | ||
""" | ||
Manage api-backup | ||
""" | ||
backup_api = Backup(api_client) | ||
ctx.obj = ApibackupBackupFacade(backup_api) | ||
|
||
|
||
@backup.command() | ||
@click.option( | ||
"-p", | ||
"--path", | ||
help="The target path.", | ||
type=click.Path(dir_okay=False), | ||
default="./config.xml", | ||
is_eager=True, | ||
show_default=True, | ||
callback=expand_path, | ||
show_envvar=True, | ||
required=True, | ||
) | ||
@click.option( | ||
"--output", | ||
"-o", | ||
help="Specifies the Output format.", | ||
default="plain", | ||
type=click.Choice(available_formats()), | ||
callback=formatter_from_formatter_name, | ||
show_default=True, | ||
) | ||
@click.option( | ||
"--cols", | ||
"-c", | ||
help="Which columns should be printed? Pass empty string (-c " ") to show all columns", | ||
default="status", | ||
show_default=True, | ||
) | ||
@pass_apibackup_backup_svc | ||
def download(apibackup_backup_svc: ApibackupBackupFacade, **kwargs): | ||
""" | ||
Download config.xml from OPNsense. | ||
""" | ||
result = apibackup_backup_svc.download_backup(kwargs["path"]) | ||
CliOutputFormatter(result, kwargs["output"], kwargs["cols"].split(",")).echo() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from opnsense_cli.api.core.configbackup import Backup | ||
from opnsense_cli.facades.commands.base import CommandFacade | ||
|
||
|
||
class ApibackupBackupFacade(CommandFacade): | ||
def __init__(self, backup_api: Backup): | ||
self._backup_api = backup_api | ||
|
||
def download_backup(self, path): | ||
config = self._backup_api.download("this") | ||
self._write_xml_string_to_file(path, config) | ||
return {"status": f"successfully saved to: {path}"} |
Empty file.
Oops, something went wrong.