Skip to content

Commit

Permalink
Merge branch 'main' into fix_autoloader
Browse files Browse the repository at this point in the history
# Conflicts:
#	opnsense_cli/cli.py
#	opnsense_cli/click_addons/command_autoloader.py
#	opnsense_cli/code_generators/base.py
#	opnsense_cli/code_generators/opn_cli/base.py
#	opnsense_cli/code_generators/opn_cli/factories.py
#	opnsense_cli/code_generators/opn_cli/unit_test/codegenerator.py
#	opnsense_cli/code_generators/puppet_code/acceptance_test/codegenerator.py
#	opnsense_cli/code_generators/puppet_code/base.py
#	opnsense_cli/code_generators/puppet_code/factories.py
#	opnsense_cli/code_generators/puppet_code/factory_types.py
#	opnsense_cli/code_generators/puppet_code/provider_unit_test/codegenerator.py
#	opnsense_cli/code_generators/puppet_code/type/codegenerator.py
#	opnsense_cli/code_generators/puppet_code/type_unit_test/codegenerator.py
#	opnsense_cli/commands/core/configbackup/tests/test_configbackup.py
#	opnsense_cli/commands/core/firewall/alias.py
#	opnsense_cli/commands/core/unbound/tests/test_unbound_alias.py
#	opnsense_cli/commands/new/command.py
#	opnsense_cli/commands/new/puppet.py
#	opnsense_cli/commands/new/tests/test_new_api.py
#	opnsense_cli/commands/new/tests/test_new_command.py
#	opnsense_cli/commands/plugin/apibackup/tests/test_apibackup.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_acl.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_action.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_backend.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_cpu.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_errorfile.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_frontend.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_group.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_healthcheck.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_lua.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_mapfile.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_resolver.py
#	opnsense_cli/commands/plugin/haproxy/tests/test_haproxy_server.py
#	opnsense_cli/commands/plugin/nodeexporter/tests/test_nodeexporter_config.py
#	opnsense_cli/formatters/cli_output/output_format_factory.py
#	opnsense_cli/formatters/cli_output/tests/test_plain_output_format.py
  • Loading branch information
andreas-stuerz committed Mar 22, 2024
2 parents f808614 + 9fab426 commit 3bfd196
Show file tree
Hide file tree
Showing 36 changed files with 105 additions and 54 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
BSD 2-Clause License

Copyright (c) 2021, Andreas Stürz
Copyright (c) 2024, Andreas Stürz
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
3 changes: 1 addition & 2 deletions MANIFEST.in
@@ -1,2 +1 @@
recursive-include opnsense_cli/templates *
recursive-include opnsense_cli/fixtures *
recursive-include opnsense_cli *.j2
2 changes: 0 additions & 2 deletions opnsense_cli/code_generators/base.py
Expand Up @@ -25,5 +25,3 @@ def get_code(self):
@abstractmethod
def _get_template_vars(self):
""" " This method should be implemented."""


19 changes: 9 additions & 10 deletions opnsense_cli/code_generators/opn_cli/base.py
Expand Up @@ -7,15 +7,15 @@

