Skip to content
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

Update ruleset.go to accept a context.Context #306

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 140 additions & 35 deletions ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,23 @@ type RuleActionExtraction struct {
Regex string `json:"regex,omitempty"`
}

// ListRulesets gets all rulesets.
// ListRulesets gets all rulesets. This method currently handles pagination of
// the response, so all rulesets should be present.
//
// Please note that the automatic pagination will be removed in v2 of this
// package, so it's recommended to use ListRulesetsPaginated instead.
func (c *Client) ListRulesets() (*ListRulesetsResponse, error) {
rulesetResponse := new(ListRulesetsResponse)
rulesets := make([]*Ruleset, 0)
rs, err := c.ListRulesetsPaginated(context.Background())
if err != nil {
return nil, err
}

return &ListRulesetsResponse{Rulesets: rs}, nil
}

// ListRulesetsPaginated gets all rulesets.
func (c *Client) ListRulesetsPaginated(ctx context.Context) ([]*Ruleset, error) {
var rulesets []*Ruleset

// Create a handler closure capable of parsing data from the rulesets endpoint
// and appending resultant rulesets to the return slice.
Expand All @@ -158,61 +171,118 @@ func (c *Client) ListRulesets() (*ListRulesetsResponse, error) {
}

// Make call to get all pages associated with the base endpoint.
if err := c.pagedGet(context.TODO(), "/rulesets/", responseHandler); err != nil {
if err := c.pagedGet(ctx, "/rulesets/", responseHandler); err != nil {
return nil, err
}
rulesetResponse.Rulesets = rulesets

return rulesetResponse, nil
return rulesets, nil
}

// CreateRuleset creates a new ruleset.
// CreateRuleset creates a new ruleset. It's recommended to use
// CreateRulesetWithContext instead.
func (c *Client) CreateRuleset(r *Ruleset) (*Ruleset, *http.Response, error) {
data := make(map[string]*Ruleset)
data["ruleset"] = r
resp, err := c.post(context.TODO(), "/rulesets", data, nil)
return c.createRulesetWithContext(context.Background(), r)
}

// CreateRulesetWithContext creates a new ruleset.
func (c *Client) CreateRulesetWithContext(ctx context.Context, r *Ruleset) (*Ruleset, error) {
rs, _, err := c.createRulesetWithContext(ctx, r)
return rs, err
}

func (c *Client) createRulesetWithContext(ctx context.Context, r *Ruleset) (*Ruleset, *http.Response, error) {
d := map[string]*Ruleset{
"ruleset": r,
}

resp, err := c.post(ctx, "/rulesets", d, nil)
return getRulesetFromResponse(c, resp, err)
}

// DeleteRuleset deletes a ruleset.
// DeleteRuleset deletes a ruleset. It's recommended to use
// DeleteRulesetWithContext instead.
func (c *Client) DeleteRuleset(id string) error {
_, err := c.delete(context.TODO(), "/rulesets/"+id)
return c.DeleteRulesetWithContext(context.Background(), id)
}

// DeleteRulesetWithContext deletes a ruleset.
func (c *Client) DeleteRulesetWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/rulesets/"+id)
return err
}

// GetRuleset gets details about a ruleset.
func (c *Client) GetRuleset(id string) (*Ruleset, *http.Response, error) {
resp, err := c.get(context.TODO(), "/rulesets/"+id)
return c.getRulesetWithContext(context.Background(), id)
}

// GetRulesetWithContext gets details about a ruleset.
func (c *Client) GetRulesetWithContext(ctx context.Context, id string) (*Ruleset, error) {
rs, _, err := c.getRulesetWithContext(ctx, id)
return rs, err
}

func (c *Client) getRulesetWithContext(ctx context.Context, id string) (*Ruleset, *http.Response, error) {
resp, err := c.get(ctx, "/rulesets/"+id)
return getRulesetFromResponse(c, resp, err)
}

// UpdateRuleset updates a ruleset.
// UpdateRuleset updates a ruleset. It's recommended to use
// UpdateRulesetWithContext instead.
func (c *Client) UpdateRuleset(r *Ruleset) (*Ruleset, *http.Response, error) {
v := make(map[string]*Ruleset)
v["ruleset"] = r
resp, err := c.put(context.TODO(), "/rulesets/"+r.ID, v, nil)
return c.updateRulesetWithContext(context.Background(), r)
}

// UpdateRulesetWithContext updates a ruleset.
func (c *Client) UpdateRulesetWithContext(ctx context.Context, r *Ruleset) (*Ruleset, error) {
rs, _, err := c.updateRulesetWithContext(ctx, r)
return rs, err
}

func (c *Client) updateRulesetWithContext(ctx context.Context, r *Ruleset) (*Ruleset, *http.Response, error) {
d := map[string]*Ruleset{
"ruleset": r,
}

resp, err := c.put(ctx, "/rulesets/"+r.ID, d, nil)
return getRulesetFromResponse(c, resp, err)
}

func getRulesetFromResponse(c *Client, resp *http.Response, err error) (*Ruleset, *http.Response, error) {
if err != nil {
return nil, nil, err
}

var target map[string]Ruleset
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}

t, nodeOK := target["ruleset"]
if !nodeOK {
return nil, nil, fmt.Errorf("JSON response does not have ruleset field")
}

return &t, resp, nil
}

