Skip to content

Commit

Permalink
Add new google_chat module
Browse files Browse the repository at this point in the history
To send a message directly to google chat via the api (not via a bot)
you need to configure a webhook and post directly to that webhook url.
  • Loading branch information
luke-orden committed Aug 6, 2018
1 parent c034a86 commit 969d7d6
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/ref/modules/all/index.rst
Expand Up @@ -154,6 +154,7 @@ execution modules
glanceng
glusterfs
gnomedesktop
google_chat
gpg
grafana4
grains
Expand Down
6 changes: 6 additions & 0 deletions doc/ref/modules/all/salt.modules.google_chat.rst
@@ -0,0 +1,6 @@
========================
salt.modules.google_chat
========================

.. automodule:: salt.modules.google_chat
:members:
9 changes: 9 additions & 0 deletions doc/topics/releases/fluorine.rst
Expand Up @@ -907,3 +907,12 @@ The password option is only needed if the minion process is run under a
restricted (non-administrator) account. In the aforementioned case, a password
is only required when using the ``runas`` argument to run command as a
different user.

=======
New Modules
===========

Execution modules
-----------------

- :mod:`salt.modules.google_chat <salt.modules.google_chat>`
56 changes: 56 additions & 0 deletions salt/modules/google_chat.py
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
'''
Module for sending messages to google chat.
.. versionadded:: Fluorine
To use this module you need to configure a webhook in the google chat room
where you would like the message to be sent, see:
https://developers.google.com/hangouts/chat/how-tos/webhooks
'''
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import json


# ------------------------------------------------------------------------------
# module properties
# ------------------------------------------------------------------------------

__virtualname__ = 'google_chat'

# ------------------------------------------------------------------------------
# property functions
# ------------------------------------------------------------------------------


def __virtual__():
return __virtualname__

# ------------------------------------------------------------------------------
# callable functions
# ------------------------------------------------------------------------------


def send_message(url, message):
'''
Send a message to the google chat room specified in the webhook url.
.. code-block:: bash
salt '*' google_chat.send_message "https://chat.googleapis.com/v1/spaces/example_space/messages?key=example_key" "This is a test message"
'''
headers = {'Content-Type': 'application/json'}
data = {'text': message}
result = __utils__['http.query'](url,
'POST',
data=json.dumps(data),
header_dict=headers,
decode=True,
status=True)

if result.get('status', 0) == 200:
return True
else:
return False
52 changes: 52 additions & 0 deletions tests/unit/modules/test_google_chat.py
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
'''
Test the Google Chat Execution module.
'''
from __future__ import absolute_import, print_function, unicode_literals

# Import Salt Testing libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch

# Import Salt Libs
import salt.modules.google_chat as gchat


def mocked_http_query(url, method, **kwargs): # pylint: disable=unused-argument
'''
Mocked data for test_send_message_success
'''
return {'status': 200,
'dict': None}


def mocked_http_query_failure(url, method, **kwargs): # pylint: disable=unused-argument
'''
Mocked data for test_send_message_failure
'''
return {'status': 522,
'dict': None}


@skipIf(NO_MOCK, NO_MOCK_REASON)
class TestModulesCfutils(TestCase, LoaderModuleMockMixin):
'''
Test cases for salt.modules.google_chat
'''
def setup_loader_modules(self):
return {gchat: {}}

def test_send_message_success(self):
'''
Testing a successful message
'''
with patch.dict(gchat.__utils__, {'http.query': mocked_http_query}): # pylint: disable=no-member
self.assertTrue(gchat.send_message('https://example.com', 'Yupiii'))

def test_send_message_failure(self):
'''
Testing a failed message
'''
with patch.dict(gchat.__utils__, {'http.query': mocked_http_query_failure}): # pylint: disable=no-member
self.assertFalse(gchat.send_message('https://example.com', 'Yupiii'))

0 comments on commit 969d7d6

Please sign in to comment.