topic.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright 2021 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6.     http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package v1
  14. import (
  15. "net/url"
  16. "strings"
  17. "github.com/pkg/errors"
  18. )
  19. func validateURI(uri string, expectedSchemas []string) error {
  20. parsedURI, err := url.Parse(uri)
  21. if err != nil {
  22. return err
  23. }
  24. schema := strings.ToLower(parsedURI.Scheme)
  25. for _, s := range expectedSchemas {
  26. if s == schema {
  27. return nil
  28. }
  29. }
  30. return errors.Errorf("URI schema %q no in %v", schema, expectedSchemas)
  31. }
  32. func ValidateHTTPSpec(s *HTTPEndpointSpec) error {
  33. return validateURI(s.URI, []string{"http", "https"})
  34. }
  35. func ValidateAMQPSpec(s *AMQPEndpointSpec) error {
  36. return validateURI(s.URI, []string{"amqp", "amqps"})
  37. }
  38. func ValidateKafkaSpec(s *KafkaEndpointSpec) error {
  39. return validateURI(s.URI, []string{"kafka"})
  40. }
  41. // ValidateTopicSpec validate the bucket notification topic arguments
  42. func (t *CephBucketTopic) ValidateTopicSpec() error {
  43. hasEndpoint := false
  44. if t.Spec.Endpoint.HTTP != nil {
  45. hasEndpoint = true
  46. if err := ValidateHTTPSpec(t.Spec.Endpoint.HTTP); err != nil {
  47. return err
  48. }
  49. }
  50. if t.Spec.Endpoint.AMQP != nil {
  51. if hasEndpoint {
  52. return errors.New("multiple endpoint specs")
  53. }
  54. hasEndpoint = true
  55. if err := ValidateAMQPSpec(t.Spec.Endpoint.AMQP); err != nil {
  56. return err
  57. }
  58. }
  59. if t.Spec.Endpoint.Kafka != nil {
  60. if hasEndpoint {
  61. return errors.New("multiple endpoint specs")
  62. }
  63. hasEndpoint = true
  64. if err := ValidateKafkaSpec(t.Spec.Endpoint.Kafka); err != nil {
  65. return err
  66. }
  67. }
  68. if !hasEndpoint {
  69. return errors.New("missing endpoint spec")
  70. }
  71. return nil
  72. }