cloudflare

package
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2023 License: MIT Imports: 4 Imported by: 0

README

goutils/cloudflare

The cloudflare package is a part of goutils library. It provides utility functions to interface with cloudflore using go.


Functions

GetDNSRecords
func GetDNSRecords(cf Cloudflare) error

Retrieve the DNS records from Cloudflare for a specified zone ID using the provided Cloudflare credentials.


Installation

To use the goutils/cloudflare package, you need to install it via go get:

go get github.com/l50/goutils/cloudflare

Usage

After installation, you can import it in your project:

import "github.com/l50/goutils/cloudflare"

Tests

To run the tests for the goutils/cloudflare package, navigate to your $GOPATH/src/github.com/l50/goutils/cloudflare directory and run go test:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDNSRecords

func GetDNSRecords(cf Cloudflare) error

GetDNSRecords retrieves the DNS records from Cloudflare for a specified zone ID using the provided Cloudflare credentials. It makes a GET request to the Cloudflare API, reads the response, and prints the 'name' and 'content' fields of each DNS record.

Parameters:

cf: A Cloudflare struct containing the necessary credentials (email, API key) and the zone ID for which the DNS records should be retrieved.

Returns:

error: An error if any issue occurs while trying to get the DNS records.

Example:

cf := Cloudflare{
  CFEmail: "your-email@example.com",
  CFApiKey: "your-api-key",
  CFZoneID: "your-zone-id",
  Client: &http.Client{},
}

err := GetDNSRecords(cf)

if err != nil {
  log.Fatalf("failed to get DNS records: %v", err)
}

Types

type Cloudflare

type Cloudflare struct {
	CFApiKey string
	CFEmail  string
	CFZoneID string
	Email    string
	Endpoint string
	Client   HTTPClient
}

Cloudflare holds information needed to interface with the Cloudflare API.

Parameters:

CFApiKey: Cloudflare API key. CFEmail: Email address associated with the Cloudflare account. CFZoneID: Zone ID of the domain on Cloudflare. Email: Email address for notifications. Endpoint: API endpoint for Cloudflare. Client: HTTP client for making requests.

Example:

cf := Cloudflare{
  CFApiKey: "your_api_key",
  CFEmail: "your_email@example.com",
  CFZoneID: "your_zone_id",
  Email: "your_notification_email@example.com",
  Endpoint: "",  // This will be set in the function.
  Client: http.Client{},
}

err := GetDNSRecords(cf)

if err != nil {
  log.Fatalf("failed to get DNS records: %v", err)
}

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is an interface defining the behavior of an HTTP client. This allows for more flexibility in HTTP interactions and facilitates testing by enabling the use of mock HTTP clients.

The HttpClient interface includes a single method, Do, which sends an HTTP request and returns the HTTP response or an error.

Parameters:

req: The HTTP request to be sent.

Returns:

*http.Response: The HTTP response to the request. error: An error, if one occurred during the execution of the request.

Example:

type MockClient struct {
    MockDo func(req *http.Request) (*http.Response, error)
}

func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
    return m.MockDo(req)
}

mockClient := &MockClient{
    MockDo: func(req *http.Request) (*http.Response, error) {
        return &http.Response{
            StatusCode: 200,
            Body:       io.NopCloser(strings.NewReader(`{"result": "ok"}`)),
        }, nil
    },
}

cf := Cloudflare{
    CFApiKey: "valid_key",
    CFEmail:  "valid_email@example.com",
    CFZoneID: "valid_zone",
    Email:    "notification@example.com",
    Client:   mockClient,
}

err := GetDNSRecords(cf)

if err != nil {
    log.Fatalf("failed to get DNS records: %v", err)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL