Skip to content

Commit

Permalink
Implement handling for missing PUT mapping
Browse files Browse the repository at this point in the history
Signed-off-by: arielsepton <[email protected]>
  • Loading branch information
arielsepton authored May 16, 2024
1 parent 76c63e4 commit 42e6921
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
19 changes: 18 additions & 1 deletion internal/controller/request/observe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package request

import (
"context"
"fmt"
"net/http"
"strings"

Expand Down Expand Up @@ -61,6 +62,12 @@ func (c *external) isUpToDate(ctx context.Context, cr *v1alpha2.Request) (Observ
c.patchResponseToSecret(ctx, cr, &details.HttpResponse)
desiredState, err := c.desiredState(ctx, cr)
if err != nil {
if isErrorMappingNotFound(err) {
// Since there is no PUT mapping, we skip the check for its presence in the GET response.
return NewObserve(details, responseErr, true), nil
}

// For any other error, we return a failed observation.
return FailedObserve(), err
}

Expand Down Expand Up @@ -96,7 +103,11 @@ func (c *external) compareResponseAndDesiredState(details httpClient.HttpDetails

func (c *external) desiredState(ctx context.Context, cr *v1alpha2.Request) (string, error) {
requestDetails, err := c.requestDetails(ctx, cr, http.MethodPut)
return requestDetails.Body.Encrypted.(string), err
if err != nil {
return "", err
}

return requestDetails.Body.Encrypted.(string), nil
}

func (c *external) requestDetails(ctx context.Context, cr *v1alpha2.Request, method string) (requestgen.RequestDetails, error) {
Expand All @@ -107,3 +118,9 @@ func (c *external) requestDetails(ctx context.Context, cr *v1alpha2.Request, met

return generateValidRequestDetails(ctx, c.localKube, cr, mapping)
}

// isErrorMappingNotFound checks if the provided error indicates that the
// mapping for an HTTP PUT request is not found.
func isErrorMappingNotFound(err error) bool {
return errors.Cause(err).Error() == fmt.Sprintf(errMappingNotFound, http.MethodPut)
}
40 changes: 40 additions & 0 deletions internal/controller/request/observe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,46 @@ func Test_isUpToDate(t *testing.T) {
},
},
},
"SuccessNoPUTMapping": {
args: args{
http: &MockHttpClient{
MockSendRequest: func(ctx context.Context, method string, url string, body, headers httpClient.Data, skipTLSVerify bool) (resp httpClient.HttpDetails, err error) {
return httpClient.HttpDetails{
HttpResponse: httpClient.HttpResponse{
Body: `{"username":"old_name"}`,
StatusCode: 200,
},
}, nil
},
},
localKube: &test.MockClient{
MockStatusUpdate: test.NewMockSubResourceUpdateFn(nil),
},
mg: httpRequest(func(r *v1alpha2.Request) {
r.Status.Response.Body = `{"username":"john_doe_new_username"}`
r.Status.Response.StatusCode = 200
r.Spec.ForProvider.Mappings = []v1alpha2.Mapping{
testPostMapping,
testGetMapping,
testDeleteMapping,
}
}),
},
want: want{
err: nil,
result: ObserveRequestDetails{
Details: httpClient.HttpDetails{
HttpResponse: httpClient.HttpResponse{
Body: `{"username":"old_name"}`,
Headers: nil,
StatusCode: 200,
},
},
ResponseError: nil,
Synced: true,
},
},
},
"SuccessJSONBody": {
args: args{
http: &MockHttpClient{
Expand Down

0 comments on commit 42e6921

Please sign in to comment.