diff --git a/internal/controller/request/request.go b/internal/controller/request/request.go index f1e6293..ca0fb44 100644 --- a/internal/controller/request/request.go +++ b/internal/controller/request/request.go @@ -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 } @@ -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) { diff --git a/internal/controller/request/statushandler/status.go b/internal/controller/request/statushandler/status.go index ba300cb..42db83c 100644 --- a/internal/controller/request/statushandler/status.go +++ b/internal/controller/request/statushandler/status.go @@ -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() } @@ -26,6 +26,7 @@ type RequestStatusHandler interface { type requestStatusHandler struct { logger logging.Logger resource *utils.RequestResource + httpError error basicSetters *[]utils.SetRequestStatusFunc } @@ -33,9 +34,9 @@ type requestStatusHandler struct { // 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) { @@ -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{