コンテンツにスキップ

ルールセットのルールを更新する

アカウントまたはゾーンレベルで既存のルールに1つ以上の変更を適用します。

次のAPIエンドポイントのいずれかを使用します:

操作メソッド + エンドポイント
アカウントのルールセットルールを更新するPATCH /accounts/{account_id}/rulesets/{ruleset_id}/rules/{rule_id}
ゾーンのルールセットルールを更新するPATCH /zones/{zone_id}/rulesets/{ruleset_id}/rules/{rule_id}

ルールの定義を更新し、そのフィールドを変更するか、ルールセット内のルールの順序を変更できます。このメソッドを呼び出すと、ルールセットの新しいバージョンが作成されます。

ルールの定義を更新する

ルールの定義を更新するには、新しいルール定義をリクエストボディに含めます。値を変更しない場合でも、新しいルール定義の一部として含めたいすべてのルールフィールドを含める必要があります。

リクエスト

Terminal window
curl --request PATCH \
https://api.cloudflare.com/client/v4/accounts/{account_id}/rulesets/{ruleset_id}/rules/{rule_id_1} \
--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": "custom",
"version": "11",
"rules": [
{
"id": "<RULE_ID_1>",
"version": "2",
"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": "2023-03-22T12:54:58.144683Z",
"ref": "<RULE_REF_1>",
"enabled": true
},
{
"id": "<RULE_ID_2>",
"version": "1",
"action": "challenge",
"expression": "not http.request.uri.path matches \"^/api/.*$\"",
"last_updated": "2022-11-23T11:36:24.192361Z",
"ref": "<RULE_REF_2>",
"enabled": true
}
],
"last_updated": "2023-03-22T12:54: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 with 1. 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 using index, 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 a 400 HTTP status code.

ルールの定義を変更せずに順序を変更するには、PATCHリクエストボディにpositionオブジェクトのみを含めます。また、ruleオブジェクトとpositionオブジェクトの両方を含めることで、同じPATCHリクエスト内でルール定義を更新し、順序を変更することもできます。

以下の例は、次の(省略された)ルールセットに基づいています:

{
"rules": [
{ "id": "<RULE_ID_1>" },
{ "id": "<RULE_ID_2>" },
{ "id": "<RULE_ID_3>" },
{ "id": "<RULE_ID_4>" }
]
}

例 #1

次のリクエストはpositionオブジェクトを使用して、ルール<RULE_ID_2>を最初のルールとして配置します:

リクエスト
curl --request PATCH \
https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id}/rules/{rule_id_2} \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"position": {
"before": ""
}
}

この場合、新しいルールの順序は次のようになります:

<RULE_ID_2>, <RULE_ID_1>, <RULE_ID_3>, <RULE_ID_4>

例 #2

次のリクエストはpositionオブジェクトを使用して、ルール<RULE_ID_2>をルール3の後に配置します:

リクエスト
curl --request PATCH \
https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id}/rules/{rule_id_2} \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"position": {
"after": "<RULE_ID_3>"
}
}

この場合、新しいルールの順序は次のようになります:

<RULE_ID_1>, <RULE_ID_3>, <RULE_ID_2>, <RULE_ID_4>

例 #3

次のリクエストはpositionオブジェクトを使用して、ルール<RULE_ID_1>を位置3に配置し、ルールセット内の3番目のルールになります:

リクエスト
curl --request PATCH \
https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id}/rules/{rule_id_1} \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"position": {
"index": 3
}
}'

この場合、新しいルールの順序は次のようになります:

<RULE_ID_2>, <RULE_ID_3>, <RULE_ID_1>, <RULE_ID_4>