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

Add support for updating individual rules within Rulesets #1211

Open
DuncanGener8 opened this issue Feb 20, 2023 · 1 comment
Open

Add support for updating individual rules within Rulesets #1211

DuncanGener8 opened this issue Feb 20, 2023 · 1 comment

Comments

@DuncanGener8
Copy link

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

@tcinbis
Copy link

tcinbis commented Apr 10, 2024

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

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants