-
Notifications
You must be signed in to change notification settings - Fork 1
FEATURE: Flow Plugin #75
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
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c04847b
Add Flow plugin and entity kind
Go2Engle a0b54f4
Refine Flow two-way edge rendering
Go2Engle 92a7a17
Align bidirectional flow arrowheads
Go2Engle f234b58
Add Flow entity support across catalog and detail views
Go2Engle 49767e4
Refine Flow mock shapes and dynamic sizing
Go2Engle 006fa09
Add resizable flow mock nodes and pointer zoom
Go2Engle 3bc7e9e
Refine Flow mockups and route loading
Go2Engle b356a8e
Enforce Flow permissions and theme flow UI
Go2Engle 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,305 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/go-chi/chi/v5" | ||
| "github.com/go2engle/gantry/internal/api/middleware" | ||
| "github.com/go2engle/gantry/internal/auth" | ||
| "github.com/go2engle/gantry/internal/db" | ||
| "github.com/go2engle/gantry/internal/entity" | ||
| "github.com/go2engle/gantry/internal/events" | ||
| "github.com/go2engle/gantry/internal/plugins" | ||
| ) | ||
|
|
||
| type flowSettingsResponse struct { | ||
| ShowInSidebar bool `json:"showInSidebar"` | ||
| EditorRole string `json:"editorRole"` | ||
| CanEdit bool `json:"canEdit"` | ||
| } | ||
|
|
||
| func flowEditorRole(config map[string]any) string { | ||
| role, _ := config["editorRole"].(string) | ||
| role = strings.TrimSpace(role) | ||
| if role == "" || !auth.IsValidRole(role) { | ||
| return "developer" | ||
| } | ||
| return role | ||
| } | ||
|
|
||
| func flowShowInSidebar(config map[string]any) bool { | ||
| show, ok := config["showInSidebar"].(bool) | ||
| if !ok { | ||
| return true | ||
| } | ||
| return show | ||
| } | ||
|
|
||
| func (h *Handlers) getFlowPlugin(r *http.Request) (*plugins.Plugin, error) { | ||
| plugin, err := h.DB.GetPlugin(r.Context(), "flow") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if plugin == nil { | ||
| return nil, entity.ErrEntityNotFound | ||
| } | ||
| return plugin, nil | ||
| } | ||
|
|
||
| func (h *Handlers) getFlowSettings(r *http.Request) (*plugins.Plugin, flowSettingsResponse, error) { | ||
| plugin, err := h.getFlowPlugin(r) | ||
| if err != nil { | ||
| return nil, flowSettingsResponse{}, err | ||
| } | ||
|
|
||
| editorRole := flowEditorRole(plugin.Config) | ||
| effectiveRole := middleware.GetEffectiveRole(r.Context()) | ||
| canEdit := auth.RoleLevel(effectiveRole) >= auth.RoleLevel(editorRole) | ||
|
|
||
| return plugin, flowSettingsResponse{ | ||
| ShowInSidebar: flowShowInSidebar(plugin.Config), | ||
| EditorRole: editorRole, | ||
| CanEdit: canEdit, | ||
| }, nil | ||
| } | ||
|
|
||
| func (h *Handlers) ensureFlowWriteAccess(w http.ResponseWriter, r *http.Request) bool { | ||
| plugin, settings, err := h.getFlowSettings(r) | ||
| if err != nil { | ||
| if errors.Is(err, entity.ErrEntityNotFound) { | ||
| writeError(w, http.StatusNotFound, "flow plugin not installed") | ||
| return false | ||
| } | ||
| writeError(w, http.StatusInternalServerError, "failed to load flow plugin") | ||
| return false | ||
| } | ||
| if !plugin.Enabled { | ||
| writeError(w, http.StatusBadRequest, "flow plugin is not enabled") | ||
| return false | ||
| } | ||
| if !settings.CanEdit { | ||
| writeError(w, http.StatusForbidden, "insufficient permissions to edit flows") | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // GetFlowSettings returns the non-sensitive Flow plugin settings needed by the UI. | ||
| func (h *Handlers) GetFlowSettings(w http.ResponseWriter, r *http.Request) { | ||
| plugin, settings, err := h.getFlowSettings(r) | ||
| if err != nil { | ||
| if errors.Is(err, entity.ErrEntityNotFound) { | ||
| writeError(w, http.StatusNotFound, "flow plugin not installed") | ||
| return | ||
| } | ||
| writeError(w, http.StatusInternalServerError, "failed to load flow settings") | ||
| return | ||
| } | ||
| if !plugin.Enabled { | ||
| writeError(w, http.StatusBadRequest, "flow plugin is not enabled") | ||
| return | ||
| } | ||
|
|
||
| writeJSON(w, http.StatusOK, settings) | ||
| } | ||
|
|
||
| // CreateFlowEntity handles POST /plugins/flow/entities. | ||
| func (h *Handlers) CreateFlowEntity(w http.ResponseWriter, r *http.Request) { | ||
| if !h.ensureFlowWriteAccess(w, r) { | ||
| return | ||
| } | ||
|
|
||
| var e entity.Entity | ||
| if err := json.NewDecoder(r.Body).Decode(&e); err != nil { | ||
| writeError(w, http.StatusBadRequest, "invalid request body") | ||
| return | ||
| } | ||
| if e.Kind != "" && e.Kind != "Flow" { | ||
| writeError(w, http.StatusBadRequest, "flow endpoint only accepts Flow entities") | ||
| return | ||
| } | ||
| e.Kind = "Flow" | ||
| e.SetDefaults() | ||
|
|
||
| claims := middleware.GetClaims(r.Context()) | ||
| if claims != nil { | ||
| e.Metadata.CreatedBy = claims.Username | ||
| } | ||
|
|
||
| if err := h.Validator.Validate(&e); err != nil { | ||
| writeError(w, http.StatusBadRequest, err.Error()) | ||
| return | ||
| } | ||
|
|
||
| if err := h.DB.CreateEntity(r.Context(), &e); err != nil { | ||
| if errors.Is(err, entity.ErrEntityAlreadyExists) { | ||
| writeError(w, http.StatusConflict, "entity already exists") | ||
| return | ||
| } | ||
| writeError(w, http.StatusInternalServerError, "failed to create flow") | ||
| return | ||
| } | ||
|
|
||
| h.Events.Publish(events.Event{ | ||
| Type: events.EntityCreated, | ||
| Data: map[string]any{ | ||
| "kind": e.Kind, | ||
| "name": e.Metadata.Name, | ||
| "namespace": e.Metadata.Namespace, | ||
| }, | ||
| }) | ||
|
|
||
| userName := "" | ||
| userID := "" | ||
| if claims != nil { | ||
| userName = claims.Username | ||
| userID = claims.UserID | ||
| } | ||
| h.DB.CreateAuditEntry(r.Context(), &db.AuditEntry{ | ||
| UserID: userID, | ||
| UserName: userName, | ||
| Action: "entity.created", | ||
| ResourceType: e.Kind, | ||
| ResourceName: e.Metadata.Name, | ||
| AfterState: marshalEntityState(&e), | ||
| Source: "api", | ||
| IPAddress: clientIP(r), | ||
| }) | ||
|
|
||
| writeJSON(w, http.StatusCreated, e) | ||
| } | ||
|
|
||
| // UpdateFlowEntity handles PUT /plugins/flow/entities/{name}. | ||
| func (h *Handlers) UpdateFlowEntity(w http.ResponseWriter, r *http.Request) { | ||
| if !h.ensureFlowWriteAccess(w, r) { | ||
| return | ||
| } | ||
|
|
||
| name := chi.URLParam(r, "name") | ||
| var e entity.Entity | ||
| if err := json.NewDecoder(r.Body).Decode(&e); err != nil { | ||
| writeError(w, http.StatusBadRequest, "invalid request body") | ||
| return | ||
| } | ||
|
|
||
| if e.Kind != "" && e.Kind != "Flow" { | ||
| writeError(w, http.StatusBadRequest, "flow endpoint only accepts Flow entities") | ||
| return | ||
| } | ||
| e.Kind = "Flow" | ||
| e.Metadata.Name = name | ||
| e.SetDefaults() | ||
|
|
||
| if err := h.Validator.Validate(&e); err != nil { | ||
| writeError(w, http.StatusBadRequest, err.Error()) | ||
| return | ||
| } | ||
|
|
||
| ns := e.Metadata.Namespace | ||
| if ns == "" { | ||
| ns = entity.DefaultNamespace | ||
| } | ||
|
|
||
| var beforeState string | ||
| if prev, err := h.DB.GetEntity(r.Context(), e.Kind, ns, e.Metadata.Name); err == nil { | ||
| beforeState = marshalEntityState(prev) | ||
| } | ||
|
|
||
| if err := h.DB.UpdateEntity(r.Context(), &e); err != nil { | ||
| if errors.Is(err, entity.ErrEntityNotFound) { | ||
| writeError(w, http.StatusNotFound, "entity not found") | ||
| return | ||
| } | ||
| writeError(w, http.StatusInternalServerError, "failed to update flow") | ||
| return | ||
| } | ||
|
|
||
| h.Events.Publish(events.Event{ | ||
| Type: events.EntityUpdated, | ||
| Data: map[string]any{ | ||
| "kind": e.Kind, | ||
| "name": e.Metadata.Name, | ||
| "namespace": e.Metadata.Namespace, | ||
| }, | ||
| }) | ||
|
|
||
| claims := middleware.GetClaims(r.Context()) | ||
| userName := "" | ||
| userID := "" | ||
| if claims != nil { | ||
| userName = claims.Username | ||
| userID = claims.UserID | ||
| } | ||
| h.DB.CreateAuditEntry(r.Context(), &db.AuditEntry{ | ||
| UserID: userID, | ||
| UserName: userName, | ||
| Action: "entity.updated", | ||
| ResourceType: e.Kind, | ||
| ResourceName: e.Metadata.Name, | ||
| BeforeState: beforeState, | ||
| AfterState: marshalEntityState(&e), | ||
| Source: "api", | ||
| IPAddress: clientIP(r), | ||
| }) | ||
|
|
||
| writeJSON(w, http.StatusOK, e) | ||
| } | ||
|
|
||
| // DeleteFlowEntity handles DELETE /plugins/flow/entities/{name}. | ||
| func (h *Handlers) DeleteFlowEntity(w http.ResponseWriter, r *http.Request) { | ||
| if !h.ensureFlowWriteAccess(w, r) { | ||
| return | ||
| } | ||
|
|
||
| name := chi.URLParam(r, "name") | ||
| namespace := r.URL.Query().Get("namespace") | ||
| if namespace == "" { | ||
| namespace = entity.DefaultNamespace | ||
| } | ||
|
|
||
| var beforeState string | ||
| if prev, err := h.DB.GetEntity(r.Context(), "Flow", namespace, name); err == nil { | ||
| beforeState = marshalEntityState(prev) | ||
| } | ||
|
|
||
| if err := h.DB.DeleteEntity(r.Context(), "Flow", namespace, name); err != nil { | ||
| if errors.Is(err, entity.ErrEntityNotFound) { | ||
| writeError(w, http.StatusNotFound, "entity not found") | ||
| return | ||
| } | ||
| writeError(w, http.StatusInternalServerError, "failed to delete flow") | ||
| return | ||
| } | ||
|
|
||
| h.Events.Publish(events.Event{ | ||
| Type: events.EntityDeleted, | ||
| Data: map[string]any{ | ||
| "kind": "Flow", | ||
| "name": name, | ||
| "namespace": namespace, | ||
| }, | ||
| }) | ||
|
|
||
| claims := middleware.GetClaims(r.Context()) | ||
| userName := "" | ||
| userID := "" | ||
| if claims != nil { | ||
| userName = claims.Username | ||
| userID = claims.UserID | ||
| } | ||
| h.DB.CreateAuditEntry(r.Context(), &db.AuditEntry{ | ||
| UserID: userID, | ||
| UserName: userName, | ||
| Action: "entity.deleted", | ||
| ResourceType: "Flow", | ||
| ResourceName: name, | ||
| BeforeState: beforeState, | ||
| Source: "api", | ||
| IPAddress: clientIP(r), | ||
| }) | ||
|
|
||
| w.WriteHeader(http.StatusNoContent) | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "strings" | ||
|
|
||
| "github.com/go-chi/chi/v5" | ||
| "github.com/go2engle/gantry/internal/auth" | ||
| "github.com/go2engle/gantry/internal/gitops" | ||
| "github.com/go2engle/gantry/internal/plugins" | ||
| argocd "github.com/go2engle/gantry/internal/plugins/argocd" | ||
|
|
@@ -190,6 +191,16 @@ func (h *Handlers) UpdatePluginConfig(w http.ResponseWriter, r *http.Request) { | |
| } | ||
|
|
||
| merged, _ := preserveSecretValues(existing.Config, config).(map[string]any) | ||
| if name == "flow" { | ||
| role, _ := merged["editorRole"].(string) | ||
| role = strings.TrimSpace(role) | ||
| if role == "" { | ||
| merged["editorRole"] = "developer" | ||
| } else if !auth.IsValidRole(role) { | ||
| writeError(w, http.StatusBadRequest, "invalid flow editor role") | ||
| return | ||
| } | ||
| } | ||
|
Comment on lines
+194
to
+205
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Persist the normalized Flow editor role. A value like 🐛 Proposed fix if name == "flow" {
role, _ := merged["editorRole"].(string)
role = strings.TrimSpace(role)
if role == "" {
merged["editorRole"] = "developer"
} else if !auth.IsValidRole(role) {
writeError(w, http.StatusBadRequest, "invalid flow editor role")
return
+ } else {
+ merged["editorRole"] = role
}
}🤖 Prompt for AI Agents |
||
| if err := h.DB.UpdatePluginConfig(r.Context(), name, merged); err != nil { | ||
| writeError(w, http.StatusInternalServerError, err.Error()) | ||
| return | ||
|
|
||
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
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.