/pkg/templateservicebroker/openservicebroker/api/validation.go

https://gitlab.com/unofficial-mirrors/openshift-origin · Go · 45 lines · 35 code · 10 blank · 0 comment · 10 complexity · c935d77ff17efd1e336ceeca58ba7002 MD5 · raw file

  1. package api
  2. import (
  3. "regexp"
  4. "k8s.io/apimachinery/pkg/api/validation"
  5. "k8s.io/apimachinery/pkg/util/validation/field"
  6. )
  7. func ValidateProvisionRequest(preq *ProvisionRequest) field.ErrorList {
  8. errors := ValidateUUID(field.NewPath("service_id"), preq.ServiceID)
  9. errors = append(errors, ValidateUUID(field.NewPath("plan_id"), preq.PlanID)...)
  10. if preq.Context.Platform == "" {
  11. errors = append(errors, field.Required(field.NewPath("context.platform"), ""))
  12. } else if preq.Context.Platform != ContextPlatformKubernetes {
  13. errors = append(errors, field.Invalid(field.NewPath("context.platform"), preq.Context.Platform, "must equal "+ContextPlatformKubernetes))
  14. }
  15. if preq.Context.Namespace == "" {
  16. errors = append(errors, field.Required(field.NewPath("context.namespace"), ""))
  17. } else {
  18. for _, msg := range validation.ValidateNamespaceName(preq.Context.Namespace, false) {
  19. errors = append(errors, field.Invalid(field.NewPath("context.namespace"), preq.Context.Namespace, msg))
  20. }
  21. }
  22. return errors
  23. }
  24. func ValidateBindRequest(breq *BindRequest) field.ErrorList {
  25. errors := ValidateUUID(field.NewPath("service_id"), breq.ServiceID)
  26. errors = append(errors, ValidateUUID(field.NewPath("plan_id"), breq.PlanID)...)
  27. return errors
  28. }
  29. var uuidRegex = regexp.MustCompile("^(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
  30. func ValidateUUID(path *field.Path, uuid string) field.ErrorList {
  31. if uuidRegex.MatchString(uuid) {
  32. return nil
  33. }
  34. return field.ErrorList{field.Invalid(path, uuid, "must be a valid UUID")}
  35. }