| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/release" |
| 8 | ) |
| 9 | |
| 10 | // The ship door's swap-demand sentences live here, not in apicli.go: the |
| 11 | // moves copy gate pins the jsonError text of that file. |
| 12 | const ( |
| 13 | apiShipSwapBad = `swap must be "hold" or left out` |
| 14 | apiShipSwapConflict = "version %s was already shipped with a different swap demand. Versions are immutable: ship under a new version" |
| 15 | ) |
| 16 | |
| 17 | // refuseBadSwap answers 400 and reports true when the meta part carries a |
| 18 | // swap demand other than "hold" (zero-downtime spec §4.2). |
| 19 | func refuseBadSwap(w http.ResponseWriter, swap string) bool { |
| 20 | if swap != "" && swap != release.DemandHold { |
| 21 | jsonError(w, http.StatusBadRequest, apiShipSwapBad) |
| 22 | return true |
| 23 | } |
| 24 | return false |
| 25 | } |
| 26 | |
| 27 | // refuseSwapConflict answers 409 and reports true when a re-ship of an |
| 28 | // existing version adds or drops the demand. The demand decides how the bytes |
| 29 | // are swapped in, so such a re-ship is a different release: the capability |
| 30 | // block's rule. |
| 31 | func refuseSwapConflict(w http.ResponseWriter, stored, sent, ver string) bool { |
| 32 | if stored != sent { |
| 33 | jsonError(w, http.StatusConflict, fmt.Sprintf(apiShipSwapConflict, ver)) |
| 34 | return true |
| 35 | } |
| 36 | return false |
| 37 | } |
| 38 | |
| 39 | // addSwapEcho adds the release's own swap demand to a ship response |
| 40 | // (zero-downtime spec §4.2), closing the gap addCapabilitiesEcho closes: a |
| 41 | // console from before the field ignores it and stores a release that demands |
| 42 | // nothing, which would swap in overlap; a client that sent a demand refuses |
| 43 | // an answer without its echo. |
| 44 | func addSwapEcho(body map[string]any, swap string) { |
| 45 | if swap != "" { |
| 46 | body["swap"] = swap |
| 47 | } |
| 48 | } |
| 49 | |