Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/reference/endpoints.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
type: reference
audience: developer calling the API
verified: 2026-11-08
verified: 2026-11-14
---

# Endpoints
Expand All @@ -16,7 +16,7 @@ verified: 2026-11-08
| `POST /v1/tenants`, `DELETE /v1/tenants/{t}` | Create a tenant for yourself (one name, and every tenant has the same limits), and delete it (its admins, the owner among them) |
| `GET`, `POST /v1/tenants/{t}/images`, `GET`, `DELETE /v1/tenants/{t}/images/{name}` | The images of a tenant: read with a role or a key tuple (`images:read`), write with `operator` or `images:write` |
| `GET /v1/tenants/{t}/quota` | What a tenant may hold (instances, vCPU, memory, disk, networks, profiles; the same for every tenant) and what it holds. Read with the right of the instances |
| `GET`, `POST /v1/tenants/{t}/profiles`, `GET`, `DELETE /v1/tenants/{t}/profiles/{name}` | The sizes an instance can take: the profiles `s`, `m` and `l` every tenant is created with, and the ones its users make. Read with any role or `profiles:read`, write with `operator` or `profiles:write` |
| `GET`, `POST /v1/tenants/{t}/profiles`, `GET`, `PATCH`, `DELETE /v1/tenants/{t}/profiles/{name}` | The sizes an instance can take: the profiles `s`, `m` and `l` every tenant is created with, and the ones its users make. Read with any role or `profiles:read`, write with `operator` or `profiles:write` |
| `GET`, `POST /v1/tenants/{t}/networks`, `GET`, `PATCH`, `DELETE /v1/tenants/{t}/networks/{name}` | The networks of a tenant, which comes without one: read with a role (`viewer` and above) or `networks:read`, write with `operator` or `networks:write` |
| `GET`, `POST /v1/tenants/{t}/memberships`, `GET`, `DELETE /v1/tenants/{t}/memberships/{name}` | The roles of the users: admins of the tenant, and the keys with `memberships:read` and `memberships:write` on it |
| `GET /v1/apikeys/scopes` | The scopes a key can be given, `<resource>:<verb>`, with what each does. For a person, like the other routes of the keys |
Expand Down
5 changes: 3 additions & 2 deletions docs/reference/images-and-instances.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
type: reference
audience: developer or operator who works with images and virtual machines through the API
verified: 2026-11-11
verified: 2026-11-14
---

