cloudflare-go icon indicating copy to clipboard operation
cloudflare-go copied to clipboard

Add support for updating individual rules within Rulesets

Open DuncanGener8 opened this issue 2 years ago • 1 comments

Current cloudflare-go version

v0.61.0

Description

Started encountering an issue with one of our integrations, where we use the API to update transform rules in Cloudflare. Upon review of the API docs, I found that there are a pair of methods that can be used for updating individual transform rules, rather than the entire ruleset, and these methods do no appear to represented in the SDK:

https://developers.cloudflare.com/api/operations/account-rulesets-update-an-account-ruleset-rule https://developers.cloudflare.com/api/operations/zone-rulesets-update-a-zone-ruleset-rule

Unrelated, but potentially relevant - the method update the entire ruleset no longer works on one of my zones, but this PATCH method does. I'm not entirely sure why.

Use cases

Updating a user-friendly URL to redirect to an updated versioned URL, without having to resubmit the entire ruleset for the phase.

Potential cloudflare-go usage

// Append to the bottom of rulesets.go

// UpdateZoneRulesetRule updates an individual rule within a ruleset for a zone.
//
// API reference: https://developers.cloudflare.com/api/operations/zone-rulesets-update-a-zone-ruleset-rule
func (api *API) UpdateZoneRulesetRule(ctx context.Context, zoneID, rulesetIdentifier, ruleIdentifier string, rule RulesetRule) (Ruleset, error) {
	return api.updateRulesetRule(ctx, ZoneRouteRoot, zoneID, rulesetIdentifier, ruleIdentifier, rule)
}

// UpdateAccountRulesetRule updates an individual rule within a ruleset for an account.
//
// API reference:  https://developers.cloudflare.com/api/operations/account-rulesets-update-an-account-ruleset-rule
func (api *API) UpdateAccountRulesetRule(ctx context.Context, accountID, rulesetIdentifier, ruleIdentifier string, rule RulesetRule) (Ruleset, error) {
	return api.updateRulesetRule(ctx, AccountRouteRoot, accountID, rulesetIdentifier, ruleIdentifier, rule)
}

// updateRulesetRule updates a ruleset based on the zone or account, the
// identifier and the rules.
func (api *API) updateRulesetRule(ctx context.Context, identifierType RouteRoot, identifier, rulesetIdentifier, ruleIdentifier string, rule RulesetRule) (Ruleset, error) {
	uri := fmt.Sprintf("/%s/%s/rulesets/%s/rules/%s", identifierType, identifier, rulesetIdentifier, ruleIdentifier)
	res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, rule)
	if err != nil {
		return Ruleset{}, err
	}

	result := GetRulesetResponse{}
	if err := json.Unmarshal(res, &result); err != nil {
		return Ruleset{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
	}

	return result.Result, nil
}

References

No response

DuncanGener8 avatar Feb 20 '23 14:02 DuncanGener8

@jacobbednarz sorry for pinging you directly here, but is there a reason for not supporting a update action of an individual rule?

Thanks!

tcinbis avatar Apr 10 '24 08:04 tcinbis