Skip to content

Commit

Permalink
cloudwatch module: add backward-compatible support for Python 3.3+
Browse files Browse the repository at this point in the history
  • Loading branch information
felixonmars committed Jul 13, 2014
1 parent 5a28d1c commit 925adfc
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ At the moment, boto supports:

* Monitoring

* Amazon CloudWatch (EC2 Only)
* Amazon CloudWatch (EC2 Only) (Python 3)
* Amazon CloudWatch Logs (Python 3)

* Networking
Expand Down
5 changes: 5 additions & 0 deletions boto/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
except ImportError:
import unittest

try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict

from boto.vendored import six

from boto.vendored.six import BytesIO, StringIO
Expand Down
4 changes: 2 additions & 2 deletions boto/ec2/cloudwatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def build_dimension_param(self, dimension, params):
for dim_name in dimension:
dim_value = dimension[dim_name]
if dim_value:
if isinstance(dim_value, basestring):
if isinstance(dim_value, six.string_types):
dim_value = [dim_value]
for value in dim_value:
params['%s.%d.Name' % (prefix, i+1)] = dim_name
Expand All @@ -121,7 +121,7 @@ def build_dimension_param(self, dimension, params):
i += 1

def build_list_params(self, params, items, label):
if isinstance(items, basestring):
if isinstance(items, six.string_types):
items = [items]
for index, item in enumerate(items):
i = index + 1
Expand Down
12 changes: 5 additions & 7 deletions boto/ec2/cloudwatch/alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#

from datetime import datetime
from boto.resultset import ResultSet
from boto.ec2.cloudwatch.listelement import ListElement
from boto.ec2.cloudwatch.dimension import Dimension
from boto.compat import json
Expand Down Expand Up @@ -253,11 +252,11 @@ def describe_history(self, start_date=None, end_date=None, max_records=None,

def add_alarm_action(self, action_arn=None):
"""
Adds an alarm action, represented as an SNS topic, to this alarm.
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
:param action_arn: SNS topics to which notification should be
sent if the alarm goes to state ALARM.
"""
if not action_arn:
Expand All @@ -271,21 +270,21 @@ def add_insufficient_data_action(self, action_arn=None):
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
:param action_arn: SNS topics to which notification should be
sent if the alarm goes to state INSUFFICIENT_DATA.
"""
if not action_arn:
return
self.actions_enabled = 'true'
self.insufficient_data_actions.append(action_arn)

def add_ok_action(self, action_arn=None):
"""
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
:param action_arn: SNS topics to which notification should be
sent if the alarm goes to state INSUFFICIENT_DATA.
"""
if not action_arn:
Expand Down Expand Up @@ -321,4 +320,3 @@ def endElement(self, name, value, connection):
'%Y-%m-%dT%H:%M:%S.%fZ')
except ValueError:
self.timestamp = datetime.strptime(value, '%Y-%m-%dT%H:%M:%SZ')

1 change: 0 additions & 1 deletion boto/ec2/cloudwatch/datapoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ def endElement(self, name, value, connection):
self[name] = datetime.strptime(value, '%Y-%m-%dT%H:%M:%SZ')
elif name != 'member':
self[name] = value

1 change: 0 additions & 1 deletion boto/ec2/cloudwatch/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ def endElement(self, name, value, connection):
self[self._name] = [value]
else:
setattr(self, name, value)

2 changes: 0 additions & 2 deletions boto/ec2/cloudwatch/listelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ def startElement(self, name, attrs, connection):
def endElement(self, name, value, connection):
if name == 'member':
self.append(value)


2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Currently Supported Services

* **Monitoring**

* :doc:`CloudWatch <cloudwatch_tut>` -- (:doc:`API Reference <ref/cloudwatch>`)
* :doc:`CloudWatch <cloudwatch_tut>` -- (:doc:`API Reference <ref/cloudwatch>`) (Python 3)
* CloudWatch Logs -- (:doc:`API Reference <ref/logs>`) (Python 3)

* **Networking**
Expand Down
1 change: 1 addition & 0 deletions requirements-py26.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
unittest2==0.5.1
ordereddict==1.1
3 changes: 1 addition & 2 deletions tests/integration/ec2/cloudwatch/test_cert_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
"""
Check that all of the certs on all service endpoints validate.
"""
import unittest

from tests.integration import ServiceCertVerificationTest

import boto.ec2.cloudwatch
from boto.compat import unittest


class CloudWatchCertVerificationTest(unittest.TestCase, ServiceCertVerificationTest):
Expand Down
13 changes: 4 additions & 9 deletions tests/integration/ec2/cloudwatch/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
"""

import datetime
import time
import unittest

from boto.ec2.cloudwatch import CloudWatchConnection
from boto.ec2.cloudwatch.metric import Metric
from boto.compat import unittest, OrderedDict

# HTTP response body for CloudWatchConnection.describe_alarms
DESCRIBE_ALARMS_BODY = """<DescribeAlarmsResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
Expand Down Expand Up @@ -160,7 +158,6 @@ def test_build_put_params_multiple_dimensions(self):
self.assertEqual(params, expected_params)

def test_build_put_params_multiple_parameter_dimension(self):
from collections import OrderedDict
self.maxDiff = None
c = CloudWatchConnection()
params = {}
Expand All @@ -180,7 +177,6 @@ def test_build_put_params_multiple_parameter_dimension(self):
self.assertEqual(params, expected_params)

def test_build_get_params_multiple_parameter_dimension1(self):
from collections import OrderedDict
self.maxDiff = None
c = CloudWatchConnection()
params = {}
Expand All @@ -195,7 +191,6 @@ def test_build_get_params_multiple_parameter_dimension1(self):
self.assertEqual(params, expected_params)

def test_build_get_params_multiple_parameter_dimension2(self):
from collections import OrderedDict
self.maxDiff = None
c = CloudWatchConnection()
params = {}
Expand Down Expand Up @@ -225,14 +220,14 @@ def test_build_put_params_invalid(self):
def test_get_metric_statistics(self):
c = CloudWatchConnection()
m = c.list_metrics()[0]
end = datetime.datetime.now()
end = datetime.datetime.utcnow()
start = end - datetime.timedelta(hours=24*14)
c.get_metric_statistics(
3600*24, start, end, m.name, m.namespace, ['Average', 'Sum'])

def test_put_metric_data(self):
c = CloudWatchConnection()
now = datetime.datetime.now()
now = datetime.datetime.utcnow()
name, namespace = 'unit-test-metric', 'boto-unit-test'
c.put_metric_data(namespace, name, 5, now, 'Bytes')

Expand All @@ -245,7 +240,7 @@ def test_put_metric_data(self):
# time.sleep(60)
# l = metric.query(
# now - datetime.timedelta(seconds=60),
# datetime.datetime.now(),
# datetime.datetime.utcnow(),
# 'Average')
# assert l
# for row in l:
Expand Down
1 change: 1 addition & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'tests/unit/glacier',
'tests/unit/iam',
'tests/unit/ec2/autoscale',
'tests/unit/ec2/cloudwatch',
'tests/unit/ec2/elb',
'tests/unit/manage',
'tests/unit/provider',
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ envlist = py26,py27,py33,py34
skipsdist = True

[testenv:py26]
# Python 2.6 requires an extra test dependency
# Python 2.6 requires two extra test dependencies
deps =
unittest2
ordereddict
-rrequirements.txt
# Some tests expect specific ordering, so we set the hash
# seed for Python 2.x until all tests are updated for Python 3.
Expand Down

0 comments on commit 925adfc

Please sign in to comment.