-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add customizable removal check for Request resource
Signed-off-by: Ariel Septon <[email protected]>
- Loading branch information
1 parent
4758bde
commit 6ebb631
Showing
20 changed files
with
816 additions
and
266 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,77 @@ | ||
package observe | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/crossplane-contrib/provider-http/apis/request/v1alpha2" | ||
httpClient "github.com/crossplane-contrib/provider-http/internal/clients/http" | ||
"github.com/crossplane/crossplane-runtime/pkg/logging" | ||
"github.com/pkg/errors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
ErrObjectNotFound = "object wasn't found" | ||
) | ||
|
||
// isDeletedCheck is an interface for performing isDeleted checks. | ||
type isDeletedCheck interface { | ||
Check(ctx context.Context, cr *v1alpha2.Request, details httpClient.HttpDetails, responseErr error) error | ||
} | ||
|
||
// defaultIsRemovedResponseCheck performs a default comparison between the response and desired state. | ||
type defaultIsRemovedResponseCheck struct { | ||
localKube client.Client | ||
logger logging.Logger | ||
http httpClient.Client | ||
} | ||
|
||
// Check performs a default comparison between the response and desired state. | ||
func (d *defaultIsRemovedResponseCheck) Check(ctx context.Context, cr *v1alpha2.Request, details httpClient.HttpDetails, responseErr error) error { | ||
if details.HttpResponse.StatusCode == http.StatusNotFound { | ||
return errors.New(ErrObjectNotFound) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// // customIsRemovedResponseCheck performs a custom response check using JQ logic. | ||
type customIsRemovedResponseCheck struct { | ||
localKube client.Client | ||
logger logging.Logger | ||
http httpClient.Client | ||
} | ||
|
||
// Check performs a custom response check using JQ logic. | ||
func (c *customIsRemovedResponseCheck) Check(ctx context.Context, cr *v1alpha2.Request, details httpClient.HttpDetails, responseErr error) error { | ||
logic := cr.Spec.ForProvider.IsRemovedCheck.Logic | ||
customCheck := &customCheck{localKube: c.localKube, logger: c.logger, http: c.http} | ||
|
||
isRemoved, err := customCheck.check(ctx, cr, details, logic) | ||
if err != nil { | ||
return errors.Errorf(errExpectedFormat, "isRemovedCheck", err.Error()) | ||
} else if isRemoved { | ||
return errors.New(ErrObjectNotFound) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// isRemovedCheckFactoryMap is a map that associates each check type with its corresponding factory function. | ||
var isRemovedCheckFactoryMap = map[string]func(localKube client.Client, logger logging.Logger, http httpClient.Client) isDeletedCheck{ | ||
v1alpha2.ExpectedResponseCheckTypeDefault: func(localKube client.Client, logger logging.Logger, http httpClient.Client) isDeletedCheck { | ||
return &defaultIsRemovedResponseCheck{localKube: localKube, logger: logger, http: http} | ||
}, | ||
v1alpha2.ExpectedResponseCheckTypeCustom: func(localKube client.Client, logger logging.Logger, http httpClient.Client) isDeletedCheck { | ||
return &customIsRemovedResponseCheck{localKube: localKube, logger: logger, http: http} | ||
}, | ||
} | ||
|
||
// GetIsRemovedResponseCheck uses a map to select and return the appropriate ResponseCheck. | ||
func GetIsRemovedResponseCheck(cr *v1alpha2.Request, localKube client.Client, logger logging.Logger, http httpClient.Client) isDeletedCheck { | ||
if factory, ok := isRemovedCheckFactoryMap[cr.Spec.ForProvider.IsRemovedCheck.Type]; ok { | ||
return factory(localKube, logger, http) | ||
} | ||
return isRemovedCheckFactoryMap[v1alpha2.ExpectedResponseCheckTypeDefault](localKube, logger, http) | ||
} |
Oops, something went wrong.