Skip to content

Commit

Permalink
Merge pull request apache#2 from rphillips/monitoring
Browse files Browse the repository at this point in the history
Monitoring add notifications and notification plans
  • Loading branch information
pquerna committed Sep 27, 2011
2 parents b4ea39f + 5f16bbf commit c2bc48b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
27 changes: 27 additions & 0 deletions libcloud/monitoring/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ def __repr__(self):
(self.id, self.name, self.driver.name))


class Notification(object):
def __init__(self, id, type, details):
self.id = id
self.type = type
self.details = details

def __repr__(self):
return ('<Object: id=%s type=%s ...>' % (self.id, self.type))

class NotificationPlan(object):
"""
Represents a notification plan.
"""
def __init__(self, id, name, error_state, warning_state, ok_state, driver):
self.id = id
self.name = name
self.error_state = error_state
self.warning_state = warning_state
self.ok_state = ok_state
self.driver = driver

def delete(self):
return self.driver.delete_notification_plan(self)

def __repr__(self):
return ('<Object: id=%s ...>' % (self.id))

class MonitoringDriver(object):
"""
A base MonitoringDriver to derive from.
Expand Down
86 changes: 84 additions & 2 deletions libcloud/monitoring/drivers/rackspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from libcloud.common.base import Response

from libcloud.monitoring.providers import Provider
from libcloud.monitoring.base import MonitoringDriver, Entity #, Check, Alarm
from libcloud.monitoring.base import MonitoringDriver, Entity, NotificationPlan #, Check, Alarm

from libcloud.common.rackspace import AUTH_URL_US
from libcloud.common.openstack import OpenStackBaseConnection
Expand Down Expand Up @@ -154,6 +154,11 @@ def _read_entity(self, enId):
method='GET')
return self._to_entity(resp.object)

def _read_notification_plan(self, npId):
resp = self.connection.request("/notification_plan/%s" % (npId),
method='GET')
return self._to_notification_plan(resp.object)

def list_entities(self):
response = self.connection.request('/entities')

Expand All @@ -176,6 +181,11 @@ def _to_entity(self, entity):
ips.append((key, ipaddrs[key]))
return Entity(id=entity['key'], name=entity['label'], extra=entity['metadata'], driver=self, ip_addresses = ips)

def _to_notification_plan(self, notification_plan):
return NotificationPlan(id=notification_plan['key'], name=notification_plan['name'],
error_state=notification_plan['error_state'], warning_state=notification_plan['warning_state'],
ok_state=notification_plan['ok_state'], driver=self)

def _to_entity_list(self, response):
# @TODO: Handle more then 10k containers - use "lazy list"?
entities = []
Expand All @@ -196,6 +206,78 @@ def list_monitoring_zones(self):
print resp.object
return resp.status == httplib.NO_CONTENT

def list_notifications(self):
resp = self.connection.request("/notifications",
method='GET')
print resp.object
return resp.status == httplib.NO_CONTENT

def delete_notification(self, notification):
resp = self.connection.request("/notifications/%s" % (notification.id),
method='DELETE')
return resp.status == httplib.NO_CONTENT

def create_notification(self, **kwargs):
data = {'type': kwargs.get('type'),
'details': kwargs.get('details')}

for k in data.keys():
if data[k] == None:
del data[k]

resp = self.connection.request("/notifications",
method='POST',
data=data)
if resp.status == httplib.NO_CONTENT:
# location
# /1.0/notification_plans/npycekKZoN
location = resp.headers.get('location')
if not location:
raise LibcloudError('Missing location header')

npId = location.rsplit('/')[-1]
return self._read_notification(npId)
else:
raise LibcloudError('Unexpected status code: %s' % (response.status))

def delete_notification_plans(self, notification_plan):
resp = self.connection.request("/notifications_plans/%s" % (notification_plan.id),
method='DELETE')
return resp.status == httplib.NO_CONTENT


def list_notification_plans(self):
resp = self.connection.request("/notification_plans",
method='GET')
print resp.object
return resp.status == httplib.NO_CONTENT

def create_notification_plan(self, **kwargs):
data = {'name': kwargs.get('name'),
'error_state': kwargs.get('error_state', []),
'warning_state': kwargs.get('warning_state', []),
'ok_state': kwargs.get('ok_state', []),
}

for k in data.keys():
if data[k] == None:
del data[k]

resp = self.connection.request("/notification_plans",
method='POST',
data=data)
if resp.status == httplib.NO_CONTENT:
# location
# /1.0/notification_plans/npycekKZoN
location = resp.headers.get('location')
if not location:
raise LibcloudError('Missing location header')

enId = location.rsplit('/')[-1]
return self._read_entity(enId)
else:
raise LibcloudError('Unexpected status code: %s' % (response.status))

def list_checks(self, entity):
resp = self.connection.request("/entities/%s/checks" % (entity.id),
method='GET')
Expand Down Expand Up @@ -224,7 +306,7 @@ def create_check(self, entity, **kwargs):
data=data)
if resp.status == httplib.NO_CONTENT:
# location
# /1.0/entities/enycekKZoN

location = resp.headers.get('location')
if not location:
raise LibcloudError('Missing location header')
Expand Down

0 comments on commit c2bc48b

Please sign in to comment.