// ListRulesetRules gets all rules for a ruleset.
// ListRulesetRules gets all rules for a ruleset. This method currently handles pagination of
// the response, so all RuleseRule should be present.
//
// Please note that the automatic pagination will be removed in v2 of this
// package, so it's recommended to use ListRulesetRulesPaginated instead.
func (c *Client) ListRulesetRules(rulesetID string) (*ListRulesetRulesResponse, error) {
rulesResponse := new(ListRulesetRulesResponse)
rules := make([]*RulesetRule, 0)
rsr, err := c.ListRulesetRulesPaginated(context.Background(), rulesetID)
if err != nil {
return nil, err
}

return &ListRulesetRulesResponse{Rules: rsr}, nil
}

// ListRulesetRulesPaginated gets all rules for a ruleset.
func (c *Client) ListRulesetRulesPaginated(ctx context.Context, rulesetID string) ([]*RulesetRule, error) {
var rules []*RulesetRule

// Create a handler closure capable of parsing data from the ruleset rules endpoint
// and appending resultant ruleset rules to the return slice.
Expand All @@ -235,54 +305,89 @@ func (c *Client) ListRulesetRules(rulesetID string) (*ListRulesetRulesResponse,
}

// Make call to get all pages associated with the base endpoint.
if err := c.pagedGet(context.TODO(), "/rulesets/"+rulesetID+"/rules", responseHandler); err != nil {
if err := c.pagedGet(ctx, "/rulesets/"+rulesetID+"/rules", responseHandler); err != nil {
return nil, err
}
rulesResponse.Rules = rules

return rulesResponse, nil
return rules, nil
}

// GetRulesetRule gets an event rule
// GetRulesetRule gets an event rule. It's recommended to use
// GetRulesetRuleWithContext instead.
func (c *Client) GetRulesetRule(rulesetID, ruleID string) (*RulesetRule, *http.Response, error) {
resp, err := c.get(context.TODO(), "/rulesets/"+rulesetID+"/rules/"+ruleID)
return c.getRulesetRuleWithContext(context.Background(), rulesetID, ruleID)
}

// GetRulesetRuleWithContext gets an event rule
func (c *Client) GetRulesetRuleWithContext(ctx context.Context, rulesetID, ruleID string) (*RulesetRule, error) {
rsr, _, err := c.getRulesetRuleWithContext(ctx, rulesetID, ruleID)
return rsr, err
}

func (c *Client) getRulesetRuleWithContext(ctx context.Context, rulesetID, ruleID string) (*RulesetRule, *http.Response, error) {
resp, err := c.get(ctx, "/rulesets/"+rulesetID+"/rules/"+ruleID)
return getRuleFromResponse(c, resp, err)
}

// DeleteRulesetRule deletes a rule.
// DeleteRulesetRule deletes a rule. It's recommended to use
// DeleteRulesetRuleWithContext instead.
func (c *Client) DeleteRulesetRule(rulesetID, ruleID string) error {
_, err := c.delete(context.TODO(), "/rulesets/"+rulesetID+"/rules/"+ruleID)
return c.DeleteRulesetRuleWithContext(context.Background(), rulesetID, ruleID)
}

// DeleteRulesetRuleWithContext deletes a rule.
func (c *Client) DeleteRulesetRuleWithContext(ctx context.Context, rulesetID, ruleID string) error {
_, err := c.delete(ctx, "/rulesets/"+rulesetID+"/rules/"+ruleID)
return err
}

// CreateRulesetRule creates a new rule for a ruleset.
// CreateRulesetRule creates a new rule for a ruleset. It's recommended to use
// CreateRulesetRuleWithContext instead.
func (c *Client) CreateRulesetRule(rulesetID string, rule *RulesetRule) (*RulesetRule, *http.Response, error) {
data := make(map[string]*RulesetRule)
data["rule"] = rule
resp, err := c.post(context.TODO(), "/rulesets/"+rulesetID+"/rules/", data, nil)
return c.CreateRulesetRuleWithContext(context.Background(), rulesetID, rule)
}

// CreateRulesetRuleWithContext creates a new rule for a ruleset.
func (c *Client) CreateRulesetRuleWithContext(ctx context.Context, rulesetID string, rule *RulesetRule) (*RulesetRule, *http.Response, error) {
d := map[string]*RulesetRule{
"rule": rule,
}

resp, err := c.post(ctx, "/rulesets/"+rulesetID+"/rules/", d, nil)
return getRuleFromResponse(c, resp, err)
}

// UpdateRulesetRule updates a rule.
func (c *Client) UpdateRulesetRule(rulesetID, ruleID string, r *RulesetRule) (*RulesetRule, *http.Response, error) {
v := make(map[string]*RulesetRule)
v["rule"] = r
resp, err := c.put(context.TODO(), "/rulesets/"+rulesetID+"/rules/"+ruleID, v, nil)
return c.UpdateRulesetRuleWithContext(context.Background(), rulesetID, ruleID, r)
}

// UpdateRulesetRuleWithContext updates a rule.
func (c *Client) UpdateRulesetRuleWithContext(ctx context.Context, rulesetID, ruleID string, r *RulesetRule) (*RulesetRule, *http.Response, error) {
d := map[string]*RulesetRule{
"rule": r,
}

resp, err := c.put(ctx, "/rulesets/"+rulesetID+"/rules/"+ruleID, d, nil)
return getRuleFromResponse(c, resp, err)
}

func getRuleFromResponse(c *Client, resp *http.Response, err error) (*RulesetRule, *http.Response, error) {
if err != nil {
return nil, nil, err
}

var target map[string]RulesetRule
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
rootNode := "rule"

const rootNode = "rule"

t, nodeOK := target[rootNode]
if !nodeOK {
return nil, nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}

return &t, resp, nil
}