hostname-service/rules/interface.go

50 lines
1.4 KiB
Go

package rules
import (
"fmt"
"log"
"git.beisel.it/florian/hostname-service/db"
)
type HostnameRule interface {
Generate(params map[string]interface{}) (string, []byte, error)
Insert(category string, hostname string, paramsJSON []byte) error
Update(category string, oldhostname string, hostname string, paramsJSON []byte) error
}
type BaseRule struct{}
func (br *BaseRule) baseInsert(category string, hostname string, paramsJSON []byte) error {
exists, err := db.HostnameExists(category, hostname)
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", hostname, category)
}
err = db.InsertHostname(category, hostname, paramsJSON)
if err != nil {
log.Printf("Error inserting hostname into DB: %v", 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)
if err != nil {
log.Printf("Error inserting hostname into DB: %v", err)
return err
}
return nil
}