From 270f90d50e22a892bfb2a798aeb882210fa3efa4 Mon Sep 17 00:00:00 2001 From: Nicholas Muesch Date: Fri, 25 Jan 2019 17:28:28 -0500 Subject: [PATCH] Support Python 3 (#3035) --- .travis.yml | 2 +- openstack/datadog_checks/openstack/openstack.py | 17 ++++++++++------- openstack/tests/test_openstack.py | 11 ++++------- openstack/tox.ini | 4 +--- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6d76ccf15dc6d..a10d8227f5dc3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -198,7 +198,7 @@ jobs: - stage: test env: CHECK=openldap PYTHON3=true - stage: test - env: CHECK=openstack + env: CHECK=openstack PYTHON3=true - stage: test env: CHECK=openstack_controller PYTHON3=true - stage: test diff --git a/openstack/datadog_checks/openstack/openstack.py b/openstack/datadog_checks/openstack/openstack.py index ce196065b8ff1..3de64753372a8 100644 --- a/openstack/datadog_checks/openstack/openstack.py +++ b/openstack/datadog_checks/openstack/openstack.py @@ -1,8 +1,9 @@ # (C) Datadog, Inc. 2010-2017 # All rights reserved # Licensed under Simplified BSD License (see LICENSE) +from __future__ import division + from datetime import datetime, timedelta -from urlparse import urljoin import re import time import random @@ -10,6 +11,8 @@ import requests import simplejson as json +from six import iteritems +from six.moves.urllib.parse import urljoin from datadog_checks.checks import AgentCheck from datadog_checks.config import is_affirmative @@ -612,7 +615,7 @@ def _instance_key(self, instance): def delete_current_scope(self): scope_to_delete = self._parent_scope if self._parent_scope else self._current_scope - for i_key, scope in self.instance_map.items(): + for i_key, scope in list(self.instance_map.items()): if scope is scope_to_delete: self.log.debug("Deleting current scope: %s", i_key) del self.instance_map[i_key] @@ -635,7 +638,7 @@ def do_backoff(self, instance): jitter = min(MAX_BACKOFF_SECS, BASE_BACKOFF_SECS * 2 ** self.backoff[i_key]['retries']) # let's add some jitter (half jitter) - backoff_interval = jitter / 2 + backoff_interval = jitter // 2 backoff_interval += random.randint(0, backoff_interval) tags = instance.get('tags', []) @@ -760,7 +763,7 @@ def _parse_uptime_string(self, uptime): load_averages = uptime[uptime.find('load average:'):].split(':')[1].split(',') uptime_sec = uptime.split(',')[0] - return {'loads': map(float, load_averages), 'uptime_sec': uptime_sec} + return {'loads': list(map(float, load_averages)), 'uptime_sec': uptime_sec} def get_all_hypervisor_ids(self, filter_by_host=None): nova_version = self.init_config.get("nova_api_version", DEFAULT_NOVA_API_VERSION) @@ -861,7 +864,7 @@ def get_stats_for_single_hypervisor(self, hyp_id, instance, host_tags=None, cust else: self.service_check(self.HYPERVISOR_SC, AgentCheck.OK, tags=service_check_tags) - for label, val in hyp.iteritems(): + for label, val in iteritems(hyp): if label in NOVA_HYPERVISOR_METRICS: metric_label = "openstack.nova.{0}".format(label) self.gauge(metric_label, val, tags=tags) @@ -1205,7 +1208,7 @@ def check(self, instance): # The scopes we iterate over should all be OpenStackProjectScope # instances projects = [] - for _, scope in scope_map.iteritems(): + for _, scope in iteritems(scope_map): # Store the scope on the object so we don't have to keep passing it around self._current_scope = scope @@ -1417,7 +1420,7 @@ def get_external_host_tags(self): """ self.log.debug("Collecting external_host_tags now") external_host_tags = [] - for k, v in self.external_host_tags.iteritems(): + for k, v in iteritems(self.external_host_tags): external_host_tags.append((k, {SOURCE_TYPE: v})) self.log.debug("Sending external_host_tags: %s", external_host_tags) diff --git a/openstack/tests/test_openstack.py b/openstack/tests/test_openstack.py index adb60c5032103..b40c6277a8518 100644 --- a/openstack/tests/test_openstack.py +++ b/openstack/tests/test_openstack.py @@ -1,19 +1,17 @@ # (C) Datadog, Inc. 2018 # All rights reserved # Licensed under Simplified BSD License (see LICENSE) - -# stdlib import copy import re import time -# 3p import mock import pytest +from six import iteritems -# project -import common +from . import common +from datadog_checks.checks import AgentCheck from datadog_checks.openstack.openstack import ( OpenStackCheck, OpenStackProjectScope, @@ -24,7 +22,6 @@ IncompleteIdentity ) -from datadog_checks.checks import AgentCheck instance = common.MOCK_CONFIG["instances"][0] instance['tags'] = ['optional:tag1'] @@ -146,7 +143,7 @@ def test_unscoped_from_config(): assert scope.auth_token == 'fake_token' assert len(scope.project_scope_map) == 1 - for _, scope in scope.project_scope_map.iteritems(): + for _, scope in iteritems(scope.project_scope_map): assert isinstance(scope, OpenStackProjectScope) assert scope.auth_token == 'fake_token' assert scope.tenant_id == '263fd9' diff --git a/openstack/tox.ini b/openstack/tox.ini index 12f5e077d7d39..59fa66d9dc992 100644 --- a/openstack/tox.ini +++ b/openstack/tox.ini @@ -2,14 +2,12 @@ minversion = 2.0 basepython = py27 envlist = - openstack + {py27,py36}-openstack flake8 [testenv] usedevelop = true platform = linux|darwin|win32 - -[testenv:openstack] deps = -e../datadog_checks_base[deps] -rrequirements-dev.txt