Skip to content

Conversation

@BregaladTaran
Copy link
Collaborator

Added support for lab_autostart feature.
Currently using the already existing PATH /labs/{lab_id}

) -> Lab:
"""
Create a new lab with optional title, description, and notes.
Create a new lab with optional title, description, notes, and autostart configuration.
Copy link
Collaborator

@marpauli-cisco marpauli-cisco Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not be able to create new lab and set autostart at the same time. You need to create lab and after that (if you are admin), you can setup autostart.
So, when lab is created, you cannot set values, so all the code below could be removed

Copy link
Collaborator

@marpauli-cisco marpauli-cisco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autostart_config changed to autostart
Can not set autostart parameters during lab create, therefore you can remove it along with tests from lab creation

self._title = title
self._description = ""
self._notes = ""
self._autostart_config = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self._autostart_config = {
self._autostart = {

def autostart_enabled(self) -> bool:
"""Return whether autostart is enabled for the lab."""
self.sync_topology_if_outdated()
return self._autostart_config["enabled"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self._autostart_config["enabled"]
return self._autostart["enabled"]

def autostart_priority(self) -> int | None:
"""Return the autostart priority of the lab."""
self.sync_topology_if_outdated()
return self._autostart_config["priority"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self._autostart_config["priority"]
return self._autostart["priority"]

def autostart_delay(self) -> int | None:
"""Return the autostart delay of the lab."""
self.sync_topology_if_outdated()
return self._autostart_config["delay"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self._autostart_config["delay"]
return self._autostart["delay"]

Comment on lines 1464 to 1465
self._autostart_config = lab_dict.get(
"autostart_config",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self._autostart_config = lab_dict.get(
"autostart_config",
self._autostart = lab_dict.get(
"autostart",

("autostart_delay", -1, "between 0 and 84600"),
],
)
def test_create_lab_autostart_validation(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as mentioned elsewhere, this test can be removed, because you cannot create lab and set autostart at the same time. As a metter of fact, I think, all tests in this file can be removed

Comment on lines 579 to 599
lab.set_autostart_config(enabled=True, priority=500, delay=120)

assert lab._autostart_config == {
"enabled": True,
"priority": 500,
"delay": 120,
}
assert lab.autostart_enabled is True
assert lab.autostart_priority == 500
assert lab.autostart_delay == 120

# Test validation in convenience method
with pytest.raises(
ValueError, match="autostart_priority must be between 0 and 10000"
):
lab.set_autostart_config(enabled=True, priority=15000)

with pytest.raises(
ValueError, match="autostart_delay must be between 0 and 84600"
):
lab.set_autostart_config(enabled=True, delay=100000)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lab.set_autostart_config(enabled=True, priority=500, delay=120)
assert lab._autostart_config == {
"enabled": True,
"priority": 500,
"delay": 120,
}
assert lab.autostart_enabled is True
assert lab.autostart_priority == 500
assert lab.autostart_delay == 120
# Test validation in convenience method
with pytest.raises(
ValueError, match="autostart_priority must be between 0 and 10000"
):
lab.set_autostart_config(enabled=True, priority=15000)
with pytest.raises(
ValueError, match="autostart_delay must be between 0 and 84600"
):
lab.set_autostart_config(enabled=True, delay=100000)
lab.set_autostart(enabled=True, priority=500, delay=120)
assert lab._autostart == {
"enabled": True,
"priority": 500,
"delay": 120,
}
assert lab.autostart_enabled is True
assert lab.autostart_priority == 500
assert lab.autostart_delay == 120
# Test validation in convenience method
with pytest.raises(
ValueError, match="autostart_priority must be between 0 and 10000"
):
lab.set_autostart(enabled=True, priority=15000)
with pytest.raises(
ValueError, match="autostart_delay must be between 0 and 84600"
):
lab.set_autostart(enabled=True, delay=100000)

}

if has_autostart:
topology["lab"]["autostart_config"] = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
topology["lab"]["autostart_config"] = {
topology["lab"]["autostart"] = {

expected = {"enabled": False, "priority": None, "delay": None}

lab._import_lab(topology)
assert lab._autostart_config == expected
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert lab._autostart_config == expected
assert lab._autostart == expected

Comment on lines 675 to 685
lab._autostart_config = {"enabled": False, "priority": 100, "delay": 200}

properties = {
"title": "Updated Lab",
"autostart_config": {"enabled": True, "priority": 300},
}

lab.update_lab_properties(properties)

assert lab._title == "Updated Lab"
assert lab._autostart_config == {"enabled": True, "priority": 300, "delay": 200}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lab._autostart_config = {"enabled": False, "priority": 100, "delay": 200}
properties = {
"title": "Updated Lab",
"autostart_config": {"enabled": True, "priority": 300},
}
lab.update_lab_properties(properties)
assert lab._title == "Updated Lab"
assert lab._autostart_config == {"enabled": True, "priority": 300, "delay": 200}
lab._autostart = {"enabled": False, "priority": 100, "delay": 200}
properties = {
"title": "Updated Lab",
"autostart": {"enabled": True, "priority": 300},
}
lab.update_lab_properties(properties)
assert lab._title == "Updated Lab"
assert lab._autostart == {"enabled": True, "priority": 300, "delay": 200}

@autostart_priority.setter
def autostart_priority(self, value: int | None) -> None:
"""Set the autostart priority of the lab."""
if value is not None and not (0 <= value <= 10000):
Copy link
Collaborator

@tmikuska tmikuska Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if value is not None and not (0 <= value <= 10000):
if not (value is None or 0 <= value <= 10000):

def autostart_priority(self, value: int | None) -> None:
"""Set the autostart priority of the lab."""
if value is not None and not (0 <= value <= 10000):
raise ValueError("autostart_priority must be between 0 and 10000")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValueError("autostart_priority must be between 0 and 10000")
raise ValueError("autostart_priority must be between 0 and 10000, or None")

@autostart_delay.setter
def autostart_delay(self, value: int | None) -> None:
"""Set the autostart delay of the lab."""
if value is not None and (value < 0 or value > 84600):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this better, but the original is fine too except that it's 86400

Suggested change
if value is not None and (value < 0 or value > 84600):
if not (value is None or 0 <= value <= 86400):

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marpauli-cisco 84600 -> 86400 needs to be changed in API and UI as well

"priority": None,
"delay": None,
},
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be moved out of if/else and if autostart is not present, we shouldn't set it

"""
url = self._url_for("labs")
body = {"title": title, "description": description, "notes": notes}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

"priority": priority,
"delay": delay,
}
self._set_property("autostart", self._autostart)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the same comment in the node staging, this method is imo sufficient (we can add a getter) and we don't need the individual getters/setters @marpauli-cisco thoughts?

Copy link
Collaborator

@tmikuska tmikuska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the delay max needs to be changed and we shouldn't reset autostart on update if it's not present in the lab_dict (not sure if that's possible)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants