Skip to content

Commit b9d320f

Browse files
committed
CMLDEV-655 Refactored opt-in to enum (#178)
Added telemetry support
1 parent 22d76f4 commit b9d320f

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

virl2_client/models/system.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from virl2_client.exceptions import ControllerNotFound
2929

30-
from ..utils import _deprecated_argument, get_url_from_template
30+
from ..utils import OptInStatus, _deprecated_argument, get_url_from_template
3131

3232
if TYPE_CHECKING:
3333
import httpx
@@ -43,6 +43,8 @@ class SystemManagement:
4343
"external_connectors": "system/external_connectors",
4444
"web_session_timeout": "web_session_timeout",
4545
"host_configuration": "system/compute_hosts/configuration",
46+
"telemetry": "telemetry",
47+
"telemetry_events": "telemetry/events",
4648
}
4749

4850
def __init__(
@@ -139,6 +141,28 @@ def maintenance_notice(self, notice: SystemNotice | None) -> None:
139141
notice._update(resolved, push_to_server=False)
140142
self._maintenance_notice = notice
141143

144+
@property
145+
def telemetry(self) -> dict[str, OptInStatus | str]:
146+
"""Return the telemetry state."""
147+
url = self._url_for("telemetry")
148+
return self._session.get(url).json()
149+
150+
@property
151+
def telemetry_state(self) -> OptInStatus:
152+
"""Return the telemetry state."""
153+
return OptInStatus(self.telemetry["opt_in"])
154+
155+
@telemetry_state.setter
156+
def telemetry_state(self, mode: OptInStatus) -> None:
157+
"""Set the telemetry state."""
158+
url = self._url_for("telemetry")
159+
self._session.put(url, json={"opt_in": mode.name})
160+
161+
def get_telemetry_events(self) -> list[dict[str, Any]]:
162+
"""Return the list of telemetry events."""
163+
url = self._url_for("telemetry_events")
164+
return self._session.get(url).json()
165+
142166
def sync_compute_hosts_if_outdated(self) -> None:
143167
"""Synchronize compute hosts if they are outdated."""
144168
timestamp = time.time()

virl2_client/models/user.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020

2121
from __future__ import annotations
2222

23+
import warnings
2324
from typing import TYPE_CHECKING, Any
2425

25-
from ..utils import UNCHANGED, _Sentinel, get_url_from_template
26+
from ..utils import UNCHANGED, OptInStatus, _Sentinel, get_url_from_template
2627

2728
if TYPE_CHECKING:
2829
import httpx
@@ -142,9 +143,9 @@ def _prepare_body(
142143
password_dict: dict[str, str] | None = None,
143144
pubkey: str | None = None,
144145
resource_pool: str | None | _Sentinel = UNCHANGED,
145-
opt_in: bool | None | _Sentinel = UNCHANGED,
146+
opt_in: OptInStatus | bool | None | _Sentinel = UNCHANGED,
146147
tour_version: str | None = None,
147-
) -> dict[str, Any]:
148+
) -> None:
148149
optional_data = {
149150
"fullname": fullname,
150151
"description": description,
@@ -156,16 +157,26 @@ def _prepare_body(
156157
"pubkey": pubkey,
157158
"tour_version": tour_version,
158159
}
159-
sentinel_data = {
160-
"resource_pool": resource_pool,
161-
"opt_in": opt_in,
162-
}
163160
for key, value in optional_data.items():
164161
if value is not None:
165162
data[key] = value
166-
for key, value in sentinel_data.items():
167-
if value != UNCHANGED:
168-
data[key] = value
163+
if resource_pool is not UNCHANGED:
164+
data["resource_pool"] = resource_pool
165+
if opt_in is UNCHANGED:
166+
return
167+
if not isinstance(opt_in, OptInStatus):
168+
warnings.warn(
169+
"Using boolean or None values for opt_in is deprecated. "
170+
f"Use one of {[status.value for status in OptInStatus]} instead.",
171+
DeprecationWarning,
172+
)
173+
if opt_in is True:
174+
opt_in = OptInStatus.ACCEPTED
175+
elif opt_in is False:
176+
opt_in = OptInStatus.DECLINED
177+
else:
178+
opt_in = OptInStatus.UNSET
179+
data["opt_in"] = opt_in.value
169180

170181
def user_groups(self, user_id: str) -> list[str]:
171182
"""

virl2_client/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import warnings
2424
from collections.abc import Callable
2525
from contextlib import nullcontext
26+
from enum import Enum
2627
from functools import wraps
2728
from typing import TYPE_CHECKING, Any, Type, TypeVar, cast
2829

@@ -56,6 +57,12 @@ def __repr__(self):
5657
_CONFIG_MODE = "exclude_configurations=false"
5758

5859

60+
class OptInStatus(Enum):
61+
ACCEPTED = "accepted"
62+
DECLINED = "declined"
63+
UNSET = "unset"
64+
65+
5966
def _make_not_found(instance: Element) -> ElementNotFound:
6067
"""Composes and raises an ElementNotFound error for the given instance."""
6168
class_name = type(instance).__name__

0 commit comments

Comments
 (0)