52 lines
1.9 KiB
Go
52 lines
1.9 KiB
Go
package validator
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"browser.local/platform/domain"
|
|
)
|
|
|
|
const maxLifecycleIdempotencyKeyLength = 160
|
|
|
|
func ValidateServerLifecycleCreate(create domain.ServerLifecycleCreate) error {
|
|
var violations []string
|
|
violations = appendRequired(violations, "id", create.ID)
|
|
violations = appendRequired(violations, "pluginId", create.PluginID)
|
|
violations = appendRequired(violations, "runEndpointId", create.RunEndpointID)
|
|
violations = appendRequired(violations, "name", create.Name)
|
|
violations = appendRequired(violations, "profileKey", create.ProfileKey)
|
|
violations = appendLifecycleIdempotencyViolations(violations, create.IdempotencyKey)
|
|
return finish(violations)
|
|
}
|
|
|
|
func ValidateServerLifecycleCommand(command domain.ServerLifecycleCommand) error {
|
|
var violations []string
|
|
violations = appendRequired(violations, "serverInstanceId", command.ServerInstanceID)
|
|
if command.ExpectedConfigVersion <= 0 {
|
|
violations = append(violations, "expectedConfigVersion must be positive")
|
|
}
|
|
violations = appendLifecycleIdempotencyViolations(violations, command.IdempotencyKey)
|
|
return finish(violations)
|
|
}
|
|
|
|
func ValidateServerLifecycleAction(action domain.ServerLifecycleAction) error {
|
|
switch action {
|
|
case domain.ServerLifecycleActionCreate, domain.ServerLifecycleActionStart, domain.ServerLifecycleActionStop, domain.ServerLifecycleActionStatus:
|
|
return nil
|
|
default:
|
|
return ValidationError{Violations: []string{fmt.Sprintf("action %q is invalid", action)}}
|
|
}
|
|
}
|
|
|
|
func appendLifecycleIdempotencyViolations(violations []string, key string) []string {
|
|
violations = appendRequired(violations, "idempotencyKey", key)
|
|
if len(key) > maxLifecycleIdempotencyKeyLength {
|
|
violations = append(violations, "idempotencyKey is too long")
|
|
}
|
|
if strings.TrimSpace(key) != key {
|
|
violations = append(violations, "idempotencyKey must not have surrounding whitespace")
|
|
}
|
|
return violations
|
|
}
|