forked from jaxxstorm/dnsscale
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
139 lines (121 loc) · 4.01 KB
/
Copy pathconfig.go
File metadata and controls
139 lines (121 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"time"
)
// Config represents the application configuration
type Config struct {
// Tailscale configuration
Tailscale TailscaleConfig `mapstructure:"tailscale" yaml:"tailscale"`
// DNS provider configuration
DNS DNSConfig `mapstructure:"dns" yaml:"dns"`
// Application settings
App AppConfig `mapstructure:"app" yaml:"app"`
// Logging configuration
Logging LoggingConfig `mapstructure:"logging" yaml:"logging"`
}
// TailscaleConfig holds Tailscale-specific configuration
type TailscaleConfig struct {
APIKey string `mapstructure:"api_key" yaml:"api_key"`
Tailnet string `mapstructure:"tailnet" yaml:"tailnet"`
}
// DNSConfig holds DNS provider configuration
type DNSConfig struct {
Provider string `mapstructure:"provider" yaml:"provider"`
Domain string `mapstructure:"domain" yaml:"domain"`
ZoneID string `mapstructure:"zone_id" yaml:"zone_id"`
Route53 Route53Config `mapstructure:"route53" yaml:"route53,omitempty"`
Cloudflare CloudflareConfig `mapstructure:"cloudflare" yaml:"cloudflare,omitempty"`
}
// Route53Config holds AWS Route53 specific configuration
type Route53Config struct {
// AWS credentials can be provided via environment variables or IAM roles
// No additional config needed here for now
Profile string `mapstructure:"profile" yaml:"profile,omitempty"`
Region string `mapstructure:"region" yaml:"region,omitempty"`
}
// CloudflareConfig holds Cloudflare specific configuration
type CloudflareConfig struct {
APIToken string `mapstructure:"api_token" yaml:"api_token"`
}
// AppConfig holds general application configuration
type AppConfig struct {
Workers int `mapstructure:"workers" yaml:"workers"`
PollInterval time.Duration `mapstructure:"poll_interval" yaml:"poll_interval"`
RequiredTags []string `mapstructure:"required_tags" yaml:"required_tags,omitempty"`
}
// LoggingConfig holds logging configuration
type LoggingConfig struct {
Level string `mapstructure:"level" yaml:"level"`
Format string `mapstructure:"format" yaml:"format"` // json or console
}
// Validate checks if the configuration is valid
func (c *Config) Validate() error {
// Validate Tailscale configuration
if c.Tailscale.APIKey == "" {
return fmt.Errorf("tailscale.api_key is required")
}
if c.Tailscale.Tailnet == "" {
return fmt.Errorf("tailscale.tailnet is required")
}
// Validate DNS configuration
if c.DNS.Provider == "" {
return fmt.Errorf("dns.provider is required")
}
if c.DNS.Domain == "" {
return fmt.Errorf("dns.domain is required")
}
if c.DNS.ZoneID == "" {
return fmt.Errorf("dns.zone_id is required")
}
// Provider-specific validation
switch c.DNS.Provider {
case "route53":
// Route53 validation - credentials are typically handled via AWS SDK
case "cloudflare":
if c.DNS.Cloudflare.APIToken == "" {
return fmt.Errorf("dns.cloudflare.api_token is required when using cloudflare provider")
}
default:
return fmt.Errorf("unsupported dns provider: %s (supported: route53, cloudflare)", c.DNS.Provider)
}
// Validate app configuration
if c.App.Workers <= 0 {
c.App.Workers = 2 // Set default
}
if c.App.PollInterval <= 0 {
c.App.PollInterval = 30 * time.Second // Set default
}
// Validate logging configuration
validLevels := []string{"debug", "info", "warn", "error"}
levelValid := false
for _, level := range validLevels {
if c.Logging.Level == level {
levelValid = true
break
}
}
if !levelValid {
if c.Logging.Level == "" {
c.Logging.Level = "info" // Set default
} else {
return fmt.Errorf("invalid logging level: %s (supported: %v)", c.Logging.Level, validLevels)
}
}
validFormats := []string{"json", "console"}
formatValid := false
for _, format := range validFormats {
if c.Logging.Format == format {
formatValid = true
break
}
}
if !formatValid {
if c.Logging.Format == "" {
c.Logging.Format = "console" // Set default
} else {
return fmt.Errorf("invalid logging format: %s (supported: %v)", c.Logging.Format, validFormats)
}
}
return nil
}