Reorganize receivers directory

This patch reorganizes receivers directory: the base
receiver class is now located in receivers/base.py
while Webhook receiver module is located in
receivers/webhook.py. More types of receiver modules
(e.g. message based receiver) will be located in
receivers/ directory as well.

Partial-bp: message-based-receiver
Change-Id: I8078da26af30611588dcd5cffe0886a414692881
This commit is contained in:
yanyanhu 2016-08-18 23:10:45 -04:00
parent c853a4e8cb
commit 79cc006689
8 changed files with 97 additions and 60 deletions

View File

View File

@ -13,7 +13,6 @@
from oslo_config import cfg
from oslo_utils import timeutils
from oslo_utils import uuidutils
from six.moves.urllib import parse
from senlin.common import consts
from senlin.common import exception
@ -37,7 +36,8 @@ class Receiver(object):
:returns: An instance of a specific sub-class of Receiver.
"""
if rtype == consts.RECEIVER_WEBHOOK:
ReceiverClass = Webhook
from senlin.engine.receivers import webhook
ReceiverClass = webhook.Webhook
else:
ReceiverClass = Receiver
@ -187,24 +187,3 @@ class Receiver(object):
def initialize_channel(self):
return {}
class Webhook(Receiver):
"""Webhook flavor of receivers."""
def initialize_channel(self):
host = CONF.webhook.host
port = CONF.webhook.port
base = "http://%(h)s:%(p)s/v1" % {'h': host, 'p': port}
webhook = "/webhooks/%(id)s/trigger" % {'id': self.id}
if self.params:
normalized = sorted(self.params.items(), key=lambda d: d[0])
qstr = parse.urlencode(normalized)
url = "".join([base, webhook, '?V=1&', qstr])
else:
url = "".join([base, webhook, '?V=1'])
self.channel = {
'alarm_url': url
}
return self.channel

View File

@ -0,0 +1,39 @@
# Licensed 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.
from oslo_config import cfg
from six.moves.urllib import parse
from senlin.engine.receivers import base
CONF = cfg.CONF
class Webhook(base.Receiver):
"""Webhook flavor of receivers."""
def initialize_channel(self):
host = CONF.webhook.host
port = CONF.webhook.port
base = "http://%(h)s:%(p)s/v1" % {'h': host, 'p': port}
webhook = "/webhooks/%(id)s/trigger" % {'id': self.id}
if self.params:
normalized = sorted(self.params.items(), key=lambda d: d[0])
qstr = parse.urlencode(normalized)
url = "".join([base, webhook, '?V=1&', qstr])
else:
url = "".join([base, webhook, '?V=1'])
self.channel = {
'alarm_url': url
}
return self.channel

View File

@ -38,7 +38,7 @@ from senlin.engine import dispatcher
from senlin.engine import environment
from senlin.engine import health_manager
from senlin.engine import node as node_mod
from senlin.engine import receiver as receiver_mod
from senlin.engine.receivers import base as receiver_mod
from senlin.engine import scheduler
from senlin.objects import action as action_obj
from senlin.objects import cluster as cluster_obj

View File

@ -11,13 +11,12 @@
# under the License.
import mock
from oslo_config import cfg
from oslo_utils import timeutils
import six
from senlin.common import exception
from senlin.common import utils as common_utils
from senlin.engine import receiver as rb
from senlin.engine.receivers import base as rb
from senlin.objects import receiver as ro
from senlin.tests.unit.common import base
from senlin.tests.unit.common import utils
@ -243,36 +242,3 @@ class TestReceiver(base.SenlinTestCase):
result = rb.Receiver.load(self.context, receiver_id=receiver.id)
self.assertEqual(expected, result.to_dict())
class TestWebhook(base.SenlinTestCase):
def test_initialize_channel(self):
cfg.CONF.set_override('host', 'web.com', 'webhook')
cfg.CONF.set_override('port', '1234', 'webhook')
webhook = rb.Webhook('webhook', CLUSTER_ID, 'FAKE_ACTION',
id=UUID1)
channel = webhook.initialize_channel()
expected = {
'alarm_url': ('http://web.com:1234/v1/webhooks/%s/trigger'
'?V=1' % UUID1)
}
self.assertEqual(expected, channel)
self.assertEqual(expected, webhook.channel)
def test_initialize_channel_with_params(self):
cfg.CONF.set_override('host', 'web.com', 'webhook')
cfg.CONF.set_override('port', '1234', 'webhook')
webhook = rb.Webhook(
'webhook', CLUSTER_ID, 'FAKE_ACTION',
id=UUID1, params={'KEY': 884, 'FOO': 'BAR'})
channel = webhook.initialize_channel()
expected = {
'alarm_url': ('http://web.com:1234/v1/webhooks/%s/trigger'
'?V=1&FOO=BAR&KEY=884' % UUID1)
}
self.assertEqual(expected, channel)
self.assertEqual(expected, webhook.channel)

View File

@ -0,0 +1,53 @@
# Licensed 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.
from oslo_config import cfg
from senlin.engine.receivers import webhook as wmod
from senlin.tests.unit.common import base
CLUSTER_ID = '2c5139a6-24ba-4a6f-bd53-a268f61536de'
UUID1 = 'aa5f86b8-e52b-4f2b-828a-4c14c770938d'
UUID2 = '60efdaa1-06c2-4fcf-ae44-17a2d85ff3ea'
class TestWebhook(base.SenlinTestCase):
def test_initialize_channel(self):
cfg.CONF.set_override('host', 'web.com', 'webhook')
cfg.CONF.set_override('port', '1234', 'webhook')
webhook = wmod.Webhook('webhook', CLUSTER_ID, 'FAKE_ACTION',
id=UUID1)
channel = webhook.initialize_channel()
expected = {
'alarm_url': ('http://web.com:1234/v1/webhooks/%s/trigger'
'?V=1' % UUID1)
}
self.assertEqual(expected, channel)
self.assertEqual(expected, webhook.channel)
def test_initialize_channel_with_params(self):
cfg.CONF.set_override('host', 'web.com', 'webhook')
cfg.CONF.set_override('port', '1234', 'webhook')
webhook = wmod.Webhook(
'webhook', CLUSTER_ID, 'FAKE_ACTION',
id=UUID1, params={'KEY': 884, 'FOO': 'BAR'})
channel = webhook.initialize_channel()
expected = {
'alarm_url': ('http://web.com:1234/v1/webhooks/%s/trigger'
'?V=1&FOO=BAR&KEY=884' % UUID1)
}
self.assertEqual(expected, channel)
self.assertEqual(expected, webhook.channel)

View File

@ -17,7 +17,7 @@ from oslo_utils import uuidutils
import six
from senlin.common import exception as exc
from senlin.engine import receiver as rb
from senlin.engine.receivers import base as rb
from senlin.engine import service
from senlin.objects import receiver as ro
from senlin.tests.unit.common import base