Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor chatops to process input alias matching server side #2895

Merged
merged 66 commits into from
Sep 15, 2016
Merged
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
18aad27
MongoDB suffered the same fate, doesn't support chkconfig on newer ve…
tonybaloney Oct 23, 2015
85e66f2
only yum was starting mongo, apt now does too.
tonybaloney Oct 23, 2015
e5045b9
get mongo status and start as a sudo
tonybaloney Oct 23, 2015
5e17291
Update deb list for docker to use a different repo
tonybaloney Oct 26, 2015
84bda92
Merge branch 'master' of https://github.com/StackStorm/st2
tonybaloney Nov 11, 2015
4329758
Merge branch 'master' of https://github.com/StackStorm/st2
tonybaloney Nov 23, 2015
e991ae6
Merge remote-tracking branch 'me/master'
Sep 12, 2016
a01a5c2
add a util class for pattern matching, should not require API lib
tonybaloney Sep 12, 2016
af0a1b3
reset st2_deploy.sh
tonybaloney Sep 12, 2016
1e1446b
massively simplify the util methods based on existing code (thanks @e…
tonybaloney Sep 12, 2016
62ddf51
__all__ convention
tonybaloney Sep 12, 2016
dce3e12
add a chatops controller for sending chatops commands to
tonybaloney Sep 12, 2016
cdd294c
ipv6 binding support for the dev script
tonybaloney Sep 13, 2016
88abf6e
correct command help text
tonybaloney Sep 13, 2016
dffd48b
Merge branch 'ipv6dev' into serversiderules
tonybaloney Sep 13, 2016
b6d70ec
added match custom command, added new exception type for alias matching
tonybaloney Sep 13, 2016
166c2b3
moved the alias matching functions to new module name to better fit i…
tonybaloney Sep 13, 2016
82fabd0
decorators in the wrong order
tonybaloney Sep 13, 2016
003d220
change the rbac decorator to allow custom method names
tonybaloney Sep 13, 2016
388d6a6
remove the permissions checks, I dont think they apply here
tonybaloney Sep 13, 2016
7c2f07e
I didn't mean to do that
tonybaloney Sep 13, 2016
3c0a989
add tests for the util module
tonybaloney Sep 13, 2016
bb10deb
extend tests
tonybaloney Sep 13, 2016
0346b33
extend tests to checkout the matchin command logic
tonybaloney Sep 13, 2016
80f04a7
debug detailed info on type error for jsexpose func wrapper
tonybaloney Sep 14, 2016
5700dd9
change the alias API to call base class directly
tonybaloney Sep 14, 2016
de9a845
Merge branch 'serversiderules' of github.com:tonybaloney/st2 into ser…
tonybaloney Sep 14, 2016
01249f2
return the options when failing on too many matches
tonybaloney Sep 14, 2016
49f8e6e
return a dict instead of a list of tuples
tonybaloney Sep 14, 2016
c4600c1
flake it before you make it
tonybaloney Sep 14, 2016
903b386
fix broken test
tonybaloney Sep 14, 2016
3a77fc8
add test for api
tonybaloney Sep 14, 2016
3f2cf9b
add tests for the API controller action, both pass and fail
tonybaloney Sep 14, 2016
a20ea84
take a stab at a new command in the client
tonybaloney Sep 14, 2016
d087128
Merge branch 'serversiderules' of github.com:tonybaloney/st2 into ser…
tonybaloney Sep 14, 2016
8b7383b
change the way the client command is registered copying custom comman…
tonybaloney Sep 14, 2016
84c83b3
lint fixes in new test and CLI command
tonybaloney Sep 14, 2016
a9c9798
add an alias execute command
tonybaloney Sep 14, 2016
d1cc1b5
use the correct resource type on execute command
tonybaloney Sep 14, 2016
e792d1b
remove conflicting parameters
tonybaloney Sep 14, 2016
0a641e8
use mixin from action runner command, remove dep on DB methods outsid…
tonybaloney Sep 14, 2016
3fc9790
set poll interval
tonybaloney Sep 14, 2016
44f7244
use the print as a mixin from the action execute command
tonybaloney Sep 14, 2016
6b95434
Merge branch 'serversiderules' of github.com:tonybaloney/st2 into ser…
tonybaloney Sep 14, 2016
2031095
too hard to maintain 2 mixins.
tonybaloney Sep 14, 2016
67d807b
Merge branch 'serversiderules' of github.com:tonybaloney/st2 into ser…
tonybaloney Sep 14, 2016
164dffa
remove duplicate class properties
tonybaloney Sep 14, 2016
94e988e
fix lint errors on api test
tonybaloney Sep 14, 2016
ac003c1
update based on comments from @kami
tonybaloney Sep 14, 2016
a615f99
include command in ActionAliasAmbiguityException
tonybaloney Sep 15, 2016
041dc34
fix test with new logic
tonybaloney Sep 15, 2016
8754931
flake fixes to new tests
tonybaloney Sep 15, 2016
aa43362
update the unit tests to patch out the UID generator instead of creat…
tonybaloney Sep 15, 2016
3ac1ff3
pylint fixes
tonybaloney Sep 15, 2016
f002d3f
Merge branch 'serversiderules' of github.com:tonybaloney/st2 into ser…
tonybaloney Sep 15, 2016
179f9e7
refactor the CLI client to not use the common classes and call the AP…
tonybaloney Sep 15, 2016
3a519ab
use the correct base class for the command
tonybaloney Sep 15, 2016
5c71fb0
add run_and_print method
tonybaloney Sep 15, 2016
f579dfd
mmmmm import *
tonybaloney Sep 15, 2016
62ff395
move the match method to the manager not the model
tonybaloney Sep 15, 2016
3096601
use a raw request method
tonybaloney Sep 15, 2016
0ead24d
refactor match method to correct deserialize responses
tonybaloney Sep 15, 2016
e66bad4
use fields instead keys
tonybaloney Sep 15, 2016
f8ccbf5
parse the correct URL and format the response message
tonybaloney Sep 15, 2016
23aa2a2
format the execute response with the execution ID and the chatops res…
tonybaloney Sep 15, 2016
21a737a
fixed broken test with missing space in expected answer
tonybaloney Sep 15, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 95 additions & 0 deletions st2api/st2api/controllers/v1/chatops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pecan
import six

from mongoengine import ValidationError
from st2api.controllers import resource
from st2common import log as logging
from st2common.exceptions.apivalidation import ValueValidationException
from st2common.models.api.action import ActionAliasAPI
from st2common.persistence.actionalias import ActionAlias
from st2common.models.api.base import jsexpose
from st2common.rbac.types import PermissionType
from st2common.rbac.decorators import request_user_has_permission
from st2common.rbac.decorators import request_user_has_resource_api_permission
from st2common.rbac.decorators import request_user_has_resource_db_permission
from st2common.util.alias_matching import (list_format_strings_from_aliases,
match_command_to_alias)


http_client = six.moves.http_client

LOG = logging.getLogger(__name__)


class ChatopsController(resource.ContentPackResourceController):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opinions here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to keep those methods / operations on the alias controller?

The reason is that right now StackStorm concept is called action alias and the code doesn't use "ChatOps" term anywhere else (afaik) so it might be a bit confusing to call it ChatOps here...

"""
Implements the RESTful interface for Chatops.
A super-set of ActionAliasController
"""
model = ActionAliasAPI
access = ActionAlias
supported_filters = {
'name': 'name',
'pack': 'pack'
}

query_options = {
'sort': ['pack', 'name']
}

@request_user_has_permission(permission_type=PermissionType.ACTION_ALIAS_LIST)
@jsexpose()
def get_all(self, **kwargs):
return list_format_strings_from_aliases(
super(ChatopsController, self)._get_all(**kwargs))

@request_user_has_resource_db_permission(permission_type=PermissionType.ACTION_ALIAS_VIEW)
@jsexpose(arg_types=[str])
def get_one(self, ref_or_id):
return list_format_strings_from_aliases(
super(ChatopsController, self)._get_one(ref_or_id))[0]

@jsexpose(arg_types=[str], status_code=http_client.CREATED)
@request_user_has_resource_api_permission(permission_type=PermissionType.ACTION_ALIAS_CREATE)
def post(self, command):
"""
Run a chatops command

Handles requests:
POST /chatops/
"""
try:
# 1. Get aliases
aliases = self.get_all()
# 2. Match alias(es) to command
match = match_command_to_alias(command, aliases)
if len(match) > 1:
raise AmbiguityError("Too much choice, not enough action (alias).")
# 3. Check user's ability to execute action?
# 4. Run action.
# action_execution = whatever_the_api_is()
except (ValidationError, ValueError, ValueValidationException) as e:
# TODO : error on unmatched alias
LOG.exception('Validation failed for action alias data=%s.', action_alias)
pecan.abort(http_client.BAD_REQUEST, str(e))
return

# extra = {'action_alias_db': action_alias_db}
# LOG.audit('Action alias created. ActionAlias.id=%s' % (action_alias_db.id), extra=extra)

return action_execution