2024-01-01 15:31:33 -06:00
|
|
|
// This is a simple example
|
|
|
|
package cloudflare
|
|
|
|
|
2024-01-18 01:33:28 -06:00
|
|
|
import (
|
|
|
|
"bytes"
|
2024-01-01 15:31:33 -06:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
curl --request POST \
|
|
|
|
--url https://api.cloudflare.com/client/v4/zones/zone_identifier/dns_records \
|
|
|
|
--header 'Content-Type: application/json' \
|
|
|
|
--header 'X-Auth-Email: ' \
|
|
|
|
--data '{
|
|
|
|
"content": "198.51.100.4",
|
|
|
|
"name": "example.com",
|
|
|
|
"proxied": false,
|
|
|
|
"type": "A",
|
|
|
|
"comment": "Domain verification record",
|
|
|
|
"tags": [
|
|
|
|
"owner:dns-team"
|
|
|
|
],
|
|
|
|
"ttl": 3600
|
|
|
|
}'
|
|
|
|
*/
|
|
|
|
|
|
|
|
func doCurlDelete(auth string, email string, zoneId string, rrId string) string {
|
|
|
|
var err error
|
|
|
|
var req *http.Request
|
|
|
|
|
|
|
|
if zoneId == "" {
|
|
|
|
log.Warn("doCurlDelete() zoneId == nil")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if rrId == "" {
|
|
|
|
log.Warn("doCurlDelete() rrId == nil")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []byte("")
|
|
|
|
|
|
|
|
url := "https://api.cloudflare.com/client/v4/zones/" + zoneId + "/dns_records/" + rrId
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodDelete, url, bytes.NewBuffer(data))
|
|
|
|
|
|
|
|
// Set headers
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2024-01-18 01:33:28 -06:00
|
|
|
req.Header.Set("Authorization", "Bearer "+auth)
|
2024-01-05 19:18:36 -06:00
|
|
|
// changed from this 2024-01-05
|
|
|
|
// req.Header.Set("X-Auth-Key", auth)
|
|
|
|
// req.Header.Set("X-Auth-Email", email)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Error(err)
|
2024-01-01 15:31:33 -06:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Error(err)
|
2024-01-01 15:31:33 -06:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doCurlCreate(auth string, email string, zoneId string, data string) string {
|
|
|
|
var err error
|
|
|
|
var req *http.Request
|
|
|
|
|
|
|
|
if zoneId == "" {
|
|
|
|
log.Warn("doCurlDelete() zoneId == nil")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
url := "https://api.cloudflare.com/client/v4/zones/" + zoneId + "/dns_records/"
|
|
|
|
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Log(CURL, "doCurlCreate() POST url =", url)
|
|
|
|
log.Log(CURL, "doCurlCreate() POST Auth =", auth)
|
|
|
|
log.Log(CURL, "doCurlCreate() POST Email =", email)
|
|
|
|
log.Log(CURL, "doCurlCreate() POST data =", data)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
2024-01-18 01:33:28 -06:00
|
|
|
req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data)))
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
// Set headers
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2024-01-18 01:33:28 -06:00
|
|
|
req.Header.Set("Authorization", "Bearer "+auth)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Error(err, "client.Do() failed")
|
2024-01-01 15:31:33 -06:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Error(err, "ioutil.ReadAll(body) failed")
|
2024-01-01 15:31:33 -06:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-01-05 19:18:36 -06:00
|
|
|
pretty, _ := FormatJSON(string(body))
|
|
|
|
log.Log(CURL, "Create() result =", pretty)
|
2024-01-01 15:31:33 -06:00
|
|
|
return string(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doCurl(method string, rr *RRT) string {
|
|
|
|
var err error
|
|
|
|
var req *http.Request
|
|
|
|
|
|
|
|
data := []byte(rr.data)
|
|
|
|
|
2024-01-18 01:33:28 -06:00
|
|
|
if method == "PUT" {
|
2024-01-01 15:31:33 -06:00
|
|
|
req, err = http.NewRequest(http.MethodPut, rr.url, bytes.NewBuffer(data))
|
|
|
|
} else {
|
|
|
|
req, err = http.NewRequest(http.MethodPost, rr.url, bytes.NewBuffer(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set headers
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2024-01-18 01:33:28 -06:00
|
|
|
req.Header.Set("Authorization", "Bearer "+rr.Auth)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Log(CURL, "http PUT url =", rr.url)
|
|
|
|
log.Log(CURL, "http PUT Auth =", rr.Auth)
|
|
|
|
log.Log(CURL, "http PUT Email =", rr.Email)
|
|
|
|
log.Log(CURL, "http PUT data =", rr.data)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Error(err)
|
2024-01-01 15:31:33 -06:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Error(err)
|
2024-01-01 15:31:33 -06:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func curlPost(dnsRow *RRT) string {
|
|
|
|
var authKey string = dnsRow.Auth
|
|
|
|
var email string = dnsRow.Email
|
|
|
|
|
|
|
|
url := dnsRow.url
|
|
|
|
tmp := dnsRow.data
|
|
|
|
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Log(CURL, "curlPost() START")
|
|
|
|
log.Log(CURL, "curlPost() authkey = ", authKey)
|
|
|
|
log.Log(CURL, "curlPost() email = ", email)
|
|
|
|
log.Log(CURL, "curlPost() url = ", url)
|
2024-01-01 15:31:33 -06:00
|
|
|
data := []byte(tmp)
|
|
|
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
|
|
|
|
|
|
|
|
// Set headers
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2024-01-18 01:33:28 -06:00
|
|
|
req.Header.Set("Authorization", "Bearer "+authKey)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Error(err, "client.Do() failed")
|
2024-01-01 15:31:33 -06:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Error(err)
|
2024-01-01 15:31:33 -06:00
|
|
|
return ""
|
|
|
|
}
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Spew("http PUT body =", body)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
pretty, _ := FormatJSON(string(body))
|
2024-01-05 19:18:36 -06:00
|
|
|
log.Log(CURL, "result =", pretty)
|
|
|
|
log.Log(CURL, "curl() END")
|
2024-01-01 15:31:33 -06:00
|
|
|
return pretty
|
|
|
|
}
|