Skip to content

Commit

Permalink
code review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
przsus committed Oct 9, 2024
1 parent 588394a commit 3601009
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
4 changes: 2 additions & 2 deletions playbooks/tests/test_module_cluster_management.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
cisco.catalystwan.cluster_management:
wait_until_configured_seconds: 1800
device_ip: "{{ vmanage.cluster_private_ip }}"
username: "{{ (vmanage_instances | first).admin_username }}"
password: "{{ (vmanage_instances | first).admin_password }}"
username: "{{ vmanage.admin_username }}"
password: "{{ vmanage.admin_password }}"
gen_csr: false
persona: "{{ vmanage.persona }}"
services:
Expand Down
12 changes: 10 additions & 2 deletions plugins/module_utils/vmanage_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def send_request_safely(
action_name: str,
send_func: Callable,
response_key: str = None,
fail_on_exception: bool = True,
num_retries: int = 0,
retry_interval_seconds: int = 1,
**kwargs: Any,
Expand All @@ -199,9 +200,16 @@ def send_request_safely(
if num_retries:
time.sleep(retry_interval_seconds)
self.send_request_safely(
result, action_name, send_func, response_key, num_retries - 1, retry_interval_seconds, **kwargs
result,
action_name,
send_func,
response_key,
fail_on_exception,
num_retries - 1,
retry_interval_seconds,
**kwargs,
)
else:
elif fail_on_exception:
self.fail_json(
msg=f"Could not perform '{action_name}' action.\nManager error: {ex.info}",
exception=traceback.format_exc(),
Expand Down
30 changes: 22 additions & 8 deletions plugins/modules/cluster_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,28 @@
from ..module_utils.vmanage_module import AnsibleCatalystwanModule


def wait_for_connected_device(module, device_ip, timeout) -> Optional[str]:
def get_connected_devices(module, device_ip):
result = ModuleResult()
module.send_request_safely(
result,
action_name=f"Get connected devices for {device_ip}",
send_func=module.session.endpoints.cluster_management.get_connected_devices,
vmanageIP=device_ip,
response_key="connected_devices",
fail_on_exception=False,
)
try:
return result.response["connected_devices"]
except KeyError:
return None


def wait_for_connected_devices(module, device_ip, timeout) -> Optional[str]:
start = time.time()
while True:
try:
connected_device = module.session.endpoints.cluster_management.get_connected_devices(device_ip)
if connected_device:
connected_devices = get_connected_devices(module, device_ip)
if connected_devices:
return None
if (time.time() - start) > timeout:
return f"reached timeout of {timeout}s"
Expand Down Expand Up @@ -156,12 +172,11 @@ def run_module():
vmanage_id = module.params.get("vmanage_id")
device_ip = module.params.get("device_ip")

connected_device = module.session.endpoints.cluster_management.get_connected_devices(device_ip)
if connected_device:
connected_devices = get_connected_devices(module, device_ip)
if connected_devices:
result.changed = False
result.msg = f"Device {device_ip} already configured"
module.exit_json(**result.model_dump(mode="json"))
return

payload = VManageSetup(
vmanage_id=vmanage_id,
Expand Down Expand Up @@ -195,10 +210,9 @@ def run_module():
if result.changed:
wait_until_configured_seconds = module.params.get("wait_until_configured_seconds")
if wait_until_configured_seconds:
error_msg = wait_for_connected_device(module, device_ip, wait_until_configured_seconds)
error_msg = wait_for_connected_devices(module, device_ip, wait_until_configured_seconds)
if error_msg:
module.fail_json(msg=f"Error during vManage configuration: {error_msg}")
return
result.msg = "Successfully updated requested vManage configuration."
else:
result.msg = "No changes to vManage configuration applied."
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readme = "README.md"
python = "^3.10"
ansible-core = "2.16.6"
ansible = "9.4.0"
catalystwan = "v0.34.0dev5"
catalystwan = "v0.35.5dev0"
flake8 = "5.0.4"
black = "24.3.0"
pre-commit = "3.7"
Expand Down
5 changes: 3 additions & 2 deletions roles/cluster/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
cisco.catalystwan.cluster_management:
wait_until_configured_seconds: 1800
device_ip: "{{ vmanage.cluster_private_ip }}"
username: "{{ (vmanage_instances | first).admin_username }}"
password: "{{ (vmanage_instances | first).admin_password }}"
username: "{{ vmanage.admin_username }}"
password: "{{ vmanage.admin_password }}"
gen_csr: false
persona: "{{ vmanage.persona }}"
services: "{{ vmanage.cluster_services | default(default_services) }}"
Expand All @@ -36,3 +36,4 @@
loop: "{{ vmanage_instances[1:] }}"
loop_control:
loop_var: vmanage
when: vmanage.cluster_private_ip is defined
1 change: 1 addition & 0 deletions roles/cluster/tasks/variables_assertion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
quiet: true
loop:
- "{{ vmanage_instances }}"
- "{{ (vmanage_instances | first).cluster_private_ip }}"
loop_control:
loop_var: required_var

0 comments on commit 3601009

Please sign in to comment.