2024-01-01 15:31:33 -06:00
|
|
|
/*
|
|
|
|
This will attempt to create a RR in a DNS zone file.
|
|
|
|
|
|
|
|
Create("wit.com", "test.wit.com", "1.1.1.1"
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cloudflare
|
|
|
|
|
2024-01-18 01:33:28 -06:00
|
|
|
import (
|
2024-01-01 15:31:33 -06:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Create(zone string, hostname string, value string) bool {
|
|
|
|
log.Info("cloudflare.Create() START", zone, hostname, value)
|
|
|
|
key := os.Getenv("CF_API_KEY")
|
|
|
|
email := os.Getenv("CF_API_EMAIL")
|
|
|
|
|
2024-01-18 01:33:28 -06:00
|
|
|
if key == "" {
|
2024-01-01 15:31:33 -06:00
|
|
|
log.Warn("cloudflare.Create() MISSING environment variable CF_API_KEY")
|
|
|
|
return false
|
|
|
|
}
|
2024-01-18 01:33:28 -06:00
|
|
|
if email == "" {
|
2024-01-01 15:31:33 -06:00
|
|
|
log.Warn("cloudflare.Create() MISSING environment variable CF_API_EMAIL")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
GetZones(key, email)
|
|
|
|
var z *ConfigT
|
|
|
|
for d, v := range Config {
|
2024-01-06 21:04:31 -06:00
|
|
|
log.Log(INFO, "cloudflare.Create() zone =", d, "value =", v)
|
2024-01-18 01:33:28 -06:00
|
|
|
if zone == d {
|
2024-01-01 15:31:33 -06:00
|
|
|
z = Config[zone]
|
2024-01-06 21:04:31 -06:00
|
|
|
log.Log(INFO, "cloudflare.Create() FOUND ZONE", zone, "ID =", z.ZoneID)
|
2024-01-01 15:31:33 -06:00
|
|
|
}
|
|
|
|
}
|
2024-01-18 01:33:28 -06:00
|
|
|
if z == nil {
|
2024-01-01 15:31:33 -06:00
|
|
|
log.Warn("cloudflare.Create() COULD NOT FIND ZONE", zone)
|
|
|
|
return false
|
|
|
|
}
|
2024-01-06 21:04:31 -06:00
|
|
|
log.Log(INFO, "cloudflare.Create() FOUND ZONE", z)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
// make a json record to send on port 80 to cloudflare
|
|
|
|
var data string
|
|
|
|
data = `{"content": "` + value + `", `
|
|
|
|
data += `"name": "` + hostname + `", `
|
|
|
|
data += `"type": "AAAA", `
|
|
|
|
data += `"ttl": "1", `
|
|
|
|
data += `"comment": "WIT DNS Control Panel"`
|
2024-01-18 01:33:28 -06:00
|
|
|
data += `}`
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
result := doCurlCreate(key, email, z.ZoneID, data)
|
|
|
|
pretty, _ := FormatJSON(result)
|
2024-01-06 21:04:31 -06:00
|
|
|
log.Spew("cloudflare.Create() result =", pretty)
|
2024-01-01 15:31:33 -06:00
|
|
|
return true
|
|
|
|
}
|