|
|
|
|
|
""" |
|
|
Example usage of Cloudflare Manager |
|
|
Demonstrates key features: |
|
|
1. Deploy Pages project |
|
|
2. Bind domain |
|
|
3. Get nameservers |
|
|
4. Configure worker routes |
|
|
""" |
|
|
|
|
|
from cloudflare_manager import CloudflareManager, CloudflareAccount |
|
|
|
|
|
|
|
|
def example_pages_deployment(): |
|
|
"""Example: Deploy a Pages project and bind a domain""" |
|
|
print("\n" + "="*60) |
|
|
print("Example 1: Deploy Pages Project with Domain") |
|
|
print("="*60) |
|
|
|
|
|
|
|
|
account = CloudflareAccount( |
|
|
email="exslym@closedbyme.com", |
|
|
token="21f3fb278a15b732a4f52c95d5042d78d1a21" |
|
|
) |
|
|
cf = CloudflareManager(account) |
|
|
|
|
|
|
|
|
print("\nπ Step 1: Creating Pages project...") |
|
|
project_name = "my-test-site" |
|
|
project = cf.create_pages_project(project_name, "main") |
|
|
|
|
|
if project: |
|
|
print(f"β Project URL: https://{project.get('subdomain')}") |
|
|
|
|
|
|
|
|
print("\nπ¦ Step 2: Deploying from directory...") |
|
|
deployment = cf.deploy_pages_project( |
|
|
project_name=project_name, |
|
|
directory=".", |
|
|
branch="main", |
|
|
commit_message="Initial deployment via API" |
|
|
) |
|
|
|
|
|
if deployment: |
|
|
print(f"β Deployment URL: {deployment.get('url')}") |
|
|
|
|
|
|
|
|
print("\nπ Step 3: Listing all Pages projects...") |
|
|
projects = cf.list_pages_projects() |
|
|
for proj in projects: |
|
|
print(f" - {proj['name']}: https://{proj.get('subdomain')}") |
|
|
|
|
|
return cf, project_name |
|
|
|
|
|
|
|
|
def example_domain_and_nameservers(cf, project_name=None): |
|
|
"""Example: Add domain and get nameservers""" |
|
|
print("\n" + "="*60) |
|
|
print("Example 2: Domain Binding and Nameservers") |
|
|
print("="*60) |
|
|
|
|
|
domain_name = input("\nπ Enter a domain name to add (e.g., example.com): ").strip() |
|
|
|
|
|
if not domain_name: |
|
|
print("β οΈ Skipping domain example (no domain provided)") |
|
|
return |
|
|
|
|
|
|
|
|
print(f"\nπ Step 1: Creating zone for {domain_name}...") |
|
|
zone = cf.create_zone(domain_name) |
|
|
|
|
|
if zone: |
|
|
nameservers = zone.get("name_servers", []) |
|
|
zone_id = zone.get("id") |
|
|
|
|
|
print(f"\nπ Nameservers to add to your domain registrar:") |
|
|
print("="*60) |
|
|
for ns in nameservers: |
|
|
print(f" {ns}") |
|
|
print("="*60) |
|
|
|
|
|
|
|
|
if project_name: |
|
|
print(f"\nπ Step 2: Adding domain to Pages project '{project_name}'...") |
|
|
result = cf.add_pages_domain(project_name, domain_name) |
|
|
|
|
|
if result: |
|
|
print(f"β Domain added: {result.get('name')}") |
|
|
print(f" Status: {result.get('status')}") |
|
|
|
|
|
|
|
|
if result.get('validation_data'): |
|
|
val = result['validation_data'] |
|
|
print(f"\nπ DNS Validation Required:") |
|
|
print(f" Type: {val.get('type')}") |
|
|
print(f" Name: {val.get('name')}") |
|
|
print(f" Value: {val.get('value')}") |
|
|
|
|
|
return zone_id |
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
def example_worker_upload(cf): |
|
|
"""Example: Upload a Worker script""" |
|
|
print("\n" + "="*60) |
|
|
print("Example 3: Upload Worker Script") |
|
|
print("="*60) |
|
|
|
|
|
configure = input("\nπ Do you want to upload a worker? (y/n): ").strip().lower() |
|
|
|
|
|
if configure != 'y': |
|
|
print("β οΈ Skipping worker upload") |
|
|
return None |
|
|
|
|
|
|
|
|
print("\nπ€ Uploading example worker...") |
|
|
result = cf.upload_worker( |
|
|
script_name="example-worker", |
|
|
worker_file="example_worker.js" |
|
|
) |
|
|
|
|
|
if result: |
|
|
print(f"β Worker uploaded successfully!") |
|
|
print(f" Worker ID: {result.get('id')}") |
|
|
return "example-worker" |
|
|
else: |
|
|
print("β Failed to upload worker") |
|
|
return None |
|
|
|
|
|
|
|
|
def example_worker_routes(cf, zone_id=None): |
|
|
"""Example: Configure worker routes""" |
|
|
print("\n" + "="*60) |
|
|
print("Example 4: Worker Routes Configuration (Optional)") |
|
|
print("="*60) |
|
|
|
|
|
if not zone_id: |
|
|
print("β οΈ No zone_id available. Skipping worker routes example.") |
|
|
print(" (You need a zone/domain first)") |
|
|
return |
|
|
|
|
|
configure = input("\nπ Do you want to configure worker routes? (y/n): ").strip().lower() |
|
|
|
|
|
if configure != 'y': |
|
|
print("β οΈ Skipping worker routes configuration") |
|
|
return |
|
|
|
|
|
|
|
|
pattern = input("Enter route pattern (e.g., example.com/api/*): ").strip() |
|
|
script_name = input("Enter worker script name: ").strip() |
|
|
|
|
|
if pattern and script_name: |
|
|
|
|
|
print(f"\nπ Creating worker route...") |
|
|
route = cf.create_worker_route(zone_id, pattern, script_name) |
|
|
|
|
|
if route: |
|
|
print(f"β Route created: {route.get('id')}") |
|
|
|
|
|
|
|
|
print(f"\nπ Listing all worker routes...") |
|
|
routes = cf.list_worker_routes(zone_id) |
|
|
|
|
|
if routes: |
|
|
for route in routes: |
|
|
print(f" - {route.get('pattern')} -> {route.get('script', 'N/A')}") |
|
|
else: |
|
|
print(" No routes found") |
|
|
|
|
|
|
|
|
def example_list_zones(cf): |
|
|
"""Example: List all zones""" |
|
|
print("\n" + "="*60) |
|
|
print("Example 5: List All Zones") |
|
|
print("="*60) |
|
|
|
|
|
zones = cf.list_zones() |
|
|
|
|
|
if zones: |
|
|
print(f"\nπ Found {len(zones)} zone(s):") |
|
|
for zone in zones: |
|
|
print(f"\n Domain: {zone['name']}") |
|
|
print(f" Zone ID: {zone['id']}") |
|
|
print(f" Status: {zone.get('status', 'unknown')}") |
|
|
|
|
|
nameservers = zone.get("name_servers", []) |
|
|
if nameservers: |
|
|
print(f" Nameservers:") |
|
|
for ns in nameservers: |
|
|
print(f" - {ns}") |
|
|
else: |
|
|
print(" No zones found") |
|
|
|
|
|
|
|
|
def main(): |
|
|
"""Run all examples""" |
|
|
print(""" |
|
|
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
|
|
β Cloudflare Manager - Example Usage β |
|
|
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
|
|
""") |
|
|
|
|
|
print("This script demonstrates the key features:") |
|
|
print(" 1. Deploy Pages project") |
|
|
print(" 2. Bind domain and get nameservers") |
|
|
print(" 3. Upload Worker script") |
|
|
print(" 4. Configure worker routes (optional)") |
|
|
print(" 5. List all zones") |
|
|
|
|
|
proceed = input("\nDo you want to proceed? (y/n): ").strip().lower() |
|
|
|
|
|
if proceed != 'y': |
|
|
print("\nπ Exiting...") |
|
|
return |
|
|
|
|
|
|
|
|
cf, project_name = example_pages_deployment() |
|
|
|
|
|
|
|
|
zone_id = example_domain_and_nameservers(cf, project_name) |
|
|
|
|
|
|
|
|
worker_name = example_worker_upload(cf) |
|
|
|
|
|
|
|
|
example_worker_routes(cf, zone_id) |
|
|
|
|
|
|
|
|
example_list_zones(cf) |
|
|
|
|
|
print("\n" + "="*60) |
|
|
print("β All examples completed!") |
|
|
print("="*60) |
|
|
print("\nNext steps:") |
|
|
print(" 1. Update nameservers at your domain registrar") |
|
|
print(" 2. Wait for DNS propagation (5-30 minutes)") |
|
|
print(" 3. Your site will be live!") |
|
|
if worker_name: |
|
|
print(f" 4. Your worker is accessible at: https://{worker_name}.<account>.workers.dev") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|