|
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
|
|
|
import pecan |
|
17
|
|
|
import six |
|
18
|
|
|
|
|
19
|
|
|
from mongoengine import ValidationError |
|
20
|
|
|
from st2api.controllers import resource |
|
21
|
|
|
from st2common import log as logging |
|
22
|
|
|
from st2common.exceptions.apivalidation import ValueValidationException |
|
23
|
|
|
from st2common.models.api.action import ActionAliasAPI |
|
24
|
|
|
from st2common.persistence.actionalias import ActionAlias |
|
25
|
|
|
from st2common.models.api.base import jsexpose |
|
26
|
|
|
from st2common.rbac.types import PermissionType |
|
27
|
|
|
from st2common.rbac.decorators import request_user_has_permission |
|
28
|
|
|
from st2common.rbac.decorators import request_user_has_resource_api_permission |
|
29
|
|
|
from st2common.rbac.decorators import request_user_has_resource_db_permission |
|
30
|
|
|
from st2common.util.alias_matching import (list_format_strings_from_aliases, |
|
31
|
|
|
match_command_to_alias) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
http_client = six.moves.http_client |
|
35
|
|
|
|
|
36
|
|
|
LOG = logging.getLogger(__name__) |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
class ChatopsController(resource.ContentPackResourceController): |
|
40
|
|
|
""" |
|
41
|
|
|
Implements the RESTful interface for Chatops. |
|
42
|
|
|
A super-set of ActionAliasController |
|
43
|
|
|
""" |
|
44
|
|
|
model = ActionAliasAPI |
|
45
|
|
|
access = ActionAlias |
|
46
|
|
|
supported_filters = { |
|
47
|
|
|
'name': 'name', |
|
48
|
|
|
'pack': 'pack' |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
query_options = { |
|
52
|
|
|
'sort': ['pack', 'name'] |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
@request_user_has_permission(permission_type=PermissionType.ACTION_ALIAS_LIST) |
|
56
|
|
|
@jsexpose() |
|
57
|
|
|
def get_all(self, **kwargs): |
|
58
|
|
|
return list_format_strings_from_aliases( |
|
59
|
|
|
super(ChatopsController, self)._get_all(**kwargs)) |
|
60
|
|
|
|
|
61
|
|
|
@request_user_has_resource_db_permission(permission_type=PermissionType.ACTION_ALIAS_VIEW) |
|
62
|
|
|
@jsexpose(arg_types=[str]) |
|
63
|
|
|
def get_one(self, ref_or_id): |
|
|
|
|
|
|
64
|
|
|
return list_format_strings_from_aliases( |
|
65
|
|
|
super(ChatopsController, self)._get_one(ref_or_id))[0] |
|
66
|
|
|
|
|
67
|
|
|
@jsexpose(arg_types=[str], status_code=http_client.CREATED) |
|
68
|
|
|
@request_user_has_resource_api_permission(permission_type=PermissionType.ACTION_ALIAS_CREATE) |
|
69
|
|
|
def post(self, command): |
|
70
|
|
|
""" |
|
71
|
|
|
Run a chatops command |
|
72
|
|
|
|
|
73
|
|
|
Handles requests: |
|
74
|
|
|
POST /chatops/ |
|
75
|
|
|
""" |
|
76
|
|
|
try: |
|
77
|
|
|
# 1. Get aliases |
|
78
|
|
|
aliases = self.get_all() |
|
79
|
|
|
# 2. Match alias(es) to command |
|
80
|
|
|
match = match_command_to_alias(command, aliases) |
|
81
|
|
|
if len(match) > 1: |
|
82
|
|
|
raise AmbiguityError("Too much choice, not enough action (alias).") |
|
|
|
|
|
|
83
|
|
|
# 3. Check user's ability to execute action? |
|
84
|
|
|
# 4. Run action. |
|
85
|
|
|
# action_execution = whatever_the_api_is() |
|
86
|
|
|
except (ValidationError, ValueError, ValueValidationException) as e: |
|
87
|
|
|
# TODO : error on unmatched alias |
|
88
|
|
|
LOG.exception('Validation failed for action alias data=%s.', action_alias) |
|
|
|
|
|
|
89
|
|
|
pecan.abort(http_client.BAD_REQUEST, str(e)) |
|
90
|
|
|
return |
|
91
|
|
|
|
|
92
|
|
|
# extra = {'action_alias_db': action_alias_db} |
|
93
|
|
|
# LOG.audit('Action alias created. ActionAlias.id=%s' % (action_alias_db.id), extra=extra) |
|
94
|
|
|
|
|
95
|
|
|
return action_execution |
|
|
|
|
|
|
96
|
|
|
|