Skip to content

Commit

Permalink
Refactor(plugins): Move internal cv_client code to PyAVD (#4141)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausHolbechArista authored Jun 21, 2024
1 parent f95b5e9 commit becc92f
Show file tree
Hide file tree
Showing 85 changed files with 9,298 additions and 4,632 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ max-line-length = 160

exclude =
# The cv_client api is generated from proto files, so it should not be linted.
ansible_collections/arista/avd/plugins/plugin_utils/cv_client/api
python-avd/pyavd/_cv/api
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ repos:
# ignoring errors and selecting line length as per
# https://github.com/ansible/ansible/blob/devel/test/lib/ansible_test/_util/controller/sanity/pep8/current-ignore.txt
types: [python]
# The cv_client api is generated from proto files, so it should not be linted.
exclude: ^ansible_collections/arista/avd/plugins/plugin_utils/cv_client/api/
# The cv client api is generated from proto files, so it should not be linted.
exclude: ^python-avd/pyavd/_cv/api

- repo: https://github.com/pycqa/pylint
rev: "v3.1.1"
Expand Down Expand Up @@ -160,8 +160,8 @@ repos:
ansible_collections/arista/avd/roles/.*/docs/tables/.*\.md| # Exclude all .md files in tables
python-avd/pyavd/.*/schema/.*schema\.yml| # Exclude YAML schemas file
python-avd/pyavd/.*/schema/.*jsonschema\.json| # Exclude JSON schemas file
ansible_collections/arista/avd/plugins/plugin_utils/cv_client/api/.*| # Exclude auto generated files for CV API
ansible_collections/arista/avd/plugins/plugin_utils/cv_client/extra_cv_protos/.*| # Exclude extra CV proto files
python-avd/pyavd/_cv/api/.*| # Exclude auto generated files for CV API
python-avd/pyavd/_cv/extra_cv_protos/.*| # Exclude extra CV proto files
ansible-avd/ansible_collections/arista/avd/docs/plugins/.* | # Excluded auto generated doc for Ansible plugins
)$
Expand Down
107 changes: 60 additions & 47 deletions ansible_collections/arista/avd/plugins/action/cv_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,72 @@
from ansible.plugins.action import ActionBase, display
from yaml import load

from ansible_collections.arista.avd.plugins.plugin_utils.cv_client import deploy_to_cv
from ansible_collections.arista.avd.plugins.plugin_utils.cv_client.workflows.models import (
CloudVision,
CVChangeControl,
CVDevice,
CVDeviceTag,
CVEosConfig,
CVInterfaceTag,
CVPathfinderMetadata,
CVTimeOuts,
CVWorkspace,
)
from ansible_collections.arista.avd.plugins.plugin_utils.strip_empties import strip_empties_from_dict
from ansible_collections.arista.avd.plugins.plugin_utils.utils import PythonToAnsibleHandler, YamlLoader, get

PLUGIN_NAME = "arista.avd.cv_workflow"

try:
from pyavd._cv.workflows.deploy_to_cv import deploy_to_cv
from pyavd._cv.workflows.models import (
CloudVision,
CVChangeControl,
CVDevice,
CVDeviceTag,
CVEosConfig,
CVInterfaceTag,
CVPathfinderMetadata,
CVTimeOuts,
CVWorkspace,
)

HAS_PYAVD = True
except ImportError:
HAS_PYAVD = False


LOGGER = logging.getLogger("ansible_collections.arista.avd")
LOGGING_LEVELS = ["DEBUG", "INFO", "ERROR", "WARNING", "CRITICAL"]

