U
    W+d/                     @   sp   d dl m Z  d dlmZ d dlmZ d dlmZ d dlmZ G dd deZ	G dd	 d	e
ZG d
d de
ZdS )    )datetime)ListElement)	Dimension)json)sixc                   @   s&   e Zd ZdddZdd Zdd ZdS )	MetricAlarmsNc                 C   s   t |  || _dS )z0
        Parses a list of MetricAlarms.
        N)list__init__
connectionselfr
    r   =/tmp/pip-unpacked-wheel-dlxw5sjy/boto/ec2/cloudwatch/alarm.pyr	      s    
zMetricAlarms.__init__c                 C   s"   |dkrt |}| | |S d S )Nmember)MetricAlarmappend)r   nameattrsr
   Zmetric_alarmr   r   r   startElement&   s    
zMetricAlarms.startElementc                 C   s   d S Nr   r   r   valuer
   r   r   r   
endElement,   s    zMetricAlarms.endElement)N)__name__
__module____qualname__r	   r   r   r   r   r   r   r      s   
r   c                   @   s   e Zd ZdZdZdZdddddZed	d
 e	eD Z
d'ddZdd Zdd Zdd Zd(ddZdd Zdd Zdd Zd)ddZd*dd Zd+d!d"Zd,d#d$Zd%d& ZdS )-r   OKALARMINSUFFICIENT_DATAZGreaterThanOrEqualToThresholdZGreaterThanThresholdZLessThanThresholdZLessThanOrEqualToThreshold)z>=><z<=c                 c   s   | ]\}}||fV  qd S r   r   ).0kvr   r   r   	<genexpr><   s     zMetricAlarm.<genexpr>N c                 C   s   || _ || _|| _|| _|| _|dk	r2t|| _nd| _| j|| _	|dk	rZt
|| _nd| _|	dk	rtt
|	| _nd| _d| _d| _d| _|| _|| _d| _d| _|
| _|| _|| _|| _dS )a
  
        Creates a new Alarm.

        :type name: str
        :param name: Name of alarm.

        :type metric: str
        :param metric: Name of alarm's associated metric.

        :type namespace: str
        :param namespace: The namespace for the alarm's metric.

        :type statistic: str
        :param statistic: The statistic to apply to the alarm's associated
                          metric.
                          Valid values: SampleCount|Average|Sum|Minimum|Maximum

        :type comparison: str
        :param comparison: Comparison used to compare statistic with threshold.
                           Valid values: >= | > | < | <=

        :type threshold: float
        :param threshold: The value against which the specified statistic
                          is compared.

        :type period: int
        :param period: The period in seconds over which the specified
                       statistic is applied.

        :type evaluation_periods: int
        :param evaluation_periods: The number of periods over which data is
                                  compared to the specified threshold.

        :type unit: str
        :param unit: Allowed Values are:
                     Seconds|Microseconds|Milliseconds,
                     Bytes|Kilobytes|Megabytes|Gigabytes|Terabytes,
                     Bits|Kilobits|Megabits|Gigabits|Terabits,
                     Percent|Count|
                     Bytes/Second|Kilobytes/Second|Megabytes/Second|
                     Gigabytes/Second|Terabytes/Second,
                     Bits/Second|Kilobits/Second|Megabits/Second,
                     Gigabits/Second|Terabits/Second|Count/Second|None

        :type description: str
        :param description: Description of MetricAlarm

        :type dimensions: dict
        :param dimensions: A dictionary of dimension key/values where
                           the key is the dimension name and the value
                           is either a scalar value or an iterator
                           of values to be associated with that
                           dimension.
                           Example: {
                               'InstanceId': ['i-0123456', 'i-0123457'],
                               'LoadBalancerName': 'test-lb'
                           }

        :type alarm_actions: list of strs
        :param alarm_actions: A list of the ARNs of the actions to take in
                              ALARM state

        :type insufficient_data_actions: list of strs
        :param insufficient_data_actions: A list of the ARNs of the actions to
                                          take in INSUFFICIENT_DATA state

        :type ok_actions: list of strs
        :param ok_actions: A list of the ARNs of the actions to take in OK state
        N)r   r
   metric	namespace	statisticfloat	threshold_cmp_mapget
comparisonintperiodevaluation_periodsactions_enabled	alarm_arnlast_updateddescription
dimensionsstate_reasonstate_valueunitalarm_actionsinsufficient_data_actions
ok_actions)r   r
   r   r&   r'   r(   r-   r*   r/   r0   r8   r4   r5   r9   r:   r;   r   r   r   r	   >   s4    KzMetricAlarm.__init__c                 C   s   d| j | j| j| j| jf S )NzMetricAlarm:%s[%s(%s) %s %s])r   r&   r(   r-   r*   r   r   r   r   __repr__   s
    
zMetricAlarm.__repr__c                 C   s\   |dkrt  | _| jS |dkr,t  | _| jS |dkrBt  | _| jS |dkrXt | _| jS d S )NZAlarmActionsZInsufficientDataActionsZ	OKActionsZ
Dimensions)r   r9   r:   r;   r   r5   r   r   r   r
   r   r   r   r      s    zMetricAlarm.startElementc                 C   s  |dkr|| _ n|dkr"|| _n|dkr2|| _n|dkrB|| _n|dkrR|| _n|dkrnt| d| j|  n|dkrt|| _n|d	kr|| _	n|d
