aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arthur/apis/cloudflare/zones.py33
-rw-r--r--arthur/config.py3
-rw-r--r--arthur/exts/cloudflare/zones.py28
3 files changed, 58 insertions, 6 deletions
diff --git a/arthur/apis/cloudflare/zones.py b/arthur/apis/cloudflare/zones.py
index 48714a6..93c29d1 100644
--- a/arthur/apis/cloudflare/zones.py
+++ b/arthur/apis/cloudflare/zones.py
@@ -1,10 +1,36 @@
+from typing import Optional
+
import aiohttp
-async def purge_zone(zone_identifier: str, api_token: str) -> dict:
+from arthur.config import CONFIG
+
+
+CF_TOKEN = CONFIG.cloudflare_token
+
+async def list_zones(zone_name: Optional[str] = None) -> dict[str, str]:
+
+ endpoint = f"https://api.cloudflare.com/client/v4/zones"
+ request_headers = {
+ "X-Auth-User-Service-Key": CF_TOKEN
+ }
+
+ if zone_name is not None:
+ endpoint += f"?name={zone_name}"
+
+ async with aiohttp.ClientSession() as session:
+ async with session.post(endpoint, headers=request_headers) as response:
+ info = await response.json()
+
+ zones = info["result"]
+
+ return {zone.name: zone.id for zone in zones}
+
+
+async def purge_zone(zone_identifier: str) -> dict:
endpoint = f"https://api.cloudflare.com/client/v4/zones/{zone_identifier}/purge_cache?purge_everything=true"
request_headers = {
- "X-Auth-User-Service-Key": api_token
+ "X-Auth-User-Service-Key": CF_TOKEN
}
async with aiohttp.ClientSession() as session:
@@ -13,7 +39,6 @@ async def purge_zone(zone_identifier: str, api_token: str) -> dict:
return {
"success": info["success"],
- "errors": info["errors"],
- "messages": info["messages"]
+ "errors": info["errors"]
}
diff --git a/arthur/config.py b/arthur/config.py
index 982aa68..03eab03 100644
--- a/arthur/config.py
+++ b/arthur/config.py
@@ -14,6 +14,9 @@ class Config(BaseSettings):
# Authorised role ID for usage
devops_role: int = 409416496733880320
+ # Token for authorising with the Cloudflare API
+ cloudflare_token: str
+
class Config: # noqa: D106
env_file = ".env"
env_prefix = "KING_ARTHUR_"
diff --git a/arthur/exts/cloudflare/zones.py b/arthur/exts/cloudflare/zones.py
index 64cd5f3..20169bc 100644
--- a/arthur/exts/cloudflare/zones.py
+++ b/arthur/exts/cloudflare/zones.py
@@ -1,4 +1,6 @@
"""The zones cog helps with managing Cloudflare zones"""
+from typing import Optional
+
from discord.ext import commands
from arthur.apis.cloudflare import zones
@@ -15,5 +17,27 @@ class Zones(commands.Cog):
await ctx.send_help(ctx.command)
@zones.command(name="purge")
- async def purge(self, ctx: commands.Context, zone_name: str = "pythondiscord.com"):
- pass \ No newline at end of file
+ async def purge(self, ctx: commands.Context, zone_name: Optional[str] = "pythondiscord.com"):
+ """Command to clear the Cloudflare cache of the specified zone"""
+ pydis_zones = await zones.list_zones(zone_name)
+ required_id = pydis_zones[zone_name]
+ purge_attempt_response = await zones.purge_zone(required_id)
+
+ message = "__Cloudflare cache purge status__\n"
+
+ if purge_attempt_response["success"]:
+ message += "Purge status: **Successful** :white_check_mark:\n"
+ message += f"Zone purged: **Zone purged: `{zone_name}`"
+ return await ctx.send(message)
+ else:
+ message += "Purge status: **Failed** :x:\n"
+ if errors := purge_attempt_response["errors"]:
+ message += "__Errors:__\n"
+ for error in errors:
+ message += f"**Code**: `{error.code}`\n"
+ message += f"**Message**: {error.message}\n"
+ return await ctx.send(message)
+
+def setup(bot: KingArthur) -> None:
+ """Add the extension to the bot."""
+ bot.add_cog(Zones(bot)) \ No newline at end of file