# Images and instances
Expand All @@ -20,4 +20,5 @@ Both live in the namespace `t-<tenant>` of the `phorge.fr` group.
- **The size of an instance is a profile or its own values, never both.** `profile` is one of `GET /v1/tenants/{t}/profiles` (`s` 1 vCPU, 2 GiB, 64 GiB; `m` 2, 4, 128; `l` 4, 8, 256); the other way is `cpu`, `memoryMiB` and `diskGiB`, and what a request leaves out takes 1, 1024 and 10 (the Crossplane kind has no defaults, the API completes the size). A profile with a value is `422`, so is a profile that does not exist, and the answer lists the profiles. The body of an instance carries its `profile` and, in every case, the `cpu`, `memoryMiB` and `diskGiB` it comes to. `GET /v1/limits` has the bounds.
- **A size changes in place**: `PATCH` with a `profile` moves the instance to it and removes its own values; an instance that has a profile leaves it by being given the three values together (a value alone is `422`, it would sit next to the profile). Incus applies it live on a stopped instance.
- **The resources of every tenant hold two instances of the profile `s`, or one `m`**: 2 instances, 2 vCPU, 4 GiB of memory and 128 GiB of disk, the same for every tenant. An instance that asks for more than what is left is refused with the limit it passes, before Incus is asked (see [Limits and quotas](limits-and-quotas.md)).
- **A user makes their own profiles** (`POST /v1/tenants/{t}/profiles`): CPU, memory and disk, all three given, under a name of 1 to 32 characters (`s`, `m`, `l` and `default` are taken, `422`). They are listed after the three every tenant has, with a phase and what is wrong in words. An instance takes one as it takes `s`, `m` or `l`, and counts in the quota what it comes to. Deleting one is refused (`409`, `in_use`) while an instance has it, and names the instances; `s`, `m` and `l` are not changed nor deleted (`403`). A tenant holds at most 2 of them (the same for every tenant, a third is `409`, `quota_exceeded`, `this tenant has 2 profiles out of 2 allowed (s, m and l do not count): delete one first`). A profile may also say two settings of an instance, `secureBoot` and `sshPublicKeys` (up to 20, checked as the keys of an instance, see the item on SSH keys). They are written on the object only when the request gives them: a profile that does not say one has none, and the body shows none, so an absent `secureBoot` is not `false`. An empty list is the same as none. An instance takes the settings of its profile unless it says its own: keys given by the instance replace the profile's, they do not add to them. A profile never has a network nor `nesting` (Incus refuses nesting on a profile of a tenant project, so nesting stays a setting of the instance): a request with either is `400`, an unknown field. Changing a profile in place is not in the API: make another one and move the instances to it.
- **A user makes their own profiles** (`POST /v1/tenants/{t}/profiles`): CPU, memory and disk, all three given, under a name of 1 to 32 characters (`s`, `m`, `l` and `default` are taken, `422`). They are listed after the three every tenant has, with a phase and what is wrong in words. An instance takes one as it takes `s`, `m` or `l`, and counts in the quota what it comes to. Deleting one is refused (`409`, `in_use`) while an instance has it, and names the instances; `s`, `m` and `l` are not changed nor deleted (`403`). A tenant holds at most 2 of them (the same for every tenant, a third is `409`, `quota_exceeded`, `this tenant has 2 profiles out of 2 allowed (s, m and l do not count): delete one first`). A profile may also say two settings of an instance, `secureBoot` and `sshPublicKeys` (up to 20, checked as the keys of an instance, see the item on SSH keys). They are written on the object only when the request gives them: a profile that does not say one has none, and the body shows none, so an absent `secureBoot` is not `false`. An empty list is the same as none. An instance takes the settings of its profile unless it says its own: keys given by the instance replace the profile's, they do not add to them. A profile never has a network nor `nesting` (Incus refuses nesting on a profile of a tenant project, so nesting stays a setting of the instance): a request with either is `400`, an unknown field. A profile is changed in place, see the next item.
- **A profile is changed in place** (`PATCH /v1/tenants/{t}/profiles/{name}`): `description`, `cpu`, `memoryMiB`, `diskGiB`, `secureBoot` and `sshPublicKeys`, with the bounds and the checks of the creation; what the body leaves out is left alone, and `s`, `m` and `l` are `403`. An empty `description` goes back to the one made from the size, `secureBoot: null` removes the setting and `sshPublicKeys: []` removes the keys. The instances that have the profile follow the change, the ones that say their own value do not. Measured on the platform with a running VM: a higher memory is seen by the guest at once, a higher CPU or disk is applied by Incus and the guest sees it after a restart, and a change never starts, stops or restarts an instance. What cannot be done in place is refused with `409` and a sentence that says what to do, before anything is sent: **a smaller disk** while an instance has the profile (`in_use`, naming them: Incus saves the profile and the VM keeps its disk, "Block volumes cannot be shrunk", so the two differ; make another profile and create the instances again with it); **less memory** while an instance that has it is running (`running`, as for an instance: stop it, change the profile, start it again); **a size that takes the tenant over a limit** (`quota_exceeded`: every instance that has the profile counts what it comes to, and Incus refuses it and leaves the Profile in error, "Reached maximum aggregate value"). A smaller CPU, or a smaller memory while the instances are stopped, is accepted.
30 changes: 30 additions & 0 deletions internal/api/compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ type profileStore struct {
data map[string]map[string]compute.Profile
err error
created []compute.CreateProfile
patched []compute.PatchProfile
deleted []string
}

Expand Down Expand Up @@ -221,6 +222,35 @@ func (s *profileStore) Create(_ context.Context, tenant string, r compute.Create
return p, nil
}

// Patch applies what the patch gives, as the merge patch of the real store does.
func (s *profileStore) Patch(_ context.Context, tenant, name string, r compute.PatchProfile) (compute.Profile, error) {
p, err := s.Get(context.Background(), tenant, name)
if err != nil {
return p, err
}
s.patched = append(s.patched, r)
if r.Description != nil {
p.Description = *r.Description
}
if r.CPU != nil {
p.CPU = *r.CPU
}
if r.MemoryMiB != nil {
p.MemoryMiB = *r.MemoryMiB
}
if r.DiskGiB != nil {
p.DiskGiB = *r.DiskGiB
}
if r.SecureBoot.Present {
p.SecureBoot = r.SecureBoot.Value
}
if r.SSHPublicKeys != nil {
p.SSHPublicKeys = *r.SSHPublicKeys
}
s.put(tenant, p)
return p, nil
}

func (s *profileStore) Delete(_ context.Context, tenant, name string) error {
p, err := s.Get(context.Background(), tenant, name)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/api/keyscopes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAKeyWithTheScopesOfTheNetworksTheProfilesAndTheTenantPassesTheirRoutes(
// the profiles
{"GET", "/v1/tenants/demo/profiles", "", http.StatusOK},
{"POST", "/v1/tenants/demo/profiles", `{"name":"api","cpu":1,"memoryMiB":1024,"diskGiB":10}`, http.StatusAccepted},
{"PATCH", "/v1/tenants/demo/profiles/api", `{"cpu":2}`, http.StatusOK},
{"DELETE", "/v1/tenants/demo/profiles/api", "", http.StatusAccepted},
// what the scopes do not give
{"GET", "/v1/tenants/demo/instances", "", http.StatusForbidden},
Expand Down
118 changes: 118 additions & 0 deletions internal/api/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"slices"
"strings"
"time"

"github.com/phorge-fr/phorge-api/internal/auth"
Expand All @@ -30,6 +31,7 @@ func (p *profileRoutes) register(s *server.Server, authn func(http.Handler) http
s.Handle("GET "+collection, authn(p.guard(authz.ReadProfiles, http.HandlerFunc(p.list))))
s.Handle("POST "+collection, authn(p.guard(authz.WriteProfiles, http.HandlerFunc(p.create))))
s.Handle("GET "+item, authn(p.guard(authz.ReadProfiles, http.HandlerFunc(p.get))))
s.Handle("PATCH "+item, authn(p.guard(authz.WriteProfiles, http.HandlerFunc(p.patch))))
s.Handle("DELETE "+item, authn(p.guard(authz.WriteProfiles, http.HandlerFunc(p.delete))))
}

Expand Down Expand Up @@ -138,6 +140,122 @@ func (p *profileRoutes) create(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusAccepted, p.body(pr))
}

// patch changes a profile a user made, in place: the instances that have it follow, the ones that say their own value do not.
// Incus applies a higher memory, a higher disk and any CPU to a running VM, and the instances are never stopped or started by it: what
// Incus would refuse, or leave half done, is refused here with what to do (measured on the platform, see the reference page).
func (p *profileRoutes) patch(w http.ResponseWriter, r *http.Request) {
tenant, name := r.PathValue("t"), r.PathValue("name")
if compute.Taken(name) {
httperr.Write(w, http.StatusForbidden, "forbidden", "the profile "+name+" is one every tenant has: it is not changed nor deleted")
return
}
if !compute.ValidNewProfileName(name) {
httperr.Write(w, http.StatusNotFound, "not_found", "no such profile")
return
}
var req compute.PatchProfile
if !decode(w, r, &req) {
return
}
if err := req.Validate(); err != nil {
p.storeError(w, err, "profile")
return
}
current, err := p.store.Get(r.Context(), tenant, name)
if err != nil {
p.storeError(w, err, "profile")
return
}
instances, err := p.instances.List(r.Context(), tenant)
if err != nil {
p.storeError(w, err, "instance")
return
}
if refused := p.refuseWhileInUse(w, current, req, instances); refused {
return
}
// Bigger, the profile makes the tenant bigger: every instance that has it counts what it comes to. Incus refuses a profile that
// passes a limit of the project and leaves the Profile in error until it is put back, so it is said here.
if req.CPU != nil || req.MemoryMiB != nil || req.DiskGiB != nil {
made, err := p.store.List(r.Context(), tenant)
if err != nil {
p.storeError(w, err, "profile")
return
}
cpu, mem, disk := orElse(req.CPU, current.CPU), orElse(req.MemoryMiB, current.MemoryMiB), orElse(req.DiskGiB, current.DiskGiB)
if e := quota.CheckProfileSize(instances, made, name, cpu, mem, disk); e != nil {
quotaExceeded(w, e)
return
}
}

pr, err := p.store.Patch(r.Context(), tenant, name, req)
if err != nil {
p.storeError(w, err, "profile")
return
}
id, _ := auth.FromContext(r.Context())
p.log.Info("profile changed", "tenant", tenant, "profile", name, "by", id.Subject)
writeJSON(w, http.StatusOK, p.body(pr))
}

// refuseWhileInUse answers, and reports true, when the patch asks for what a profile that instances have cannot do: a smaller disk (Incus
// saves the profile and the VM keeps its disk: "Block volumes cannot be shrunk"), or less memory while a VM is running, as for an
// instance (the API never stops one). It does not say who may change what: the route already did.
func (p *profileRoutes) refuseWhileInUse(w http.ResponseWriter, current compute.Profile, req compute.PatchProfile, instances []compute.Instance) bool {
var users, running []string
for _, in := range instances {
if in.Profile != current.Name {
continue
}
users = append(users, in.Name)
if in.State == "Running" {
running = append(running, in.Name)
}
}
slices.Sort(users)
slices.Sort(running)
switch {
case len(users) > 0 && req.DiskGiB != nil && *req.DiskGiB < current.DiskGiB:
httperr.Write(w, http.StatusConflict, "in_use", "a disk cannot be made smaller: "+listOf(users)+" "+haveOrHas(users)+" this profile. "+
"Make another profile with the disk you want and create the instances again with it")
return true
case len(running) > 0 && req.MemoryMiB != nil && *req.MemoryMiB < current.MemoryMiB:
httperr.Write(w, http.StatusConflict, "running", "the memory of a running instance can only be raised: to lower it, stop "+listOf(running)+
", change the profile, then start "+themOrIt(running)+" again")
return true
}
return false
}

func orElse(v *int, otherwise int) int {
if v != nil {
return *v
}
return otherwise
}

func listOf(names []string) string {
if len(names) == 1 {
return "the instance " + names[0]
}
return "the instances " + strings.Join(names, ", ")
}

func haveOrHas(names []string) string {
if len(names) == 1 {
return "has"
}
return "have"
}

func themOrIt(names []string) string {
if len(names) == 1 {
return "it"
}
return "them"
}

func (p *profileRoutes) delete(w http.ResponseWriter, r *http.Request) {
tenant, name := r.PathValue("t"), r.PathValue("name")
if compute.Taken(name) {
Expand Down
Loading
Loading