Skip to content

Commit

Permalink
Merge pull request #13 from zenoss/feature/SVC-2151
Browse files Browse the repository at this point in the history
SVC-2151 Add user-configurable API Timeout to PagerDuty Settings
  • Loading branch information
ViktoriiaKit authored Jun 24, 2019
2 parents cd3937e + 6f3e296 commit aa939d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
7 changes: 4 additions & 3 deletions ZenPacks/zenoss/PagerDuty/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from Products.ZenModel.actions import processTalSource, _signalToContextDict
from Products.ZenModel.ZVersion import VERSION as ZENOSS_VERSION

from ZenPacks.zenoss.PagerDuty.routers import ACCOUNT_ATTR, _dmdRoot
from ZenPacks.zenoss.PagerDuty.interfaces import IPagerDutyEventsAPIActionContentInfo
from ZenPacks.zenoss.PagerDuty.constants import EVENT_API_URI, EventType, enum
from ZenPacks.zenoss.PagerDuty import version as zenpack_version
Expand All @@ -34,8 +35,6 @@
EVENT_MAPPING = {'0': 'info', '1': 'info', '2': 'info',
'3': 'warning', '4': 'error', '5': 'critical'}

API_TIMEOUT_SECONDS = 40


class PagerDutyEventsAPIAction(IActionBase):
"""
Expand Down Expand Up @@ -139,6 +138,8 @@ def _performRequest(self, body, environ):
PagerDuty's Event API (e.g., API down, invalid service key).
"""

dmdRoot = _dmdRoot(self.dmd)
apiTimeout = getattr(dmdRoot, ACCOUNT_ATTR).apiTimeout
bodyWithProcessedTalesExpressions = self._processTalExpressions(body, environ)
bodyWithProcessedTalesExpressions['payload']['severity'] = EVENT_MAPPING[bodyWithProcessedTalesExpressions['payload']['severity']]
requestBody = json.dumps(bodyWithProcessedTalesExpressions)
Expand All @@ -148,7 +149,7 @@ def _performRequest(self, body, environ):
try:
# bypass default handler SVC-1819
opener = urllib2.build_opener()
f = opener.open(req, None, API_TIMEOUT_SECONDS)
f = opener.open(req, None, apiTimeout)
except urllib2.URLError as e:
if hasattr(e, 'reason'):
msg = 'Failed to contact the PagerDuty server: %s' % (e.reason)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
width: 400,
xtype: 'textfield'
},
{
fieldLabel: 'API Timeout(seconds)',
labelWidth: 150,
name: 'apiTimeout',
width: 400,
xtype: 'numberfield'
},
{
xtype: 'button',
text: 'Apply',
Expand Down Expand Up @@ -71,7 +78,7 @@

this.getForm().setValues(result.data);

if (result.data.apiAccessKey && result.data.subdomain) {
if (result.data.apiAccessKey && result.data.subdomain && result.data.apiTimeout) {
servicesRouter.getServices({wantsMessages: true}, function(result) {
var pdServiceStore = Ext.data.StoreManager.lookup('pdServiceStore');
pdServiceStore.loadData((result.success && result.data) ? result.data : []);
Expand Down
6 changes: 4 additions & 2 deletions ZenPacks/zenoss/PagerDuty/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ class Account(Persistent):
"""
A PagerDuty account consists of a subdomain and API access key.
"""
def __init__(self, subdomain, apiAccessKey):
def __init__(self, subdomain, apiAccessKey, apiTimeout=40):
self.subdomain = subdomain
self.apiAccessKey = apiAccessKey
self.apiTimeout = apiTimeout

def fqdn(self):
return "%s.pagerduty.com" % self.subdomain

def getDict(self):
return {
'subdomain': self.subdomain,
'apiAccessKey': self.apiAccessKey
'apiAccessKey': self.apiAccessKey,
'apiTimeout': self.apiTimeout
}
15 changes: 10 additions & 5 deletions ZenPacks/zenoss/PagerDuty/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,26 @@ def getAccountSettings(self):
account = getattr(dmdRoot, ACCOUNT_ATTR, models.account.Account(None, None))
return DirectResponse.succeed(msg=None, data=account.getDict())

def updateAccountSettings(self, apiAccessKey=None, subdomain=None, wantsMessages=True):
def updateAccountSettings(self, apiAccessKey=None, subdomain=None, apiTimeout=None, wantsMessages=True):
"""
Saves the account object and returns a list of services associated
with that account. Returns nothing if invalid account info is set.
The account object is saved as /zport/dmd/pagerduty_account
(aka, dmdRoot.pagerduty_account)
"""
account = models.account.Account(subdomain, apiAccessKey)
if not apiAccessKey or not subdomain:
return DirectResponse.fail(msg="Api Access Key and subdomain are needed for PagerDuty account")

if not apiTimeout:
account = models.account.Account(subdomain, apiAccessKey)
log.info("The API Timeout zero value results in a default timeout of 40 seconds")
else:
account = models.account.Account(subdomain, apiAccessKey, int(apiTimeout))

dmdRoot = _dmdRoot(self.context)
setattr(dmdRoot, ACCOUNT_ATTR, account)

if not account.apiAccessKey or not account.subdomain:
return DirectResponse.succeed()

servicesRouter = ServicesRouter(self.context, self.request)
result = servicesRouter.getServices(wantsMessages)

Expand Down

0 comments on commit aa939d1

Please sign in to comment.