| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
import typing as t |
| 5 |
import uuid |
| 6 |
|
| 7 |
from globus_sdk import client, response |
| 8 |
from globus_sdk._internal.remarshal import commajoin |
| 9 |
from globus_sdk._missing import MISSING, MissingType |
| 10 |
from globus_sdk.scopes import GroupsScopes, Scope |
| 11 |
|
| 12 |
from .data import BatchMembershipActions, GroupPolicies |
| 13 |
from .errors import GroupsAPIError |
| 14 |
|
| 15 |
if sys.version_info >= (3, 10): |
| 16 |
from typing import TypeAlias |
| 17 |
else: |
| 18 |
from typing_extensions import TypeAlias |
| 19 |
|
| 20 |
_VALID_STATUSES_T: TypeAlias = t.Literal[ |
| 21 |
"active", |
| 22 |
"declined", |
| 23 |
"invited", |
| 24 |
"left", |
| 25 |
"pending", |
| 26 |
"rejected", |
| 27 |
"removed", |
| 28 |
] |
| 29 |
|
| 30 |
|
| 31 |
class GroupsClient(client.BaseClient): |
| 32 |
""" |
| 33 |
Client for the |
| 34 |
`Globus Groups API <https://docs.globus.org/api/groups/>`_. |
| 35 |
|
| 36 |
.. sdk-sphinx-copy-params:: BaseClient |
| 37 |
|
| 38 |
This provides a relatively low level client to public groups API endpoints. |
| 39 |
You may also consider looking at the GroupsManager as a simpler interface |
| 40 |
to more common actions. |
| 41 |
|
| 42 |
.. automethodlist:: globus_sdk.GroupsClient |
| 43 |
""" |
| 44 |
|
| 45 |
error_class = GroupsAPIError |
| 46 |
service_name = "groups" |
| 47 |
scopes = GroupsScopes |
| 48 |
|
| 49 |
@property |
| 50 |
def default_scope_requirements(self) -> list[Scope]: |
| 51 |
return [GroupsScopes.view_my_groups_and_memberships] |
| 52 |
|
| 53 |
def get_my_groups( |
| 54 |
self, |
| 55 |
*, |
| 56 |
statuses: ( |
| 57 |
_VALID_STATUSES_T | t.Iterable[_VALID_STATUSES_T] | MissingType |
| 58 |
) = MISSING, |
| 59 |
query_params: dict[str, t.Any] | None = None, |
| 60 |
) -> response.ArrayResponse: |
| 61 |
""" |
| 62 |
Return a list of groups your identity belongs to. |
| 63 |
|
| 64 |
:param statuses: |
| 65 |
If provided, only groups containing memberships with the given status |
| 66 |
are returned. |
| 67 |
Valid values are ``active``, ``invited``, ``pending``, ``rejected``, |
| 68 |
``removed``, ``left``, and ``declined``. |
| 69 |
:param query_params: Additional passthrough query parameters |
| 70 |
|
| 71 |
.. tab-set:: |
| 72 |
|
| 73 |
.. tab-item:: API Info |
| 74 |
|
| 75 |
``GET /v2/groups/my_groups`` |
| 76 |
|
| 77 |
.. extdoclink:: Retrieve your groups and membership |
| 78 |
:service: groups |
| 79 |
:ref: get_my_groups_and_memberships_v2_groups_my_groups_get |
| 80 |
""" |
| 81 |
query_params = {"statuses": commajoin(statuses), **(query_params or {})} |
| 82 |
return response.ArrayResponse( |
| 83 |
self.get("/v2/groups/my_groups", query_params=query_params) |
| 84 |
) |
| 85 |
|
| 86 |
def get_group( |
| 87 |
self, |
| 88 |
group_id: uuid.UUID | str, |
| 89 |
*, |
| 90 |
include: str | t.Iterable[str] | MissingType = MISSING, |
| 91 |
query_params: dict[str, t.Any] | None = None, |
| 92 |
) -> response.GlobusHTTPResponse: |
| 93 |
""" |
| 94 |
Get details about a specific group |
| 95 |
|
| 96 |
:param group_id: the ID of the group |
| 97 |
:param include: list of additional fields to include (allowed fields are |
| 98 |
``memberships``, ``my_memberships``, ``policies``, ``allowed_actions``, and |
| 99 |
``child_ids``) |
| 100 |
:param query_params: additional passthrough query parameters |
| 101 |
|
| 102 |
.. tab-set:: |
| 103 |
|
| 104 |
.. tab-item:: API Info |
| 105 |
|
| 106 |
``GET /v2/groups/<group_id>`` |
| 107 |
|
| 108 |
.. extdoclink:: Get Group |
| 109 |
:service: groups |
| 110 |
:ref: get_group_v2_groups__group_id__get |
| 111 |
""" |
| 112 |
query_params = {"include": commajoin(include), **(query_params or {})} |
| 113 |
return self.get(f"/v2/groups/{group_id}", query_params=query_params) |
| 114 |
|
| 115 |
def get_group_by_subscription_id( |
| 116 |
self, subscription_id: uuid.UUID | str |
| 117 |
) -> response.GlobusHTTPResponse: |
| 118 |
""" |
| 119 |
Using a subscription ID, find the group which provides that subscription. |
| 120 |
|
| 121 |
:param subscription_id: the subscription ID of the group |
| 122 |
|
| 123 |
.. tab-set:: |
| 124 |
|
| 125 |
.. tab-item:: Example Usage |
| 126 |
|
| 127 |
.. code-block:: python |
| 128 |
|
| 129 |
from globus_sdk import GroupsClient |
| 130 |
|
| 131 |
groups = GroupsClient(...) |
| 132 |
group_id = groups.get_group_by_subscription_id(subscription_id)["group_id"] |
| 133 |
|
| 134 |
.. tab-item:: Example Response Data |
| 135 |
|
| 136 |
.. expandtestfixture:: groups.get_group_by_subscription_id |
| 137 |
|
| 138 |
.. tab-item:: API Info |
| 139 |
|
| 140 |
``GET /v2/subscription_info/<subscription_id>`` |
| 141 |
|
| 142 |
.. extdoclink:: Get Group by Subscription ID |
| 143 |
:service: groups |
| 144 |
:ref: get_group_by_subscription_id_v2_subscription_info__subscription_id__get |
| 145 |
""" # noqa: E501 |
| 146 |
return self.get(f"/v2/subscription_info/{subscription_id}") |
| 147 |
|
| 148 |
def delete_group( |
| 149 |
self, |
| 150 |
group_id: uuid.UUID | str, |
| 151 |
*, |
| 152 |
query_params: dict[str, t.Any] | None = None, |
| 153 |
) -> response.GlobusHTTPResponse: |
| 154 |
""" |
| 155 |
Delete a group. |
| 156 |
|
| 157 |
:param group_id: the ID of the group |
| 158 |
:param query_params: additional passthrough query parameters |
| 159 |
|
| 160 |
.. tab-set:: |
| 161 |
|
| 162 |
.. tab-item:: API Info |
| 163 |
|
| 164 |
``DELETE /v2/groups/<group_id>`` |
| 165 |
|
| 166 |
.. extdoclink:: Delete a Group |
| 167 |
:service: groups |
| 168 |
:ref: delete_group_v2_groups__group_id__delete |
| 169 |
""" |
| 170 |
return self.delete(f"/v2/groups/{group_id}", query_params=query_params) |
| 171 |
|
| 172 |
def create_group( |
| 173 |
self, |
| 174 |
data: dict[str, t.Any], |
| 175 |
*, |
| 176 |
query_params: dict[str, t.Any] | None = None, |
| 177 |
) -> response.GlobusHTTPResponse: |
| 178 |
""" |
| 179 |
Create a group. |
| 180 |
|
| 181 |
:param data: the group document to create |
| 182 |
:param query_params: additional passthrough query parameters |
| 183 |
|
| 184 |
.. tab-set:: |
| 185 |
|
| 186 |
.. tab-item:: API Info |
| 187 |
|
| 188 |
``POST /v2/groups`` |
| 189 |
|
| 190 |
.. extdoclink:: Create a Group |
| 191 |
:service: groups |
| 192 |
:ref: create_group_v2_groups_post |
| 193 |
""" |
| 194 |
return self.post("/v2/groups", data=data, query_params=query_params) |
| 195 |
|
| 196 |
def update_group( |
| 197 |
self, |
| 198 |
group_id: uuid.UUID | str, |
| 199 |
data: dict[str, t.Any], |
| 200 |
*, |
| 201 |
query_params: dict[str, t.Any] | None = None, |
| 202 |
) -> response.GlobusHTTPResponse: |
| 203 |
""" |
| 204 |
Update a given group. |
| 205 |
|
| 206 |
:param group_id: the ID of the group |
| 207 |
:param data: the group document to use for update |
| 208 |
:param query_params: additional passthrough query parameters |
| 209 |
|
| 210 |
.. tab-set:: |
| 211 |
|
| 212 |
.. tab-item:: API Info |
| 213 |
|
| 214 |
``PUT /v2/groups/<group_id>`` |
| 215 |
|
| 216 |
.. extdoclink:: Update a Group |
| 217 |
:service: groups |
| 218 |
:ref: update_group_v2_groups__group_id__put |
| 219 |
""" |
| 220 |
return self.put(f"/v2/groups/{group_id}", data=data, query_params=query_params) |
| 221 |
|
| 222 |
def get_group_policies( |
| 223 |
self, |
| 224 |
group_id: uuid.UUID | str, |
| 225 |
*, |
| 226 |
query_params: dict[str, t.Any] | None = None, |
| 227 |
) -> response.GlobusHTTPResponse: |
| 228 |
""" |
| 229 |
Get policies for the given group. |
| 230 |
|
| 231 |
:param group_id: the ID of the group |
| 232 |
:param query_params: additional passthrough query parameters |
| 233 |
|
| 234 |
.. tab-set:: |
| 235 |
|
| 236 |
.. tab-item:: API Info |
| 237 |
|
| 238 |
``GET /v2/groups/<group_id>/policies`` |
| 239 |
|
| 240 |
.. extdoclink:: Get the policies for a group |
| 241 |
:service: groups |
| 242 |
:ref: get_policies_v2_groups__group_id__policies_get |
| 243 |
""" |
| 244 |
return self.get(f"/v2/groups/{group_id}/policies", query_params=query_params) |
| 245 |
|
| 246 |
def set_group_policies( |
| 247 |
self, |
| 248 |
group_id: uuid.UUID | str, |
| 249 |
data: dict[str, t.Any] | GroupPolicies, |
| 250 |
*, |
| 251 |
query_params: dict[str, t.Any] | None = None, |
| 252 |
) -> response.GlobusHTTPResponse: |
| 253 |
""" |
| 254 |
Set policies for the group. |
| 255 |
|
| 256 |
:param group_id: the ID of the group |
| 257 |
:param data: the group policy document to set |
| 258 |
:param query_params: additional passthrough query parameters |
| 259 |
|
| 260 |
.. tab-set:: |
| 261 |
|
| 262 |
.. tab-item:: API Info |
| 263 |
|
| 264 |
``PUT /v2/groups/<group_id>/policies`` |
| 265 |
|
| 266 |
.. extdoclink:: Set the policies for a group |
| 267 |
:service: groups |
| 268 |
:ref: update_policies_v2_groups__group_id__policies_put |
| 269 |
""" |
| 270 |
return self.put( |
| 271 |
f"/v2/groups/{group_id}/policies", data=data, query_params=query_params |
| 272 |
) |
| 273 |
|
| 274 |
def get_identity_preferences( |
| 275 |
self, *, query_params: dict[str, t.Any] | None = None |
| 276 |
) -> response.GlobusHTTPResponse: |
| 277 |
""" |
| 278 |
Get identity preferences. Currently this only includes whether the |
| 279 |
user allows themselves to be added to groups. |
| 280 |
|
| 281 |
:param query_params: additional passthrough query parameters |
| 282 |
|
| 283 |
.. tab-set:: |
| 284 |
|
| 285 |
.. tab-item:: API Info |
| 286 |
|
| 287 |
``GET /v2/preferences`` |
| 288 |
|
| 289 |
.. extdoclink:: Get the preferences for your identity set |
| 290 |
:service: groups |
| 291 |
:ref: get_identity_set_preferences_v2_preferences_get |
| 292 |
""" |
| 293 |
return self.get("/v2/preferences", query_params=query_params) |
| 294 |
|
| 295 |
def set_identity_preferences( |
| 296 |
self, |
| 297 |
data: dict[str, t.Any], |
| 298 |
*, |
| 299 |
query_params: dict[str, t.Any] | None = None, |
| 300 |
) -> response.GlobusHTTPResponse: |
| 301 |
""" |
| 302 |
Set identity preferences. Currently this only includes whether the |
| 303 |
user allows themselves to be added to groups. |
| 304 |
|
| 305 |
:param data: the identity set preferences document |
| 306 |
:param query_params: additional passthrough query parameters |
| 307 |
|
| 308 |
.. tab-set:: |
| 309 |
|
| 310 |
.. tab-item:: Example Usage |
| 311 |
|
| 312 |
.. code-block:: python |
| 313 |
|
| 314 |
gc = globus_sdk.GroupsClient(...) |
| 315 |
gc.set_identity_preferences({"allow_add": False}) |
| 316 |
|
| 317 |
.. tab-item:: API Info |
| 318 |
|
| 319 |
``PUT /v2/preferences`` |
| 320 |
|
| 321 |
.. extdoclink:: Set the preferences for your identity set |
| 322 |
:service: groups |
| 323 |
:ref: put_identity_set_preferences_v2_preferences_put |
| 324 |
""" |
| 325 |
return self.put("/v2/preferences", data=data, query_params=query_params) |
| 326 |
|
| 327 |
def get_membership_fields( |
| 328 |
self, |
| 329 |
group_id: uuid.UUID | str, |
| 330 |
*, |
| 331 |
query_params: dict[str, t.Any] | None = None, |
| 332 |
) -> response.GlobusHTTPResponse: |
| 333 |
""" |
| 334 |
Get membership fields for your identities. |
| 335 |
|
| 336 |
:param group_id: the ID of the group |
| 337 |
:param query_params: additional passthrough query parameters |
| 338 |
|
| 339 |
.. tab-set:: |
| 340 |
|
| 341 |
.. tab-item:: API Info |
| 342 |
|
| 343 |
``GET /v2/groups/<group_id>/membership_fields`` |
| 344 |
|
| 345 |
.. extdoclink:: Get the membership fields for your identity set |
| 346 |
:service: groups |
| 347 |
:ref: get_membership_fields_v2_groups__group_id__membership_fields_get |
| 348 |
""" # noqa: E501 |
| 349 |
return self.get( |
| 350 |
f"/v2/groups/{group_id}/membership_fields", query_params=query_params |
| 351 |
) |
| 352 |
|
| 353 |
def set_membership_fields( |
| 354 |
self, |
| 355 |
group_id: uuid.UUID | str, |
| 356 |
data: dict[t.Any, str], |
| 357 |
*, |
| 358 |
query_params: dict[str, t.Any] | None = None, |
| 359 |
) -> response.GlobusHTTPResponse: |
| 360 |
""" |
| 361 |
Set membership fields for your identities. |
| 362 |
|
| 363 |
:param group_id: the ID of the group |
| 364 |
:param data: the membership fields document |
| 365 |
:param query_params: additional passthrough query parameters |
| 366 |
|
| 367 |
.. tab-set:: |
| 368 |
|
| 369 |
.. tab-item:: API Info |
| 370 |
|
| 371 |
``PUT /v2/groups/<group_id>/membership_fields`` |
| 372 |
|
| 373 |
.. extdoclink:: Set the membership fields for your identity set |
| 374 |
:service: groups |
| 375 |
:ref: put_membership_fields_v2_groups__group_id__membership_fields_put |
| 376 |
""" # noqa: E501 |
| 377 |
return self.put( |
| 378 |
f"/v2/groups/{group_id}/membership_fields", |
| 379 |
data=data, |
| 380 |
query_params=query_params, |
| 381 |
) |
| 382 |
|
| 383 |
def batch_membership_action( |
| 384 |
self, |
| 385 |
group_id: uuid.UUID | str, |
| 386 |
actions: dict[str, t.Any] | BatchMembershipActions, |
| 387 |
*, |
| 388 |
query_params: dict[str, t.Any] | None = None, |
| 389 |
) -> response.GlobusHTTPResponse: |
| 390 |
""" |
| 391 |
Execute a batch of actions against several group memberships. |
| 392 |
|
| 393 |
:param group_id: the ID of the group |
| 394 |
:param actions: the batch of membership actions to perform, modifying, creating, |
| 395 |
and removing memberships in the group |
| 396 |
:param query_params: additional passthrough query parameters |
| 397 |
|
| 398 |
.. tab-set:: |
| 399 |
|
| 400 |
.. tab-item:: Example Usage |
| 401 |
|
| 402 |
.. code-block:: python |
| 403 |
|
| 404 |
gc = globus_sdk.GroupsClient(...) |
| 405 |
group_id = ... |
| 406 |
batch = globus_sdk.BatchMembershipActions() |
| 407 |
batch.add_members("ae332d86-d274-11e5-b885-b31714a110e9") |
| 408 |
batch.invite_members("c699d42e-d274-11e5-bf75-1fc5bf53bb24") |
| 409 |
gc.batch_membership_action(group_id, batch) |
| 410 |
|
| 411 |
.. tab-item:: API Info |
| 412 |
|
| 413 |
``PUT /v2/groups/<group_id>/membership_fields`` |
| 414 |
|
| 415 |
.. extdoclink:: Perform actions on members of the group |
| 416 |
:service: groups |
| 417 |
:ref: group_membership_post_actions_v2_groups__group_id__post |
| 418 |
""" |
| 419 |
return self.post( |
| 420 |
f"/v2/groups/{group_id}", data=actions, query_params=query_params |
| 421 |
) |
| 422 |
|
| 423 |
def set_subscription_admin_verified( |
| 424 |
self, |
| 425 |
group_id: uuid.UUID | str, |
| 426 |
subscription_id: uuid.UUID | str | None, |
| 427 |
*, |
| 428 |
query_params: dict[str, t.Any] | None = None, |
| 429 |
) -> response.GlobusHTTPResponse: |
| 430 |
""" |
| 431 |
Verify a group as belonging to a subscription or disassociate a verified group |
| 432 |
from a subscription. |
| 433 |
|
| 434 |
:param group_id: the ID of the group |
| 435 |
:param subscription_id: the ID of the subscription to which the group belongs, |
| 436 |
or ``None`` to disassociate the group from a subscription |
| 437 |
:param query_params: additional passthrough query parameters |
| 438 |
|
| 439 |
.. tab-set:: |
| 440 |
|
| 441 |
.. tab-item:: API Info |
| 442 |
|
| 443 |
``PUT /v2/groups/<group_id>/subscription_admin_verified`` |
| 444 |
|
| 445 |
.. extdoclink:: Update the group's subscription admin verified ID |
| 446 |
:service: groups |
| 447 |
:ref: update_subscription_admin_verified_id_v2_groups__group_id__ |
| 448 |
subscription_admin_verified_put |
| 449 |
""" |
| 450 |
return self.put( |
| 451 |
f"/v2/groups/{group_id}/subscription_admin_verified", |
| 452 |
data={"subscription_admin_verified_id": subscription_id}, |
| 453 |
query_params=query_params, |
| 454 |
) |