kr|| _
nv|dkrt|| _nb|dkr|| _nR|dkr|| _nB|dkr|| _n2|dkrt|| _n|dkr|| _nt| || d S )NZActionsEnabledZAlarmArnZ"AlarmConfigurationUpdatedTimestampZAlarmDescription	AlarmNameZComparisonOperatorr-   ZEvaluationPeriodsZ
MetricName	NamespaceZPeriodZStateReasonZ
StateValueZ	StatisticZ	ThresholdZUnit)r1   r2   r3   r4   r   setattr_rev_cmp_mapr.   r0   r&   r'   r/   r6   r7   r(   r)   r*   r8   r   r   r   r   r      s>    

zMetricAlarm.endElementc                 C   s   | j | j|||S )a$   Temporarily sets the state of an alarm.

        :type value: str
        :param value: OK | ALARM | INSUFFICIENT_DATA

        :type reason: str
        :param reason: Reason alarm set (human readable).

        :type data: str
        :param data: Reason data (will be jsonified).
        )r
   Zset_alarm_stater   )r   r   reasondatar   r   r   	set_state   s    zMetricAlarm.set_statec                 C   s   | j | S r   )r
   Zupdate_alarmr<   r   r   r   update   s    zMetricAlarm.updatec                 C   s   | j | jgS r   )r
   Zenable_alarm_actionsr   r<   r   r   r   enable_actions   s    zMetricAlarm.enable_actionsc                 C   s   | j | jgS r   )r
   Zdisable_alarm_actionsr   r<   r   r   r   disable_actions   s    zMetricAlarm.disable_actionsc                 C   s   | j | j|||||S r   )r
   Zdescribe_alarm_historyr   )r   Z
start_dateend_dateZmax_recordsZhistory_item_typeZ
next_tokenr   r   r   describe_history   s     zMetricAlarm.describe_historyc                 C   s   |sdS d| _ | j| dS )a&  
        Adds an alarm action, represented as an SNS topic, to this alarm.
        What do do when alarm is triggered.

        :type action_arn: str
        :param action_arn: SNS topics to which notification should be
                           sent if the alarm goes to state ALARM.
        Ntrue)r1   r9   r   r   Z
action_arnr   r   r   add_alarm_action   s    	zMetricAlarm.add_alarm_actionc                 C   s   |sdS d| _ | j| dS )aR  
        Adds an insufficient_data action, represented as an SNS topic, to
        this alarm. What to do when the insufficient_data state is reached.

        :type action_arn: str
        :param action_arn: SNS topics to which notification should be
                           sent if the alarm goes to state INSUFFICIENT_DATA.
        NrK   )r1   r:   r   rL   r   r   r   add_insufficient_data_action  s    	z(MetricAlarm.add_insufficient_data_actionc                 C   s   |sdS d| _ | j| dS )a4  
        Adds an ok action, represented as an SNS topic, to this alarm. What
        to do when the ok state is reached.

        :type action_arn: str
        :param action_arn: SNS topics to which notification should be
                           sent if the alarm goes to state INSUFFICIENT_DATA.
        NrK   )r1   r;   r   rL   r   r   r   add_ok_action  s    	zMetricAlarm.add_ok_actionc                 C   s   | j | jg d S r   )r
   Zdelete_alarmsr   r<   r   r   r   delete'  s    zMetricAlarm.delete)NNNNNNNNNNr%   NNNN)N)NNNNN)N)N)N)r   r   r   r   r   r   r+   dictr   	iteritemsrB   r	   r=   r   r   rE   rF   rG   rH   rJ   rM   rN   rO   rP   r   r   r   r   r   0   sL                       
i"
    



r   c                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )AlarmHistoryItemNc                 C   s
   || _ d S r   )r
   r   r   r   r   r	   ,  s    zAlarmHistoryItem.__init__c                 C   s   d| j | j| jf S )NzAlarmHistory:%s[%s at %s])r   summary	timestampr<   r   r   r   r=   /  s    zAlarmHistoryItem.__repr__c                 C   s   d S r   r   r>   r   r   r   r   2  s    zAlarmHistoryItem.startElementc                 C   s   |dkr|| _ nt|dkr&t|| _n^|dkr6|| _nN|dkrF|| _n>|dkrzt|d| _W n" t	k
r   t|d| _Y nX d S )Nr?   ZHistoryDataZHistoryItemTypeZHistorySummaryZ	Timestampz%Y-%m-%dT%H:%M:%S.%fZz%Y-%m-%dT%H:%M:%SZ)
r   r   loadsrD   Ztem_typerT   r   strptimerU   
ValueErrorr   r   r   r   r   5  s    
zAlarmHistoryItem.endElement)N)r   r   r   r	   r=   r   r   r   r   r   r   rS   +  s   
rS   N)r   Zboto.ec2.cloudwatch.listelementr   Zboto.ec2.cloudwatch.dimensionr   Zboto.compatr   r   r   r   objectr   rS   r   r   r   r   <module>   s    |