cloudflare: handle init failures properly
Otherwise the process explodes with: ``` panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x656b1b] goroutine 1 [running]: infra-sshfp-cf/cloudflare.(*service).FindHostByName(0xc000436600, {0xc00030c510, 0x18}) /home/admin/sshfp-generator/cloudflare/service.go:25 +0x15b main.main() /home/admin/sshfp-generator/main.go:89 +0x8d0 exit status 2 ``` Due to inability to find the configured zone. Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
1d070a80c4
commit
16bc968e21
|
@ -15,20 +15,26 @@ type repository struct {
|
|||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewRepository(token string, domain string) Repository {
|
||||
func NewRepository(token string, domain string) (Repository, error) {
|
||||
logrus.Infof("cloudflare: Creating Repository")
|
||||
api, err := cloudflare.NewWithAPIToken(token)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return nil
|
||||
logrus.Errorf("cloudflare: token init failure: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id, err := api.ZoneIDByName(domain)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return nil
|
||||
logrus.Errorf("cloudflare: zone init failure: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
return &repository{api: api, domainId: id, domainName: domain, ctx: context.Background()}
|
||||
repo := repository{
|
||||
api: api,
|
||||
domainId: id,
|
||||
domainName: domain,
|
||||
ctx: context.Background(),
|
||||
}
|
||||
return &repo, nil
|
||||
}
|
||||
|
||||
func (r *repository) FindRecords(hostname string, recordType string) ([]cloudflare.DNSRecord, error) {
|
||||
|
|
7
main.go
7
main.go
|
@ -41,7 +41,12 @@ func main() {
|
|||
}
|
||||
|
||||
//Create cloudflare components
|
||||
cloudflare := cloudflare.NewService(cloudflare.NewRepository(config.CloudflareToken, config.DomainName))
|
||||
cfRepo, err := cloudflare.NewRepository(config.CloudflareToken, config.DomainName)
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to Init CloudFlare: %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
cloudflare := cloudflare.NewService(cfRepo)
|
||||
|
||||
//Create buffer to catch output from consul
|
||||
var buf bytes.Buffer
|
||||
|
|
Loading…
Reference in New Issue