Groups¶
Use groups to organize collaborators under a single ID. A group contains users and their roles. Use Accounts and users to find the people you want to add. Resource permissions remain with the operation that shares the resource.
Each example opens and closes its own client. Set NOMAD_PAT to a
token from your deployment. The examples use NOMAD Central by default.
For an Oasis, set the endpoint on your credential.
| Action | Required scope |
|---|---|
| Create without role-bearing members | groups:write |
| Create with members or maintainers | groups:write and users:read |
| Fetch or list groups | groups:read |
| Edit metadata or replace membership | groups:write |
| Resolve a collaborator by email or username | users:read |
| Delete a group | groups:delete |
Create a group with a known team¶
Replace email and username with values for two different existing users.
NOMAD makes the signed-in user the sole owner.
With role-bearing members, the SDK reads the signed-in account once to
fill in the owner record required by NOMAD.
import os
from nomad_python_api import NomadClient
from nomad_python_api.identity import BearerTokenCredential, GroupMember, GroupRole
# Choose two different users who already have accounts.
email = "email1@example.org"
username = "user2"
credential = BearerTokenCredential(os.environ["NOMAD_PAT"])
with NomadClient(credential=credential) as client:
users = client.identity.users
collaborator = users.get_by_email(email)
maintainer = users.get_by_username(username)
# NOMAD assigns the signed-in user as owner.
group = client.identity.groups.create(
name="Diffraction team",
members=[collaborator, GroupMember(user_id=maintainer.user_id, role=GroupRole.MAINTAINER)],
)
# Save the group ID for later scripts.
print(group.name, group.group_id)
The returned Group includes its ID, owner, and the membership fields
provided by NOMAD. You can pass loaded users, IDs, or exact selector
dictionaries. Loaded User and Account objects supply IDs locally.
Selectors may require one lookup each. Choose a user from prefix search
before adding them.
Edit group metadata and replace roles¶
Replace group_id with the ID of a group you can edit and username
with the account to add or promote. The
example updates the description and preserves the other members.
import os
from nomad_python_api import NomadClient
from nomad_python_api.identity import BearerTokenCredential, GroupMember, GroupRole
# Use the ID of an existing group and the username to promote.
group_id = "group-id-1"
username = "user2"
credential = BearerTokenCredential(os.environ["NOMAD_PAT"])
with NomadClient(credential=credential) as client:
groups = client.identity.groups
group = groups.get(group_id)
maintainer = client.identity.users.get_by_username(username)
if group.members is None or maintainer.user_id == group.owner_id:
raise ValueError("Choose a non-owner and a group with membership details.")
groups.update(group, description="Collaborators for the next beamtime")
# Preserve everyone else and add or promote the chosen maintainer.
members = [member for member in group.members if member.user_id != maintainer.user_id]
members.append(GroupMember(user_id=maintainer.user_id, role=GroupRole.MAINTAINER))
group = groups.replace_members(group, members=members)
print(group.group_id, len(members))
Membership replacement
replace_members() sends the entire role-bearing list. Anyone omitted
from the list is removed, except that the SDK preserves the owner.
Use the current group state when other people may be editing membership.
When passing only a group ID, supply its current owner explicitly.
Maintainers can manage membership. To change the owner, use a pending ownership transfer that the target user must accept.
List groups and work in pages¶
Iterating groups.list() fetches groups as needed. .filter() returns a
new listing for criteria such as group_id, user_id, and search_terms.
Use limit for a total item cap. For batch work, use .by_page() to read
whole pages and their opaque continuation cursors. An offset is calculated
from the current sorted results. Changes can shift records across pages.
import os
from nomad_python_api import NomadClient
from nomad_python_api.identity import BearerTokenCredential
credential = BearerTokenCredential(os.environ["NOMAD_PAT"])
with NomadClient(credential=credential) as client:
# Fetch results as needed and stop after five matches.
matches = client.identity.groups.list(limit=5).filter(search_terms="Diffraction")
for group in matches:
print(group.name, group.group_id)
Delete a group¶
Replace group_id with the ID of a group you intend to delete. Only the owner or
a deployment administrator can delete it. Successful deletion returns None.
import os
from nomad_python_api import NomadClient
from nomad_python_api.identity import BearerTokenCredential
# Replace this placeholder only with the ID of a group you intend to delete.
group_id = "group-id-1"
credential = BearerTokenCredential(os.environ["NOMAD_PAT"])
with NomadClient(credential=credential) as client:
# Set this ID only for a group you have decided to delete.
client.identity.groups.delete(group_id)
See the Python reference and Groups REST routes for every input and response field.