Crate lmrc_cloudflare

Crate lmrc_cloudflare 

Source
Expand description

§Cloudflare Client

A comprehensive, well-documented Rust client for the Cloudflare API, designed specifically for CI/CD and automation workflows.

§Features

  • DNS Management: Create, read, update, and delete DNS records with full control
  • Zone Management: List and query zones (domains)
  • Cache Purging: Clear cache by various methods (all, URLs, tags, hosts, prefixes)
  • Builder Pattern: Ergonomic API with builder pattern for all operations
  • Custom Error Types: Detailed error handling for programmatic responses
  • Diff Output: See what changes will be made before applying them
  • Idempotent Operations: Safe to run multiple times (perfect for CI/CD)
  • Well Documented: Comprehensive docs and examples

§Quick Start

use lmrc_cloudflare::{CloudflareClient, dns::RecordType};

// Create a client
let client = CloudflareClient::builder()
    .api_token("your-api-token")
    .build()?;

// List zones
let zones = client.zones().list().send().await?;
println!("Found {} zones", zones.len());

// Get zone ID
let zone_id = client.zones().get_zone_id("example.com").await?;

// Create a DNS record
let record = client.dns()
    .create_record(&zone_id)
    .name("api.example.com")
    .record_type(RecordType::A)
    .content("192.0.2.1")
    .proxied(true)
    .send()
    .await?;

println!("Created record: {}", record.id);

// List DNS records
let records = client.dns()
    .list_records(&zone_id)
    .record_type(RecordType::A)
    .send()
    .await?;

// Update a record
client.dns()
    .update_record(&zone_id, &record.id)
    .content("192.0.2.2")
    .send()
    .await?;

// Purge cache
client.cache()
    .purge_urls(&zone_id)
    .urls(vec!["https://example.com/page1"])
    .send()
    .await?;

§CI/CD Integration

The sync operations are designed for CI/CD workflows:

use lmrc_cloudflare::{CloudflareClient, dns::{RecordType, DnsRecordBuilder}};

let client = CloudflareClient::new("your-api-token")?;
let zone_id = client.zones().get_zone_id("example.com").await?;

// Define desired state
let desired_records = vec![
    DnsRecordBuilder::new()
        .name("api.example.com")
        .record_type(RecordType::A)
        .content("192.0.2.1")
        .proxied(true),
    DnsRecordBuilder::new()
        .name("www.example.com")
        .record_type(RecordType::CNAME)
        .content("example.com")
        .proxied(true),
];

// Dry run to see what would change
let changes = client.dns()
    .sync_records(&zone_id)
    .records(desired_records.clone())
    .dry_run(true)
    .send()
    .await?;

for change in &changes {
    println!("{:?}: {}", change.action, change.description);
}

// Apply changes
let changes = client.dns()
    .sync_records(&zone_id)
    .records(desired_records)
    .dry_run(false)
    .send()
    .await?;

§Error Handling

The library uses custom error types for better error handling:

use lmrc_cloudflare::{CloudflareClient, Error};

let client = CloudflareClient::new("your-api-token")?;

match client.zones().get_zone_id("nonexistent.com").await {
    Ok(zone_id) => println!("Zone ID: {}", zone_id),
    Err(Error::NotFound(msg)) => println!("Zone not found: {}", msg),
    Err(Error::Unauthorized(msg)) => println!("Auth error: {}", msg),
    Err(Error::RateLimited { retry_after }) => {
        println!("Rate limited, retry after: {:?}", retry_after);
    }
    Err(e) => println!("Other error: {}", e),
}

Re-exports§

pub use adapter::CloudflareAdapter;
pub use client::CloudflareClient;
pub use client::CloudflareClientBuilder;
pub use client::RetryConfig;
pub use error::Error;
pub use error::Result;
pub use types::Change;
pub use types::ChangeAction;
pub use dns::DnsRecord;
pub use dns::DnsRecordBuilder;
pub use dns::RecordType;
pub use zones::Zone;

Modules§

adapter
Cloudflare Adapter
cache
Cache purging for Cloudflare.
client
Core Cloudflare API client.
dns
DNS record management for Cloudflare.
error
Error types for the Cloudflare client.
types
Common types used across the Cloudflare API.
zones
Zone management for Cloudflare.