Pass ServiceNetMap to services
[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 def exit_usage():
21     print('Usage %s <yaml file or directory>' % sys.argv[0])
22     sys.exit(1)
23
24
25 def validate_service(filename, tpl):
26     if 'outputs' in tpl and 'role_data' in tpl['outputs']:
27         if 'value' not in tpl['outputs']['role_data']:
28             print('ERROR: invalid role_data for filename: %s'
29                   % filename)
30             return 1
31         role_data = tpl['outputs']['role_data']['value']
32         if 'service_name' not in role_data:
33             print('ERROR: service_name is required in role_data for %s.'
34                   % filename)
35             return 1
36         # service_name must match the filename, but with an underscore
37         if (role_data['service_name'] !=
38                 os.path.basename(filename).split('.')[0].replace("-", "_")):
39             print('ERROR: service_name should match file name for service: %s.'
40                   % filename)
41             return 1
42     if 'parameters' in tpl:
43         required_params = ['EndpointMap', 'ServiceNetMap']
44         for param in required_params:
45             if param not in tpl['parameters']:
46                 print('ERROR: parameter %s is required for %s.'
47                       % (param, filename))
48                 return 1
49     return 0
50
51
52 def validate(filename):
53     print('Validating %s' % filename)
54     retval = 0
55     try:
56         tpl = yaml.load(open(filename).read())
57
58         if (filename.startswith('./puppet/services/') and
59                 filename != './puppet/services/services.yaml'):
60             retval = validate_service(filename, tpl)
61
62     except Exception:
63         print(traceback.format_exc())
64         return 1
65     # yaml is OK, now walk the parameters and output a warning for unused ones
66     for p in tpl.get('parameters', {}):
67         str_p = '\'%s\'' % p
68         in_resources = str_p in str(tpl.get('resources', {}))
69         in_outputs = str_p in str(tpl.get('outputs', {}))
70         if not in_resources and not in_outputs:
71             print('Warning: parameter %s in template %s appears to be unused'
72                   % (p, filename))
73
74     return retval
75
76 if len(sys.argv) < 2:
77     exit_usage()
78
79 path_args = sys.argv[1:]
80 exit_val = 0
81 failed_files = []
82
83 for base_path in path_args:
84     if os.path.isdir(base_path):
85         for subdir, dirs, files in os.walk(base_path):
86             for f in files:
87                 if f.endswith('.yaml'):
88                     file_path = os.path.join(subdir, f)
89                     failed = validate(file_path)
90                     if failed:
91                         failed_files.append(file_path)
92                     exit_val |= failed
93     elif os.path.isfile(base_path) and base_path.endswith('.yaml'):
94         failed = validate(base_path)
95         if failed:
96             failed_files.append(base_path)
97         exit_val |= failed
98     else:
99         print('Unexpected argument %s' % base_path)
100         exit_usage()
101
102 if failed_files:
103     print('Validation failed on:')
104     for f in failed_files:
105         print(f)
106 else:
107     print('Validation successful!')
108 sys.exit(exit_val)