<s> try : <EOL> import simplejson as json <EOL> except : <EOL> import json <EOL> from libcloud . utils . py3 import httplib , urlparse <EOL> from libcloud . common . types import MalformedResponseError , LibcloudError <EOL> from libcloud . common . types import LazyList <EOL> from libcloud . common . base import Response <EOL> from rackspace_monitoring . providers import Provider <EOL> from rackspace_monitoring . utils import to_underscore_separated <EOL> from rackspace_monitoring . utils import value_to_bool <EOL> from rackspace_monitoring . base import ( MonitoringDriver , Entity , <EOL> NotificationPlan , MonitoringZone , <EOL> Notification , CheckType , Alarm , Check , <EOL> NotificationType , AlarmChangelog , <EOL> LatestAlarmState , Agent , AgentToken , <EOL> AgentConnection , Metric , DataPoint , <EOL> Suppression , SuppressionLog ) <EOL> from libcloud . common . rackspace import AUTH_URL <EOL> from libcloud . common . openstack import OpenStackBaseConnection <EOL> from libcloud . common . openstack import OpenStackDriverMixin <EOL> class RackspaceMonitoringValidationError ( LibcloudError ) : <EOL> def __init__ ( self , code , type , message , details , driver ) : <EOL> self . code = code <EOL> self . type = type <EOL> self . message = message <EOL> self . details = details <EOL> super ( RackspaceMonitoringValidationError , self ) . __init__ ( value = message , <EOL> driver = driver ) <EOL> def __str__ ( self ) : <EOL> return self . __repr__ ( ) <EOL> def __repr__ ( self ) : <EOL> string = '<STR_LIT>' % ( self . type ) <EOL> string += '<STR_LIT>' % ( self . message , self . details ) <EOL> return string . encode ( '<STR_LIT:utf-8>' ) <EOL> class RackspaceMonitoringResponse ( Response ) : <EOL> valid_response_codes = [ httplib . CONFLICT ] <EOL> def success ( self ) : <EOL> i = int ( self . status ) <EOL> return i >= <NUM_LIT:200> and i <= <NUM_LIT> or i in self . valid_response_codes <EOL> def parse_body ( self ) : <EOL> if not self . body : <EOL> return None <EOL> if '<STR_LIT>' in self . headers : <EOL> key = '<STR_LIT>' <EOL> elif '<STR_LIT:Content-Type>' in self . headers : <EOL> key = '<STR_LIT:Content-Type>' <EOL> else : <EOL> raise LibcloudError ( '<STR_LIT>' ) <EOL> content_type = self . headers [ key ] <EOL> if content_type . find ( '<STR_LIT:;>' ) != - <NUM_LIT:1> : <EOL> content_type = content_type . split ( '<STR_LIT:;>' ) [ <NUM_LIT:0> ] <EOL> if content_type == '<STR_LIT:application/json>' : <EOL> try : <EOL> data = json . loads ( self . body ) <EOL> except : <EOL> raise MalformedResponseError ( '<STR_LIT>' , <EOL> body = self . body , <EOL> driver = RackspaceMonitoringDriver ) <EOL> elif content_type == '<STR_LIT>' : <EOL> data = self . body <EOL> else : <EOL> data = self . body <EOL> return data <EOL> def parse_error ( self ) : <EOL> body = self . parse_body ( ) <EOL> if self . status == httplib . BAD_REQUEST : <EOL> error = RackspaceMonitoringValidationError ( message = body [ '<STR_LIT:message>' ] , <EOL> code = body [ '<STR_LIT:code>' ] , <EOL> type = body [ '<STR_LIT:type>' ] , <EOL> details = body [ '<STR_LIT>' ] , <EOL> driver = self . connection . driver ) <EOL> raise error <EOL> return body <EOL> class RackspaceMonitoringConnection ( OpenStackBaseConnection ) : <EOL> """<STR_LIT>""" <EOL> type = Provider . RACKSPACE <EOL> responseCls = RackspaceMonitoringResponse <EOL> auth_url = AUTH_URL <EOL> service_name = '<STR_LIT>' <EOL> service_type = '<STR_LIT>' <EOL> def __init__ ( self , user_id , key , secure = False , ex_force_base_url = None , <EOL> ex_force_auth_url = auth_url , ex_force_auth_version = '<STR_LIT>' , <EOL> ex_force_auth_token = None ) : <EOL> self . accept_format = '<STR_LIT:application/json>' <EOL> super ( RackspaceMonitoringConnection , self ) . __init__ ( user_id , key , <EOL> secure = secure , <EOL> ex_force_base_url = ex_force_base_url , <EOL> ex_force_auth_url = ex_force_auth_url , <EOL> ex_force_auth_version = ex_force_auth_version , <EOL> ex_force_auth_token = ex_force_auth_token ) <EOL> def request ( self , action , params = None , data = '<STR_LIT>' , headers = None , method = '<STR_LIT:GET>' , <EOL> raw = False ) : <EOL> if not headers : <EOL> headers = { } <EOL> if not params : <EOL> params = { } <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> if method in [ '<STR_LIT:POST>' , '<STR_LIT>' ] : <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> data = json . dumps ( data ) <EOL> return super ( RackspaceMonitoringConnection , self ) . request ( <EOL> action = action , <EOL> params = params , data = data , <EOL> method = method , headers = headers , <EOL> raw = raw <EOL> ) <EOL> class RackspaceMonitoringDriver ( MonitoringDriver , OpenStackDriverMixin ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> connectionCls = RackspaceMonitoringConnection <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if kwargs . get ( '<STR_LIT>' , None ) is not None : <EOL> if kwargs [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] == '<STR_LIT:/>' : <EOL> kwargs [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] [ : - <NUM_LIT:1> ] <EOL> OpenStackDriverMixin . __init__ ( self , * args , ** kwargs ) <EOL> super ( RackspaceMonitoringDriver , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _initialize_connection_base_url ( constructor_kwargs = kwargs ) <EOL> def _initialize_connection_base_url ( self , constructor_kwargs ) : <EOL> ex_force_base_url = constructor_kwargs . pop ( '<STR_LIT>' , None ) <EOL> ex_force_auth_token = constructor_kwargs . pop ( '<STR_LIT>' , None ) <EOL> if ex_force_base_url and not ex_force_auth_token : <EOL> self . connection . _populate_hosts_and_request_paths ( ) <EOL> eps = self . connection . service_catalog . get_endpoints ( name = self . connection . service_name , <EOL> service_type = self . connection . service_type ) <EOL> if len ( eps ) == <NUM_LIT:0> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> ep = eps [ <NUM_LIT:0> ] <EOL> tenant_id = ep [ '<STR_LIT>' ] <EOL> self . connection . _ex_force_base_url = '<STR_LIT>' % ( <EOL> self . connection . _ex_force_base_url , tenant_id ) <EOL> def _ex_connection_class_kwargs ( self ) : <EOL> return self . openstack_connection_kwargs ( ) <EOL> def _get_more ( self , last_key , value_dict ) : <EOL> key = None <EOL> params = value_dict . get ( '<STR_LIT>' , { } ) <EOL> if not last_key : <EOL> key = value_dict . get ( '<STR_LIT>' ) <EOL> else : <EOL> key = last_key <EOL> if key : <EOL> params [ '<STR_LIT>' ] = key <EOL> response = self . connection . request ( value_dict [ '<STR_LIT:url>' ] , params ) <EOL> if response . status == httplib . NO_CONTENT : <EOL> return [ ] , None , False <EOL> elif response . status == httplib . OK : <EOL> resp = json . loads ( response . body ) <EOL> l = None <EOL> if '<STR_LIT>' in value_dict : <EOL> func = value_dict [ '<STR_LIT>' ] <EOL> l = [ func ( x , value_dict ) for x in resp [ '<STR_LIT>' ] ] <EOL> else : <EOL> l = value_dict [ '<STR_LIT>' ] ( resp , value_dict ) <EOL> m = resp [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> return l , m , ( m is None ) <EOL> body = json . loads ( response . body ) <EOL> details = body [ '<STR_LIT>' ] if '<STR_LIT>' in body else '<STR_LIT>' <EOL> raise LibcloudError ( '<STR_LIT>' % <EOL> ( response . status , value_dict [ '<STR_LIT:url>' ] , details ) ) <EOL> def _plural_to_singular ( self , name ) : <EOL> kv = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> return kv [ name ] <EOL> def _url_to_obj_ids ( self , url ) : <EOL> rv = { } <EOL> path = urlparse . urlparse ( url ) . path <EOL> path = path . replace ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> chunks = path . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> : ] <EOL> for i in range ( <NUM_LIT:2> , len ( chunks ) , <NUM_LIT:2> ) : <EOL> chunk = chunks [ i ] <EOL> if not chunk : <EOL> continue <EOL> key = self . _plural_to_singular ( chunk ) + '<STR_LIT>' <EOL> key = to_underscore_separated ( key ) <EOL> rv [ key ] = chunks [ i + <NUM_LIT:1> ] <EOL> return rv <EOL> def _create ( self , url , data , coerce , handler = None , headers = None ) : <EOL> params = { } <EOL> for k in data . keys ( ) : <EOL> if data [ k ] is None : <EOL> del data [ k ] <EOL> if '<STR_LIT>' in data : <EOL> if data [ '<STR_LIT>' ] is not None : <EOL> params [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> del data [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in data : <EOL> if data [ '<STR_LIT>' ] is not None : <EOL> params [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> del data [ '<STR_LIT>' ] <EOL> resp = self . connection . request ( url , <EOL> method = '<STR_LIT:POST>' , <EOL> params = params , <EOL> data = data , <EOL> headers = headers ) <EOL> if handler is not None : <EOL> return handler ( resp ) <EOL> elif resp . status == httplib . CREATED : <EOL> location = resp . headers . get ( '<STR_LIT:location>' ) <EOL> if not location : <EOL> raise LibcloudError ( '<STR_LIT>' ) <EOL> obj_ids = self . _url_to_obj_ids ( location ) <EOL> return coerce ( ** obj_ids ) <EOL> else : <EOL> raise LibcloudError ( '<STR_LIT>' % ( resp . status ) ) <EOL> def _update ( self , url , data , kwargs , coerce ) : <EOL> params = { } <EOL> for k in data . keys ( ) : <EOL> if data [ k ] is None : <EOL> del data [ k ] <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] is not None : <EOL> params [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] is not None : <EOL> params [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> headers = kwargs . get ( '<STR_LIT>' , { } ) <EOL> resp = self . connection . request ( url , method = '<STR_LIT>' , params = params , <EOL> data = data , headers = headers ) <EOL> if resp . status == httplib . NO_CONTENT : <EOL> location = resp . headers . get ( '<STR_LIT:location>' ) <EOL> if not location : <EOL> raise LibcloudError ( '<STR_LIT>' ) <EOL> obj_ids = self . _url_to_obj_ids ( location ) <EOL> return coerce ( ** obj_ids ) <EOL> else : <EOL> raise LibcloudError ( '<STR_LIT>' % ( resp . status ) ) <EOL> def _delete ( self , url , kwargs = None ) : <EOL> kwargs = kwargs or { } <EOL> params = { } <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] is not None : <EOL> params [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] is not None : <EOL> params [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> headers = kwargs . get ( '<STR_LIT>' , { } ) <EOL> resp = self . connection . request ( action = url , method = '<STR_LIT>' , <EOL> params = params , headers = headers ) <EOL> return resp . status == httplib . NO_CONTENT <EOL> def list_check_types ( self ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . _to_check_type } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def _to_check_type ( self , obj , value_dict ) : <EOL> return CheckType ( id = obj [ '<STR_LIT:id>' ] , <EOL> fields = obj . get ( '<STR_LIT>' , [ ] ) , <EOL> is_remote = obj . get ( '<STR_LIT:type>' ) == '<STR_LIT>' ) <EOL> def list_notification_types ( self ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . _to_notification_type } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def _to_notification_type ( self , obj , value_dict ) : <EOL> return NotificationType ( id = obj [ '<STR_LIT:id>' ] , <EOL> fields = obj . get ( '<STR_LIT>' , [ ] ) ) <EOL> def _to_metrics ( self , obj , value_dict = None ) : <EOL> return Metric ( name = obj [ '<STR_LIT:name>' ] , driver = self ) <EOL> def _to_datapoints ( self , obj , value_dict = None ) : <EOL> return DataPoint ( name = obj [ '<STR_LIT>' ] , driver = self , <EOL> average = obj [ '<STR_LIT>' ] , numPoints = obj [ '<STR_LIT>' ] ) <EOL> def list_metrics ( self , entity_id , check_id , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' % <EOL> ( entity_id , check_id ) , <EOL> '<STR_LIT>' : self . _to_metrics } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def fetch_data_point ( self , entity_id , check_id , metric_name , from_timestamp , to_timestamp , points = <NUM_LIT:1> ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' % <EOL> ( entity_id , check_id , metric_name ) , <EOL> '<STR_LIT>' : { '<STR_LIT>' : from_timestamp , '<STR_LIT:to>' : to_timestamp , '<STR_LIT>' : points } , <EOL> '<STR_LIT>' : self . _to_datapoints } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def _to_monitoring_zone ( self , obj , value_dict = None ) : <EOL> return MonitoringZone ( id = obj [ '<STR_LIT:id>' ] , label = obj [ '<STR_LIT:label>' ] , <EOL> country_code = obj [ '<STR_LIT>' ] , <EOL> source_ips = obj [ '<STR_LIT>' ] , <EOL> driver = self ) <EOL> def list_monitoring_zones ( self ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . _to_monitoring_zone } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def get_monitoring_zone ( self , monitoring_zone_id ) : <EOL> url = '<STR_LIT>' % ( monitoring_zone_id ) <EOL> resp = self . connection . request ( url ) . object <EOL> return self . _to_monitoring_zone ( obj = resp ) <EOL> def get_alarm ( self , entity_id , alarm_id ) : <EOL> url = "<STR_LIT>" % ( entity_id , alarm_id ) <EOL> resp = self . connection . request ( url ) <EOL> return self . _to_alarm ( resp . object , { '<STR_LIT>' : entity_id } ) <EOL> def _to_alarm ( self , alarm , value_dict ) : <EOL> return Alarm ( id = alarm [ '<STR_LIT:id>' ] , check_id = alarm [ '<STR_LIT>' ] , <EOL> criteria = alarm [ '<STR_LIT>' ] , <EOL> metadata = alarm . get ( '<STR_LIT>' ) , <EOL> confd_name = alarm . get ( '<STR_LIT>' ) , <EOL> confd_hash = alarm . get ( '<STR_LIT>' ) , <EOL> notification_plan_id = alarm . get ( '<STR_LIT>' , None ) , label = alarm . get ( '<STR_LIT:label>' , None ) , <EOL> driver = self , entity_id = value_dict [ '<STR_LIT>' ] ) <EOL> def list_alarms ( self , entity , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' % ( entity . id ) , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : self . _to_alarm , <EOL> '<STR_LIT>' : entity . id } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def list_alarm_changelog ( self , ex_next_marker = None , entity = None , <EOL> reverse = False ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : self . _to_alarm_changelog } <EOL> if entity is not None : <EOL> value_dict [ '<STR_LIT>' ] = entity . id <EOL> value_dict [ '<STR_LIT:url>' ] += '<STR_LIT>' % entity . id <EOL> if reverse : <EOL> value_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] = reverse <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def _to_alarm_changelog ( self , values , value_dict ) : <EOL> alarm_changelog = AlarmChangelog ( id = values [ '<STR_LIT:id>' ] , <EOL> alarm_id = values [ '<STR_LIT>' ] , <EOL> entity_id = values [ '<STR_LIT>' ] , <EOL> check_id = values [ '<STR_LIT>' ] , <EOL> previous_state = values [ '<STR_LIT>' ] , <EOL> timestamp = values [ '<STR_LIT>' ] , <EOL> state = values [ '<STR_LIT:state>' ] ) <EOL> return alarm_changelog <EOL> def delete_alarm ( self , alarm , ** kwargs ) : <EOL> return self . _delete ( url = "<STR_LIT>" % ( alarm . entity_id , <EOL> alarm . id ) , <EOL> kwargs = kwargs ) <EOL> def update_alarm ( self , alarm , data , ** kwargs ) : <EOL> return self . _update ( "<STR_LIT>" % ( alarm . entity_id , <EOL> alarm . id ) , <EOL> data = data , kwargs = kwargs , coerce = self . get_alarm ) <EOL> def create_alarm ( self , entity , ** kwargs ) : <EOL> data = { '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:label>' : kwargs . get ( '<STR_LIT:label>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) } <EOL> headers = kwargs . get ( '<STR_LIT>' , { } ) <EOL> return self . _create ( "<STR_LIT>" % ( entity . id ) , <EOL> data = data , coerce = self . get_alarm , headers = headers ) <EOL> def test_alarm ( self , entity , ** kwargs ) : <EOL> data = { '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) } <EOL> resp = self . connection . request ( "<STR_LIT>" % ( entity . id ) , <EOL> method = '<STR_LIT:POST>' , <EOL> data = data ) <EOL> return resp . object <EOL> def list_suppressions ( self , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : self . _to_suppression } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def _to_suppression ( self , suppression , value_dict ) : <EOL> return Suppression ( id = suppression [ '<STR_LIT:id>' ] , <EOL> notification_plans = suppression [ '<STR_LIT>' ] , <EOL> entities = suppression [ '<STR_LIT>' ] , <EOL> checks = suppression [ '<STR_LIT>' ] , <EOL> alarms = suppression [ '<STR_LIT>' ] , <EOL> start_time = suppression [ '<STR_LIT>' ] , <EOL> end_time = suppression [ '<STR_LIT>' ] , <EOL> driver = self ) <EOL> def _to_suppression_log ( self , suppression_log , value_dict ) : <EOL> return SuppressionLog ( id = suppression_log [ '<STR_LIT:id>' ] , <EOL> entity_id = suppression_log [ '<STR_LIT>' ] , <EOL> alarm_id = suppression_log [ '<STR_LIT>' ] , <EOL> check_id = suppression_log [ '<STR_LIT>' ] , <EOL> notification_plan_id = suppression_log [ '<STR_LIT>' ] , <EOL> suppressions = suppression_log [ '<STR_LIT>' ] , <EOL> state = suppression_log [ '<STR_LIT:state>' ] , <EOL> timestamp = suppression_log [ '<STR_LIT>' ] , <EOL> transaction_id = suppression_log [ '<STR_LIT>' ] , <EOL> driver = self ) <EOL> def get_suppression ( self , suppression_id ) : <EOL> resp = self . connection . request ( "<STR_LIT>" % ( suppression_id ) ) <EOL> return self . _to_suppression ( resp . object , { } ) <EOL> def create_suppression ( self , ** kwargs ) : <EOL> data = { '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) } <EOL> return self . _create ( "<STR_LIT>" , data = data , <EOL> coerce = self . get_suppression ) <EOL> def delete_suppression ( self , suppression , ** kwargs ) : <EOL> return self . _delete ( url = "<STR_LIT>" % ( suppression . id ) , <EOL> kwargs = kwargs ) <EOL> def update_suppression ( self , suppression , data , ** kwargs ) : <EOL> return self . _update ( '<STR_LIT>' % ( suppression . id ) , <EOL> data = data , kwargs = kwargs , coerce = self . get_suppression ) <EOL> def list_suppression_logs ( self , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : self . _to_suppression_log } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def list_notifications ( self , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : self . _to_notification } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def _to_notification ( self , notification , value_dict ) : <EOL> return Notification ( id = notification [ '<STR_LIT:id>' ] , label = notification [ '<STR_LIT:label>' ] , <EOL> type = notification [ '<STR_LIT:type>' ] , <EOL> details = notification [ '<STR_LIT>' ] , <EOL> metadata = notification . get ( '<STR_LIT>' ) , <EOL> driver = self ) <EOL> def get_notification ( self , notification_id ) : <EOL> resp = self . connection . request ( "<STR_LIT>" % ( notification_id ) ) <EOL> return self . _to_notification ( resp . object , { } ) <EOL> def delete_notification ( self , notification , ** kwargs ) : <EOL> return self . _delete ( url = "<STR_LIT>" % ( notification . id ) , <EOL> kwargs = kwargs ) <EOL> def update_notification ( self , notification , data , ** kwargs ) : <EOL> return self . _update ( '<STR_LIT>' % ( notification . id ) , <EOL> data = data , kwargs = kwargs , coerce = self . get_notification ) <EOL> def create_notification ( self , ** kwargs ) : <EOL> data = { '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:label>' : kwargs . get ( '<STR_LIT:label>' ) , <EOL> '<STR_LIT:type>' : kwargs . get ( '<STR_LIT:type>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) } <EOL> return self . _create ( "<STR_LIT>" , data = data , <EOL> coerce = self . get_notification ) <EOL> def test_existing_notification ( self , notification ) : <EOL> resp = self . connection . request ( '<STR_LIT>' % ( notification . id ) , <EOL> method = '<STR_LIT:POST>' ) <EOL> return resp . object <EOL> def test_notification ( self , ** kwargs ) : <EOL> data = { '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:type>' : kwargs . get ( '<STR_LIT:type>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) } <EOL> resp = self . connection . request ( '<STR_LIT>' , method = '<STR_LIT:POST>' , <EOL> data = data ) <EOL> return resp . object <EOL> def _to_notification_plan ( self , notification_plan , value_dict ) : <EOL> critical_state = notification_plan . get ( '<STR_LIT>' , [ ] ) <EOL> warning_state = notification_plan . get ( '<STR_LIT>' , [ ] ) <EOL> ok_state = notification_plan . get ( '<STR_LIT>' , [ ] ) <EOL> return NotificationPlan ( id = notification_plan [ '<STR_LIT:id>' ] , <EOL> label = notification_plan [ '<STR_LIT:label>' ] , <EOL> metadata = notification_plan . get ( '<STR_LIT>' ) , <EOL> critical_state = critical_state , warning_state = warning_state , <EOL> ok_state = ok_state , driver = self ) <EOL> def get_notification_plan ( self , notification_plan_id ) : <EOL> resp = self . connection . request ( "<STR_LIT>" % ( <EOL> notification_plan_id ) ) <EOL> return self . _to_notification_plan ( resp . object , { } ) <EOL> def delete_notification_plan ( self , notification_plan , ** kwargs ) : <EOL> return self . _delete ( url = "<STR_LIT>" % <EOL> ( notification_plan . id ) , <EOL> kwargs = kwargs ) <EOL> def list_notification_plans ( self , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : self . _to_notification_plan } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def update_notification_plan ( self , notification_plan , data , ** kwargs ) : <EOL> return self . _update ( "<STR_LIT>" % ( notification_plan . id ) , <EOL> data = data , kwargs = kwargs , <EOL> coerce = self . get_notification_plan ) <EOL> def create_notification_plan ( self , ** kwargs ) : <EOL> data = { '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:label>' : kwargs . get ( '<STR_LIT:label>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , [ ] ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , [ ] ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , [ ] ) , <EOL> } <EOL> return self . _create ( "<STR_LIT>" , data = data , <EOL> coerce = self . get_notification_plan ) <EOL> def get_check ( self , entity_id , check_id ) : <EOL> resp = self . connection . request ( '<STR_LIT>' % ( entity_id , <EOL> check_id ) ) <EOL> return self . _to_check ( resp . object , { '<STR_LIT>' : entity_id } ) <EOL> def _to_check ( self , obj , value_dict ) : <EOL> return Check ( ** { <EOL> '<STR_LIT:id>' : obj [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:label>' : obj . get ( '<STR_LIT:label>' ) , <EOL> '<STR_LIT>' : obj [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : obj [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : obj [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : obj . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : obj . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT:type>' : obj [ '<STR_LIT:type>' ] , <EOL> '<STR_LIT>' : obj . get ( '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : value_to_bool ( obj . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) , <EOL> '<STR_LIT>' : self , <EOL> '<STR_LIT>' : obj . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : obj . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : obj . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : value_dict [ '<STR_LIT>' ] } ) <EOL> def list_checks ( self , entity , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : "<STR_LIT>" % ( entity . id ) , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : self . _to_check , <EOL> '<STR_LIT>' : entity . id } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def _check_kwarg_to_data ( self , kwargs ) : <EOL> filtered = { } <EOL> data = { '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:label>' : kwargs . get ( '<STR_LIT:label>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:type>' : kwargs . get ( '<STR_LIT:type>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> } <EOL> if '<STR_LIT>' in kwargs : <EOL> data [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in kwargs : <EOL> data [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> for k in data . keys ( ) : <EOL> if data [ k ] is not None : <EOL> filtered [ k ] = data [ k ] <EOL> return filtered <EOL> def test_check ( self , entity , ** kwargs ) : <EOL> data = self . _check_kwarg_to_data ( kwargs ) <EOL> resp = self . connection . request ( '<STR_LIT>' % ( entity . id ) , <EOL> method = '<STR_LIT:POST>' , <EOL> data = data ) <EOL> return resp . object <EOL> def test_existing_check ( self , check , ** kwargs ) : <EOL> resp = self . connection . request ( '<STR_LIT>' % <EOL> ( check . entity_id , check . id ) , <EOL> method = '<STR_LIT:POST>' ) <EOL> return resp . object <EOL> def create_check ( self , entity , ** kwargs ) : <EOL> data = self . _check_kwarg_to_data ( kwargs ) <EOL> headers = kwargs . get ( '<STR_LIT>' , { } ) <EOL> return self . _create ( "<STR_LIT>" % ( entity . id ) , <EOL> data = data , coerce = self . get_check , headers = headers ) <EOL> def update_check ( self , check , data , ** kwargs ) : <EOL> data = self . _check_kwarg_to_data ( kwargs = data ) <EOL> return self . _update ( "<STR_LIT>" % ( check . entity_id , <EOL> check . id ) , <EOL> data = data , kwargs = kwargs , coerce = self . get_check ) <EOL> def delete_check ( self , check , ** kwargs ) : <EOL> return self . _delete ( url = "<STR_LIT>" % <EOL> ( check . entity_id , check . id ) , <EOL> kwargs = kwargs ) <EOL> def get_entity ( self , entity_id ) : <EOL> resp = self . connection . request ( "<STR_LIT>" % ( entity_id ) ) <EOL> return self . _to_entity ( resp . object , { } ) <EOL> def _to_entity ( self , entity , value_dict ) : <EOL> ips = [ ] <EOL> ipaddrs = entity . get ( '<STR_LIT>' , { } ) <EOL> if ipaddrs is not None : <EOL> for key in ipaddrs . keys ( ) : <EOL> ips . append ( ( key , ipaddrs [ key ] ) ) <EOL> return Entity ( id = entity [ '<STR_LIT:id>' ] , label = entity [ '<STR_LIT:label>' ] , uri = entity . get ( '<STR_LIT>' , None ) , <EOL> extra = entity [ '<STR_LIT>' ] , agent_id = entity . get ( '<STR_LIT>' , None ) , <EOL> driver = self , ip_addresses = ips ) <EOL> def delete_entity ( self , entity , ** kwargs ) : <EOL> return self . _delete ( url = "<STR_LIT>" % ( entity . id ) , <EOL> kwargs = kwargs ) <EOL> def list_entities ( self , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : self . _to_entity } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def create_entity ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> data = kwargs <EOL> headers = kwargs . get ( '<STR_LIT>' , { } ) <EOL> if '<STR_LIT>' in data : <EOL> del data [ '<STR_LIT>' ] <EOL> return self . _create ( "<STR_LIT>" , data = data , coerce = self . get_entity , headers = headers ) <EOL> def update_entity ( self , entity , data , ** kwargs ) : <EOL> return self . _update ( "<STR_LIT>" % ( entity . id ) , <EOL> data = data , kwargs = kwargs , coerce = self . get_entity ) <EOL> def usage ( self ) : <EOL> resp = self . connection . request ( "<STR_LIT>" ) <EOL> return resp . object <EOL> def _to_audit ( self , audit , value_dict ) : <EOL> return audit <EOL> def list_audits ( self , start_from = None , to = None , reverse = False ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:200> } , <EOL> '<STR_LIT>' : self . _to_audit } <EOL> if reverse : <EOL> value_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] = reverse <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def get_entity_host_info ( self , entity_id , info_type ) : <EOL> url = "<STR_LIT>" % ( entity_id , info_type ) <EOL> resp = self . connection . request ( url ) <EOL> return resp . object <EOL> def get_entity_agent_targets ( self , entity_id , check_type ) : <EOL> url = "<STR_LIT>" % ( entity_id , <EOL> check_type ) <EOL> resp = self . connection . request ( url ) <EOL> return resp . object <EOL> def list_agent_tokens ( self ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . _to_agent_token } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def get_agent_token ( self , agent_token_id ) : <EOL> url = "<STR_LIT>" % ( agent_token_id ) <EOL> resp = self . connection . request ( url ) <EOL> return self . _to_agent_token ( resp . object , { } ) <EOL> def create_agent_token ( self , label = None , ** kwargs ) : <EOL> data = { '<STR_LIT:label>' : label , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) } <EOL> return self . _create ( '<STR_LIT>' , data = data , <EOL> coerce = self . get_agent_token ) <EOL> def delete_agent_token ( self , agent_token , ** kwargs ) : <EOL> return self . _delete ( url = "<STR_LIT>" % ( agent_token . id ) , <EOL> kwargs = kwargs ) <EOL> def _to_agent_token ( self , agent_token , value_dict ) : <EOL> return AgentToken ( id = agent_token [ '<STR_LIT:id>' ] , label = agent_token [ '<STR_LIT:label>' ] , <EOL> token = agent_token [ '<STR_LIT>' ] ) <EOL> def _get_installer_headers ( self , resp ) : <EOL> return { '<STR_LIT>' : resp . headers . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : resp . headers . get ( '<STR_LIT:location>' ) } <EOL> def create_agent_installer ( self , entity_id , ** kwargs ) : <EOL> data = { '<STR_LIT>' : entity_id , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) } <EOL> return self . _create ( '<STR_LIT>' , data = data , <EOL> coerce = None , handler = self . _get_installer_headers ) <EOL> def _to_agents ( self , agent , value_dict ) : <EOL> return Agent ( id = agent [ '<STR_LIT:id>' ] , last_connected = agent [ '<STR_LIT>' ] ) <EOL> def _to_agent_connection ( self , conn , value_dict ) : <EOL> return AgentConnection ( id = conn [ '<STR_LIT:id>' ] , guid = conn [ '<STR_LIT>' ] , endpoint = conn [ '<STR_LIT>' ] , <EOL> agent_id = conn [ '<STR_LIT>' ] , bundle_version = conn [ '<STR_LIT>' ] , <EOL> process_version = conn [ '<STR_LIT>' ] , agent_ip = conn [ '<STR_LIT>' ] ) <EOL> def list_agents ( self ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . _to_agents } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def list_agent_connections ( self , agent_id ) : <EOL> value_dict = { '<STR_LIT:url>' : "<STR_LIT>" % ( agent_id ) , <EOL> '<STR_LIT>' : self . _to_agent_connection } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def get_agent_host_info_types ( self , agent_id ) : <EOL> url = "<STR_LIT>" % ( agent_id ) <EOL> resp = self . connection . request ( url ) <EOL> return resp . object <EOL> def get_agent_host_info ( self , agent_id , info_type ) : <EOL> url = "<STR_LIT>" % ( agent_id , info_type ) <EOL> resp = self . connection . request ( url ) <EOL> return resp . object <EOL> def test_check_and_alarm ( self , entity , criteria , ** kwargs ) : <EOL> check_data = self . test_check ( entity = entity , ** kwargs ) <EOL> data = { '<STR_LIT>' : criteria , '<STR_LIT>' : check_data } <EOL> result = self . test_alarm ( entity = entity , ** data ) <EOL> return result <EOL> def ex_list_alarm_notification_history_checks ( self , entity , alarm ) : <EOL> resp = self . connection . request ( <EOL> '<STR_LIT>' % <EOL> ( entity . id , alarm . id ) ) . object <EOL> return resp <EOL> def ex_list_alarm_notification_history ( self , entity , alarm , check , <EOL> ex_next_marker = None , reverse = False ) : <EOL> value_dict = { <EOL> '<STR_LIT:url>' : '<STR_LIT>' % <EOL> ( entity . id , alarm . id , check . id ) , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : self . _to_alarm_notification_history_obj } <EOL> if reverse : <EOL> value_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] = reverse <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def _to_alarm_notification_history_obj ( self , values , value_dict ) : <EOL> return values <EOL> def ex_delete_checks ( self , entity ) : <EOL> checks = self . list_checks ( entity = entity ) <EOL> for check in checks : <EOL> self . delete_check ( check = check ) <EOL> def ex_delete_alarms ( self , entity ) : <EOL> alarms = self . list_alarms ( entity = entity ) <EOL> for alarm in alarms : <EOL> self . delete_alarm ( alarm = alarm ) <EOL> def ex_limits ( self ) : <EOL> resp = self . connection . request ( '<STR_LIT>' , <EOL> method = '<STR_LIT:GET>' ) <EOL> return resp . object <EOL> def ex_views_overview ( self , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : self . _to_overview_obj } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def ex_views_metric_list ( self , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : self . _to_metric_list_obj } <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def ex_views_agent_host_info ( self , agentIds , includes , ex_next_marker = None ) : <EOL> value_dict = { '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ex_next_marker , <EOL> '<STR_LIT>' : self . _to_agent_host_info_overview_obj } <EOL> params = [ ] <EOL> params . extend ( ( '<STR_LIT>' , agentId ) for agentId in agentIds ) <EOL> params . extend ( ( '<STR_LIT>' , include ) for include in includes ) <EOL> value_dict [ '<STR_LIT>' ] = params <EOL> return LazyList ( get_more = self . _get_more , value_dict = value_dict ) <EOL> def ex_traceroute ( self , monitoring_zone , target , target_resolver = '<STR_LIT>' ) : <EOL> data = { '<STR_LIT:target>' : target , '<STR_LIT>' : target_resolver } <EOL> path = '<STR_LIT>' % ( monitoring_zone . id ) <EOL> resp = self . connection . request ( path , data = data , method = '<STR_LIT:POST>' ) . object <EOL> return resp [ '<STR_LIT:result>' ] <EOL> def _to_latest_alarm_state ( self , obj , value_dict ) : <EOL> return LatestAlarmState ( entity_id = obj [ '<STR_LIT>' ] , <EOL> check_id = obj [ '<STR_LIT>' ] , alarm_id = obj [ '<STR_LIT>' ] , <EOL> timestamp = obj [ '<STR_LIT>' ] , state = obj [ '<STR_LIT:state>' ] ) <EOL> def _to_agent_host_info_overview_obj ( self , data , value_dict ) : <EOL> return { '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] } <EOL> def _to_metric_list_obj ( self , data , value_dict ) : <EOL> obj = { } <EOL> obj [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> obj [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> obj [ '<STR_LIT>' ] = [ ] <EOL> for ch in data [ '<STR_LIT>' ] : <EOL> for metric in ch [ '<STR_LIT>' ] : <EOL> m = self . _to_metrics ( metric ) <EOL> obj [ '<STR_LIT>' ] . append ( m ) <EOL> return obj <EOL> def _to_overview_obj ( self , data , value_dict ) : <EOL> entity = self . _to_entity ( data [ '<STR_LIT>' ] , { } ) <EOL> child_value_dict = { '<STR_LIT>' : entity . id } <EOL> checks = [ self . _to_check ( check , child_value_dict ) for check <EOL> in data [ '<STR_LIT>' ] ] <EOL> alarms = [ self . _to_alarm ( alarm , child_value_dict ) for alarm <EOL> in data [ '<STR_LIT>' ] ] <EOL> latest_alarm_states = [ self . _to_latest_alarm_state ( item , { } ) for item <EOL> in data [ '<STR_LIT>' ] ] <EOL> obj = { '<STR_LIT>' : entity , '<STR_LIT>' : checks , '<STR_LIT>' : alarms , <EOL> '<STR_LIT>' : latest_alarm_states } <EOL> return obj </s>
