20
20
21
21
from __future__ import annotations
22
22
23
- from typing import TYPE_CHECKING
23
+ from typing import TYPE_CHECKING , Any
24
+ import warnings
24
25
25
- from ..utils import get_url_from_template
26
+ from ..utils import _deprecated_argument , get_url_from_template
26
27
27
28
if TYPE_CHECKING :
28
29
import httpx
@@ -34,8 +35,6 @@ class GroupManagement:
34
35
_URL_TEMPLATES = {
35
36
"groups" : "groups" ,
36
37
"group" : "groups/{group_id}" ,
37
- "members" : "groups/{group_id}/members" ,
38
- "labs" : "groups/{group_id}/labs" ,
39
38
"id" : "groups/{group_name}/id" ,
40
39
}
41
40
@@ -80,83 +79,120 @@ def delete_group(self, group_id: str) -> None:
80
79
url = self ._url_for ("group" , group_id = group_id )
81
80
self ._session .delete (url )
82
81
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 :
90
83
"""
91
84
Create a group.
92
85
93
86
: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
+
97
96
:returns: The created group object.
98
97
"""
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 )
108
100
url = self ._url_for ("groups" )
109
101
return self ._session .post (url , json = data ).json ()
110
102
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 :
119
104
"""
120
105
Update a group.
121
106
122
107
: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
+
127
118
:returns: The updated group object.
128
119
"""
129
120
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 )
138
122
url = self ._url_for ("group" , group_id = group_id )
139
123
return self ._session .patch (url , json = data ).json ()
140
124
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
+
141
148
def group_members (self , group_id : str ) -> list [str ]:
142
149
"""
143
150
Get the members of a group.
144
151
145
152
:param group_id: The UUID4 of the group.
146
153
:returns: A list of users associated with this group.
147
154
"""
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" ]
150
156
151
157
def group_labs (self , group_id : str ) -> list [str ]:
152
158
"""
159
+ DEPRECATED: Use `.associations()` instead.
160
+ (Reason: adapted to the new format of lab associations)
161
+
153
162
Get a list of labs associated with a group.
154
163
155
164
:param group_id: The UUID4 of the group.
156
165
:returns: A list of labs associated with this group.
157
166
"""
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 ()
160
196
161
197
def group_id (self , group_name : str ) -> str :
162
198
"""
0 commit comments