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 mongoengine import ValidationError |
||
17 | import six |
||
18 | |||
19 | from st2common import log as logging |
||
20 | from st2common.models.api.rule import RuleTypeAPI |
||
21 | from st2common.persistence.rule import RuleType |
||
22 | from st2common.router import abort |
||
23 | |||
24 | http_client = six.moves.http_client |
||
25 | |||
26 | LOG = logging.getLogger(__name__) |
||
27 | |||
28 | |||
29 | class RuleTypesController(object): |
||
30 | """ |
||
31 | Implements the RESTful web endpoint that handles |
||
32 | the lifecycle of a RuleType in the system. |
||
33 | """ |
||
34 | |||
35 | @staticmethod |
||
36 | def __get_by_id(id): |
||
37 | try: |
||
38 | return RuleType.get_by_id(id) |
||
39 | except (ValueError, ValidationError) as e: |
||
40 | msg = 'Database lookup for id="%s" resulted in exception. %s' % (id, e) |
||
41 | LOG.exception(msg) |
||
42 | abort(http_client.NOT_FOUND, msg) |
||
43 | |||
44 | @staticmethod |
||
45 | def __get_by_name(name): |
||
46 | try: |
||
47 | return [RuleType.get_by_name(name)] |
||
48 | except ValueError as e: |
||
49 | LOG.debug('Database lookup for name="%s" resulted in exception : %s.', name, e) |
||
50 | return [] |
||
51 | |||
52 | def get_one(self, id): |
||
0 ignored issues
–
show
|
|||
53 | """ |
||
54 | List RuleType objects by id. |
||
55 | |||
56 | Handle: |
||
57 | GET /ruletypes/1 |
||
58 | """ |
||
59 | ruletype_db = RuleTypesController.__get_by_id(id) |
||
60 | ruletype_api = RuleTypeAPI.from_model(ruletype_db) |
||
61 | return ruletype_api |
||
62 | |||
63 | def get_all(self): |
||
64 | """ |
||
65 | List all RuleType objects. |
||
66 | |||
67 | Handles requests: |
||
68 | GET /ruletypes/ |
||
69 | """ |
||
70 | ruletype_dbs = RuleType.get_all() |
||
71 | ruletype_apis = [RuleTypeAPI.from_model(runnertype_db) |
||
72 | for runnertype_db in ruletype_dbs] |
||
73 | return ruletype_apis |
||
74 | |||
75 | |||
76 | rule_types_controller = RuleTypesController() |
||
77 |
It is generally discouraged to redefine built-ins as this makes code very hard to read.