2024-01-01 15:31:33 -06:00
// This is a simple example
package cloudflare
2024-01-18 01:33:28 -06:00
import (
2024-01-01 15:31:33 -06:00
"os"
2024-01-21 02:10:00 -06:00
"go.wit.com/gui"
2024-01-18 01:33:28 -06:00
"go.wit.com/lib/gadgets"
"go.wit.com/log"
2024-01-01 15:31:33 -06:00
)
// This creates a window
2024-01-05 09:38:20 -06:00
func MakeCloudflareWindow ( n * gui . Node ) * gadgets . BasicWindow {
2024-01-01 15:31:33 -06:00
CFdialog . rootGui = n
2024-01-05 19:33:41 -06:00
log . Log ( INFO , "buttonWindow() START" )
2024-01-01 15:31:33 -06:00
2024-01-18 01:33:28 -06:00
CFdialog . mainWindow = gadgets . NewBasicWindow ( n , "Cloudflare Config" )
2024-01-21 02:10:00 -06:00
// CFdialog.mainWindow.Vertical()
CFdialog . mainWindow . Make ( )
CFdialog . mainWindow . Draw ( )
2024-01-01 15:31:33 -06:00
// this tab has the master cloudflare API credentials
2024-01-05 09:38:20 -06:00
makeConfigWindow ( CFdialog . mainWindow . Box ( ) )
2024-01-01 15:31:33 -06:00
2024-01-18 01:33:28 -06:00
win := gadgets . NewBasicWindow ( n , "Zones" )
2024-01-21 02:10:00 -06:00
win . Make ( )
win . Draw ( )
2024-01-05 09:38:20 -06:00
g1 := win . Box ( ) . NewGroup ( "zones" )
2024-01-01 15:31:33 -06:00
// make dropdown list of zones
2024-01-18 01:23:49 -06:00
CFdialog . zonedrop = g1 . NewDropdown ( )
2024-01-01 15:31:33 -06:00
CFdialog . zonedrop . AddText ( "example.org" )
for d , _ := range Config {
CFdialog . zonedrop . AddText ( d )
}
CFdialog . zonedrop . AddText ( "stablesid.org" )
2024-01-18 01:33:28 -06:00
CFdialog . zonedrop . Custom = func ( ) {
2024-01-18 01:23:49 -06:00
domain := CFdialog . zonedrop . String ( )
log . Log ( ZONE , "custom dropdown() zone (domain name) =" , CFdialog . zonedrop . String ( ) , domain )
2024-01-18 01:33:28 -06:00
if Config [ domain ] == nil {
2024-01-05 19:33:41 -06:00
log . Log ( ZONE , "custom dropdown() Config[domain] = nil for domain =" , domain )
2024-01-01 15:31:33 -06:00
CFdialog . domainWidget . SetText ( domain )
CFdialog . zoneWidget . SetText ( "" )
CFdialog . authWidget . SetText ( "" )
CFdialog . emailWidget . SetText ( "" )
} else {
2024-01-05 19:33:41 -06:00
log . Log ( ZONE , "custom dropdown() a =" , domain , Config [ domain ] . ZoneID , Config [ domain ] . Auth , Config [ domain ] . Email )
2024-01-01 15:31:33 -06:00
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 )
return CFdialog . mainWindow
}
2024-01-05 09:38:20 -06:00
func makeConfigWindow ( vb * gui . Node ) {
2024-01-01 15:31:33 -06:00
g1 := vb . NewGroup ( "Cloudflare API Config" )
g1 . NewLabel ( "If you have an API key with access to list all of /n your zone files, enter it here. \n \n Alternatively, you can set the enviroment variables: \n env $CF_API_KEY \n env $CF_API_EMAIL\n" )
// make grid to display credentials
grid := g1 . NewGrid ( "credsGrid" , 2 , 4 ) // width = 2
grid . NewLabel ( "Auth Key" )
aw := grid . NewEntryLine ( "CF_API_KEY" )
aw . SetText ( os . Getenv ( "CF_API_KEY" ) )
grid . NewLabel ( "Email" )
ew := grid . NewEntryLine ( "CF_API_EMAIL" )
ew . SetText ( os . Getenv ( "CF_API_EMAIL" ) )
var url string = "https://api.cloudflare.com/client/v4/zones/"
grid . NewLabel ( "Cloudflare API" )
grid . NewLabel ( url )
hostname := gadgets . NewBasicEntry ( grid , "hostname" )
zone := gadgets . NewBasicEntry ( grid , "domain name" )
grid . Pad ( )
2024-01-18 01:33:28 -06:00
vb . NewButton ( "Lookup Hostname" , func ( ) {
2024-01-18 01:23:49 -06:00
log . Log ( INFO , "Find all the Resource Records for hostname:" , hostname . String ( ) )
log . Log ( INFO , "Find all the Resource Records for zone:" , zone . String ( ) )
GetZones ( aw . String ( ) , ew . String ( ) )
2024-01-01 15:31:33 -06:00
for d , v := range Config {
2024-01-05 19:33:41 -06:00
log . Log ( ZONE , "Zone =" , d , "v =" , v )
2024-01-01 15:31:33 -06:00
}
} )
2024-01-18 01:33:28 -06:00
vb . NewButton ( "getZones()" , func ( ) {
2024-01-05 19:33:41 -06:00
log . Log ( ZONE , "getZones()" )
2024-01-18 01:23:49 -06:00
GetZones ( aw . String ( ) , ew . String ( ) )
2024-01-01 15:31:33 -06:00
for d , _ := range Config {
CFdialog . zonedrop . AddText ( d )
}
} )
2024-01-18 01:33:28 -06:00
vb . NewButton ( "cloudflare wit.com" , func ( ) {
2024-01-01 15:31:33 -06:00
CreateRR ( CFdialog . rootGui , "wit.com" , "3777302ac4a78cd7fa4f6d3f72086d06" )
} )
vb . Pad ( )
vb . Margin ( )
g1 . Pad ( )
g1 . Margin ( )
}
func showCloudflareCredentials ( box * gui . Node ) {
// make grid to display credentials
grid := box . NewGrid ( "credsGrid" , 2 , 4 ) // width = 2
grid . NewLabel ( "Domain" )
CFdialog . domainWidget = grid . NewEntryLine ( "CF_API_DOMAIN" )
grid . NewLabel ( "Zone ID" )
CFdialog . zoneWidget = grid . NewEntryLine ( "CF_API_ZONEID" )
grid . NewLabel ( "Auth Key" )
CFdialog . authWidget = grid . NewEntryLine ( "CF_API_KEY" )
grid . NewLabel ( "Email" )
CFdialog . emailWidget = grid . NewEntryLine ( "CF_API_EMAIL" )
var url string = "https://api.cloudflare.com/client/v4/zones/"
grid . NewLabel ( "Cloudflare API" )
grid . NewLabel ( url )
grid . Pad ( )
2024-01-18 01:33:28 -06:00
CFdialog . loadButton = box . NewButton ( "Load Cloudflare DNS zonefile" , func ( ) {
2024-01-01 15:31:33 -06:00
var domain ConfigT
2024-01-18 01:23:49 -06:00
domain . Domain = CFdialog . domainWidget . String ( )
domain . ZoneID = CFdialog . zoneWidget . String ( )
domain . Auth = CFdialog . authWidget . String ( )
domain . Email = CFdialog . emailWidget . String ( )
2024-01-05 09:38:20 -06:00
LoadZoneWindow ( CFdialog . mainWindow . Box ( ) , & domain )
2024-01-01 15:31:33 -06:00
} )
}