Creating and updating a dynamic group
This page explains how to create and update a dynamic group.
Before you begin
Perform the following tasks before proceeding with the information on this page:
Read the Groups API overview.
Read the dynamic groups overview.
(Optional) Formulate and test a membership query.
Creating a dynamic group
The following is an example of creating a dynamic group:
REST
To create a dynamic group, call
groups.create()
with an instance of the group. The instance of the group must contain the
dynamicGroupMetadata
with the query used to populate the group.
Python
To create a dynamic group, call the service.groups().create
method with
an instance of the group. The instance of the group must contain the
dynamicGroupMetadata
with the query used to populate the group. The
following sample shows how to create a dynamic group containing all users
existing in the Engineering department:
def create_dynamic_group(customer_id, email, query):
service = build_service()
groupDef = {
"parent": "customerId/{}".format(customer_id),
"groupKey": {"id": email},
"labels": {"cloudidentity.googleapis.com/groups.discussion_forum": ""},
"dynamicGroupMetadata": {
"queries": [
{
"resourceType": "USER",
"query": "user.organizations.exists(org, org.department=='engineering')"
}
]
}
}
request = service.groups().create(body=groupDef)
request.uri += "&initialGroupConfig=EMPTY"
response = request.execute()
return response
Updating a dynamic group
The following is an example of updating a dynamic group:
REST
To update a dynamic group, call
groups.patch()
with an instance of the group. The instance of the group must contain the
dynamicGroupMetadata
with the new query used to populate the group.
Python
To update a dynamic group, call the service.groups().patch
method with
the name of the group to update and an instance of the group. The instance
of the group must contain the
dynamicGroupMetadata
with the query used to populate the group. The
following sample shows how to update a dynamic group containing all users in
Engineering to a group containing all users in either Engineering or Finance
departments:
# name (i.e. groups/01234abcdeef) uniquely identifies the group
def update_dynamic_group(name, query):
service = build_service()
groupDef = {
"dynamicGroupMetadata": {
"queries": [
{
"resourceType": "USER",
"query": "user.organizations.exists(org, org.department=='engineering' || org.department=='finance')"
}
]
}
}
request = service.groups().patch(name=name, body=groupDef)
request.uri += "&updateMask=dynamicGroupMetadata"
response = request.execute()
return response
What's next
After the dynamic group exists, you can retrieve it and list its memberships. For more information, refer to Retrieving a dynamic group and listing members.