1# Copyright (c) 2006-2012 Mitch Garnaat http://garnaat.org/
2# Copyright (c) 2012 Amazon.com, Inc. or its affiliates.
3# All Rights Reserved
4#
5# Permission is hereby granted, free of charge, to any person obtaining a
6# copy of this software and associated documentation files (the
7# "Software"), to deal in the Software without restriction, including
8# without limitation the rights to use, copy, modify, merge, publish, dis-
9# tribute, sublicense, and/or sell copies of the Software, and to permit
10# persons to whom the Software is furnished to do so, subject to the fol-
11# lowing conditions:
12#
13# The above copyright notice and this permission notice shall be included
14# in all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
18# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22# IN THE SOFTWARE.
23
24
25class HealthCheck(object):
26    """
27    Represents an EC2 Access Point Health Check. See
28    :ref:`elb-configuring-a-health-check` for a walkthrough on configuring
29    load balancer health checks.
30    """
31    def __init__(self, access_point=None, interval=30, target=None,
32                 healthy_threshold=3, timeout=5, unhealthy_threshold=5):
33        """
34        :ivar str access_point: The name of the load balancer this
35            health check is associated with.
36        :ivar int interval: Specifies how many seconds there are between
37            health checks.
38        :ivar str target: Determines what to check on an instance. See the
39            Amazon HealthCheck_ documentation for possible Target values.
40
41        .. _HealthCheck: http://docs.amazonwebservices.com/ElasticLoadBalancing/latest/APIReference/API_HealthCheck.html
42        """
43        self.access_point = access_point
44        self.interval = interval
45        self.target = target
46        self.healthy_threshold = healthy_threshold
47        self.timeout = timeout
48        self.unhealthy_threshold = unhealthy_threshold
49
50    def __repr__(self):
51        return 'HealthCheck:%s' % self.target
52
53    def startElement(self, name, attrs, connection):
54        return None
55
56    def endElement(self, name, value, connection):
57        if name == 'Interval':
58            self.interval = int(value)
59        elif name == 'Target':
60            self.target = value
61        elif name == 'HealthyThreshold':
62            self.healthy_threshold = int(value)
63        elif name == 'Timeout':
64            self.timeout = int(value)
65        elif name == 'UnhealthyThreshold':
66            self.unhealthy_threshold = int(value)
67        else:
68            setattr(self, name, value)
69
70    def update(self):
71        """
72        In the case where you have accessed an existing health check on a
73        load balancer, this method applies this instance's health check
74        values to the load balancer it is attached to.
75
76        .. note:: This method will not do anything if the :py:attr:`access_point`
77            attribute isn't set, as is the case with a newly instantiated
78            HealthCheck instance.
79        """
80        if not self.access_point:
81            return
82
83        new_hc = self.connection.configure_health_check(self.access_point,
84                                                        self)
85        self.interval = new_hc.interval
86        self.target = new_hc.target
87        self.healthy_threshold = new_hc.healthy_threshold
88        self.unhealthy_threshold = new_hc.unhealthy_threshold
89        self.timeout = new_hc.timeout
90