🏗️ Move calling Generate function to rules

This change moves the responsibility of calling the HostnameRule.Generate
function to the corresponding HostnameRule.{Insert,Update} functions
which will allow autonomy especially for the update functions on when
a hostname needs to be regenerated.
This commit is contained in:
Florian Beisel 2024-01-21 16:00:33 +01:00
parent df6a785ac4
commit f4b6197728
Signed by: florian
GPG Key ID: 79ECA2E54996FF4D
1 changed files with 35 additions and 23 deletions

View File

@ -9,41 +9,53 @@ import (
type HostnameRule interface { type HostnameRule interface {
Generate(params map[string]interface{}) (string, []byte, error) Generate(params map[string]interface{}) (string, []byte, error)
Insert(category string, hostname string, paramsJSON []byte) error Insert(category string, params map[string]interface{}) (string, error)
Update(category string, oldhostname string, hostname string, paramsJSON []byte) error Update(category string, oldhostname string, params map[string]interface{}) (string, error)
} }
type BaseRule struct{} type BaseRule struct{}
func (br *BaseRule) baseInsert(category string, hostname string, paramsJSON []byte) error { func (br *BaseRule) baseInsert(rule HostnameRule, category string, params map[string]interface{}) (string, error) {
// Generate the hostname using the passed rule's Generate method
hostname, paramsJSON, err := rule.Generate(params)
if err != nil {
return "", err
}
exists, err := db.HostnameExists(category, hostname) exists, err := db.HostnameExists(category, hostname)
if err != nil { if err != nil {
return fmt.Errorf("error checking existence of hostname: %v", err.Error()) log.Printf("error checking existence of hostname: %v", err)
return "", err
} }
if exists { if exists {
return fmt.Errorf("hostname %s does not exist in category %s", hostname, category) log.Printf("hostname %s does not exist in category %s", hostname, category)
return "", fmt.Errorf("hostname-exists")
} }
// Insert the hostname into the database
err = db.InsertHostname(category, hostname, paramsJSON) err = db.InsertHostname(category, hostname, paramsJSON)
if err != nil { if err != nil {
log.Printf("Error inserting hostname into DB: %v", err) log.Printf("Error inserting hostname into DB: %v", err)
return err return "", err
}
return nil
}
func (br *BaseRule) baseUpdate(category string, oldhostname string, hostname string, paramsJSON []byte) error {
exists, err := db.HostnameExists(category, oldhostname)
if err != nil {
return fmt.Errorf("error checking existence of hostname: %v", err.Error())
}
if !exists {
return fmt.Errorf("hostname %s does not exist in category %s", oldhostname, category)
} }
err = db.UpdateHostname(category, oldhostname, hostname, paramsJSON) return hostname, nil
if err != nil { }
log.Printf("Error inserting hostname into DB: %v", err)
return err // baseUpdate method for BaseRule
} func (br *BaseRule) baseUpdate(rule HostnameRule, category string, oldhostname string, params map[string]interface{}) (string, error) {
return nil // Generate the new hostname using the passed rule's Generate method
newHostname, paramsJSON, err := rule.Generate(params)
if err != nil {
return "", err
}
// Update the hostname in the database
err = db.UpdateHostname(category, oldhostname, newHostname, paramsJSON)
if err != nil {
log.Printf("Error updating hostname in DB: %v", err)
return "", err
}
return newHostname, nil
} }