-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,38 @@ | ||
#!/bin/bash | ||
|
||
# Function to update zone settings | ||
response=$(curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/settings" \ | ||
# Cloudflare API endpoint | ||
API_ENDPOINT="https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/rulesets" | ||
|
||
# Function to create or update ruleset | ||
method="POST" | ||
endpoint="${API_ENDPOINT}" | ||
|
||
# Check if ruleset already exists | ||
existing_ruleset=$(curl -s -X GET "${API_ENDPOINT}?phase=${PHASE}" \ | ||
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \ | ||
-H "Content-Type: application/json") | ||
|
||
if echo "$existing_ruleset" | jq -e '.result[0]' > /dev/null; then | ||
ruleset_id=$(echo "$existing_ruleset" | jq -r '.result[0].id') | ||
method="PUT" | ||
endpoint="${API_ENDPOINT}/${ruleset_id}" | ||
fi | ||
|
||
# Send request to create or update ruleset | ||
response=$(curl -s -X $method "$endpoint" \ | ||
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \ | ||
-H "Content-Type: application/json" \ | ||
--data "{ | ||
\"items\": [ | ||
{\"id\": \"ssl\", \"value\": \"${SSL}\"}, | ||
{\"id\": \"always_use_https\", \"value\": \"${ALWAYS_USE_HTTPS}\"} | ||
] | ||
\"name\": \"Dynamic Main Ruleset\", | ||
\"description\": \"Dynamic ruleset for managing app settings\", | ||
\"kind\": \"${KIND}\", | ||
\"phase\": \"${PHASE}\", | ||
\"rules\": ${RULESET_RULES} | ||
}") | ||
|
||
if echo "$response" | grep -q '"success":true'; then | ||
echo "Successfully updated settings for zone ${ZONE_ID}" | ||
echo "Successfully managed ruleset for zone ${ZONE_ID}" | ||
else | ||
echo "Failed to update settings for zone ${ZONE_ID}" | ||
echo "Failed to manage ruleset for zone ${ZONE_ID}" | ||
echo "Response: $response" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,19 @@ | ||
#!/bin/bash | ||
|
||
# Cloudflare API endpoint | ||
API_ENDPOINT="https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/rulesets" | ||
|
||
# Function to create or update ruleset | ||
method="POST" | ||
endpoint="${API_ENDPOINT}" | ||
|
||
# Check if ruleset already exists | ||
existing_ruleset=$(curl -s -X GET "${API_ENDPOINT}?phase=${PHASE}" \ | ||
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \ | ||
-H "Content-Type: application/json") | ||
|
||
if echo "$existing_ruleset" | jq -e '.result[0]' > /dev/null; then | ||
ruleset_id=$(echo "$existing_ruleset" | jq -r '.result[0].id') | ||
method="PUT" | ||
endpoint="${API_ENDPOINT}/${ruleset_id}" | ||
fi | ||
|
||
# Prepare the rules JSON | ||
rules_json=$(echo $RULESET_RULES | jq -c ' | ||
[.[] | { | ||
action: .action, | ||
action_parameters: ( | ||
if .action_parameters.ssl != null then | ||
{ssl: .action_parameters.ssl} | ||
else | ||
{} | ||
end | ||
), | ||
expression: .expression, | ||
description: .description | ||
}] | ||
') | ||
|
||
# Send request to create or update ruleset | ||
response=$(curl -s -X $method "$endpoint" \ | ||
# Function to update zone settings | ||
response=$(curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/settings" \ | ||
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \ | ||
-H "Content-Type: application/json" \ | ||
--data "{ | ||
\"name\": \"Dynamic Main Ruleset\", | ||
\"description\": \"Dynamic ruleset for managing app settings\", | ||
\"kind\": \"${KIND}\", | ||
\"phase\": \"${PHASE}\", | ||
\"rules\": ${rules_json} | ||
\"items\": [ | ||
{\"id\": \"ssl\", \"value\": \"${SSL}\"}, | ||
{\"id\": \"always_use_https\", \"value\": \"${ALWAYS_USE_HTTPS}\"} | ||
] | ||
}") | ||
|
||
if echo "$response" | grep -q '"success":true'; then | ||
echo "Successfully managed ruleset for zone ${ZONE_ID}" | ||
echo "Successfully updated settings for zone ${ZONE_ID}" | ||
else | ||
echo "Failed to manage ruleset for zone ${ZONE_ID}" | ||
echo "Failed to update settings for zone ${ZONE_ID}" | ||
echo "Response: $response" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters