B
    ?F[/                 @   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   8lib/python3.7/site-packages/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 )Nr   ).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,   r-   r5   r1   r2   r6   r7   r8   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   r6   r7   r8   r   r2   )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
MetricNameZ	NamespaceZPeriodZStateReasonZ
StateValueZ	StatisticZ	ThresholdZUnit)r.   r/   r0   r1   r   setattr_rev_cmp_mapr+   r-   r#   r$   r,   r3   r4   r%   r&   r'   r5   )r   r   r   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 )N)r
   Zupdate_alarm)r   r   r   r   update   s    zMetricAlarm.updatec             C   s   | j | jgS )N)r
   Zenable_alarm_actionsr   )r   r   r   r   enable_actions   s    zMetricAlarm.enable_actionsc             C   s   | j | jgS )N)r
   Zdisable_alarm_actionsr   )r   r   r   r   disable_actions   s    zMetricAlarm.disable_actionsc             C   s   | j | j|||||S )N)r
   Zdescribe_alarm_historyr   )r   Z
start_dateZ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)r.   r6   r   )r   
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.
        NrD   )r.   r7   r   )r   rE   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.
        NrD   )r.   r8   r   )r   rE   r   r   r   add_ok_action  s    	zMetricAlarm.add_ok_actionc             C   s   | j | jg d S )N)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   Z	iteritemsr<   r	   r9   r   r   r?   r@   rA   rB   rC   rF   rG   rH   rI   r   r   r   r   r   0   s6        
d"
 



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 )N)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	timestamp)r   r   r   r   r9   /  s    zAlarmHistoryItem.__repr__c             C   s   d S )Nr   )r   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ry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   loadsr>   Ztem_typerL   r   ZstrptimerM   
ValueError)r   r   r   r
   r   r   r   r   5  s    zAlarmHistoryItem.endElement)N)r   r   r   r	   r9   r   r   r   r   r   r   rK   +  s   
rK   N)r   Zboto.ec2.cloudwatch.listelementr   Zboto.ec2.cloudwatch.dimensionr   Zboto.compatr   r   r   r   objectr   rK   r   r   r   r   <module>   s    |