0

List Org Units

by
Published today

List the organizational units of the Workspace account: all of them, or only the direct children of `org_unit_path`. Needs admin.directory.orgunit or admin.directory.orgunit.readonly on the connection.

Script gworkspace Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
/**
4
 * List Org Units
5
 * List the organizational units of the Workspace account: all of them, or only the direct children of `org_unit_path`. Needs admin.directory.orgunit or admin.directory.orgunit.readonly on the connection.
6
 */
7
export async function main(
8
  auth: RT.Gworkspace,
9
  type: "all" | "children" | "allIncludingParent" = "all",
10
  org_unit_path: string | undefined,
11
) {
12
  const url = new URL(
13
    "https://admin.googleapis.com/admin/directory/v1/customer/my_customer/orgunits",
14
  )
15
  url.searchParams.append("type", type)
16
  if (org_unit_path !== undefined && org_unit_path !== "") {
17
    url.searchParams.append("orgUnitPath", org_unit_path)
18
  }
19

20
  const response = await fetch(url, {
21
    headers: {
22
      Authorization: `Bearer ${auth.token}`,
23
      Accept: "application/json",
24
    },
25
  })
26

27
  if (!response.ok) {
28
    throw new Error(`${response.status} ${await response.text()}`)
29
  }
30

31
  return await response.json()
32
}
33