OpenStack Service Types Authority
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.3 KiB

  1. # Copyright (c) 2017 Red Hat, Inc.
  2. #
  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. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import jsonschema
  16. def validate_schema(schema, data, resolver=None):
  17. """Validate mapping against schema using the given resolver.
  18. :return: An iterator over messages for any errors encountered.
  19. """
  20. validator = jsonschema.Draft4Validator(schema, resolver=resolver)
  21. for error in validator.iter_errors(data):
  22. yield error.message
  23. def validate_unique_tokens(data):
  24. """Ensure service types, aliases and primary projects are all unique.
  25. :return: An iterator over messages for any errors encountered.
  26. """
  27. service_types = []
  28. aliases = []
  29. projects = []
  30. for service in data['services']:
  31. service_types.append(service['service_type'])
  32. if "aliases" in service:
  33. for alias in service['aliases']:
  34. if alias in aliases:
  35. yield "Alias '{alias}' appears twice".format(alias=alias)
  36. aliases.append(alias)
  37. if service.get('secondary', False):
  38. continue
  39. if service['project'] in projects:
  40. yield "'{service}' is duplicate service from '{project}'".format(
  41. service=service['service_type'],
  42. project=service['project'])
  43. projects.append(service['project'])
  44. for alias in aliases:
  45. if alias in service_types:
  46. yield "Alias '{alias}' conflicts with a service_type".format(
  47. alias=alias)
  48. def validate_all(schema, data, resolver=None):
  49. """Runs all validators, printing any errors to stdout.
  50. :return: True if all checks passed; False if any checks failed.
  51. """
  52. ret = True
  53. for msg in validate_schema(schema, data, resolver=resolver):
  54. print(msg)
  55. ret = False
  56. for msg in validate_unique_tokens(data):
  57. print(msg)
  58. ret = False
  59. return ret