Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2common/st2common/models/api/policy.py (2 issues)

Labels
Severity
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
from __future__ import absolute_import
17
from st2common.persistence.policy import PolicyType
18
from st2common.models.api.base import BaseAPI
19
from st2common.models.api.base import APIUIDMixin
20
from st2common.models.db.policy import PolicyTypeDB, PolicyDB
21
from st2common import log as logging
22
from st2common.util import schema as util_schema
23
24
25
__all__ = ['PolicyTypeAPI']
26
27
LOG = logging.getLogger(__name__)
28
29
30
class PolicyTypeAPI(BaseAPI, APIUIDMixin):
31
    model = PolicyTypeDB
32
    schema = {
33
        "title": "Policy Type",
34
        "type": "object",
35
        "properties": {
36
            "id": {
37
                "type": "string",
38
                "default": None
39
            },
40
            'uid': {
41
                'type': 'string'
42
            },
43
            "name": {
44
                "type": "string",
45
                "required": True
46
            },
47
            "resource_type": {
48
                "enum": ["action"],
49
                "required": True
50
            },
51
            "ref": {
52
                "type": "string"
53
            },
54
            "description": {
55
                "type": "string"
56
            },
57
            "enabled": {
58
                "type": "boolean",
59
                "default": True
60
            },
61
            "module": {
62
                "type": "string",
63
                "required": True
64
            },
65
            "parameters": {
66
                "type": "object",
67
                "patternProperties": {
68
                    "^\w+$": util_schema.get_draft_schema()
0 ignored issues
show
A suspicious escape sequence \w was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
69
                },
70
                'additionalProperties': False
71
            }
72
        },
73
        "additionalProperties": False
74
    }
75
76
    @classmethod
77
    def to_model(cls, instance):
78
        return cls.model(name=str(instance.name),
79
                         description=getattr(instance, 'description', None),
80
                         resource_type=str(instance.resource_type),
81
                         ref=getattr(instance, 'ref', None),
82
                         enabled=getattr(instance, 'enabled', None),
83
                         module=str(instance.module),
84
                         parameters=getattr(instance, 'parameters', dict()))
85
86
87
class PolicyAPI(BaseAPI, APIUIDMixin):
88
    model = PolicyDB
89
    schema = {
90
        "title": "Policy",
91
        "type": "object",
92
        "properties": {
93
            "id": {
94
                "type": "string",
95
                "default": None
96
            },
97
            'uid': {
98
                'type': 'string'
99
            },
100
            "name": {
101
                "type": "string",
102
                "required": True
103
            },
104
            "pack": {
105
                "type": "string"
106
            },
107
            "ref": {
108
                "type": "string"
109
            },
110
            "description": {
111
                "type": "string"
112
            },
113
            "enabled": {
114
                "type": "boolean",
115
                "default": True
116
            },
117
            "resource_ref": {
118
                "type": "string",
119
                "required": True
120
            },
121
            "policy_type": {
122
                "type": "string",
123
                "required": True
124
            },
125
            "parameters": {
126
                "type": "object",
127
                "patternProperties": {
128
                    "^\w+$": {
0 ignored issues
show
A suspicious escape sequence \w was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
129
                        "anyOf": [
130
                            {"type": "array"},
131
                            {"type": "boolean"},
132
                            {"type": "integer"},
133
                            {"type": "number"},
134
                            {"type": "object"},
135
                            {"type": "string"}
136
                        ]
137
                    }
138
                },
139
                'additionalProperties': False
140
141
            }
142
        },
143
        "additionalProperties": False
144
    }
145
146
    def validate(self):
147
        # Validate policy itself
148
        cleaned = super(PolicyAPI, self).validate()
149
150
        # Validate policy parameters
151
        # pylint: disable=no-member
152
        policy_type_db = PolicyType.get_by_ref(cleaned.policy_type)
153
        if not policy_type_db:
154
            raise ValueError('Referenced policy_type "%s" doesnt exist' % (cleaned.policy_type))
155
156
        parameters_schema = policy_type_db.parameters
157
        parameters = getattr(cleaned, 'parameters', {})
158
        schema = util_schema.get_schema_for_resource_parameters(
159
            parameters_schema=parameters_schema)
160
        validator = util_schema.get_validator()
161
        cleaned_parameters = util_schema.validate(parameters, schema, validator, use_default=True,
162
                                                  allow_default_none=True)
163
164
        cleaned.parameters = cleaned_parameters
165
166
        return cleaned
167
168
    @classmethod
169
    def to_model(cls, instance):
170
        return cls.model(id=getattr(instance, 'id', None),
171
                         name=str(instance.name),
172
                         description=getattr(instance, 'description', None),
173
                         pack=str(instance.pack),
174
                         ref=getattr(instance, 'ref', None),
175
                         enabled=getattr(instance, 'enabled', None),
176
                         resource_ref=str(instance.resource_ref),
177
                         policy_type=str(instance.policy_type),
178
                         parameters=getattr(instance, 'parameters', dict()))
179