-
Notifications
You must be signed in to change notification settings - Fork 24
HYPERFLEET-1154 - feat: Adapter status endpoints for generic resources #284
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
Open
tirthct
wants to merge
7
commits into
openshift-hyperfleet:main
Choose a base branch
from
tirthct:hyperfleet-1154
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
404ee0b
HYPERFLEET-1154 - feat: Adapter status endpoints for generic resources
tirthct 563fe73
HYPERFLEET-1154 - feat: fix linter error
tirthct e799cd2
HYPERFLEET-1154 - feat: fix linter error and create a private function
tirthct 373295a
HYPERFLEET-1154 - feat: add resource status handlers and routes
tirthct a6e8304
HYPERFLEET-1154 - feat: removed guard
tirthct 1fcc39a
HYPERFLEET-1154 - feat: fix linter
tirthct 180d94a
HYPERFLEET-1154 - feat: commonize by owner methods
tirthct 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
|
|
||
| "github.com/gorilla/mux" | ||
|
|
||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/api/openapi" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/api/presenters" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/errors" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/logger" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/registry" | ||
| "github.com/openshift-hyperfleet/hyperfleet-api/pkg/services" | ||
| ) | ||
|
|
||
| // ResourceStatusHandler handles GET/PUT on /{plural}/{id}/statuses for | ||
| // config-driven generic resources. Each method branches on whether "parent_id" | ||
| // is present in mux.Vars(r) to handle both flat and nested routes — matching | ||
| // the pattern established by ResourceHandler in HYPERFLEET-1157. | ||
| type ResourceStatusHandler struct { | ||
| resourceService services.ResourceService | ||
| adapterStatusService services.AdapterStatusService | ||
| descriptor registry.EntityDescriptor | ||
| } | ||
|
|
||
| func NewResourceStatusHandler( | ||
| descriptor registry.EntityDescriptor, | ||
| resourceService services.ResourceService, | ||
| adapterStatusService services.AdapterStatusService, | ||
| ) *ResourceStatusHandler { | ||
| return &ResourceStatusHandler{ | ||
| descriptor: descriptor, | ||
| resourceService: resourceService, | ||
| adapterStatusService: adapterStatusService, | ||
| } | ||
| } | ||
|
|
||
| // List returns all adapter statuses for a resource with pagination. | ||
| // Verifies ownership when parent_id is present in the route. | ||
| func (h *ResourceStatusHandler) List(w http.ResponseWriter, r *http.Request) { | ||
| cfg := &handlerConfig{ | ||
| Action: func() (interface{}, *errors.ServiceError) { | ||
| ctx := r.Context() | ||
| id := mux.Vars(r)["id"] | ||
| listArgs, err := services.NewListArguments(r.URL.Query()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if svcErr := h.verifyResource(r, id); svcErr != nil { | ||
| return nil, svcErr | ||
| } | ||
|
|
||
| return h.listStatuses(ctx, id, listArgs) | ||
| }, | ||
| ErrorHandler: handleError, | ||
| } | ||
|
|
||
| handleList(w, r, cfg) | ||
| } | ||
|
|
||
| // Create creates or updates an adapter status for a resource. | ||
| // Verifies ownership when parent_id is present in the route. | ||
| func (h *ResourceStatusHandler) Create(w http.ResponseWriter, r *http.Request) { | ||
| var req openapi.AdapterStatusCreateRequest | ||
|
|
||
| cfg := &handlerConfig{ | ||
| MarshalInto: &req, | ||
| Validate: []validate{ | ||
| validateNotEmpty(&req, "Adapter", "adapter"), | ||
| validateObservedGeneration(&req), | ||
| validateConditions(&req, "Conditions"), | ||
| validateObservedTimeRange(&req.ObservedTime), | ||
| }, | ||
| Action: func() (interface{}, *errors.ServiceError) { | ||
| ctx := r.Context() | ||
| id := mux.Vars(r)["id"] | ||
|
|
||
| if svcErr := h.verifyResource(r, id); svcErr != nil { | ||
| return nil, svcErr | ||
| } | ||
|
|
||
| return h.processStatus(ctx, id, &req) | ||
| }, | ||
| ErrorHandler: handleError, | ||
| } | ||
|
|
||
| handleCreateWithNoContent(w, r, cfg) | ||
| } | ||
|
|
||
| // verifyResource confirms the resource exists. For nested routes (parent_id | ||
| // present), also verifies the resource belongs to the parent. No-op ownership | ||
| // check for flat routes. | ||
| func (h *ResourceStatusHandler) verifyResource(r *http.Request, id string) *errors.ServiceError { | ||
| ctx := r.Context() | ||
| if parentID, hasParent := mux.Vars(r)["parent_id"]; hasParent { | ||
| if _, err := h.resourceService.GetByOwner(ctx, h.descriptor.Kind, id, parentID); err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| if _, err := h.resourceService.Get(ctx, h.descriptor.Kind, id); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // listStatuses fetches paginated adapter statuses and presents them as an OpenAPI response. | ||
| func (h *ResourceStatusHandler) listStatuses( | ||
| ctx context.Context, resourceID string, listArgs *services.ListArguments, | ||
| ) (interface{}, *errors.ServiceError) { | ||
| adapterStatuses, total, err := h.adapterStatusService.FindByResourcePaginated( | ||
| ctx, h.descriptor.Kind, resourceID, listArgs, | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| items := make([]openapi.AdapterStatus, 0, len(adapterStatuses)) | ||
| for _, as := range adapterStatuses { | ||
| presented, presErr := presenters.PresentAdapterStatus(as) | ||
| if presErr != nil { | ||
| logger.WithError(ctx, presErr).Error("Failed to present adapter status") | ||
| return nil, errors.GeneralError("Failed to present adapter status") | ||
| } | ||
| items = append(items, presented) | ||
| } | ||
|
|
||
| return openapi.AdapterStatusList{ | ||
| Items: items, | ||
| Page: int32(listArgs.Page), | ||
| Size: int32(len(items)), | ||
| Total: int32(total), | ||
| }, nil | ||
| } | ||
|
|
||
| // processStatus converts the request, delegates to ProcessAdapterStatus, and presents the result. | ||
| // Returns (nil, nil) when the status was silently discarded (triggers 204 No Content). | ||
| func (h *ResourceStatusHandler) processStatus( | ||
| ctx context.Context, resourceID string, req *openapi.AdapterStatusCreateRequest, | ||
| ) (interface{}, *errors.ServiceError) { | ||
| newStatus, convErr := presenters.ConvertAdapterStatus(h.descriptor.Kind, resourceID, req) | ||
| if convErr != nil { | ||
| logger.WithError(ctx, convErr).Error("Failed to convert adapter status") | ||
| return nil, errors.GeneralError("Failed to convert adapter status") | ||
| } | ||
|
|
||
| adapterStatus, err := h.resourceService.ProcessAdapterStatus( | ||
| ctx, h.descriptor.Kind, resourceID, newStatus, | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if adapterStatus == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| status, presErr := presenters.PresentAdapterStatus(adapterStatus) | ||
| if presErr != nil { | ||
| logger.WithError(ctx, presErr).Error("Failed to present adapter status") | ||
| return nil, errors.GeneralError("Failed to present adapter status") | ||
| } | ||
| return &status, nil | ||
| } | ||
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.