2022-05-11 18:47:31 -04:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"npm/internal/entity/certificate"
|
|
|
|
"npm/internal/entity/host"
|
2023-01-04 00:36:56 -05:00
|
|
|
"npm/internal/entity/nginxtemplate"
|
2022-05-11 18:47:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateHost will check if associated objects exist and other checks
|
|
|
|
// will return a nil error if things are OK
|
|
|
|
func ValidateHost(h host.Model) error {
|
|
|
|
if h.CertificateID > 0 {
|
|
|
|
// Check certificate exists and is valid
|
|
|
|
// This will not determine if the certificate is Ready to use,
|
|
|
|
// as this validation only cares that the row exists.
|
|
|
|
if _, cErr := certificate.GetByID(h.CertificateID); cErr != nil {
|
|
|
|
return fmt.Errorf("Certificate #%d does not exist", h.CertificateID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 00:36:56 -05:00
|
|
|
// Check the nginx template exists and has the same type.
|
|
|
|
nginxTemplate, tErr := nginxtemplate.GetByID(h.NginxTemplateID)
|
2022-05-11 18:47:31 -04:00
|
|
|
if tErr != nil {
|
2023-01-04 00:36:56 -05:00
|
|
|
return fmt.Errorf("Host Template #%d does not exist", h.NginxTemplateID)
|
2022-05-11 18:47:31 -04:00
|
|
|
}
|
2023-01-04 00:36:56 -05:00
|
|
|
if nginxTemplate.Type != h.Type {
|
|
|
|
return fmt.Errorf("Host Template #%d is not valid for this host type", h.NginxTemplateID)
|
2022-05-11 18:47:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|