Test Failed
Pull Request — master (#3376)
by Lakshmi
45:01
created

_validate_definitions()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
"""
17
A script that validates each entry defined in OpenAPI-Spec for st2 APIs
18
(in st2common/openapi.yaml) has a corresponding API model class defined
19
in st2common/models/api/.
20
"""
21
22
import os
23
import pprint
24
import sys
25
26
from oslo_config import cfg
27
from prance import ResolvingParser
28
29
from st2common import config
30
from st2common import log as logging
31
from st2common.util import spec_loader
32
from st2common.script_setup import setup as common_setup
33
from st2common.script_setup import teardown as common_teardown
34
35
36
__all__ = [
37
    'main'
38
]
39
40
41
cfg.CONF.register_cli_opt(
42
    cfg.StrOpt('spec-file', short='f', required=False,
43
               default='st2common/st2common/openapi.yaml')
44
)
45
46
cfg.CONF.register_cli_opt(
47
    cfg.BoolOpt('generate', short='-c', required=False,
48
                default=False)
49
)
50
51
LOG = logging.getLogger(__name__)
52
53
54
def setup():
55
    common_setup(config=config, setup_db=False, register_mq_exchanges=False)
56
57
58
def _validate_definitions(spec):
59
    defs = spec.get('definitions', None)
60
61
    for (model, definition) in defs.iteritems():
62
        api_model = definition.get('x-api-model', None)
63
64
        if not api_model:
65
            msg = (
66
                'API model field "x-api-model" not defined for definition %s.' % model +
67
                ' Supplied definition is %s.' % definition
68
            )
69
70
            raise Exception(msg)
71
72
73
def validate_spec():
74
    spec_file = cfg.CONF.spec_file
75
    generate_spec = cfg.CONF.generate
76
77
    if not os.path.exists(spec_file) and not generate_spec:
78
        msg = ('No spec file found in location %s. ' % spec_file +
79
               'Provide a valid spec file or ' +
80
               'pass --generate-api-spec to genrate a spec.')
81
        raise Exception(msg)
82
83
    if generate_spec:
84
        if not spec_file:
85
            raise Exception('Supply a path to write to spec file to.')
86
87
        spec_string = spec_loader.generate_spec('st2common', 'openapi.yaml.j2')
88
89
        with open(spec_file, 'w') as f:
90
            f.write(spec_string)
91
            f.flush()
92
93
    parser = ResolvingParser(spec_file)
94
    spec = parser.specification
95
    pp = pprint.PrettyPrinter(indent=4)
96
    pp.pprint(spec)
97
    _validate_definitions(spec)
98
99
100
def teartown():
101
    common_teardown()
102
103
def main():
104
    try:
105
        setup()
106
        validate_spec()
107
    except Exception, e:
108
        LOG.error(e.message)
109
        return 1
110
    finally:
111
        teartown()
112