lmrc-cloudflare
Part of the LMRC Stack - Infrastructure-as-Code toolkit for building production-ready Rust applications
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)
- Async/Await: Built on modern async Rust with tokio and reqwest
- Well Documented: Comprehensive docs and examples for every feature
Installation
Add this to your Cargo.toml:
[]
= "0.2"
= { = "1", = ["full"] }
Quick Start
use ;
async
Usage Examples
DNS Management
List DNS Records
// List all DNS records for a zone
let records = client.dns
.list_records
.send
.await?;
// Filter by type
let a_records = client.dns
.list_records
.record_type
.send
.await?;
// Filter by name and type
let specific_record = client.dns
.list_records
.name
.record_type
.send
.await?;
Create DNS Records
use RecordType;
// Create an A record
let record = client.dns
.create_record
.name
.record_type
.content
.proxied
.ttl
.comment
.send
.await?;
// Create a CNAME record
let record = client.dns
.create_record
.name
.record_type
.content
.proxied
.send
.await?;
Update DNS Records
// Update record content
let updated = client.dns
.update_record
.content
.send
.await?;
// Update multiple fields
let updated = client.dns
.update_record
.content
.proxied
.ttl
.send
.await?;
Delete DNS Records
client.dns
.delete_record
.await?;
Find Records
// Find a record by name and type
let record = client.dns
.find_record
.await?;
if let Some = record
Sync Records (Idempotent CI/CD Operations)
Perfect for CI/CD pipelines where you want to ensure DNS records match a desired state:
use ;
// Define desired state
let desired_records = vec!;
// Dry run - see what would change without applying
let changes = client.dns
.sync_records
.records
.dry_run
.send
.await?;
for change in &changes
// Apply changes
let changes = client.dns
.sync_records
.records
.dry_run
.send
.await?;
println!;
Zone Management
// List all zones
let zones = client.zones
.list
.send
.await?;
for zone in zones
// Get a specific zone
let zone = client.zones
.get
.await?;
// Find zone by name
let zone = client.zones
.find_by_name
.await?;
// Get zone ID by name (convenience method)
let zone_id = client.zones
.get_zone_id
.await?;
Cache Purging
// Purge everything (careful!)
client.cache
.purge_everything
.await?;
// Purge specific URLs
client.cache
.purge_urls
.urls
.send
.await?;
// Purge by cache tags (Enterprise plan)
client.cache
.purge_tags
.tags
.send
.await?;
// Purge by hosts (Enterprise plan)
client.cache
.purge_hosts
.hosts
.send
.await?;
// Purge by prefixes (Enterprise plan)
client.cache
.purge_prefixes
.prefixes
.send
.await?;
Error Handling
The library provides detailed error types for better error handling:
use Error;
match client.zones.get_zone_id.await
CI/CD Integration Examples
GitHub Actions
name: Update DNS
on:
push:
branches:
jobs:
update-dns:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Update DNS records
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: cargo run --example sync-dns
Example Sync Script
use ;
use env;
async
Authentication
The client supports Cloudflare API tokens. To create an API token:
- Go to Cloudflare Dashboard
- Click "Create Token"
- Select appropriate permissions (e.g., "Edit zone DNS" for DNS management)
- Copy the token and use it with the client
let client = builder
.api_token
.build?;
Supported Record Types
A- IPv4 addressAAAA- IPv6 addressCNAME- Canonical nameMX- Mail exchangeTXT- Text recordSRV- Service locatorNS- Name serverCAA- Certificate authority authorizationPTR- Pointer record
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Part of the LMRC Stack project. Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Acknowledgments
This client is not officially associated with Cloudflare, Inc.