13 Commits

Author SHA1 Message Date
Lourenço Vales
3b3b7aebb1 final fixes 2025-10-05 21:49:17 +02:00
Lourenço Vales
2182e2a4ea small typo 2025-10-05 21:36:38 +02:00
Lourenço Vales
ce4d2bfe50 changed validation for MX record corner case 2025-10-05 21:34:37 +02:00
Lourenço Vales
1787e44503 added more robust validation; small changes 2025-10-05 21:22:48 +02:00
Lourenço Vales
bc4f7b7309 fixed slight error in validation 2025-10-05 20:59:27 +02:00
Lourenço Vales
f7d8b42c7b adding doc comments 2025-10-05 20:49:02 +02:00
Lourenço Vales
1fb3ef1e71 fixed record deletion by changing the matching condition 2025-10-05 10:55:50 +02:00
Lourenço Vales
3e153f7f44 adding Poll condition to guarantee minimum time between tries 2025-10-05 10:39:47 +02:00
Lourenço Vales
8b30f7bd3d everything is implemented, now on to testing 2025-10-03 15:00:35 +02:00
Lourenço Vales
d304dafeea added partial cloudflare api integration 2025-10-03 09:29:32 +02:00
Lourenço Vales
9271906435 added cmp function 2025-10-03 09:29:32 +02:00
Lourenço Vales
7146139ae8 added CheckApply function; made some changes to structure 2025-10-03 09:29:32 +02:00
Lourenço Vales
ef74ede862 engine: resources: Add Cloudflare DNS resource 2025-10-03 09:29:32 +02:00
2 changed files with 17 additions and 29 deletions

View File

