Allow Glance API and Registry to be split
[apex-tripleo-heat-templates.git] / tools / yaml-validate.py
1 #!/usr/bin/env python
2 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
3 #    not use this file except in compliance with the License. You may obtain
4 #    a copy of the License at
5 #
6 #         http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #    Unless required by applicable law or agreed to in writing, software
9 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 #    License for the specific language governing permissions and limitations
12 #    under the License.
13
14 import os
15 import sys
16 import traceback
17 import yaml
18
19
20 required_params = ['EndpointMap', 'ServiceNetMap', 'DefaultPasswords']
21
22 def exit_usage():
23     print('Usage %s <yaml file or directory>' % sys.argv[0])
24     sys.exit(1)
25
26
27 def validate_service(filename, tpl):
28     if 'outputs' in tpl and 'role_data' in tpl['outputs']:
29         if 'value' not in tpl['outputs']['role_data']:
30             print('ERROR: invalid role_data for filename: %s'
31                   % filename)
32             return 1
33         role_data = tpl['outputs']['role_data']['value']
34         if 'service_name' not in role_data:
35             print('ERROR: service_name is required in role_data for %s.'
36                   % filename)
37             return 1
38         # service_name must match the filename, but with an underscore
39         if (role_data['service_name'] !=
40                 os.path.basename(filename).split('.')[0].replace("-", "_")):
41             print('ERROR: service_name should match file name for service: %s.'
42                   % filename)
43             return 1
44     if 'parameters' in tpl:
45         for param in required_params:
46             if param not in tpl['parameters']:
47                 print('ERROR: parameter %s is required for %s.'
48                       % (param, filename))
49                 return 1
50     return 0
51
52
53 def validate(filename):
54     print('Validating %s' % filename)
55     retval = 0
56     try:
57         tpl = yaml.load(open(filename).read())
58
59         if (filename.startswith('./puppet/services/') and
60                 filename != './puppet/services/services.yaml'):
61             retval = validate_service(filename, tpl)
62
63     except Exception:
64         print(traceback.format_exc())
65         return 1
66     # yaml is OK, now walk the parameters and output a warning for unused ones
67     if 'heat_template_version' in tpl:
68         for p in tpl.get('parameters', {}):
69             if p in required_params:
70                 continue
71             str_p = '\'%s\'' % p
72             in_resources = str_p in str(tpl.get('resources', {}))
73             in_outputs = str_p in str(tpl.get('outputs', {}))
74             if not in_resources and not in_outputs:
75                 print('Warning: parameter %s in template %s '
76                       'appears to be unused' % (p, filename))
77
78     return retval
79
80 if len(sys.argv) < 2:
81     exit_usage()
82
83 path_args = sys.argv[1:]
84 exit_val = 0
85 failed_files = []
86
87 for base_path in path_args:
88     if os.path.isdir(base_path):
89         for subdir, dirs, files in os.walk(base_path):
90             for f in files:
91                 if f.endswith('.yaml') and not f.endswith('.j2.yaml'):
92                     file_path = os.path.join(subdir, f)
93                     failed = validate(file_path)
94                     if failed:
95                         failed_files.append(file_path)
96                     exit_val |= failed
97     elif os.path.isfile(base_path) and base_path.endswith('.yaml'):
98         failed = validate(base_path)
99         if failed:
100             failed_files.append(base_path)
101         exit_val |= failed
102     else:
103         print('Unexpected argument %s' % base_path)
104         exit_usage()
105
106 if failed_files:
107     print('Validation failed on:')
108     for f in failed_files:
109         print(f)
110 else:
111     print('Validation successful!')
112 sys.exit(exit_val)