class CommandCodeGenerator(CodeGenerator):
def __init__(
self,
tag_content: Tag,
template_engine: TemplateEngine,
option_factory: ObjectTypeFromDataFactory,
template,
group,
command,
model_xml_tag,
module_type,
self,
tag_content: Tag,
template_engine: TemplateEngine,
option_factory: ObjectTypeFromDataFactory,
template,
group,
command,
model_xml_tag,
module_type,
):
self._tag_content: Tag = tag_content
self._template_engine = template_engine
Expand All @@ -25,4 +25,3 @@ def __init__(
self._click_option_factory = option_factory
self._model_xml_tag = model_xml_tag
self._module_type = module_type

10 changes: 7 additions & 3 deletions opnsense_cli/code_generators/opn_cli/factories.py
@@ -1,9 +1,13 @@
from bs4.element import Tag
from opnsense_cli.code_generators.opn_cli.factory_types import ClickOptionCodeFragment
from opnsense_cli.factories import ObjectTypeFromDataFactory, FactoryException
from opnsense_cli.code_generators.opn_cli.factory_types import ClickBoolean, ClickText, ClickInteger, ClickChoice, \
ClickTextLinkedItem

from opnsense_cli.code_generators.opn_cli.factory_types import (
ClickBoolean,
ClickText,
ClickInteger,
ClickChoice,
ClickTextLinkedItem,
)


class ClickOptionCodeTypeFactory(ObjectTypeFromDataFactory):
Expand Down
Expand Up @@ -3,7 +3,6 @@


class ClickCommandTestCodeGenerator(CommandCodeGenerator):

def _get_template_vars(self):
return CommandTestTemplateVars(
click_command=self._click_command,
Expand Down
Expand Up @@ -21,7 +21,6 @@ def __init__(
self._template = template
self._update_command_params = update_command_params


def _get_template_vars(self):
return PuppetAcceptanceTestTemplateVars(
click_command=self._click_command,
Expand Down
15 changes: 7 additions & 8 deletions opnsense_cli/code_generators/puppet_code/base.py
Expand Up @@ -2,14 +2,15 @@
from opnsense_cli.code_generators.puppet_code.factories import PuppetCodeFragmentFactory
from typing import List


class PuppetCodeGenerator(CodeGenerator):
def __init__(
self,
create_command_params,
type_factory: PuppetCodeFragmentFactory,
find_uuid_by_column,
click_group,
click_command,
self,
create_command_params,
type_factory: PuppetCodeFragmentFactory,
find_uuid_by_column,
click_group,
click_command,
):
self._create_command_params = create_command_params
self._type_factory = type_factory
Expand Down Expand Up @@ -52,5 +53,3 @@ def _get_all_columns(self):
continue
columns.append(param_line["name"])
return columns


11 changes: 9 additions & 2 deletions opnsense_cli/code_generators/puppet_code/factories.py
@@ -1,6 +1,13 @@
from opnsense_cli.factories import ObjectTypeFromDataFactory, FactoryException
from opnsense_cli.code_generators.puppet_code.factory_types import PuppetCodeFragment, PuppetBoolean, PuppetChoice, \
PuppetChoiceMultiple, PuppetCsv, PuppetInteger, PuppetString
from opnsense_cli.code_generators.puppet_code.factory_types import (
PuppetCodeFragment,
PuppetBoolean,
PuppetChoice,
PuppetChoiceMultiple,
PuppetCsv,
PuppetInteger,
PuppetString,
)


class PuppetCodeFragmentFactory(ObjectTypeFromDataFactory):
Expand Down
7 changes: 5 additions & 2 deletions opnsense_cli/code_generators/puppet_code/factory_types.py
Expand Up @@ -142,6 +142,7 @@ class PuppetBoolean(PuppetCodeFragment):
expect(r.stdout).to match %r{${name}: '0'}
"""


class PuppetChoice(PuppetCodeFragment):
TEMPLATE_PROVIDER_translate_json_object_to_puppet_resource = """
${name}: json_object['${name}'],
Expand Down Expand Up @@ -189,6 +190,7 @@ class PuppetChoice(PuppetCodeFragment):
expect(r.stdout).to match %r{${name}: 'TODO'}
"""


class PuppetChoiceMultiple(PuppetCodeFragment):
TEMPLATE_PROVIDER_translate_json_object_to_puppet_resource = """
${name}: array_from_value(json_object['${name}']),
Expand Down Expand Up @@ -240,6 +242,7 @@ class PuppetChoiceMultiple(PuppetCodeFragment):
expect(r.stdout).to match %r{${name}: 'TODO'}
"""


class PuppetCsv(PuppetCodeFragment):
TEMPLATE_PROVIDER_translate_json_object_to_puppet_resource = """
${name}: array_from_value(json_object['${name}']),
Expand Down Expand Up @@ -288,6 +291,7 @@ class PuppetCsv(PuppetCodeFragment):
expect(r.stdout).to match %r{${name}: '\\[\\]'}
"""


class PuppetInteger(PuppetCodeFragment):
TEMPLATE_PROVIDER_translate_json_object_to_puppet_resource = """
${name}: json_object['${name}'],
Expand Down Expand Up @@ -343,6 +347,7 @@ class PuppetInteger(PuppetCodeFragment):
expect(r.stdout).to match %r{${name}: 'TODO'}
"""


class PuppetString(PuppetCodeFragment):
TEMPLATE_PROVIDER_translate_json_object_to_puppet_resource = """
${name}: json_object['${name}'],
Expand Down Expand Up @@ -415,5 +420,3 @@ class PuppetString(PuppetCodeFragment):
TEMPLATE_ACCEPTANCE_TEST_match_item_namevar = """
expect(r.stdout).to match %r{${name}: acceptance test item}
"""


Expand Up @@ -21,7 +21,6 @@ def __init__(
self._template = template
self._update_command_params = update_command_params


def _get_template_vars(self):
return PuppetProviderUnitTestTemplateVars(
click_command=self._click_command,
Expand Down
Expand Up @@ -21,7 +21,6 @@ def __init__(
self._template = template
self._update_command_params = update_command_params


def _get_template_vars(self):
return PuppetTypeTemplateVars(
click_command=self._click_command,
Expand Down
Expand Up @@ -21,7 +21,6 @@ def __init__(
self._template = template
self._update_command_params = update_command_params


def _get_template_vars(self):
return PuppetTypeUnitTestTemplateVars(
click_command=self._click_command,
Expand Down
Expand Up @@ -7,7 +7,9 @@
class TestApibackupCommands(CommandTestCase):
def setUp(self):
self._setup_fakefs()
self._api_data_fixtures_download = self._read_fixture_file("fixtures/config.xml.sample", base_dir=os.path.dirname(__file__))
self._api_data_fixtures_download = self._read_fixture_file(
"fixtures/config.xml.sample", base_dir=os.path.dirname(__file__)
)
self._api_client_args_fixtures = ["api_key", "api_secret", "https://127.0.0.1/api", True, "~/.opn-cli/ca.pem", 60]

@patch("opnsense_cli.commands.core.configbackup.backup.ApiClient.execute")
Expand Down
7 changes: 6 additions & 1 deletion opnsense_cli/commands/core/firewall/alias.py
@@ -1,6 +1,11 @@
import click
from opnsense_cli.formatters.cli_output.cli_output_formatter import CliOutputFormatter
from opnsense_cli.click_addons.callbacks import formatter_from_formatter_name, bool_as_string, comma_to_newline, available_formats
from opnsense_cli.click_addons.callbacks import (
formatter_from_formatter_name,
bool_as_string,
comma_to_newline,
available_formats,
)
from opnsense_cli.commands.core.firewall import firewall
from opnsense_cli.api.client import ApiClient
from opnsense_cli.api.plugin.firewall import FirewallAlias, FirewallAliasUtil
Expand Down
Expand Up @@ -18,7 +18,9 @@ def setUp(self):
self._api_data_fixtures_delete_NOT_FOUND = {"result": "not found"}
self._api_data_fixtures_delete_OK = {"result": "deleted"}
self._api_data_fixtures_list_EMPTY = {"unbound": {"aliases": {"alias": []}}}
self._api_data_fixtures_list = self._api_data_fixtures_list = self._read_json_fixture("fixtures/model_data.json", base_dir=os.path.dirname(__file__))
self._api_data_fixtures_list = self._api_data_fixtures_list = self._read_json_fixture(
"fixtures/model_data.json", base_dir=os.path.dirname(__file__)
)
self._api_client_args_fixtures = ["api_key", "api_secret", "https://127.0.0.1/api", True, "~/.opn-cli/ca.pem", 60]

@patch("opnsense_cli.commands.core.unbound.alias.ApiClient.execute")
Expand Down
12 changes: 9 additions & 3 deletions opnsense_cli/commands/new/command.py
Expand Up @@ -32,7 +32,11 @@ def command(**kwargs):
required=True,
)
@click.option(
"--tag", "-t", help="The xml tag from the core_model.xml e.g. dnsbl for unbound dnsbl command", show_default=True, required=True
"--tag",
"-t",
help="The xml tag from the core_model.xml e.g. dnsbl for unbound dnsbl command",
show_default=True,
required=True,
)
@click.option(
"--form-url",
Expand Down Expand Up @@ -246,7 +250,7 @@ def write_command_service(type, model_tag: Tag, template_engine, option_factory,
kwargs["tag"],
type,
)
output_path = f"{kwargs['command_output_dir']}/{kwargs['click_group']}/services/{kwargs['click_group']}_{kwargs['opn_cli']}_service.py"
output_path = f"{kwargs['command_output_dir']}/{kwargs['click_group']}/services/{kwargs['click_group']}_{kwargs['opn_cli']}_service.py" # noqa: E501

click.echo(command_service_generator.write_code(output_path))

Expand All @@ -263,7 +267,9 @@ def write_command_test(type, model_tag: Tag, template_engine, option_factory, **
type,
)

output_path = f"{kwargs['command_output_dir']}/{kwargs['click_group']}/tests/test_{kwargs['click_group']}_{kwargs['opn_cli']}.py"
output_path = (
f"{kwargs['command_output_dir']}/{kwargs['click_group']}/tests/test_{kwargs['click_group']}_{kwargs['opn_cli']}.py"
)

click.echo(command_test_generator.write_code(output_path))

Expand Down
8 changes: 4 additions & 4 deletions opnsense_cli/commands/new/puppet.py
Expand Up @@ -124,7 +124,7 @@ def write_puppet_provider(ctx, template_engine, code_factory, create_command_par
update_command_params,
)

output_path = f"{kwargs['puppet_output_dir']}/provider/opnsense_{kwargs['click_group']}_{kwargs['click_command']}/opnsense_{kwargs['click_group']}_{kwargs['click_command']}.rb"
output_path = f"{kwargs['puppet_output_dir']}/provider/opnsense_{kwargs['click_group']}_{kwargs['click_command']}/opnsense_{kwargs['click_group']}_{kwargs['click_command']}.rb" # noqa: E501

click.echo(code_generator.write_code(output_path))

Expand Down Expand Up @@ -158,7 +158,7 @@ def write_puppet_type_unit_test(ctx, template_engine, code_factory, create_comma
update_command_params,
)

output_path = f"{kwargs['puppet_output_dir']}/spec/unit/puppet/type/opnsense_{kwargs['click_group']}_{kwargs['click_command']}_spec.rb"
output_path = f"{kwargs['puppet_output_dir']}/spec/unit/puppet/type/opnsense_{kwargs['click_group']}_{kwargs['click_command']}_spec.rb" # noqa: E501

click.echo(code_generator.write_code(output_path))

Expand All @@ -177,7 +177,7 @@ def write_puppet_provider_unit_test(
update_command_params,
)

output_path = f"{kwargs['puppet_output_dir']}/spec/unit/puppet/provider/opnsense_{kwargs['click_group']}_{kwargs['click_command']}_spec.rb"
output_path = f"{kwargs['puppet_output_dir']}/spec/unit/puppet/provider/opnsense_{kwargs['click_group']}_{kwargs['click_command']}_spec.rb" # noqa: E501

click.echo(code_generator.write_code(output_path))

Expand All @@ -194,7 +194,7 @@ def write_puppet_acceptance_test(ctx, template_engine, code_factory, create_comm
update_command_params,
)

output_path = f"{kwargs['puppet_output_dir']}/spec/acceptance/types/opnsense_{kwargs['click_group']}_{kwargs['click_command']}_spec.rb"
output_path = f"{kwargs['puppet_output_dir']}/spec/acceptance/types/opnsense_{kwargs['click_group']}_{kwargs['click_command']}_spec.rb" # noqa: E501

click.echo(code_generator.write_code(output_path))

Expand Down
8 changes: 6 additions & 2 deletions opnsense_cli/commands/new/tests/test_new_api.py
Expand Up @@ -12,8 +12,12 @@ def setUp(self):

self._core_html_fixture = self._read_fixture_file("fixtures/api/core.html", base_dir=os.path.dirname(__file__))
self._plugin_html_fixture = self._read_fixture_file("fixtures/api/plugin.html", base_dir=os.path.dirname(__file__))
self._list_core_html_fixture = self._read_fixture_file("fixtures/api/core_list.html", base_dir=os.path.dirname(__file__))
self._list_plugin_html_fixture = self._read_fixture_file("fixtures/api/plugin_list.html", base_dir=os.path.dirname(__file__))
self._list_core_html_fixture = self._read_fixture_file(
"fixtures/api/core_list.html", base_dir=os.path.dirname(__file__)
)
self._list_plugin_html_fixture = self._read_fixture_file(
"fixtures/api/plugin_list.html", base_dir=os.path.dirname(__file__)
)

self._mock_html_core_resp = self._mock_response(content=self._core_html_fixture)
self._mock_html_plugin_resp = self._mock_response(content=self._plugin_html_fixture)
Expand Down
16 changes: 12 additions & 4 deletions opnsense_cli/commands/new/tests/test_new_command.py
Expand Up @@ -10,10 +10,16 @@ class TestNewCommandCommands(CommandTestCase):
def setUp(self):
self._setup_fakefs()

self._core_model_fixture = self._read_fixture_file("fixtures/opn_cli/core_model.xml", base_dir=os.path.dirname(__file__))
self._core_model_fixture = self._read_fixture_file(
"fixtures/opn_cli/core_model.xml", base_dir=os.path.dirname(__file__)
)
self._core_form_fixture = self._read_fixture_file("fixtures/opn_cli/core_form.xml", base_dir=os.path.dirname(__file__))
self._plugin_model_fixture = self._read_fixture_file("fixtures/opn_cli/plugin_model.xml", base_dir=os.path.dirname(__file__))
self._plugin_form_fixture = self._read_fixture_file("fixtures/opn_cli/plugin_form.xml", base_dir=os.path.dirname(__file__))
self._plugin_model_fixture = self._read_fixture_file(
"fixtures/opn_cli/plugin_model.xml", base_dir=os.path.dirname(__file__)
)
self._plugin_form_fixture = self._read_fixture_file(
"fixtures/opn_cli/plugin_form.xml", base_dir=os.path.dirname(__file__)
)

self._mock_model_core_resp = self._mock_response(content=self._core_model_fixture)
self._mock_form_core_resp = self._mock_response(content=self._core_form_fixture)
Expand All @@ -27,7 +33,9 @@ def setUp(self):
self._output_dir = self._get_output_path()

self._generated_plugin_command_path = f"{self._output_dir}/commands/plugin/haproxy/frontend.py"
self._generated_plugin_service_path = f"{self._output_dir}/commands/plugin/haproxy/services/haproxy_frontend_service.py"
self._generated_plugin_service_path = (
f"{self._output_dir}/commands/plugin/haproxy/services/haproxy_frontend_service.py"
)
self._generated_plugin_test_path = f"{self._output_dir}/commands/plugin/haproxy/tests/test_haproxy_frontend.py"

self._generated_core_command_path = f"{self._output_dir}/commands/core/firewall/category.py"
Expand Down
Expand Up @@ -4,11 +4,11 @@
import base64
import os


class TestApibackupCommands(CommandTestCase):
def setUp(self):
self._setup_fakefs()


self._config_xml = self._read_fixture_file("fixtures/config.xml.sample", base_dir=os.path.dirname(__file__))
self._api_data_fixtures_download = {
"status": "success",
Expand Down
Expand Up @@ -3,6 +3,7 @@
from opnsense_cli.commands.test_base import CommandTestCase
import os


class TestHaproxyAclCommands(CommandTestCase):
def setUp(self):
self._api_data_fixtures_reconfigure_OK = {"status": "ok"}
Expand Down
Expand Up @@ -3,6 +3,7 @@
from opnsense_cli.commands.test_base import CommandTestCase
import os


class TestHaproxyActionCommands(CommandTestCase):
def setUp(self):
self._api_data_fixtures_reconfigure_OK = {"status": "ok"}
Expand Down
Expand Up @@ -3,6 +3,7 @@
from opnsense_cli.commands.test_base import CommandTestCase
import os


class TestHaproxyBackendCommands(CommandTestCase):
def setUp(self):
self._api_data_fixtures_reconfigure_OK = {"status": "ok"}
Expand Down
Expand Up @@ -3,6 +3,7 @@
from opnsense_cli.commands.test_base import CommandTestCase
import os


class TestHaproxyCpuCommands(CommandTestCase):
def setUp(self):
self._api_data_fixtures_reconfigure_OK = {"status": "ok"}
Expand Down
Expand Up @@ -3,6 +3,7 @@
from opnsense_cli.commands.test_base import CommandTestCase
import os


class TestHaproxyErrorfileCommands(CommandTestCase):
def setUp(self):
self._api_data_fixtures_reconfigure_OK = {"status": "ok"}
Expand Down

0 comments on commit 3bfd196

Please sign in to comment.