-
Notifications
You must be signed in to change notification settings - Fork 0
Add instance registry with health polling and fleet dashboard #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
victor-cuevas
merged 6 commits into
feat/admin-portal
from
feat/admin-instance-registry
Mar 17, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
86dd947
feat(admin): add instance registry, health poller, and fleet dashboard
victor-cuevas 8413c95
feat(admin): add welcome screen and confirm dialog
victor-cuevas dd2762d
fix(admin): address review findings for instance registry
victor-cuevas 011864b
refactor(admin): extract UI logic into testable units
victor-cuevas 80f06cf
fix(admin): resolve lint issues and switch to single quotes
victor-cuevas e72fea1
chore(admin): update go version to match other modules
victor-cuevas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| // Copyright 2026 CloudBlue LLC | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| "net" | ||
| "net/http" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/cloudblue/chaperone/admin/poller" | ||
| "github.com/cloudblue/chaperone/admin/store" | ||
| ) | ||
|
|
||
| // InstanceHandler handles instance CRUD and test-connection endpoints. | ||
| type InstanceHandler struct { | ||
| store *store.Store | ||
| client *http.Client | ||
| } | ||
|
|
||
| // NewInstanceHandler creates a handler with the given store and probe timeout. | ||
| func NewInstanceHandler(st *store.Store, probeTimeout time.Duration) *InstanceHandler { | ||
| return &InstanceHandler{ | ||
| store: st, | ||
| client: &http.Client{Timeout: probeTimeout}, | ||
| } | ||
| } | ||
|
|
||
| // Register mounts instance routes on the given mux. | ||
| func (h *InstanceHandler) Register(mux *http.ServeMux) { | ||
| mux.HandleFunc("GET /api/instances", h.list) | ||
| mux.HandleFunc("POST /api/instances", h.create) | ||
| mux.HandleFunc("POST /api/instances/test", h.testConnection) | ||
| mux.HandleFunc("GET /api/instances/{id}", h.get) | ||
| mux.HandleFunc("PUT /api/instances/{id}", h.update) | ||
| mux.HandleFunc("DELETE /api/instances/{id}", h.delete) | ||
| } | ||
|
|
||
| func (h *InstanceHandler) list(w http.ResponseWriter, r *http.Request) { | ||
| instances, err := h.store.ListInstances(r.Context()) | ||
| if err != nil { | ||
| slog.Error("listing instances", "error", err) | ||
| respondError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to list instances") | ||
| return | ||
| } | ||
| if instances == nil { | ||
| instances = []store.Instance{} | ||
| } | ||
| respondJSON(w, http.StatusOK, instances) | ||
| } | ||
|
|
||
| func (h *InstanceHandler) get(w http.ResponseWriter, r *http.Request) { | ||
| id, ok := parseID(w, r) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| inst, err := h.store.GetInstance(r.Context(), id) | ||
| if errors.Is(err, store.ErrInstanceNotFound) { | ||
| respondError(w, http.StatusNotFound, "INSTANCE_NOT_FOUND", fmt.Sprintf("No instance with ID %d", id)) | ||
| return | ||
| } | ||
| if err != nil { | ||
| slog.Error("getting instance", "id", id, "error", err) | ||
| respondError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to get instance") | ||
| return | ||
| } | ||
| respondJSON(w, http.StatusOK, inst) | ||
| } | ||
|
|
||
| type instanceRequest struct { | ||
| Name string `json:"name"` | ||
| Address string `json:"address"` | ||
| } | ||
|
|
||
| func (h *InstanceHandler) create(w http.ResponseWriter, r *http.Request) { | ||
| var req instanceRequest | ||
| if !decodeJSON(w, r, &req) { | ||
| return | ||
| } | ||
| if !validateInstanceRequest(w, &req) { | ||
| return | ||
| } | ||
|
|
||
| inst, err := h.store.CreateInstance(r.Context(), req.Name, req.Address) | ||
| if errors.Is(err, store.ErrDuplicateAddress) { | ||
| respondError(w, http.StatusConflict, "DUPLICATE_ADDRESS", | ||
| fmt.Sprintf("An instance with address %q is already registered", req.Address)) | ||
| return | ||
| } | ||
| if err != nil { | ||
| slog.Error("creating instance", "error", err) | ||
| respondError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to create instance") | ||
| return | ||
| } | ||
| respondJSON(w, http.StatusCreated, inst) | ||
| } | ||
|
|
||
| func (h *InstanceHandler) update(w http.ResponseWriter, r *http.Request) { | ||
| id, ok := parseID(w, r) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| var req instanceRequest | ||
| if !decodeJSON(w, r, &req) { | ||
| return | ||
| } | ||
| if !validateInstanceRequest(w, &req) { | ||
| return | ||
| } | ||
|
|
||
| inst, err := h.store.UpdateInstance(r.Context(), id, req.Name, req.Address) | ||
| if errors.Is(err, store.ErrInstanceNotFound) { | ||
| respondError(w, http.StatusNotFound, "INSTANCE_NOT_FOUND", fmt.Sprintf("No instance with ID %d", id)) | ||
| return | ||
| } | ||
| if errors.Is(err, store.ErrDuplicateAddress) { | ||
| respondError(w, http.StatusConflict, "DUPLICATE_ADDRESS", | ||
| fmt.Sprintf("An instance with address %q is already registered", req.Address)) | ||
| return | ||
| } | ||
| if err != nil { | ||
| slog.Error("updating instance", "id", id, "error", err) | ||
| respondError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to update instance") | ||
| return | ||
| } | ||
| respondJSON(w, http.StatusOK, inst) | ||
| } | ||
|
|
||
| func (h *InstanceHandler) delete(w http.ResponseWriter, r *http.Request) { | ||
| id, ok := parseID(w, r) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| err := h.store.DeleteInstance(r.Context(), id) | ||
| if errors.Is(err, store.ErrInstanceNotFound) { | ||
| respondError(w, http.StatusNotFound, "INSTANCE_NOT_FOUND", fmt.Sprintf("No instance with ID %d", id)) | ||
| return | ||
| } | ||
| if err != nil { | ||
| slog.Error("deleting instance", "id", id, "error", err) | ||
| respondError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to delete instance") | ||
| return | ||
| } | ||
| w.WriteHeader(http.StatusNoContent) | ||
| } | ||
|
|
||
| func (h *InstanceHandler) testConnection(w http.ResponseWriter, r *http.Request) { | ||
| var req struct { | ||
| Address string `json:"address"` | ||
| } | ||
| if !decodeJSON(w, r, &req) { | ||
| return | ||
| } | ||
|
|
||
| addr := strings.TrimSpace(req.Address) | ||
| if addr == "" { | ||
| respondError(w, http.StatusBadRequest, "VALIDATION_ERROR", "address is required") | ||
| return | ||
| } | ||
| if err := validHostPort(addr); err != nil { | ||
| respondError(w, http.StatusBadRequest, "VALIDATION_ERROR", err.Error()) | ||
| return | ||
| } | ||
|
|
||
| result := poller.Probe(r.Context(), h.client, addr) | ||
| respondJSON(w, http.StatusOK, result) | ||
| } | ||
|
|
||
| // parseID extracts and validates the {id} path parameter. | ||
| func parseID(w http.ResponseWriter, r *http.Request) (int64, bool) { | ||
| raw := r.PathValue("id") | ||
| id, err := strconv.ParseInt(raw, 10, 64) | ||
| if err != nil || id <= 0 { | ||
| respondError(w, http.StatusBadRequest, "VALIDATION_ERROR", fmt.Sprintf("Invalid instance ID: %q", raw)) | ||
| return 0, false | ||
| } | ||
| return id, true | ||
| } | ||
|
|
||
| // decodeJSON reads and decodes a JSON request body (max 1 MB). | ||
| func decodeJSON(w http.ResponseWriter, r *http.Request, dst any) bool { | ||
| r.Body = http.MaxBytesReader(w, r.Body, 1<<20) | ||
| if err := json.NewDecoder(r.Body).Decode(dst); err != nil { | ||
| respondError(w, http.StatusBadRequest, "VALIDATION_ERROR", "Invalid JSON request body") | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func validateInstanceRequest(w http.ResponseWriter, req *instanceRequest) bool { | ||
| req.Name = strings.TrimSpace(req.Name) | ||
| req.Address = strings.TrimSpace(req.Address) | ||
|
|
||
| if req.Name == "" { | ||
| respondError(w, http.StatusBadRequest, "VALIDATION_ERROR", "name is required") | ||
| return false | ||
| } | ||
| if req.Address == "" { | ||
| respondError(w, http.StatusBadRequest, "VALIDATION_ERROR", "address is required") | ||
| return false | ||
| } | ||
| if err := validHostPort(req.Address); err != nil { | ||
| respondError(w, http.StatusBadRequest, "VALIDATION_ERROR", err.Error()) | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| var errInvalidHostPort = errors.New("address must be a valid host:port (e.g. 192.168.1.10:9090)") | ||
|
|
||
| func validHostPort(addr string) error { | ||
| host, portStr, err := net.SplitHostPort(addr) | ||
| if err != nil { | ||
| return errInvalidHostPort | ||
| } | ||
| if host == "" { | ||
| return errInvalidHostPort | ||
| } | ||
| port, err := strconv.ParseUint(portStr, 10, 16) | ||
| if err != nil || port == 0 { | ||
| return errInvalidHostPort | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.