ARGUMENT_SPEC = {
"configuration_dir": {"type": "str", "required": True},
"structured_config_dir": {"type": "str", "required": True},
"structured_config_suffix": {"type": "str", "default": "yml"},
"device_list": {"type": "list", "elements": "str", "required": True},
"strict_tags": {"type": "bool", "required": False, "default": False},
"skip_missing_devices": {"type": "bool", "required": False, "default": False},
"configlet_name_template": {"type": "str", "default": "AVD-${hostname}"},
"cv_servers": {"type": "list", "elements": "str", "required": True},
"cv_token": {"type": "str", "secret": True, "required": True},
"cv_verify_certs": {"type": "bool", "default": True},
"workspace": {
"type": "dict",
"options": {
"name": {"type": "str", "required": False},
"description": {"type": "str", "required": False},
"id": {"type": "str", "required": False},
"requested_state": {"type": "str", "default": "built", "choices": ["pending", "built", "submitted", "abandoned", "deleted"]},
"force": {"type": "bool", "default": False},
if HAS_PYAVD:
ARGUMENT_SPEC = {
"configuration_dir": {"type": "str", "required": True},
"structured_config_dir": {"type": "str", "required": True},
"structured_config_suffix": {"type": "str", "default": "yml"},
"device_list": {"type": "list", "elements": "str", "required": True},
"strict_tags": {"type": "bool", "required": False, "default": False},
"skip_missing_devices": {"type": "bool", "required": False, "default": False},
"configlet_name_template": {"type": "str", "default": "AVD-${hostname}"},
"cv_servers": {"type": "list", "elements": "str", "required": True},
"cv_token": {"type": "str", "secret": True, "required": True},
"cv_verify_certs": {"type": "bool", "default": True},
"workspace": {
"type": "dict",
"options": {
"name": {"type": "str", "required": False},
"description": {"type": "str", "required": False},
"id": {"type": "str", "required": False},
"requested_state": {"type": "str", "default": "built", "choices": ["pending", "built", "submitted", "abandoned", "deleted"]},
"force": {"type": "bool", "default": False},
},
},
},
"change_control": {
"type": "dict",
"options": {
"name": {"type": "str", "required": False},
"description": {"type": "str", "required": False},
"requested_state": {"type": "str", "default": "pending approval", "choices": ["pending approval", "approved", "running", "completed"]},
"change_control": {
"type": "dict",
"options": {
"name": {"type": "str", "required": False},
"description": {"type": "str", "required": False},
"requested_state": {"type": "str", "default": "pending approval", "choices": ["pending approval", "approved", "running", "completed"]},
},
},
},
"timeouts": {
"type": "dict",
"options": {
"workspace_build_timeout": {"type": "float", "default": CVTimeOuts.workspace_build_timeout},
"change_control_creation_timeout": {"type": "float", "default": CVTimeOuts.change_control_creation_timeout},
"timeouts": {
"type": "dict",
"options": {
"workspace_build_timeout": {"type": "float", "default": CVTimeOuts.workspace_build_timeout},
"change_control_creation_timeout": {"type": "float", "default": CVTimeOuts.change_control_creation_timeout},
},
},
},
"return_details": {"type": "bool", "required": False, "default": False},
}
"return_details": {"type": "bool", "required": False, "default": False},
}


class ActionModule(ActionBase):
Expand All @@ -84,6 +94,9 @@ def run(self, tmp=None, task_vars=None):
result = super().run(tmp, task_vars)
del tmp # tmp no longer has any effect

if not HAS_PYAVD:
raise AnsibleActionFail("The Python library 'pyavd' was not found. Install using 'pip3 install'.")

# Setup module logging
setup_module_logging(result)

Expand Down
11 changes: 0 additions & 11 deletions ansible_collections/arista/avd/plugins/filter/decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@
orig_exc=e,
)
)
# TODO: Remove the below import once cv_client plugin utils have been moved to pyavd
try:
from pyavd._utils.password_utils.password import simple_7_decrypt # noqa: F401; pylint: disable=unused-import
except ImportError as e:
simple_7_decrypt = RaiseOnUse(
AnsibleFilterError(
"The 'simple_7_decrypt' plugin utils requires the 'pyavd' Python library. Got import error",
orig_exc=e,
)
)


DOCUMENTATION = r"""
---
Expand Down

This file was deleted.

Loading

0 comments on commit becc92f

Please sign in to comment.