ルールセットにルールを追加
既存のルールセットに単一のルールを追加します。このエンドポイントを使用すると、リクエストに既存のルールセットのすべてのルールを含めることなく、ルールを追加できます。
次のAPIエンドポイントのいずれかを使用します:
| 操作 | メソッド + エンドポイント |
|---|---|
| アカウントルールセットルールを作成 | POST /accounts/{account_id}/rulesets/{ruleset_id}/rules |
| ゾーンルールセットルールを作成 | POST /zones/{zone_id}/rulesets/{ruleset_id}/rules |
リクエストボディにルール定義を含めます。
デフォルトでは、ルールはルールセットの既存のルールリストの最後に追加されます。ルールの特定の位置を定義するには、リクエストボディにpositionオブジェクトを含め、ルールセット内のルールの順序を変更するのガイドラインに従います。
このメソッドを呼び出すと、ルールセットの新しいバージョンが作成されます。
以下の例では、ゾーン{zone_id}のルールセット{ruleset_id}にルールを追加します。ルールセットIDは、ゾーンルールセットのリスト操作を使用して以前に取得され、http_request_firewall_customフェーズのエントリポイントルールセットに対応します。
リクエスト
curl https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id}/rules \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "action": "js_challenge", "expression": "(ip.geoip.country eq \"GB\" or ip.geoip.country eq \"FR\") or cf.threat_score > 0", "description": "GBおよびFRに対するチャレンジまたはIPレピュテーションに基づく"}'レスポンスには、ルールを追加した後の完全なルールセットが含まれます。
レスポンス
{ "result": { "id": "<RULESET_ID>", "name": "ゾーンルールセット 1", "description": "ゾーンレベルでの私のフェーズエントリポイントルールセット", "kind": "zone", "version": "11", "rules": [ { "id": "<RULE_ID_1>", "version": "1", "action": "challenge", "expression": "not http.request.uri.path matches \"^/api/.*$\"", "last_updated": "2020-11-23T11:36:24.192361Z", "ref": "<RULE_REF_1>", "enabled": true }, { "id": "<NEW_RULE_ID>", "version": "1", "action": "js_challenge", "expression": "(ip.geoip.country eq \"GB\" or ip.geoip.country eq \"FR\") or cf.threat_score > 0", "description": "GBおよびFRに対するチャレンジまたはIPレピュテーションに基づく", "last_updated": "2021-06-22T12:35:58.144683Z", "ref": "<NEW_RULE_REF>", "enabled": true } ], "last_updated": "2021-06-22T12:35:58.144683Z", "phase": "http_request_firewall_custom" }, "success": true, "errors": [], "messages": []}ルールセット内の新しいルールの位置を定義するには、リクエストにpositionオブジェクトを含め、次のいずれかを含めます:
-
"before": "<RULE_ID>"— Places the rule before rule<RULE_ID>. Use this argument with an empty rule ID value ("") to set the rule as the first rule in the ruleset. -
"after": "<RULE_ID>"— Places the rule after rule<RULE_ID>. Use this argument with an empty rule ID value ("") to set the rule as the last rule in the ruleset. -
"index": <POSITION_NUMBER>— Places the rule in the exact position specified by the integer number<POSITION_NUMBER>. Position numbers start with1. Existing rules in the ruleset from the specified position number onward are shifted one position (no rule is overwritten). For example, when you place a rule in position n usingindex, existing rules with index n, n+1, n+2, and so on, are shifted one position — their new position will be n+1, n+2, n+3, and so forth. If the index is out of range, the method returns a400HTTP status code.
positionオブジェクトの使用例については、ルールセット内のルールを更新するを参照してください。