Skip to content

Commit cd6c0e3

Browse files
authored
SIMPLE-7569 API changes for RBAC (#141)
1 parent b454c6f commit cd6c0e3

File tree

4 files changed

+219
-129
lines changed

4 files changed

+219
-129
lines changed

virl2_client/models/group.py

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

2121
from __future__ import annotations
2222

23-
from typing import TYPE_CHECKING
23+
from typing import TYPE_CHECKING, Any
24+
import warnings
2425

25-
from ..utils import get_url_from_template
26+
from ..utils import _deprecated_argument, get_url_from_template
2627

2728
if TYPE_CHECKING:
2829
import httpx
@@ -34,8 +35,6 @@ class GroupManagement:
3435
_URL_TEMPLATES = {
3536
"groups": "groups",
3637
"group": "groups/{group_id}",
37-
"members": "groups/{group_id}/members",
38-
"labs": "groups/{group_id}/labs",
3938
"id": "groups/{group_name}/id",
4039
}
4140

@@ -80,83 +79,120 @@ def delete_group(self, group_id: str) -> None:
8079
url = self._url_for("group", group_id=group_id)
8180
self._session.delete(url)
8281

83-
def create_group(
84-
self,
85-
name: str,
86-
description: str = "",
87-
members: list[str] | None = None,
88-
labs: list[dict[str, str]] | None = None,
89-
) -> dict:
82+
def create_group(self, name: str, **kwargs: Any) -> dict:
9083
"""
9184
Create a group.
9285
9386
:param name: The name of the group.
94-
:param description: The description of the group.
95-
:param members: The members of the group.
96-
:param labs: The labs associated with the group.
87+
:param kwargs: Optional parameters. See below.
88+
89+
:Keyword Arguments:
90+
- description: The description of the group.
91+
- members: The members of the group.
92+
- associations: The lab associations for the group.
93+
- labs: DEPRECATED: replaced by 'associations'.
94+
The labs associated with the group.
95+
9796
:returns: The created group object.
9897
"""
99-
data = {"name": name}
100-
optional_data = {
101-
"description": description,
102-
"members": members,
103-
"labs": labs,
104-
}
105-
for key, value in optional_data.items():
106-
if value:
107-
data[key] = value
98+
data: dict[str, str | list] = {"name": name}
99+
self._prepare_body(data, **kwargs)
108100
url = self._url_for("groups")
109101
return self._session.post(url, json=data).json()
110102

111-
def update_group(
112-
self,
113-
group_id: str,
114-
name: str | None = None,
115-
description: str | None = None,
116-
members: list[str] | None = None,
117-
labs: list[dict[str, str]] | None = None,
118-
) -> dict:
103+
def update_group(self, group_id: str, **kwargs: Any) -> dict:
119104
"""
120105
Update a group.
121106
122107
:param group_id: The UUID4 of the group.
123-
:param name: The new name of the group.
124-
:param description: The description of the group.
125-
:param members: The members of the group.
126-
:param labs: The labs associated with the group.
108+
:param kwargs: Optional parameters. See below.
109+
110+
:Keyword Arguments:
111+
- name: The name of the group.
112+
- description: The description of the group.
113+
- members: The members of the group.
114+
- associations: The lab associations for the group.
115+
- labs: DEPRECATED: replaced by 'associations'.
116+
The labs associated with the group.
117+
127118
:returns: The updated group object.
128119
"""
129120
data: dict[str, str | list] = {}
130-
if name is not None:
131-
data["name"] = name
132-
if description is not None:
133-
data["description"] = description
134-
if members is not None:
135-
data["members"] = members
136-
if labs is not None:
137-
data["labs"] = labs
121+
self._prepare_body(data, **kwargs)
138122
url = self._url_for("group", group_id=group_id)
139123
return self._session.patch(url, json=data).json()
140124

125+
def _prepare_body(
126+
self,
127+
data: dict[str, str | list],
128+
name: str | None = None,
129+
description: str | None = None,
130+
members: list[str] | None = None,
131+
labs: list[dict[str, str]] | None = None,
132+
associations: list[dict[str, list[str]]] | None = None,
133+
):
134+
optional_data = {
135+
"name": name,
136+
"description": description,
137+
"members": members,
138+
"labs": labs,
139+
"associations": associations,
140+
}
141+
if labs is not None:
142+
func = self.create_group if data else self.update_group
143+
_deprecated_argument(func, labs, "labs")
144+
for key, value in optional_data.items():
145+
if value is not None:
146+
data[key] = value
147+
141148
def group_members(self, group_id: str) -> list[str]:
142149
"""
143150
Get the members of a group.
144151
145152
:param group_id: The UUID4 of the group.
146153
:returns: A list of users associated with this group.
147154
"""
148-
url = self._url_for("members", group_id=group_id)
149-
return self._session.get(url).json()
155+
return self.get_group(group_id)["members"]
150156

151157
def group_labs(self, group_id: str) -> list[str]:
152158
"""
159+
DEPRECATED: Use `.associations()` instead.
160+
(Reason: adapted to the new format of lab associations)
161+
153162
Get a list of labs associated with a group.
154163
155164
:param group_id: The UUID4 of the group.
156165
:returns: A list of labs associated with this group.
157166
"""
158-
url = self._url_for("labs", group_id=group_id)
159-
return self._session.get(url).json()
167+
warnings.warn(
168+
"'GroupManagement.group_labs()' is deprecated."
169+
"Use '.associations' instead.",
170+
DeprecationWarning,
171+
)
172+
return [lab["id"] for lab in self.get_group(group_id)["labs"]]
173+
174+
def associations(self, group_id: str) -> list[dict[str, list[str]]]:
175+
"""
176+
Get a list of lab associations for a group.
177+
178+
:param group_id: The UUID4 of the group.
179+
:returns: A list of lab associations for this group.
180+
"""
181+
return self.get_group(group_id)["associations"]
182+
183+
def update_associations(
184+
self, group_id: str, associations: list[dict[str, list[str]]]
185+
) -> dict:
186+
"""
187+
Update the lab associations for a group.
188+
189+
:param group_id: The UUID4 of the group.
190+
:param associations: The new list of lab associations.
191+
:returns: The updated group object.
192+
"""
193+
data = {"associations": associations}
194+
url = self._url_for("group", group_id=group_id)
195+
return self._session.patch(url, json=data).json()
160196

161197
def group_id(self, group_name: str) -> str:
162198
"""

virl2_client/models/lab.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class Lab:
8686
"pyats_testbed": "labs/{lab_id}/pyats_testbed",
8787
"layer3_addresses": "labs/{lab_id}/layer3_addresses",
8888
"download": "labs/{lab_id}/download",
89-
"groups": "labs/{lab_id}/groups",
89+
"associations": "labs/{lab_id}/associations",
9090
"connector_mappings": "labs/{lab_id}/connector_mappings",
9191
"resource_pools": "labs/{lab_id}/resource_pools",
9292
"annotations": "labs/{lab_id}/annotations",
@@ -2107,25 +2107,66 @@ def download(self) -> str:
21072107
@property
21082108
def groups(self) -> list[dict[str, str]]:
21092109
"""
2110+
DEPRECATED: Use `.associations` instead.
2111+
(Reason: adapted to new format of lab associations)
2112+
21102113
Return the groups this lab is associated with.
21112114
21122115
:returns: List of objects consisting of group ID and permissions.
21132116
"""
2114-
url = self._url_for("groups")
2115-
return self._session.get(url).json()
2117+
warnings.warn(
2118+
"'Lab.groups' is deprecated. Use '.associations' instead.",
2119+
DeprecationWarning,
2120+
)
2121+
url = self._url_for("lab")
2122+
return self._session.get(url).json()["groups"]
21162123

21172124
@check_stale
21182125
def update_lab_groups(
21192126
self, group_list: list[dict[str, str]]
21202127
) -> list[dict[str, str]]:
21212128
"""
2129+
DEPRECATED: Use `.update_associations()` instead.
2130+
(Reason: adapted to new format of lab associations)
2131+
21222132
Modify lab/group association.
21232133
21242134
:param group_list: List of objects consisting of group ID and permissions.
21252135
:returns: Updated objects consisting of group ID and permissions.
21262136
"""
2127-
url = self._url_for("groups")
2128-
return self._session.put(url, json=group_list).json()
2137+
warnings.warn(
2138+
"'Lab.update_lab_groups()' is deprecated. Use '.update_associations()' instead.",
2139+
DeprecationWarning,
2140+
)
2141+
url = self._url_for("lab")
2142+
data = {"groups": group_list}
2143+
return self._session.patch(url, json=data).json()
2144+
2145+
@property
2146+
def associations(self) -> dict[list[dict[str, str]]]:
2147+
"""
2148+
Return the group and user associations for this lab.
2149+
2150+
:returns: An object containing group and user list of objects consisting of ID
2151+
and permissions.
2152+
"""
2153+
url = self._url_for("associations")
2154+
return self._session.get(url).json()
2155+
2156+
@check_stale
2157+
def update_associations(
2158+
self, associations: dict[list[dict[str, str]]]
2159+
) -> dict[list[dict[str, str]]]:
2160+
"""
2161+
Modify lab/group/user associations.
2162+
2163+
:param associations: An object containing group and user list of objects
2164+
consisting of ID and permissions.
2165+
:returns: Updated object containing group and user list of objects consisting
2166+
of ID and permissions.
2167+
"""
2168+
url = self._url_for("associations")
2169+
return self._session.patch(url, json=associations).json()
21292170

21302171
@property
21312172
def connector_mappings(self) -> list[dict[str, Any]]:

0 commit comments

Comments
 (0)