@@ -51,10 +51,10 @@ func init() {
// CloudflareDNSRes is a resource for managing DNS records in Cloudflare zones. // CloudflareDNSRes is a resource for managing DNS records in Cloudflare zones.
// This resource uses the Cloudflare API to create, update, and delete DNS // This resource uses the Cloudflare API to create, update, and delete DNS
// records in a specified zone. It supports various record types including A, // records in a specified zone. It supports various record types including A,
// AAAA, CNAME, MX, TXT, NS, and PTR records. The resource requires polling to // AAAA, CNAME, MX, TXT, NS, SRV, and PTR records. The resource requires polling
// detect changes, as the Cloudflare API does not provide an event stream. The // to detect changes, as the Cloudflare API does not provide an event stream.
// Purge functionality allows enforcing that only managed DNS records exist in // The Purge functionality allows enforcing that only managed DNS records exist
// the zone, removing any unmanaged records. // in the zone, removing any unmanaged records.
type CloudflareDNSRes struct { type CloudflareDNSRes struct {
traits.Base traits.Base
traits.GraphQueryable traits.GraphQueryable
@@ -130,7 +130,7 @@ func (obj *CloudflareDNSRes) Validate() error {
} }
if obj.APIToken == "" { if obj.APIToken == "" {
return fmt.Errorf("api token is required") return fmt.Errorf("API token is required")
} }
if obj.Type == "" { if obj.Type == "" {
@@ -138,7 +138,7 @@ func (obj *CloudflareDNSRes) Validate() error {
} }
if (obj.TTL < 60 || obj.TTL > 86400) && obj.TTL != 1 { // API requirement if (obj.TTL < 60 || obj.TTL > 86400) && obj.TTL != 1 { // API requirement
return fmt.Errorf("ttl must be between 60s and 86400s, or set to 1") return fmt.Errorf("TTL must be between 60s and 86400s, or set to 1")
} }
if obj.Zone == "" { if obj.Zone == "" {
@@ -578,7 +578,7 @@ func (obj *CloudflareDNSRes) needsUpdate(record dns.RecordResponse) bool {
return true return true
} }
//TODO: add more checks? // TODO add more checks?
return false return false
@@ -632,7 +632,7 @@ func (obj *CloudflareDNSRes) purgeCheckApply(ctx context.Context, apply bool) (b
cfRes.Content) cfRes.Content)
if cfRes.Priority != nil { if cfRes.Priority != nil {
// corner case for MX records which require priority set // corner case for MX records which require priority set
recordKey = fmt.Sprintf("%s:%g", recordKey, *cfRes.Priority) recordKey = fmt.Sprintf("%s:%d", recordKey, *cfRes.Priority)
} }
excludes[recordKey] = true excludes[recordKey] = true
} }
@@ -644,7 +644,7 @@ func (obj *CloudflareDNSRes) purgeCheckApply(ctx context.Context, apply bool) (b
recordKey := fmt.Sprintf("%s:%s:%s", record.Name, record.Type, recordKey := fmt.Sprintf("%s:%s:%s", record.Name, record.Type,
record.Content) record.Content)
if record.Priority != 0 { if record.Priority != 0 {
recordKey = fmt.Sprintf("%s:%g", recordKey, record.Priority) recordKey = fmt.Sprintf("%s:%d", recordKey, record.Priority)
} }
if excludes[recordKey] { if excludes[recordKey] {
@@ -679,24 +679,22 @@ func (obj *CloudflareDNSRes) GraphQueryAllowed(opts ...engine.GraphQueryableOpti
return nil return nil
} }
// matchesRecordName checks if a record name from the API matches our desired // matchesRecordName checks if a record name from the API matches our desired record name.
// record name. Handles both FQDN (www.example.com) and short form (www) // Handles both FQDN (www.example.com) and short form (www) comparisons.
// comparisons.
func (obj *CloudflareDNSRes) matchesRecordName(apiRecordName string) bool { func (obj *CloudflareDNSRes) matchesRecordName(apiRecordName string) bool {
desired := obj.normalizeRecordName(obj.RecordName) desired := obj.normalizeRecordName(obj.RecordName)
actual := obj.normalizeRecordName(apiRecordName) actual := obj.normalizeRecordName(apiRecordName)
return desired == actual return desired == actual
} }
// normalizeRecordName converts a record name to a consistent format for // normalizeRecordName converts a record name to a consistent format for comparison.
// comparison. Converts to FQDN format (e.g., "www" -> "www.example.com", "@" -> // Converts to FQDN format (e.g., "www" -> "www.example.com", "@" -> "example.com")
// "example.com")
func (obj *CloudflareDNSRes) normalizeRecordName(name string) string { func (obj *CloudflareDNSRes) normalizeRecordName(name string) string {
if name == "@" || name == obj.Zone { if name == "@" || name == obj.Zone {
return obj.Zone return obj.Zone
} }
if strings.HasSuffix(name, "."+obj.Zone) { if strings.HasSuffix(name, "."+obj.Zone) || name == obj.Zone {
return name return name
} }

View File

@@ -36,7 +36,6 @@ import (
"net/url" "net/url"
"os" "os"
"os/exec" "os/exec"
"os/user"
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
@@ -622,15 +621,6 @@ func (obj *VirtBuilderRes) CheckApply(ctx context.Context, apply bool) (bool, er
cmd := exec.CommandContext(ctx, cmdName, cmdArgs...) cmd := exec.CommandContext(ctx, cmdName, cmdArgs...)
usr, err := user.Current()
if err != nil {
return false, err
}
// FIXME: consider building this from an empty environment?
cmd.Env = append(os.Environ(),
fmt.Sprintf("HOME=%s", usr.HomeDir),
)
// ignore signals sent to parent process (we're in our own group) // ignore signals sent to parent process (we're in our own group)
cmd.SysProcAttr = &syscall.SysProcAttr{ cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true, Setpgid: true,
@@ -648,7 +638,7 @@ func (obj *VirtBuilderRes) CheckApply(ctx context.Context, apply bool) (bool, er
return false, errwrap.Wrapf(err, "error starting cmd") return false, errwrap.Wrapf(err, "error starting cmd")
} }
cmderr := cmd.Wait() // we can unblock this with the timeout err := cmd.Wait() // we can unblock this with the timeout
out := b.String() out := b.String()
p := path.Join(obj.varDir, fmt.Sprintf("%d.log", start)) p := path.Join(obj.varDir, fmt.Sprintf("%d.log", start))
@@ -658,7 +648,7 @@ func (obj *VirtBuilderRes) CheckApply(ctx context.Context, apply bool) (bool, er
} }
} }
if cmderr == nil { if err == nil {
obj.init.Logf("built image successfully!") obj.init.Logf("built image successfully!")
return false, nil // success! return false, nil // success!
} }
@@ -677,7 +667,7 @@ func (obj *VirtBuilderRes) CheckApply(ctx context.Context, apply bool) (bool, er
obj.init.Logf("deleted partial output") obj.init.Logf("deleted partial output")
} }
exitErr, ok := cmderr.(*exec.ExitError) // embeds an os.ProcessState exitErr, ok := err.(*exec.ExitError) // embeds an os.ProcessState
if !ok { if !ok {
// command failed in some bad way // command failed in some bad way
return false, errwrap.Wrapf(err, "cmd failed in some bad way") return false, errwrap.Wrapf(err, "cmd failed in some bad way")