2 unstable releases

0.2.0 Dec 29, 2020
0.1.0 Dec 23, 2020

#46 in #zone

Apache-2.0 OR MIT

23KB
494 lines

Migamake Cloudflare API

This crate is a wrapper for the Cloudflare API. It includes support for 3 endpoints -

  • List Zone
  • Create a DNS Record
  • Delete a DNS Record

This crate uses API token for authentication. The token with appropriate permissions can be created on the cloudflare dashboard. You can read more about API Tokens. From this document -

API Tokens provide a new way to authenticate with the Cloudflare API. They allow for scoped and permissioned access to resources and use the RFC compliant Authorization Bearer Token Header.

Installing

Add this library to an existing project -

cargo add migamake-api-cloudflare

Building

Clone the repository and build the project -

cargo build

Documentation

To view the documentation locally run -

cargo doc --no-deps --open

Running Tests

To run the unit tests -

cargo test --lib

To run all the tests including the integration tests - ( expects enviornment variables CLOUDFLARE_API_KEY, CLOUDFLARE_DOMAIN and CLOUDFLARE_ZONE )

cargo test

Usage

This example demonstrates the usage of the library. The example creates a TXT record.

[dependencies]
migamake-api-cloudflare = { version = "0.1"}

use migamake_api_cloudflare::{Cloudflare, dns};

fn main() {
    let domain = "example.com".into();
    let zoneid = "some id";

    // initializes the object using an environment variable CLOUDFLARE_API_KEY
    // or the api key could be passed to the default method
    // let cloudflare = Cloudflare::default(Some("api-key").into());
    let cloudflare = Cloudflare::default(None);

    let mut txt_dns_record = dns::TXTRecord::new();
    txt_dns_record.name = domain;
    txt_dns_record.content = "create a txt record".into();

    let response = cloudflare.create_dns_record(txt_dns_record, &zoneid);

    let res = response.unwrap();
    if res.success {
        println!("{}", "Record created");
    }
    else{
        println!("{:?}", res.errors);
    }

}

Dependencies

~10MB
~202K SLoC