Skip to content

Commit

Permalink
modify request's status handler
Browse files Browse the repository at this point in the history
Signed-off-by: arielsepton <[email protected]>
  • Loading branch information
arielsepton committed Nov 24, 2023
1 parent 0e943e3 commit 698e591
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
26 changes: 20 additions & 6 deletions internal/controller/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,21 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.Wrap(err, errFailedToCheckIfUpToDate)
}

statusHandler := statushandler.NewStatusHandler(ctx, cr, observeRequestDetails.Response, c.localKube, c.logger)
statusHandler := statushandler.NewStatusHandler(
ctx,
cr,
c.localKube,
observeRequestDetails.Response,
observeRequestDetails.ResponseError,
c.logger,
)

synced := observeRequestDetails.Synced
if synced {
statusHandler.ResetFailures()
}

err = statusHandler.SetRequestStatus(cr.Spec.ForProvider.Mappings, cr.Spec.ForProvider, observeRequestDetails.ResponseError)
err = statusHandler.SetRequestStatus(cr.Spec.ForProvider.Mappings, cr.Spec.ForProvider)
if err != nil {
return managed.ExternalObservation{}, err
}
Expand Down Expand Up @@ -185,10 +193,16 @@ func (c *external) deployAction(ctx context.Context, cr *v1alpha1.Request, metho
}

res, err := c.http.SendRequest(ctx, mapping.Method, requestDetails.Url, requestDetails.Body, requestDetails.Headers)
statusHandler := statushandler.NewStatusHandler(ctx, cr, res, c.localKube, c.logger)

// TODO (REL): perhaps err should be in inititate.
return statusHandler.SetRequestStatus(cr.Spec.ForProvider.Mappings, cr.Spec.ForProvider, err)
statusHandler := statushandler.NewStatusHandler(
ctx,
cr,
c.localKube,
res,
err,
c.logger,
)

return statusHandler.SetRequestStatus(cr.Spec.ForProvider.Mappings, cr.Spec.ForProvider)
}

func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) {
Expand Down
11 changes: 6 additions & 5 deletions internal/controller/request/statushandler/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

// RequestStatusHandler is the interface to interact with status setting for v1alpha1.Request
type RequestStatusHandler interface {
SetRequestStatus(mappings []v1alpha1.Mapping, forProvider v1alpha1.RequestParameters, err error) error
SetRequestStatus(mappings []v1alpha1.Mapping, forProvider v1alpha1.RequestParameters) error
ResetFailures()
}

Expand All @@ -26,16 +26,17 @@ type RequestStatusHandler interface {
type requestStatusHandler struct {
logger logging.Logger
resource *utils.RequestResource
httpError error
basicSetters *[]utils.SetRequestStatusFunc
}

// SetRequestStatus updates the current Request's status to reflect the details of the last HTTP request that occurred.
// It takes the context, the Request resource, the HTTP response, the mapping configuration, and any error that occurred
// during the HTTP request. The function sets the status fields such as StatusCode, Headers, Body, Method, and Cache,
// based on the outcome of the HTTP request and the presence of an error.
func (r *requestStatusHandler) SetRequestStatus(mappings []v1alpha1.Mapping, forProvider v1alpha1.RequestParameters, err error) error {
if err != nil {
return r.setErrorAndReturn(r.resource, err)
func (r *requestStatusHandler) SetRequestStatus(mappings []v1alpha1.Mapping, forProvider v1alpha1.RequestParameters) error {
if r.httpError != nil {
return r.setErrorAndReturn(r.resource, r.httpError)
}

if utils.IsHTTPError(r.resource.HttpResponse.StatusCode) {
Expand Down Expand Up @@ -91,7 +92,7 @@ func (r *requestStatusHandler) ResetFailures() {
}

// NewClient returns a new Request statusHandler
func NewStatusHandler(ctx context.Context, cr *v1alpha1.Request, res httpClient.HttpResponse, client client.Client, logger logging.Logger) RequestStatusHandler {
func NewStatusHandler(ctx context.Context, cr *v1alpha1.Request, client client.Client, res httpClient.HttpResponse, err error, logger logging.Logger) RequestStatusHandler {
requestStatusHandler := &requestStatusHandler{
logger: logger,
resource: &utils.RequestResource{
Expand Down

0 comments on commit 698e591

Please sign in to comment.