# Realms and sub-realms

> Create a realm for a team, a partner or a customer beneath your own, with its own administrator, and suspend, reactivate or delete it.

A realm is one organization's own: its users, groups, apps, keys, policies and settings, walled off from every other
realm. Realms nest. Beneath your realm you can create realms for a business unit, a partner or a customer of yours,
and beneath those, more. You administer every realm below your own, and none above or beside it.

You'd create a sub-realm when a group of users should have its own sign-in, its own apps and its own administrators,
while you keep oversight. The new realm's administrators run it, and can create realms of their own beneath it.

## Before you begin

- **Privilege:** `RealmOwner` in your realm, or any privilege that gives `DELEGATE` over it. Every call on this page
  is made from the parent: you create, suspend and delete a realm as an administrator of the realm above it.
- **Where:** the API, `https://foobar.cvyuh.com/api/v0/am/global/realms/v1`.
- **First:** a session in `$CVYUH_SESSION` ([Call the API](/develop/api/#get-a-session)), and the name and
  host names you want for the new realm.

## How it works

A new realm is made in the background: its signing and storage keys, its settings, its sign-in chains, its first
administrator and its host names. It moves through these states:

| `status` | What it means |
|---|---|
| `creating` | Being set up. Usually seconds |
| `failed` | Setup stopped partway; resume it to carry on where it left off |
| `active` | Serving sign-ins and tokens |
| `suspended` | Switched off, its host names kept. Nothing in it answers until you activate it again |
| `deleted` | Out of service. Its host names are freed for other realms |

Each realm has an `id` that never changes, and you address a realm by it on the realms API.

**Its first administrator.** Give a password when you create the realm, and it's born with one administrator: the
user `admin@<name>`, in the group `<name>_admins`, which holds the realm's `RealmOwner`. For the realm `engineering`,
that's `admin@engineering` in `engineering_admins`. Without a password, the realm has no administrator of its own,
and only you and the administrators above it can run it.

## Set it up

Create the realm, naming its parent by the `path` the realms list shows for it:

```sh
curl -i https://foobar.cvyuh.com/api/v0/am/global/realms/v1 \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "name": "engineering", "parentPath": "/foobar",
        "hostnames": ["foobar-engineering.cvyuh.com"],
        "password": "<a strong first password>", "mailId": "maya@example.com" }'
```

The realm answers `202 Accepted`, with the new realm's `id`, a `Location` to poll, and its revision in `ETag`. Poll it
until `status` is `active`:

```sh
curl https://foobar.cvyuh.com/api/v0/am/global/realms/v1/<realm id> \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

If it shows `failed`, with a `code` and `reason`, resume it. Send the administrator's `password` again if the realm
hadn't yet made its first administrator; otherwise send `{}`:

```sh
curl -X POST https://foobar.cvyuh.com/api/v0/am/global/realms/v1/<realm id>/resume \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "password": "<a strong first password>" }'
```

To suspend or reactivate a realm, send its new `status`:

```sh
curl -X PATCH https://foobar.cvyuh.com/api/v0/am/global/realms/v1/<realm id> \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "status": "suspended" }'
```

To delete it, name the revision you expect. The realm answers `200` with `{ "status": "deleted", "path": … }`:

```sh
curl -X DELETE https://foobar.cvyuh.com/api/v0/am/global/realms/v1/<realm id> \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H 'If-Match: "<rev>"'
```

## Deleting a realm

<Aside type="caution" title="You can't bring a deleted realm back">
  Deleting is for good. There's no undelete, and Cvyūh can't restore a deleted realm either. If you might want the
  realm again, **suspend** it instead: a suspended realm answers nothing, and activating it brings everything back.
</Aside>

A deleted realm answers nothing at once: no sign-in, no token, no admin call. Its host names are freed for other
realms. The realms list still shows it, with `status` `deleted`, its `deleteTime`, and `purged`.

Its data, keys and audit trail are kept, unreadable to anyone, until the realm is **purged**. Nothing purges a realm
on a timer: Cvyūh purges one only when you ask, through your Cvyūh support contact. A purge destroys the realm's
records and keys together. By default it keeps the realm's audit trail, so you can still answer for what happened
in it; ask for the audit trail to be purged too if you want nothing kept. A realm is purged only after every realm
beneath it.

## What happens when it fails

| Answer | Cause | What to do |
|---|---|---|
| `403`, "admin of the parent realm required" | You don't hold `DELEGATE` over the parent | Act from a realm above, or ask for `RealmOwner` there |
| `403`, "sub-realms are not turned on for …" | Your realm doesn't include sub-realms | Ask Cvyūh to turn them on |
| `400`, naming a nesting limit | The parent has as many realms directly beneath it as it may, or the new realm would sit too deep | Create it elsewhere, or ask Cvyūh to raise the limit |
| `400`, "a realm holds at most 3 hostnames" | More host names than the realm may hold | Send fewer; see [Host names](/platform/realms/hostnames/) |
| `400` on suspend or delete | It's the realm you're signed in to | Act from a realm above |
| `400` on create | A `products` list, or a name that isn't a DNS label, or the reserved name `mtls` | Leave `products` out; fix the name |
| `409` on create | A realm of that name already sits beneath this parent, or a host name is held by another realm | Choose another name or host name |
| `409` on suspend or delete | Live realms sit beneath it | Add `?subrealms=true` to apply the change to all of them |
| `400` on activate, "parent realm … is not active" | Its parent is suspended | Activate the parent first |
| `428` on delete | No `If-Match` | Send `If-Match: "<rev>"`, or `If-Match: *` |
| `412` on delete | The realm changed since you read its `rev` | Read it again and retry |

A user of a suspended or deleted realm sees the realm's host name stop answering: sign-in pages don't load, and apps'
token requests fail. Their sessions stop working at once, and those of a suspended realm work again once it's active.

## Limits and defaults

| What | Default | Can it change |
|---|---|---|
| Realms directly beneath a realm | No limit | Cvyūh can set a limit per realm, on request |
| Levels of realms beneath a realm | No limit | Cvyūh can set a limit per realm, on request |
| Host names per realm | 3 | Cvyūh raises it on request |
| A realm's name | 1 to 63 characters, `a`–`z`, `0`–`9` and `-` | No |
| A deleted realm's data | Kept until purged | Purged when you ask |

Every limit is also in [Limits](/reference/limits/).

## Troubleshooting

- **The realm stays `creating`.** Setup usually takes seconds. If it's still `creating` after a minute, read it
  again: a `failed` status with a `code` and `reason` says which step stopped. Resume it; don't create it again,
  which is refused while the first one holds its name.
- **Resume asks for a password.** The realm stopped before it made its first administrator. Send the `password` again
  in the resume.
- **The new administrator can't sign in.** Use the new realm's own host name, not yours, and the user name
  `admin@<name>`.
- **A realm you suspended still answers somewhere.** A realm served from more than one site takes up to about a
  minute to stop at every site.

## Audit record

Each create, host-name change, suspension, reactivation and deletion is recorded on the realm's audit trail, naming
who made it and the realm's state before: `CREATED`, `UPDATED`, `SUSPENDED`, `ACTIVATED` and `DELETED`. No API returns
these records yet; ask Cvyūh support for them. The realms list shows when each deleted realm was deleted.

## Good to know

- **The list shows what's yours.** `GET https://foobar.cvyuh.com/api/v0/am/global/realms/v1` lists your realm and
  every realm beneath it, deleted ones included, and nothing else. It pages, filters and sorts like any other list:
  see [Call the API](/develop/api/). Filter on `status` to leave deleted realms out.
- **Names are unique among siblings.** The name `mtls` is reserved.
- **Suspending stops a realm at once.** Its host names no longer answer, so no one can sign in or get a token, and its
  users' sessions stop working. Nothing is deleted: activating it brings it all back.
- **Realms beneath come along only if you say so,** with `?subrealms=true`.
- **`If-Match: *` deletes whatever the current revision.**
- **Every realm holds every product you have.** Choosing products per realm is planned.

## Coming next

- [Products per realm](/platform/realms/whats-next/#products-per-realm): choose which products each realm holds.

## Reference

### Fields

| Field | What it is |
|---|---|
| `name` | Required. A lowercase DNS label: `a`–`z`, `0`–`9` and `-`, 1 to 63 characters, not starting or ending with `-` |
| `parentPath` | The realm it goes beneath, by the `path` the realms list shows for it |
| `hostnames` | The host names it answers on. Without any, it gets one of its own under `cvyuh.com`. See [Host names](/platform/realms/hostnames/) |
| `password`, `mailId` | The password and email address of its first administrator |

The answer gives `id`, `name`, `path`, `parentPath`, `status`, `hostnames`, `rev`, `createTime` and `products`;
while it's being made or once it has failed, `progress`, `code` and `reason`; and for a deleted realm in the list,
`deleteTime` and `purged`.

## Related

- [Get started with Platform](/platform/get-started/): a team's realm, end to end.
- [Delegated administration](/platform/realms/delegated-administration/): who may administer a realm and the realms
  beneath it.
- [Host names](/platform/realms/hostnames/): the host names a realm answers on.
- [Retention and clean-up](/platform/data/retention/): what is kept, and for how long.
