Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIMPLE-5806 PCL support for multiple config files #60

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions virl2_client/models/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def __init__(
self._resource_pool_manager = resource_pool_manager
self._resource_pools = []
self._stale = False
self._synced_configs = True

def __len__(self):
return len(self._nodes)
Expand Down Expand Up @@ -245,14 +246,17 @@ def sync_l3_addresses_if_outdated(self) -> None:

@check_stale
@locked
def sync_topology_if_outdated(self) -> None:
def sync_topology_if_outdated(self, exclude_configurations=True) -> None:
"""Sync the topology if it is outdated."""
timestamp = time.time()
if (
if not(exclude_configurations or self._synced_configs):
self._sync_topology(exclude_configurations=False)
elif (
self.auto_sync
and timestamp - self._last_sync_topology_time > self.auto_sync_interval
):
self._sync_topology(exclude_configurations=True)
self._sync_topology(exclude_configurations=exclude_configurations)
self._synced_configs = not exclude_configurations

@check_stale
@locked
Expand Down Expand Up @@ -563,7 +567,7 @@ def _create_node_local(
label: str,
node_definition: str,
image_definition: str | None,
configuration: str | None,
configuration: list[dict[str, str]] | str | None,
x: int,
y: int,
ram: int | None = None,
Expand Down
52 changes: 44 additions & 8 deletions virl2_client/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import logging
import time
import warnings
from copy import deepcopy
from functools import total_ordering
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -66,7 +67,7 @@ def __init__(
label: str,
node_definition: str,
image_definition: str | None,
configuration: str | None,
configuration: list[dict[str, str]] | str | None,
x: int,
y: int,
ram: int | None,
Expand Down Expand Up @@ -113,7 +114,10 @@ def __init__(
self._session: httpx.Client = lab._session
self._image_definition = image_definition
self._ram = ram
self._configuration = configuration
if isinstance(configuration, str):
self._configuration = [{"name": "Main", "content": configuration}]
else:
self._configuration = configuration
self._cpus = cpus
self._cpu_limit = cpu_limit
self._data_volume = data_volume
Expand Down Expand Up @@ -406,15 +410,47 @@ def boot_disk_size(self, value: int) -> None:

@property
def configuration(self) -> str | None:
"""Return the initial configuration of this node."""
self.lab._sync_topology(exclude_configurations=False)
return self._configuration
"""Return the contents of the main configuration file."""
self.lab.sync_topology_if_outdated(exclude_configurations=False)
return self._configuration[0].get("content") if self._configuration else None

@configuration.setter
def configuration(self, value) -> None:
"""Set the initial configuration of this node."""
def configuration(self, value: str | list | dict) -> None:
"""
Set the content of:
- the main configuration file if passed a string,
- one configuration file if passed a dictionary in the format of
`{"name": "filename.txt", "content": "<file content>"}`,
- or multiple configuration files if passed a list of above dictionaries.
Can also use "Main" in place of the filename of the main configuration file.

:param value: The configuration data in one of three formats.
"""
self._set_node_property("configuration", value)
self._configuration = value
if isinstance(value, str):
if not self._configuration:
self._configuration.append({"name": "Main", "content": value})
tmikuska marked this conversation as resolved.
Show resolved Hide resolved
else:
self._configuration[0]["content"] = value
tmikuska marked this conversation as resolved.
Show resolved Hide resolved
return
if self._configuration is None:
self._configuration = []
new_configs = value if isinstance(value, list) else [value]
new_configs_by_name = {
new_config["name"]: new_config for new_config in new_configs
}
for i, config in enumerate(self._configuration):
if config["name"] in new_configs_by_name:
self._configuration[i] = new_configs_by_name[config["name"]]

@property
def configuration_files(self) -> list[dict[str, str]] | None:
"""
Return all configuration files, in a list in the following format:
`[{"name": "filename.txt", "content": "<file content>"}]`
"""
self.lab.sync_topology_if_outdated(exclude_configurations=False)
return deepcopy(self._configuration)
tmikuska marked this conversation as resolved.
Show resolved Hide resolved

@property
def config(self) -> str | None:
Expand Down