Files
nginx-proxy-manager/backend/internal/validator/upstreams.go
T

39 lines
1.0 KiB
Go
Raw Normal View History

2023-01-04 15:36:56 +10:00
package validator
import (
"npm/internal/entity/nginxtemplate"
"npm/internal/entity/upstream"
2023-02-24 17:19:07 +10:00
"github.com/rotisserie/eris"
2023-01-04 15:36:56 +10:00
)
// ValidateUpstream will check if associated objects exist and other checks
// will return a nil error if things are OK
func ValidateUpstream(u upstream.Model) error {
// Needs to have more than 1 server
if len(u.Servers) < 2 {
2023-02-24 17:19:07 +10:00
return eris.New("Upstreams require at least 2 servers")
2023-01-04 15:36:56 +10:00
}
// Backup servers aren't permitted with hash balancing
if u.IPHash {
// check all servers for a backup param
for _, server := range u.Servers {
if server.Backup {
2023-02-24 17:19:07 +10:00
return eris.New("Backup servers cannot be used with hash balancing")
2023-01-04 15:36:56 +10:00
}
}
}
// Check the nginx template exists and has the same type.
nginxTemplate, err := nginxtemplate.GetByID(u.NginxTemplateID)
if err != nil {
2023-02-24 17:19:07 +10:00
return eris.Errorf("Nginx Template #%d does not exist", u.NginxTemplateID)
2023-01-04 15:36:56 +10:00
}
if nginxTemplate.Type != "upstream" {
2023-02-24 17:19:07 +10:00
return eris.Errorf("Host Template #%d is not valid for this upstream", u.NginxTemplateID)
2023-01-04 15:36:56 +10:00
}
return nil
}