cloudflare API changed

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-01-05 18:52:47 -06:00
parent c641154bf9
commit d3536a2b28
5 changed files with 93 additions and 17 deletions

46
api.go
View File

@ -99,37 +99,46 @@ func SetRow(dnsRow *RRT) {
}
func GetZonefile(c *ConfigT) *DNSRecords {
var url = cloudflareURL + c.ZoneID + "/dns_records/?per_page=100"
var url = cloudflareURL + c.ZoneID + "/dns_records/?per_page=10"
log.Println("getZonefile()", c.Domain, url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println("http.NewRequest error:", err)
log.Error(err, "http.NewRequest error")
return nil
}
// Set headers
req.Header.Set("X-Auth-Key", c.Auth)
req.Header.Set("X-Auth-Email", c.Email)
req.Header.Set("Authorization", "Bearer " + c.Auth)
// req.Header.Set("X-Auth-Key", c.Auth)
// req.Header.Set("X-Auth-Email", c.Email)
log.Println("getZonefile() auth, email", c.Auth, c.Email)
client := &http.Client{}
resp, err := client.Do(req)
// log.Spew("SPEWING", resp)
if err != nil {
log.Println("http.Client error:", err)
log.Error(err, "http.Client error")
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("ioutil.ReadAll() error", err)
log.Error(err, "ioutil.ReadAll() error")
return nil
}
log.Spew("SPEWING", body)
pretty, err := FormatJSON(string(body))
if err != nil {
log.Error(err, "FormatJSON error")
} else {
log.Warn(pretty)
}
var records DNSRecords
if err := json.Unmarshal(body, &records); err != nil {
log.Println("json.Unmarshal() error", err)
log.Error(err, "json.Unmarshal() error")
return nil
}
@ -166,35 +175,42 @@ func GetZones(auth, email string) *DNSRecords {
log.Println("getZones()", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println("http.NewRequest error:", err)
log.Error(err, "http.NewRequest error")
return nil
}
// Set headers
req.Header.Set("X-Auth-Key", auth)
req.Header.Set("X-Auth-Email", email)
req.Header.Set("Authorization", "Bearer " + auth)
// req.Header.Set("X-Auth-Key", auth)
// req.Header.Set("X-Auth-Email", email)
log.Println("getZones() auth, email", auth, email)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("getZones() http.Client error:", err)
log.Error(err, "getZones() http.Client error")
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("getZones() ioutil.ReadAll() error", err)
log.Error(err, "getZones() ioutil.ReadAll() error")
return nil
}
var records DNSRecords
if err := json.Unmarshal(body, &records); err != nil {
log.Println("getZones() json.Unmarshal() error", err)
log.Error(err, "getZones() json.Unmarshal() error")
return nil
}
pretty, err := FormatJSON(string(body))
if err != nil {
log.Error(err, "FormatJSON error")
} else {
log.Warn(pretty)
}
/* Cloudflare API returns struct[] of:
struct { ID string "json:\"id\""; Type string "json:\"type\""; Name string "json:\"name\"";
@ -206,8 +222,8 @@ func GetZones(auth, email string) *DNSRecords {
// log.Println("spew dump:")
// spew.Dump(records)
for _, record := range records.Result {
log.Println("spew record:", record)
log.Println("record:", record.Name, record.ID)
log.Spew("spew record:", record)
log.Info("record:", record.Name, record.ID)
var newc *ConfigT
newc = new(ConfigT)

20
args.go Normal file
View File

@ -0,0 +1,20 @@
package cloudflare
// initializes logging and command line options
import (
"go.wit.com/log"
)
var CURL log.LogFlag
var POLL log.LogFlag
var BUG log.LogFlag
func init() {
CURL.B = true
CURL.Name = "CURL"
CURL.Subsystem = "cloudflare"
CURL.Short = "gui cf"
CURL.Desc = "curl interactions()"
CURL.Register()
}

View File

@ -180,8 +180,7 @@ func curlPost(dnsRow *RRT) string {
log.Println(err)
return ""
}
// log.Println("http PUT body =", body)
// spew.Dump(body)
log.Spew("http PUT body =", body)
log.Println("result =", string(body))
log.Println("curl() END")

View File

@ -16,6 +16,7 @@ func MakeCloudflareWindow(n *gui.Node) *gadgets.BasicWindow {
log.Println("buttonWindow() START")
CFdialog.mainWindow = gadgets.NewBasicWindow(n,"Cloudflare Config")
CFdialog.mainWindow.Vertical()
// this tab has the master cloudflare API credentials
makeConfigWindow(CFdialog.mainWindow.Box())

40
zonesBox.go Normal file
View File

@ -0,0 +1,40 @@
// This is a simple example
package cloudflare
import (
"go.wit.com/log"
"go.wit.com/gui/gui"
)
func drawZoneBox(box *gui.Node) {
g1 := box.NewGroup("zones")
// make dropdown list of zones
CFdialog.zonedrop = g1.NewDropdown("zone")
CFdialog.zonedrop.AddText("example.org")
for d, _ := range Config {
CFdialog.zonedrop.AddText(d)
}
CFdialog.zonedrop.AddText("stablesid.org")
CFdialog.zonedrop.Custom = func () {
domain := CFdialog.zonedrop.S
log.Println("custom dropdown() zone (domain name) =", CFdialog.zonedrop.Name, domain)
if (Config[domain] == nil) {
log.Println("custom dropdown() Config[domain] = nil for domain =", domain)
CFdialog.domainWidget.SetText(domain)
CFdialog.zoneWidget.SetText("")
CFdialog.authWidget.SetText("")
CFdialog.emailWidget.SetText("")
} else {
log.Println("custom dropdown() a =", domain, Config[domain].ZoneID, Config[domain].Auth, Config[domain].Email)
CFdialog.domainWidget.SetText(Config[domain].Domain)
CFdialog.zoneWidget.SetText(Config[domain].ZoneID)
CFdialog.authWidget.SetText(Config[domain].Auth)
CFdialog.emailWidget.SetText(Config[domain].Email)
}
}
more := g1.NewGroup("data")
showCloudflareCredentials(more)
}